- multiple repositories filter reenabled
[platform/upstream/libzypp.git] / tests / zypp / PoolQuery_test.cc
1 #include <stdio.h>
2 #include <iostream>
3 #include <iterator>
4 #include <boost/test/auto_unit_test.hpp>
5 #include <list>
6
7 #include "zypp/ZYppFactory.h"
8 #include "zypp/PoolQuery.h"
9 #include "zypp/PoolQueryUtil.tcc"
10 #include "zypp/TmpPath.h"
11
12 #define BOOST_TEST_MODULE PoolQuery
13
14 using std::cout;
15 using std::endl;
16 using std::string;
17 using namespace zypp;
18 using namespace boost::unit_test;
19
20 bool result_cb( const sat::Solvable & solvable )
21 {
22   zypp::PoolItem pi( zypp::ResPool::instance().find( solvable ) );
23   cout << pi.resolvable() << endl;
24   // name: yast2-sound 2.16.2-9 i586
25   return true;
26 }
27
28 static void init_pool()
29 {
30   Pathname dir(TESTS_SRC_DIR);
31   dir += "/zypp/data/PoolQuery";
32
33   ZYpp::Ptr z = getZYpp();
34   ZConfig::instance().setSystemArchitecture(Arch("i586"));
35
36   RepoInfo i1; i1.setAlias("factory");
37   sat::Pool::instance().addRepoSolv(dir / "factory.solv", i1);
38   RepoInfo i2; i2.setAlias("factory-nonoss");
39   sat::Pool::instance().addRepoSolv(dir / "factory-nonoss.solv", i2);
40   RepoInfo i3; i3.setAlias("zypp_svn");
41   sat::Pool::instance().addRepoSolv(dir / "zypp_svn.solv", i3);
42   RepoInfo i4; i4.setAlias("@System");
43   sat::Pool::instance().addRepoSolv(dir / "@System.solv", i4);
44 }
45
46 BOOST_AUTO_TEST_CASE(pool_query_init)
47 {
48   init_pool();
49 }
50
51 /////////////////////////////////////////////////////////////////////////////
52 //  0xx basic queries
53 /////////////////////////////////////////////////////////////////////////////
54
55 // no conditions, default query
56 // result: all available resolvables
57 BOOST_AUTO_TEST_CASE(pool_query_000)
58 {
59   cout << "****000****"  << endl;
60   PoolQuery q;
61   cout << q.size() << endl;
62   BOOST_CHECK(q.size() == 11451);
63   /**!\todo should be 11453 probably according to:
64    * dumpsolv factory.solv factory-nonoss.solv zypp_svn.solv \@System.solv | \
65    * grep '^name:.*\(noarch\|i386\|i586\|i686\|src\)$' | wc -l
66    */
67 }
68
69 // default query + one search string
70 // q.addString("foo");
71 // result: all resolvables having at least one attribute matching foo
72 BOOST_AUTO_TEST_CASE(pool_query_001)
73 {
74   cout << "****001****"  << endl;
75   PoolQuery q;
76   q.addString("zypper");
77
78   std::for_each(q.begin(), q.end(), &result_cb);
79   BOOST_CHECK(q.size() == 16);
80 }
81
82 // default query + one attribute + one string
83 // q.addAttribute(foo, bar);
84 // should be the same as
85 // q.addAttribute(foo); q.addString(bar);
86 // result: resolvables with foo containing bar
87 BOOST_AUTO_TEST_CASE(pool_query_002)
88 {
89   cout << "****002****"  << endl;
90   PoolQuery q;
91   q.addString("zypper");
92   q.addAttribute(sat::SolvAttr::name);
93
94   std::for_each(q.begin(), q.end(), &result_cb);
95   BOOST_CHECK(q.size() == 6);
96
97   cout << endl;
98
99   PoolQuery q1;
100   q1.addAttribute(sat::SolvAttr::name, "zypper");
101
102   std::for_each(q1.begin(), q1.end(), &result_cb);
103   BOOST_CHECK(q1.size() == 6);
104 }
105
106 // kind filter
107 BOOST_AUTO_TEST_CASE(pool_query_003)
108 {
109   cout << "****003****"  << endl;
110   PoolQuery q;
111   q.addString("zypper");
112   q.addAttribute(sat::SolvAttr::name);
113   q.addKind(ResTraits<Package>::kind);
114
115   std::for_each(q.begin(), q.end(), &result_cb);
116   BOOST_CHECK(q.size() == 3);
117 }
118
119 // match exact
120 BOOST_AUTO_TEST_CASE(pool_query_004)
121 {
122   cout << "****004****"  << endl;
123   PoolQuery q;
124   q.addString("vim");
125   q.addAttribute(sat::SolvAttr::name);
126   q.setMatchExact();
127
128   std::for_each(q.begin(), q.end(), &result_cb);
129   BOOST_CHECK(q.size() == 3);
130
131   PoolQuery q1;
132   q1.addString("zypp");
133   q1.addAttribute(sat::SolvAttr::name);
134   q1.setMatchExact();
135
136   std::for_each(q1.begin(), q1.end(), &result_cb);
137   BOOST_CHECK(q1.empty());
138 }
139
140 // use globs
141 BOOST_AUTO_TEST_CASE(pool_query_005)
142 {
143   cout << "****005****"  << endl;
144   PoolQuery q;
145   q.addString("z?p*");
146   q.addAttribute(sat::SolvAttr::name);
147   q.setMatchGlob();
148
149   std::for_each(q.begin(), q.end(), &result_cb);
150   BOOST_CHECK(q.size() == 11);
151
152   cout << endl;
153
154   PoolQuery q1;
155   q1.addString("*zypp*");
156   q1.addAttribute(sat::SolvAttr::name);
157   q1.setMatchGlob();
158
159   std::for_each(q1.begin(), q1.end(), &result_cb);
160   BOOST_CHECK(q1.size() == 28);
161
162   // should be the same as above
163   PoolQuery q2;
164   q2.addString("zypp");
165   q2.addAttribute(sat::SolvAttr::name);
166
167   BOOST_CHECK(q2.size() == 28);
168 }
169
170 // match by installed status (basically by system vs. repo)
171 BOOST_AUTO_TEST_CASE(pool_query_006)
172 {
173   cout << "****006****"  << endl;
174   PoolQuery q;
175   q.addString("zypper");
176   q.addAttribute(sat::SolvAttr::name);
177   q.setMatchExact();
178   q.setInstalledOnly();
179
180   std::for_each(q.begin(), q.end(), &result_cb);
181   BOOST_CHECK(q.size() == 1);
182
183   cout << endl;
184
185   PoolQuery q1;
186   q1.addString("zypper");
187   q1.addAttribute(sat::SolvAttr::name);
188   q1.setMatchExact();
189   q1.setUninstalledOnly();
190
191   std::for_each(q1.begin(), q1.end(), &result_cb);
192   BOOST_CHECK(q1.size() == 5);
193 }
194
195 /////////////////////////////////////////////////////////////////////////////
196 //  1xx multiple attribute queries
197 /////////////////////////////////////////////////////////////////////////////
198
199 /*
200 BOOST_AUTO_TEST_CASE(pool_query_100)
201 {
202   cout << "****100****"  << endl;
203   PoolQuery q;
204   q.addString("browser");
205   q.addAttribute(sat::SolvAttr::name);
206   q.addAttribute(sat::SolvAttr::summary);
207   q.addAttribute(sat::SolvAttr::description);
208
209   std::for_each(q.begin(), q.end(), &result_cb);
210   BOOST_CHECK(q.size() == 15);
211
212   cout << endl;
213
214   PoolQuery q1;
215   q1.addString("browser");
216   q1.addAttribute(sat::SolvAttr::name);
217
218   std::for_each(q1.begin(), q1.end(), &result_cb);
219   BOOST_CHECK(q1.size() == 5);
220 }
221
222
223 // multi attr (same value) substring matching (case sensitive and insensitive)
224 BOOST_AUTO_TEST_CASE(pool_query_10)
225 {
226   cout << "****10****"  << endl;
227
228   PoolQuery q;
229   q.addString("SSH");
230   q.addAttribute(sat::SolvAttr::name);
231   q.addAttribute(sat::SolvAttr::summary);
232   q.addAttribute(sat::SolvAttr::description);
233
234   std::for_each(q.begin(), q.end(), &result_cb);
235   cout << q.size() << endl;
236   BOOST_CHECK(q.size() == 17);
237
238   cout << endl;
239
240   PoolQuery q2;
241   q2.addString("SSH");
242   q2.addAttribute(sat::SolvAttr::name);
243   q2.addAttribute(sat::SolvAttr::summary);
244   q2.addAttribute(sat::SolvAttr::description);
245   q2.setCaseSensitive();
246
247   std::for_each(q2.begin(), q2.end(), &result_cb);
248   cout << q2.size() << endl;
249 }
250
251
252 // multi attr (same value) glob matching (case sensitive and insensitive)
253 BOOST_AUTO_TEST_CASE(pool_query_11)
254 {
255   cout << "****11****"  << endl;
256   PoolQuery q;
257   q.addString("pack*");
258   q.addAttribute(sat::SolvAttr::name);
259   q.addAttribute(sat::SolvAttr::summary);
260   q.setMatchGlob();
261
262   std::for_each(q.begin(), q.end(), &result_cb);
263   cout << q.asString() <<  endl;
264 //  BOOST_CHECK(q.size() == 11);
265 }
266 */
267
268 /////////////////////////////////////////////////////////////////////////////
269 //  3xx repo filter queries (addRepo(alias_str))
270 /////////////////////////////////////////////////////////////////////////////
271
272 // default query + one attribute(one string) + one repo
273 BOOST_AUTO_TEST_CASE(pool_query_300)
274 {
275   cout << "****300****"  << endl;
276   PoolQuery q;
277   q.addAttribute(sat::SolvAttr::name, "zypper");
278   q.addRepo("zypp_svn");
279
280   std::for_each(q.begin(), q.end(), &result_cb);
281   BOOST_CHECK(q.size() == 3);
282 }
283
284 // default query + one repo
285 BOOST_AUTO_TEST_CASE(pool_query_301)
286 {
287   cout << "****301****"  << endl;
288   PoolQuery q;
289   q.addRepo("zypp_svn");
290
291   std::for_each(q.begin(), q.end(), &result_cb);
292   BOOST_CHECK(q.size() == 21);
293 }
294
295 // multiple repos + one attribute
296 BOOST_AUTO_TEST_CASE(pool_query_302)
297 {
298   cout << "****302****"  << endl;
299   PoolQuery q;
300   q.addString("ma");
301   q.addAttribute(sat::SolvAttr::name);
302   q.addRepo("factory-nonoss");
303   q.addRepo("zypp_svn");
304
305   std::for_each(q.begin(), q.end(), &result_cb);
306   BOOST_CHECK(q.size() == 8);
307 }
308
309 /*
310 BOOST_AUTO_TEST_CASE(pool_query_X)
311 {
312   cout << "****X****"  << endl;
313   PoolQuery q;
314   q.addString("pack*");
315   q.addAttribute(sat::SolvAttr::name);
316
317   std::for_each(q.begin(), q.end(), &result_cb);
318   BOOST_CHECK(q.size() == 28);
319 }
320 */
321
322 // test matching
323 BOOST_AUTO_TEST_CASE(pool_query_equal)
324 {
325   cout << "****equal****"  << endl;
326   PoolQuery q;
327   q.addString("zypp");
328   q.addAttribute(sat::SolvAttr::name);
329   q.setMatchGlob();
330   PoolQuery q2;
331   q2.addString("zypp");
332   q2.addAttribute(sat::SolvAttr::name);
333   q2.setMatchGlob();
334   PoolQuery q3;
335   q3.addString("zypp");
336   q3.addAttribute(sat::SolvAttr::name);
337   q3.setMatchGlob();
338   q3.setRequireAll(true);
339   PoolQuery q4;
340   q4.addAttribute(sat::SolvAttr::name,"zypp");
341   q4.setMatchGlob();
342
343   BOOST_CHECK(equal(q,q2));
344   BOOST_CHECK(!equal(q,q3));
345   //only exact equal! \TODO maybe change
346   BOOST_CHECK(!equal(q,q4));
347   BOOST_CHECK(!equal(q4,q3));
348 }
349
350 // save/load query
351 BOOST_AUTO_TEST_CASE(pool_query_save_restore)
352 {
353 #warning CAN NOT USE A FIX SOLV FILE
354 // must store some raw metadata and generate the solv file
355 // otherwise trestcases break whenever the solv format changes
356 #if 0
357   Pathname dir(TESTS_SRC_DIR);
358   dir += "/zypp/data/PoolQuery";
359
360   ZYpp::Ptr z = getZYpp();
361
362   sat::Pool::instance().addRepoSolv(dir + "foo.solv");
363
364   PoolQuery query;
365   //query.setInstalledOnly();
366   query.execute("kde", &result_cb);
367
368   cout << "search done." << endl;
369
370   query.setMatchExact(true);
371   query.execute("kde", &result_cb);
372
373   cout << "search done." << endl;
374 #endif
375
376 //test recovery from file
377   Pathname pathToQueries(TESTS_SRC_DIR);
378   pathToQueries += "/zypp/data/PoolQuery/savedqueries";
379
380   std::list<PoolQuery> savedQueries;
381
382   std::insert_iterator<std::list<PoolQuery> > ii(savedQueries, savedQueries.end());
383   readPoolQueriesFromFile(pathToQueries,ii);
384   BOOST_CHECK( savedQueries.size() == 2 );
385
386   filesystem::TmpFile tmp;
387   Pathname tmpPath = tmp.path();
388
389   savedQueries.clear();
390
391   PoolQuery q1;
392   PoolQuery q2;
393
394   q1.addKind( Resolvable::Kind::patch );
395   q2.addKind( Resolvable::Kind::patch );
396   q2.addKind( Resolvable::Kind::pattern );
397
398   savedQueries.push_front( q1 );
399   savedQueries.push_front( q2 );
400
401   writePoolQueriesToFile ( tmpPath, savedQueries.begin(), savedQueries.end() );
402   std::insert_iterator<std::list<PoolQuery> > ii2(savedQueries,
403       savedQueries.end());
404   //reread writed queries
405   readPoolQueriesFromFile( tmpPath, ii2);
406   //TODO test if 0==2 and 1==3
407   BOOST_CHECK( savedQueries.size() == 4 );
408
409 }