4 #include <boost/test/auto_unit_test.hpp>
7 #include "zypp/ZYppFactory.h"
8 #include "zypp/PoolQuery.h"
9 #include "zypp/PoolQueryUtil.tcc"
10 #include "zypp/TmpPath.h"
12 #define BOOST_TEST_MODULE PoolQuery
18 using namespace boost::unit_test;
20 bool result_cb( const sat::Solvable & solvable )
22 zypp::PoolItem pi( zypp::ResPool::instance().find( solvable ) );
23 cout << pi.resolvable() << endl;
24 // name: yast2-sound 2.16.2-9 i586
28 static void init_pool()
30 Pathname dir(TESTS_SRC_DIR);
31 dir += "/zypp/data/PoolQuery";
33 ZYpp::Ptr z = getZYpp();
34 ZConfig::instance().setSystemArchitecture(Arch("i586"));
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);
46 BOOST_AUTO_TEST_CASE(pool_query_init)
51 /////////////////////////////////////////////////////////////////////////////
53 /////////////////////////////////////////////////////////////////////////////
55 // no conditions, default query
56 // result: all available resolvables
57 BOOST_AUTO_TEST_CASE(pool_query_000)
59 cout << "****000****" << endl;
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
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)
74 cout << "****001****" << endl;
76 q.addString("zypper");
78 std::for_each(q.begin(), q.end(), &result_cb);
79 BOOST_CHECK(q.size() == 16);
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)
89 cout << "****002****" << endl;
91 q.addString("zypper");
92 q.addAttribute(sat::SolvAttr::name);
94 std::for_each(q.begin(), q.end(), &result_cb);
95 BOOST_CHECK(q.size() == 6);
100 q1.addAttribute(sat::SolvAttr::name, "zypper");
102 std::for_each(q1.begin(), q1.end(), &result_cb);
103 BOOST_CHECK(q1.size() == 6);
107 BOOST_AUTO_TEST_CASE(pool_query_003)
109 cout << "****003****" << endl;
111 q.addString("zypper");
112 q.addAttribute(sat::SolvAttr::name);
113 q.addKind(ResTraits<Package>::kind);
115 std::for_each(q.begin(), q.end(), &result_cb);
116 BOOST_CHECK(q.size() == 3);
120 BOOST_AUTO_TEST_CASE(pool_query_004)
122 cout << "****004****" << endl;
125 q.addAttribute(sat::SolvAttr::name);
128 std::for_each(q.begin(), q.end(), &result_cb);
129 BOOST_CHECK(q.size() == 3);
132 q1.addString("zypp");
133 q1.addAttribute(sat::SolvAttr::name);
136 std::for_each(q1.begin(), q1.end(), &result_cb);
137 BOOST_CHECK(q1.empty());
141 BOOST_AUTO_TEST_CASE(pool_query_005)
143 cout << "****005.1****" << endl;
146 q.addAttribute(sat::SolvAttr::name);
149 std::for_each(q.begin(), q.end(), &result_cb);
150 BOOST_CHECK(q.size() == 11);
152 cout << "****005.2****" << endl;
155 q1.addString("*zypp*");
156 q1.addAttribute(sat::SolvAttr::name);
159 std::for_each(q1.begin(), q1.end(), &result_cb);
160 BOOST_CHECK(q1.size() == 28);
162 cout << "****005.3****" << endl;
164 // should be the same as above
166 q2.addString("zypp");
167 q2.addAttribute(sat::SolvAttr::name);
169 BOOST_CHECK(q2.size() == 28);
173 BOOST_AUTO_TEST_CASE(pool_query_006)
175 cout << "****006.1***" << endl;
177 // should be the same as 005 1
179 q.addString("^z.p.*");
180 q.addAttribute(sat::SolvAttr::name);
183 std::for_each(q.begin(), q.end(), &result_cb);
184 BOOST_CHECK(q.size() == 11);
186 cout << "****006.2***" << endl;
189 q1.addString("zypper|smart");
190 q1.addAttribute(sat::SolvAttr::name);
193 std::for_each(q1.begin(), q1.end(), &result_cb);
194 BOOST_CHECK(q1.size() == 21);
196 cout << "****006.3***" << endl;
200 q2.addString("zypp\\");
202 BOOST_CHECK_THROW(q2.size(), Exception);
206 // match by installed status (basically by system vs. repo)
207 BOOST_AUTO_TEST_CASE(pool_query_050)
209 cout << "****050****" << endl;
211 q.addString("zypper");
212 q.addAttribute(sat::SolvAttr::name);
214 q.setInstalledOnly();
216 std::for_each(q.begin(), q.end(), &result_cb);
217 BOOST_CHECK(q.size() == 1);
222 q1.addString("zypper");
223 q1.addAttribute(sat::SolvAttr::name);
225 q1.setUninstalledOnly();
227 std::for_each(q1.begin(), q1.end(), &result_cb);
228 BOOST_CHECK(q1.size() == 5);
231 /////////////////////////////////////////////////////////////////////////////
232 // 1xx multiple attribute queries
233 /////////////////////////////////////////////////////////////////////////////
236 BOOST_AUTO_TEST_CASE(pool_query_100)
238 cout << "****100****" << endl;
240 /* This string is found sometimes only in solvable names (e.g. novell-lum),
241 sometimes only in summary (e.g. yast2-casa-ats) and sometimes only
242 in descriptions (e.g. beagle-quickfinder). novell-lum doesn't exist
243 in our test solv file, but let's ignore this. I didn't find a string
244 with the same characteristics giving fewer matches :-/ */
245 q.addString("novell");
246 q.addAttribute(sat::SolvAttr::name);
247 q.addAttribute(sat::SolvAttr::summary);
248 q.addAttribute(sat::SolvAttr::description);
250 std::for_each(q.begin(), q.end(), &result_cb);
251 BOOST_CHECK(q.size() == 74);
257 q1.addAttribute(sat::SolvAttr::name);
259 std::for_each(q1.begin(), q1.end(), &result_cb);
260 BOOST_CHECK(q1.size() == 7);
263 // multi attr (same value) substring matching (case sensitive and insensitive)
264 BOOST_AUTO_TEST_CASE(pool_query_101)
266 cout << "****101****" << endl;
270 q.addAttribute(sat::SolvAttr::name);
271 q.addAttribute(sat::SolvAttr::summary);
272 q.addAttribute(sat::SolvAttr::description);
274 std::for_each(q.begin(), q.end(), &result_cb);
275 BOOST_CHECK(q.size() == 30);
280 q2.addString("ZYpp");
281 q2.addAttribute(sat::SolvAttr::name);
282 q2.addAttribute(sat::SolvAttr::summary);
283 q2.addAttribute(sat::SolvAttr::description);
284 q2.setCaseSensitive();
286 std::for_each(q2.begin(), q2.end(), &result_cb);
287 BOOST_CHECK(q2.size() == 2);
291 // multi attr (same value) glob matching (case sensitive and insensitive)
292 BOOST_AUTO_TEST_CASE(pool_query_102)
294 cout << "****102****" << endl;
296 q.addString("pack*");
297 q.addAttribute(sat::SolvAttr::name);
298 q.addAttribute(sat::SolvAttr::summary);
301 std::for_each(q.begin(), q.end(), &result_cb);
302 BOOST_CHECK(q.size() == 35);
306 /////////////////////////////////////////////////////////////////////////////
307 // 3xx repo filter queries (addRepo(alias_str))
308 /////////////////////////////////////////////////////////////////////////////
310 // default query + one attribute(one string) + one repo
311 BOOST_AUTO_TEST_CASE(pool_query_300)
313 cout << "****300****" << endl;
315 q.addAttribute(sat::SolvAttr::name, "zypper");
316 q.addRepo("zypp_svn");
318 std::for_each(q.begin(), q.end(), &result_cb);
319 BOOST_CHECK(q.size() == 3);
322 // default query + one repo
323 BOOST_AUTO_TEST_CASE(pool_query_301)
325 cout << "****301****" << endl;
327 q.addRepo("zypp_svn");
329 std::for_each(q.begin(), q.end(), &result_cb);
330 BOOST_CHECK(q.size() == 21);
333 // multiple repos + one attribute
334 BOOST_AUTO_TEST_CASE(pool_query_302)
336 cout << "****302****" << endl;
339 q.addAttribute(sat::SolvAttr::name);
340 q.addRepo("factory-nonoss");
341 q.addRepo("zypp_svn");
343 std::for_each(q.begin(), q.end(), &result_cb);
344 BOOST_CHECK(q.size() == 8);
348 BOOST_AUTO_TEST_CASE(pool_query_X)
350 cout << "****X****" << endl;
352 q.addString("pack*");
353 q.addAttribute(sat::SolvAttr::name);
355 std::for_each(q.begin(), q.end(), &result_cb);
356 BOOST_CHECK(q.size() == 28);
361 BOOST_AUTO_TEST_CASE(pool_query_equal)
363 cout << "****equal****" << endl;
366 q.addAttribute(sat::SolvAttr::name);
369 q2.addString("zypp");
370 q2.addAttribute(sat::SolvAttr::name);
373 q3.addString("zypp");
374 q3.addAttribute(sat::SolvAttr::name);
376 q3.setRequireAll(true);
378 q4.addAttribute(sat::SolvAttr::name,"zypp");
381 BOOST_CHECK(equal(q,q2));
382 BOOST_CHECK(!equal(q,q3));
383 //only exact equal! \TODO maybe change
384 BOOST_CHECK(!equal(q,q4));
385 BOOST_CHECK(!equal(q4,q3));
389 BOOST_AUTO_TEST_CASE(pool_query_save_restore)
391 #warning CAN NOT USE A FIX SOLV FILE
392 // must store some raw metadata and generate the solv file
393 // otherwise trestcases break whenever the solv format changes
395 Pathname dir(TESTS_SRC_DIR);
396 dir += "/zypp/data/PoolQuery";
398 ZYpp::Ptr z = getZYpp();
400 sat::Pool::instance().addRepoSolv(dir + "foo.solv");
403 //query.setInstalledOnly();
404 query.execute("kde", &result_cb);
406 cout << "search done." << endl;
408 query.setMatchExact(true);
409 query.execute("kde", &result_cb);
411 cout << "search done." << endl;
414 //test recovery from file
415 Pathname pathToQueries(TESTS_SRC_DIR);
416 pathToQueries += "/zypp/data/PoolQuery/savedqueries";
418 std::list<PoolQuery> savedQueries;
420 std::insert_iterator<std::list<PoolQuery> > ii(savedQueries, savedQueries.end());
421 readPoolQueriesFromFile(pathToQueries,ii);
422 BOOST_CHECK( savedQueries.size() == 2 );
424 filesystem::TmpFile tmp;
425 Pathname tmpPath = tmp.path();
427 savedQueries.clear();
432 q1.addKind( Resolvable::Kind::patch );
433 q2.addKind( Resolvable::Kind::patch );
434 q2.addKind( Resolvable::Kind::pattern );
436 savedQueries.push_front( q1 );
437 savedQueries.push_front( q2 );
439 writePoolQueriesToFile ( tmpPath, savedQueries.begin(), savedQueries.end() );
440 std::insert_iterator<std::list<PoolQuery> > ii2(savedQueries,
442 //reread writed queries
443 readPoolQueriesFromFile( tmpPath, ii2);
444 //TODO test if 0==2 and 1==3
445 BOOST_CHECK( savedQueries.size() == 4 );