libept
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 
30 #include <tagcoll/coll/simple.h>
31 #include <tagcoll/stream/sink.h>
32 #include <tagcoll/patch.h>
33 
34 #include <wibble/operators.h>
35 
36 #include <ept/test.h>
37 #include <cstdio>
38 
39 #ifndef EPT_DEBTAGS_TESTH
40 #define EPT_DEBTAGS_TESTH
41 
42 using namespace tagcoll;
43 using namespace std;
44 using namespace ept;
45 using namespace ept::debtags;
46 using namespace wibble::operators;
47 
49 {
51 
53 
54  Test _1() {
55  for (Debtags::const_iterator i = debtags.begin(); i != debtags.end(); ++i)
56  {
57  *i;
58  i->first;
59  i->second;
60  }
61  int items = 0, tags = 0;
62  debtags.outputSystem(stream::countingSink(items, tags));
63 
64  int pitems = 0, ptags = 0;
65  debtags.outputPatched(stream::countingSink(pitems, ptags));
66 
67  assert(items > 10);
68  assert(tags > 10);
69  assert(items <= pitems);
70  assert(tags <= ptags);
71 }
72 
73  Test _2()
74 {
75  string p("debtags");
76  std::set<std::string> tags = debtags.getTagsOfItem(p);
77  assert( !tags.empty() );
78 
79 #if 0
80  for ( std::set< Tag >::iterator i = tags.begin(); i != tags.end(); ++ i ) {
81  std::cerr << i->id() << ": " << i->fullname() << std::endl;
82  }
83  std::cerr << "---" << std::endl;
84  Tag t = voc().tagByName( "interface::commandline" );
85  std::cerr << t.id() << ": " << t.fullname() << std::endl;
86 #endif
87 
88  assert_eq( tags.size(), 8u );
89  assert( tags.find("devel::buildtools") != tags.end() );
90  assert( tags.find("implemented-in::c++") != tags.end() );
91  assert( tags.find("interface::commandline") != tags.end() );
92  assert( tags.find("role::program") != tags.end() );
93  assert( tags.find("scope::application") != tags.end() );
94  assert( tags.find("suite::debian") != tags.end() );
95  assert( tags.find("use::searching") != tags.end() );
96  assert( tags.find("works-with::software:package") != tags.end() );
97 }
98 
99  Test _3()
100 {
101  using namespace std;
102 
103  /* Get the 'debtags' package */
104  string p("debtags");
105 
106  /* Get its tags */
107  std::set<std::string> tags = debtags.getTagsOfItem(p);
108  assert(!tags.empty());
109 
110  /*
111  cerr << "Intersection size: " << endl;
112  using namespace wibble::operators;
113  std::set<Tag>::const_iterator dbgi = tags.begin();
114  cerr << "* " << dbgi->fullname() << ": " << dbgi->id() << endl;
115  std::set<int> dbgres = debtags.tagdb().getItemsHavingTag(dbgi->id());
116  std::set<Package> dbgpres = debtags.getItemsHavingTag(*dbgi);
117  cerr << " #pkgs " << dbgres.size() << " == " << dbgpres.size() << endl;
118  cerr << " #isec " << dbgres.size() << " == " << dbgpres.size() << endl;
119  cerr << " "; ppset(dbgpres); cerr << endl;
120  cerr << " "; piset(dbgres); cerr << endl;
121  for (++dbgi ; dbgi != tags.end(); ++dbgi)
122  {
123  cerr << "* " << dbgi->fullname() << ": " << dbgi->id() << endl;
124  std::set<Package> dbgpkgs = debtags.getItemsHavingTag(*dbgi);
125  std::set<int> dbgids = debtags.tagdb().getItemsHavingTag(dbgi->id());
126  cerr << " "; ppset(dbgpkgs); cerr << endl;
127  cerr << " "; piset(dbgids); cerr << endl;
128  cerr << " #pkgs " << dbgpkgs.size() << " == " << dbgids.size() << endl;
129  dbgres &= dbgids;
130  dbgpres &= dbgpkgs;
131  cerr << " #isec " << dbgres.size() << " == " << dbgpres.size() << endl;
132  }
133  cerr << " " << dbgres.size() << endl << "Results: " << endl;
134  for (std::set<int>::const_iterator i = dbgres.begin(); i != dbgres.end(); ++i)
135  cerr << " " << *i << endl;
136  */
137 
138 
139 // cerr << "Tags of debtags: ";
140 // for (std::set<Tag>::const_iterator i = tags.begin(); i != tags.end(); ++i)
141 // {
142 // cerr << " " + i->fullname() << endl;
143 // std::set<Package> packages = debtags.getItemsHavingTag(*i);
144 // for (std::set<Package>::const_iterator p = packages.begin();
145 // p != packages.end(); ++p)
146 // cerr << " PKG " << p->name() << endl;
147 // }
148 // cerr << endl;
149 
150  /* Get the items for the tagset of 'debtags' */
151  std::set<string> packages = debtags.getItemsHavingTags(tags);
152  //cerr << packages.size() << endl;
153  assert(!packages.empty());
154  /*
155  for ( std::set< Package >::iterator i = packages.begin(); i != packages.end(); ++ i )
156  std::cerr << i->name() << std::endl;
157  std::cerr << "---" << std::endl;
158  std::cerr << p.name() << std::endl;
159  */
160  /* They should at least contain 'debtags' */
161  assert( p <= packages );
162 
163  /* Get one of the tags of 'debtags' */
164  std::string tag = *tags.begin();
165 
166  /* Get its items */
167  {
168  /* Need this workaround until I figure out how to tell the new GCC
169  * that TagDB is a TDBReadonlyDiskIndex and should behave as such
170  */
171  std::set<std::string> ts;
172  ts.insert(tag);
173  packages = debtags.getItemsHavingTags(ts);
174  }
175  //packages = c.debtags().tagdb().getItems(tag);
176  assert(!packages.empty());
177  /* They should at least contain 'debtags' */
178  assert( p <= packages );
179 
180  //c.debtags().getTags(""); // XXX HACK AWW!
181 }
182 
183  Test _4()
184 {
185  std::string patchfile = Path::debtagsUserSourceDir() + "patch";
186  unlink(patchfile.c_str());
187 
188  string p("debtags");
189 
190  /* Get its tags */
191  std::set<std::string> tags = debtags.getTagsOfItem(p);
192  assert(!tags.empty());
193 
194  // Ensure that it's not tagged with gameplaying
195  std::string t = "use::gameplaying";
196  assert(tags.find(t) == tags.end());
197 
198  // Add the gameplaying tag
199  PatchList<string, string> change;
200  change.addPatch(Patch<string, string>(p, wibble::singleton(t), wibble::Empty<string>()));
201  debtags.applyChange(change);
202 
203  // See that the patch is non empty
204  PatchList<string, string> tmp = debtags.changes();
205  assert(tmp.size() > 0);
206  assert_eq(tmp.size(), 1u);
207 
208  // Ensure that the tag has been added
209  tags = debtags.getTagsOfItem(p);
210  assert(!tags.empty());
211 
212  t = "use::gameplaying";
213  assert(tags.find(t) != tags.end());
214 
215  // Save the patch
216  debtags.savePatch();
217 
218  // Check that the saved patch is correct
219  FILE* in = fopen(patchfile.c_str(), "r");
220  string writtenPatch;
221  int c;
222  while ((c = getc(in)) != EOF)
223  writtenPatch += c;
224  fclose(in);
225 
226  assert_eq(writtenPatch, string("debtags: +use::gameplaying\n"));
227 
228  unlink(patchfile.c_str());
229 
230  // Reapply the patch and see that it doesn't disrept things
231  debtags.applyChange(change);
232 
233  // The patch should not have changed
234  tmp = debtags.changes();
235  assert_eq(tmp.size(), 1u);
236  assert_eq(tmp.begin()->first, p);
237  assert_eq(tmp.begin()->second.item, p);
238 }
239 
240 // If there is no data, Debtags should work as an empty collection
241  Test _5()
242 {
243  Path::OverrideDebtagsSourceDir odsd("./empty");
244  Path::OverrideDebtagsIndexDir odid("./empty");
245  Path::OverrideDebtagsUserSourceDir odusd("./empty");
246  Path::OverrideDebtagsUserIndexDir oduid("./empty");
247  Debtags empty;
248 
249  assert(empty.begin() == empty.end());
250  assert_eq(empty.timestamp(), 0);
251  assert(!empty.hasData());
252 
253  tagcoll::PatchList<std::string, std::string> patches = empty.changes();
254  assert(patches.empty());
255 
256  set<std::string> res = empty.getTagsOfItem("apt");
257  assert(res.empty());
258  // TODO: currently does not compile because of a bug in tagcoll
259  //res = empty.getTagsOfItems(wibble::singleton(string("apt")));
260  //assert(res.empty());
261 
262  res = empty.getAllTags();
263  assert(res.empty());
264 
265  tagcoll::coll::Simple<string, std::string> coll;
266  empty.outputSystem(tagcoll::coll::inserter(coll));
267  assert_eq(coll.itemCount(), 0u);
268 
269  coll.clear();
270 
271  empty.outputPatched(tagcoll::coll::inserter(coll));
272  assert_eq(coll.itemCount(), 0u);
273 }
274 
275 };
276 
277 #include <ept/debtags/debtags.tcc>
278 #include <tagcoll/coll/simple.tcc>
279 
280 #endif
281 
282 // vim:set ts=4 sw=4: