Imported Upstream version 17.25.3
[platform/upstream/libzypp.git] / tests / sat / LookupAttr_test.cc
1 #include "TestSetup.h"
2 #include <zypp/sat/LookupAttr.h>
3 #include <zypp/base/StrMatcher.h>
4 #include <zypp/ResObjects.h>
5
6 static TestSetup test( TestSetup::initLater );
7 struct TestInit {
8   TestInit() {
9     test = TestSetup( Arch_x86_64 );
10   }
11   ~TestInit() { test.reset(); }
12 };
13 BOOST_GLOBAL_FIXTURE( TestInit );
14
15 // Must be the first test!
16 BOOST_AUTO_TEST_CASE(bnc_435838)
17 {
18   // empty @system to pool
19   test.satpool().systemRepo();
20   BOOST_REQUIRE( test.satpool().findSystemRepo() );
21
22   // bnc_435838 crashes if iterating a just created repo.
23   sat::LookupAttr q( sat::SolvAttr::name );
24   for_( it, q.begin(),q.end() )
25     ;
26 }
27
28 BOOST_AUTO_TEST_CASE(LookupAttr_init)
29 {
30   //test.loadTarget(); // initialize and load target
31   test.loadRepo( TESTS_SRC_DIR "/data/openSUSE-11.1" );
32   test.loadRepo( TESTS_SRC_DIR "/data/obs_virtualbox_11_1" );
33   test.loadRepo( TESTS_SRC_DIR "/data/11.0-update" );
34 }
35
36 BOOST_AUTO_TEST_CASE(LookupAttr_defaultconstructed)
37 {
38   sat::LookupAttr q;
39   BOOST_CHECK( q.empty() );
40   BOOST_CHECK( q.size() == 0 );
41   BOOST_CHECK_EQUAL( q.begin(), q.end() );
42 }
43
44 BOOST_AUTO_TEST_CASE(LookupAttr_nonexistingattr)
45 {
46   sat::LookupAttr q( sat::SolvAttr("nonexistingattr") );
47   BOOST_CHECK( q.empty() );
48   BOOST_CHECK( q.size() == 0 );
49   BOOST_CHECK_EQUAL( q.begin(), q.end() );
50 }
51
52 BOOST_AUTO_TEST_CASE(LookupAttr_existingattr)
53 {
54   sat::LookupAttr q( sat::SolvAttr::name );
55   BOOST_CHECK( ! q.empty() );
56   BOOST_CHECK( q.size() != 0 );
57   BOOST_CHECK_NE( q.begin(), q.end() );
58 }
59
60 BOOST_AUTO_TEST_CASE(LookupAttr_existingattr_matcher)
61 {
62   sat::LookupAttr q( sat::SolvAttr::name );
63
64   BOOST_CHECK_THROW( q.setStrMatcher( StrMatcher("[]ypper",Match::REGEX) ), MatchInvalidRegexException );
65   BOOST_CHECK( ! q.strMatcher() );
66   BOOST_CHECK_NO_THROW( q.setStrMatcher( StrMatcher("[zZ]ypper",Match::REGEX) ) );
67   BOOST_CHECK( q.strMatcher() );
68
69   BOOST_CHECK_EQUAL( q.size(), 9 );
70 }
71
72 BOOST_AUTO_TEST_CASE(LookupAttr_iterate_solvables)
73 {
74   // sat::SolvAttr::name query should visit each solvable once.
75   // So query size and containersize are to be equal if we query
76   // pool/repo/solvable. Quick check whether the iterators
77   // position info matches the result.
78
79   sat::Pool satpool( test.satpool() );
80
81   {
82     // iterate all:
83     sat::LookupAttr q( sat::SolvAttr::name );
84     BOOST_CHECK_EQUAL( q.size(), satpool.solvablesSize() );
85     // quick test whether iterator positions actually matches the result:
86     for_( res, q.begin(), q.end() )
87     {
88       BOOST_CHECK_EQUAL( res.inRepo(), res.inSolvable().repository() );
89       BOOST_CHECK_EQUAL( res.inSolvAttr(), sat::SolvAttr::name );
90     }
91   }
92   {
93     unsigned total = 0;
94     for_( it, satpool.reposBegin(), satpool.reposEnd() )
95     {
96       // iterate one repo:
97       sat::LookupAttr q( sat::SolvAttr::name, *it );
98       BOOST_CHECK_EQUAL( q.size(), it->solvablesSize() );
99       total += q.size();
100       // test result actually matches the repo:
101       for_( res, q.begin(), q.end() )
102       {
103         BOOST_CHECK_EQUAL( res.inRepo(), *it );
104         BOOST_CHECK_EQUAL( res.inRepo(), res.inSolvable().repository() );
105         BOOST_CHECK_EQUAL( res.inSolvAttr(), sat::SolvAttr::name );
106       }
107     }
108     BOOST_CHECK_EQUAL( total, satpool.solvablesSize() );
109   }
110   {
111     unsigned total = 0;
112     for_( it, satpool.solvablesBegin(), satpool.solvablesEnd() )
113     {
114       // iterate one solvable:
115       sat::LookupAttr q( sat::SolvAttr::name, *it );
116       BOOST_CHECK_EQUAL( q.size(), 1 );
117       total += q.size();
118       // test result actually matches the solvable:
119       for_( res, q.begin(), q.end() )
120       {
121         BOOST_CHECK_EQUAL( res.inSolvable(), *it );
122         BOOST_CHECK_EQUAL( res.inRepo(), res.inSolvable().repository() );
123         BOOST_CHECK_EQUAL( res.inSolvAttr(), sat::SolvAttr::name );
124      }
125     }
126     BOOST_CHECK_EQUAL( total, satpool.solvablesSize() );
127   }
128 }
129
130 BOOST_AUTO_TEST_CASE(LookupAttr_itetate_all_attributes)
131 {
132   sat::Pool satpool( test.satpool() );
133
134   // iterate all:
135   sat::LookupAttr all( sat::SolvAttr::allAttr );
136
137   {
138     unsigned total = 0;
139     for_( it, satpool.reposBegin(), satpool.reposEnd() )
140     {
141       // iterate one repo:
142       sat::LookupAttr q( sat::SolvAttr::allAttr, *it );
143       total += q.size();
144     }
145     BOOST_CHECK_EQUAL( total, all.size() );
146   }
147   {
148     unsigned total = 0;
149     for_( it, satpool.solvablesBegin(), satpool.solvablesEnd() )
150     {
151       // iterate one solvable:
152       sat::LookupAttr q( sat::SolvAttr::allAttr, *it );
153       total += q.size();
154     }
155     BOOST_CHECK_EQUAL( total, all.size() );
156  }
157 }
158
159 BOOST_AUTO_TEST_CASE(LookupAttr_solvable_attribute_substructure)
160 {
161   sat::LookupAttr q( sat::SolvAttr::updateReference );
162   BOOST_CHECK_EQUAL( q.size(), 303 );
163
164   for_( res, q.begin(), q.end() )
165   {
166     BOOST_CHECK( ! res.subEmpty() );
167     BOOST_CHECK_EQUAL( res.subSize(), 4 );
168
169     BOOST_CHECK_EQUAL( res.subFind( sat::SolvAttr::allAttr ), res.subBegin() );
170     BOOST_CHECK_EQUAL( res.subFind( "" ),                     res.subBegin() );
171
172     BOOST_CHECK_EQUAL( res.subFind( sat::SolvAttr::updateReference ), res.subEnd() );
173     BOOST_CHECK_EQUAL( res.subFind( "noval" ),                        res.subEnd() );
174
175     BOOST_CHECK_NE( res.subFind( sat::SolvAttr::updateReferenceType ),  res.subEnd() );
176     BOOST_CHECK_NE( res.subFind( sat::SolvAttr::updateReferenceHref ),  res.subEnd() );
177     BOOST_CHECK_NE( res.subFind( sat::SolvAttr::updateReferenceId ),    res.subEnd() );
178     BOOST_CHECK_NE( res.subFind( sat::SolvAttr::updateReferenceTitle ), res.subEnd() );
179
180     BOOST_CHECK_EQUAL( res.subFind( sat::SolvAttr::updateReferenceType ),  res.subFind( "type" ) );
181     BOOST_CHECK_EQUAL( res.subFind( sat::SolvAttr::updateReferenceHref ),  res.subFind( "href" ) );
182     BOOST_CHECK_EQUAL( res.subFind( sat::SolvAttr::updateReferenceId ),    res.subFind( "id" ) );
183     BOOST_CHECK_EQUAL( res.subFind( sat::SolvAttr::updateReferenceTitle ), res.subFind( "title" ) );
184
185     // repeatedly calling subBegin() is ok:
186     BOOST_CHECK_EQUAL( res.subFind( sat::SolvAttr::updateReferenceType ).subBegin(),  res.subBegin() );
187   }
188
189   // search substructure id without parent-structure works for wellknown structures:
190   q = sat::LookupAttr( sat::SolvAttr::updateReferenceId );
191   BOOST_CHECK_EQUAL( q.size(), 303 );
192
193   // search id in parent-structure:
194   q = sat::LookupAttr( sat::SolvAttr::updateReferenceId, sat::SolvAttr::updateReference );
195   BOOST_CHECK_EQUAL( q.size(), 303 );
196
197   // search id in any parent-structure:
198   q = sat::LookupAttr( sat::SolvAttr::updateReferenceId, sat::SolvAttr::allAttr );
199   BOOST_CHECK_EQUAL( q.size(), 303 );
200
201   // search any id in parent-structure: (4 ids per updateReference)
202   q = sat::LookupAttr( sat::SolvAttr::allAttr, sat::SolvAttr::updateReference );
203   BOOST_CHECK_EQUAL( q.size(), 1212 );
204
205   // search any id in any parent-structure:
206   // - https://github.com/openSUSE/libsolv/pull/402 added an additional
207   // <update:collection> flexarray to each of the 2287 entries. This is
208   // why the total size varies depending on the libsolv version used.
209   q = sat::LookupAttr( sat::SolvAttr::allAttr, sat::SolvAttr::allAttr );
210   auto qs = q.size();
211   BOOST_CHECK( qs == 10473 || qs == 12760 );
212   // for_(it,q.begin(),q.end())
213   // { cout << it << endl;}
214 }
215
216 BOOST_AUTO_TEST_CASE(LookupAttr_repoattr)
217 {
218   sat::LookupAttr q( sat::SolvAttr::repositoryAddedFileProvides, sat::LookupAttr::REPO_ATTR );
219   BOOST_CHECK( ! q.empty() );
220   BOOST_CHECK_EQUAL( q.size(), 264 );
221
222   sat::LookupRepoAttr p( sat::SolvAttr::repositoryAddedFileProvides );
223   BOOST_CHECK( ! p.empty() );
224   BOOST_REQUIRE_EQUAL( p.size(), q.size() );
225
226   sat::LookupRepoAttr::iterator pit( p.begin() );
227   for_( qit, q.begin(), q.end() )
228   {
229     BOOST_CHECK_EQUAL( qit, pit );
230     ++pit;
231   }
232 }
233
234 #if 0
235 BOOST_AUTO_TEST_CASE(LookupAttr_)
236 {
237   base::LogControl::TmpLineWriter shutUp( new log::FileLineWriter( "/tmp/YLOG" ) );
238   MIL << "GO" << endl;
239 }
240 #endif