libept  0.5.25
debtags.test.h
Go to the documentation of this file.
1 // -*- mode: c++; tab-width: 4; indent-tabs-mode: t -*-
7 /*
8  * Test for the Debtags data provider
9  *
10  * Copyright (C) 2003-2007 Enrico Zini <enrico@debian.org>
11  *
12  * This library is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU Lesser General Public
14  * License as published by the Free Software Foundation; either
15  * version 2.1 of the License, or (at your option) any later version.
16  *
17  * This library is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20  * Lesser General Public License for more details.
21  *
22  * You should have received a copy of the GNU Lesser General Public
23  * License along with this library; if not, write to the Free Software
24  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25  */
26 
27 
28 #include <ept/debtags/debtags.h>
29 #include <ept/core/apt.h>
30 
31 #include <tagcoll/coll/simple.h>
32 #include <tagcoll/stream/sink.h>
33 
34 #include <wibble/operators.h>
35 
36 #include <ept/test.h>
37 
38 #include <stdio.h>
39 
40 #ifndef EPT_DEBTAGS_TESTH
41 #define EPT_DEBTAGS_TESTH
42 
43 using namespace tagcoll;
44 using namespace std;
45 using namespace ept;
46 using namespace ept::debtags;
47 using namespace wibble::operators;
48 
50 {
52 
54 
55  Vocabulary& voc() { return debtags.vocabulary(); }
56 
57  Test _1() {
58  for (Debtags::const_iterator i = debtags.begin(); i != debtags.end(); ++i)
59  {
60  *i;
61  i->first;
62  i->second;
63  }
64  int items = 0, tags = 0;
65  debtags.outputSystem(stream::countingSink(items, tags));
66 
67  int pitems = 0, ptags = 0;
68  debtags.outputPatched(stream::countingSink(pitems, ptags));
69 
70  assert(items > 10);
71  assert(tags > 10);
72  assert(items <= pitems);
73  assert(tags <= ptags);
74 }
75 
76  Test _2()
77 {
78  string p("debtags");
79  std::set<Tag> tags = debtags.getTagsOfItem(p);
80  assert( !tags.empty() );
81 
82 #if 0
83  for ( std::set< Tag >::iterator i = tags.begin(); i != tags.end(); ++ i ) {
84  std::cerr << i->id() << ": " << i->fullname() << std::endl;
85  }
86  std::cerr << "---" << std::endl;
87  Tag t = voc().tagByName( "interface::commandline" );
88  std::cerr << t.id() << ": " << t.fullname() << std::endl;
89 #endif
90 
91  assert_eq( tags.size(), 8u );
92  assert( voc().tagByName( "devel::buildtools" ) <= tags );
93  assert( voc().tagByName( "implemented-in::c++" ) <= tags );
94  assert( voc().tagByName( "interface::commandline" ) <= tags );
95  assert( voc().tagByName( "role::program" ) <= tags );
96  assert( voc().tagByName( "scope::application" ) <= tags );
97  assert( voc().tagByName( "suite::debian" ) <= tags );
98  assert( voc().tagByName( "use::searching" ) <= tags );
99  assert( voc().tagByName( "works-with::software:package" ) <= tags );
100 }
101 
102  Test _3()
103 {
104  using namespace std;
105 
106  /* Get the 'debtags' package */
107  string p("debtags");
108 
109  /* Get its tags */
110  std::set<Tag> tags = debtags.getTagsOfItem(p);
111  assert(!tags.empty());
112 
113  /*
114  cerr << "Intersection size: " << endl;
115  using namespace wibble::operators;
116  std::set<Tag>::const_iterator dbgi = tags.begin();
117  cerr << "* " << dbgi->fullname() << ": " << dbgi->id() << endl;
118  std::set<int> dbgres = debtags.tagdb().getItemsHavingTag(dbgi->id());
119  std::set<Package> dbgpres = debtags.getItemsHavingTag(*dbgi);
120  cerr << " #pkgs " << dbgres.size() << " == " << dbgpres.size() << endl;
121  cerr << " #isec " << dbgres.size() << " == " << dbgpres.size() << endl;
122  cerr << " "; ppset(dbgpres); cerr << endl;
123  cerr << " "; piset(dbgres); cerr << endl;
124  for (++dbgi ; dbgi != tags.end(); ++dbgi)
125  {
126  cerr << "* " << dbgi->fullname() << ": " << dbgi->id() << endl;
127  std::set<Package> dbgpkgs = debtags.getItemsHavingTag(*dbgi);
128  std::set<int> dbgids = debtags.tagdb().getItemsHavingTag(dbgi->id());
129  cerr << " "; ppset(dbgpkgs); cerr << endl;
130  cerr << " "; piset(dbgids); cerr << endl;
131  cerr << " #pkgs " << dbgpkgs.size() << " == " << dbgids.size() << endl;
132  dbgres &= dbgids;
133  dbgpres &= dbgpkgs;
134  cerr << " #isec " << dbgres.size() << " == " << dbgpres.size() << endl;
135  }
136  cerr << " " << dbgres.size() << endl << "Results: " << endl;
137  for (std::set<int>::const_iterator i = dbgres.begin(); i != dbgres.end(); ++i)
138  cerr << " " << *i << endl;
139  */
140 
141 
142 // cerr << "Tags of debtags: ";
143 // for (std::set<Tag>::const_iterator i = tags.begin(); i != tags.end(); ++i)
144 // {
145 // cerr << " " + i->fullname() << endl;
146 // std::set<Package> packages = debtags.getItemsHavingTag(*i);
147 // for (std::set<Package>::const_iterator p = packages.begin();
148 // p != packages.end(); ++p)
149 // cerr << " PKG " << p->name() << endl;
150 // }
151 // cerr << endl;
152 
153  /* Get the items for the tagset of 'debtags' */
154  std::set<string> packages = debtags.getItemsHavingTags(tags);
155  //cerr << packages.size() << endl;
156  assert(!packages.empty());
157  /*
158  for ( std::set< Package >::iterator i = packages.begin(); i != packages.end(); ++ i )
159  std::cerr << i->name() << std::endl;
160  std::cerr << "---" << std::endl;
161  std::cerr << p.name() << std::endl;
162  */
163  /* They should at least contain 'debtags' */
164  assert( p <= packages );
165 
166  /* Get one of the tags of 'debtags' */
167  Tag tag = *tags.begin();
168  assert(tag);
169 
170  /* Get its items */
171  {
172  /* Need this workaround until I figure out how to tell the new GCC
173  * that TagDB is a TDBReadonlyDiskIndex and should behave as such
174  */
175  std::set<Tag> ts;
176  ts.insert(tag);
177  packages = debtags.getItemsHavingTags(ts);
178  }
179  //packages = c.debtags().tagdb().getItems(tag);
180  assert(!packages.empty());
181  /* They should at least contain 'debtags' */
182  assert( p <= packages );
183 
184  //c.debtags().getTags(""); // XXX HACK AWW!
185 }
186 
187  Test _4()
188 {
189  std::string patchfile = Path::debtagsUserSourceDir() + "patch";
190  unlink(patchfile.c_str());
191 
192  string p("debtags");
193 
194  /* Get its tags */
195  std::set<Tag> tags = debtags.getTagsOfItem(p);
196  assert(!tags.empty());
197 
198  // Ensure that it's not tagged with gameplaying
199  Tag t = voc().tagByName("use::gameplaying");
200  assert(! (t <= tags) );
201 
202  // Add the gameplaying tag
203  PatchList<string, Tag> change;
204  change.addPatch(Patch<string, Tag>(p, wibble::singleton(t), wibble::Empty<Tag>()));
205  debtags.applyChange(change);
206 
207  // See that the patch is non empty
208  PatchList<string, Tag> tmp = debtags.changes();
209  assert(tmp.size() > 0);
210  assert_eq(tmp.size(), 1u);
211 
212  // Ensure that the tag has been added
213  tags = debtags.getTagsOfItem(p);
214  assert(!tags.empty());
215 
216  t = voc().tagByName("use::gameplaying");
217  assert( t <= tags );
218 
219  // Save the patch
220  debtags.savePatch();
221 
222  // Check that the saved patch is correct
223  FILE* in = fopen(patchfile.c_str(), "r");
224  string writtenPatch;
225  int c;
226  while ((c = getc(in)) != EOF)
227  writtenPatch += c;
228  fclose(in);
229 
230  assert_eq(writtenPatch, string("debtags: +use::gameplaying\n"));
231 
232  unlink(patchfile.c_str());
233 
234  // Reapply the patch and see that it doesn't disrept things
235  debtags.applyChange(change);
236 
237  // The patch should not have changed
238  tmp = debtags.changes();
239  assert_eq(tmp.size(), 1u);
240  assert_eq(tmp.begin()->first, p);
241  assert_eq(tmp.begin()->second.item, p);
242 }
243 
244 // If there is no data, Debtags should work as an empty collection
245  Test _5()
246 {
247  Path::OverrideDebtagsSourceDir odsd("./empty");
248  Path::OverrideDebtagsIndexDir odid("./empty");
249  Path::OverrideDebtagsUserSourceDir odusd("./empty");
250  Path::OverrideDebtagsUserIndexDir oduid("./empty");
251  Debtags empty;
252 
253  assert(empty.begin() == empty.end());
254  assert_eq(empty.timestamp(), 0);
255  assert(!empty.hasData());
256 
258  assert(patches.empty());
259 
260  set<Tag> res = empty.getTagsOfItem("apt");
261  assert(res.empty());
262  res = empty.getTagsOfItems(wibble::singleton(string("apt")));
263  assert(res.empty());
264 
265  res = empty.getAllTags();
266  assert(res.empty());
267 
268  tagcoll::coll::Simple<string, Tag> coll;
269  empty.outputSystem(tagcoll::coll::inserter(coll));
270  assert_eq(coll.itemCount(), 0u);
271 
272  coll.clear();
273 
274  empty.outputPatched(tagcoll::coll::inserter(coll));
275  assert_eq(coll.itemCount(), 0u);
276 }
277 
278 };
279 
280 #include <ept/debtags/debtags.tcc>
281 #include <tagcoll/coll/simple.tcc>
282 
283 #endif
284 
285 // vim:set ts=4 sw=4:
void outputSystem(const OUT &cons)
Output the current Debian tags database to a consumer of <std::string, Tag>
Definition: debtags.cc:232
Definition: debtags/maint/path.h:82
Definition: test.h:24
Test _1()
Definition: debtags.test.h:57
int id() const
Return the ID of this tag.
Definition: tag.h:242
std::set< Tag > getAllTags() const
Definition: debtags.h:274
Vocabulary & vocabulary()
Access the vocabulary in use.
Definition: debtags.h:280
Debtags debtags
Definition: debtags.test.h:51
std::set< std::string > getItemsHavingTags(const TAGS &tags) const
Definition: debtags.h:262
Definition: packagerecord.test.h:22
Test _5()
Definition: debtags.test.h:245
Definition: debtags/maint/path.h:72
-*- C++ -*- (c) 2006, 2007 Petr Rockai me@mornfall.net
Definition: apt.cc:43
Test _2()
Definition: debtags.test.h:76
Access the on-disk Debtags tag database.
Definition: debtags.h:75
Test _4()
Definition: debtags.test.h:187
Definition: debtags.cc:53
Representation of a tag.
Definition: tag.h:163
Definition: debtags/maint/path.h:92
Test _3()
Definition: debtags.test.h:102
void outputPatched(const OUT &cons)
Output the current Debian tags database, patched with local patch, to a Consumer of <std::string...
Definition: debtags.cc:238
tagcoll::PatchList< std::string, Tag > changes() const
Definition: debtags.cc:88
bool hasData() const
Return true if this data source has data, false if it&#39;s empty.
Definition: debtags.h:200
const_iterator begin() const
Definition: debtags.h:182
void applyChange(const tagcoll::PatchList< std::string, Tag > &change)
Definition: debtags.h:302
std::string fullname() const
Return the name of the tag, with the facet:: prefix.
Definition: tag.cc:102
Definition: vocabulary.h:37
const_iterator end() const
Definition: debtags.h:183
Definition: debtags.h:43
TestDebtags()
Definition: debtags.test.h:53
void savePatch()
Check if the tag database has been created (i.e.
Definition: debtags.cc:137
std::set< Tag > getTagsOfItem(const std::string &item) const
Definition: debtags.h:244
Definition: debtags/maint/path.h:102
Vocabulary & voc()
Definition: debtags.test.h:55
Definition: debtags.test.h:49
Definition: debtags.h:131
std::set< Tag > getTagsOfItems(const ITEMS &items) const
Definition: debtags.h:252
Definition: debtags.h:44
time_t timestamp() const
Get the timestamp of when the index was last updated.
Definition: debtags.h:197