35ad17bd026e6dfc7da0f37c9ed490c940d69cd5
[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   for_(it,q.begin(),q.end())
71   { cout << it << endl;}
72 }
73
74 BOOST_AUTO_TEST_CASE(LookupAttr_iterate_solvables)
75 {
76   // sat::SolvAttr::name query should visit each solvable once.
77   // So query size and containersize are to be equal if we query
78   // pool/repo/solvable. Quick check whether the iterators
79   // position info matches the result.
80
81   sat::Pool satpool( test.satpool() );
82
83   {
84     // iterate all:
85     sat::LookupAttr q( sat::SolvAttr::name );
86     BOOST_CHECK_EQUAL( q.size(), satpool.solvablesSize() );
87     // quick test whether iterator positions actually matches the result:
88     for_( res, q.begin(), q.end() )
89     {
90       BOOST_CHECK_EQUAL( res.inRepo(), res.inSolvable().repository() );
91       BOOST_CHECK_EQUAL( res.inSolvAttr(), sat::SolvAttr::name );
92     }
93   }
94   {
95     unsigned total = 0;
96     for_( it, satpool.reposBegin(), satpool.reposEnd() )
97     {
98       // iterate one repo:
99       sat::LookupAttr q( sat::SolvAttr::name, *it );
100       BOOST_CHECK_EQUAL( q.size(), it->solvablesSize() );
101       total += q.size();
102       // test result actually matches the repo:
103       for_( res, q.begin(), q.end() )
104       {
105         BOOST_CHECK_EQUAL( res.inRepo(), *it );
106         BOOST_CHECK_EQUAL( res.inRepo(), res.inSolvable().repository() );
107         BOOST_CHECK_EQUAL( res.inSolvAttr(), sat::SolvAttr::name );
108       }
109     }
110     BOOST_CHECK_EQUAL( total, satpool.solvablesSize() );
111   }
112   {
113     unsigned total = 0;
114     for_( it, satpool.solvablesBegin(), satpool.solvablesEnd() )
115     {
116       // iterate one solvable:
117       sat::LookupAttr q( sat::SolvAttr::name, *it );
118       BOOST_CHECK_EQUAL( q.size(), 1 );
119       total += q.size();
120       // test result actually matches the solvable:
121       for_( res, q.begin(), q.end() )
122       {
123         BOOST_CHECK_EQUAL( res.inSolvable(), *it );
124         BOOST_CHECK_EQUAL( res.inRepo(), res.inSolvable().repository() );
125         BOOST_CHECK_EQUAL( res.inSolvAttr(), sat::SolvAttr::name );
126      }
127     }
128     BOOST_CHECK_EQUAL( total, satpool.solvablesSize() );
129   }
130 }
131
132 BOOST_AUTO_TEST_CASE(LookupAttr_itetate_all_attributes)
133 {
134   sat::Pool satpool( test.satpool() );
135
136   // iterate all:
137   sat::LookupAttr all( sat::SolvAttr::allAttr );
138
139   {
140     unsigned total = 0;
141     for_( it, satpool.reposBegin(), satpool.reposEnd() )
142     {
143       // iterate one repo:
144       sat::LookupAttr q( sat::SolvAttr::allAttr, *it );
145       total += q.size();
146     }
147     BOOST_CHECK_EQUAL( total, all.size() );
148   }
149   {
150     unsigned total = 0;
151     for_( it, satpool.solvablesBegin(), satpool.solvablesEnd() )
152     {
153       // iterate one solvable:
154       sat::LookupAttr q( sat::SolvAttr::allAttr, *it );
155       total += q.size();
156     }
157     BOOST_CHECK_EQUAL( total, all.size() );
158  }
159 }
160
161 BOOST_AUTO_TEST_CASE(LookupAttr_solvable_attribute_substructure)
162 {
163   sat::LookupAttr q( sat::SolvAttr::updateReference );
164   BOOST_CHECK_EQUAL( q.size(), 303 );
165
166   for_( res, q.begin(), q.end() )
167   {
168     BOOST_CHECK( ! res.subEmpty() );
169     BOOST_CHECK_EQUAL( res.subSize(), 4 );
170
171     BOOST_CHECK_EQUAL( res.subFind( sat::SolvAttr::allAttr ), res.subBegin() );
172     BOOST_CHECK_EQUAL( res.subFind( "" ),                     res.subBegin() );
173
174     BOOST_CHECK_EQUAL( res.subFind( sat::SolvAttr::updateReference ), res.subEnd() );
175     BOOST_CHECK_EQUAL( res.subFind( "noval" ),                        res.subEnd() );
176
177     BOOST_CHECK_NE( res.subFind( sat::SolvAttr::updateReferenceType ),  res.subEnd() );
178     BOOST_CHECK_NE( res.subFind( sat::SolvAttr::updateReferenceHref ),  res.subEnd() );
179     BOOST_CHECK_NE( res.subFind( sat::SolvAttr::updateReferenceId ),    res.subEnd() );
180     BOOST_CHECK_NE( res.subFind( sat::SolvAttr::updateReferenceTitle ), res.subEnd() );
181
182     BOOST_CHECK_EQUAL( res.subFind( sat::SolvAttr::updateReferenceType ),  res.subFind( "type" ) );
183     BOOST_CHECK_EQUAL( res.subFind( sat::SolvAttr::updateReferenceHref ),  res.subFind( "href" ) );
184     BOOST_CHECK_EQUAL( res.subFind( sat::SolvAttr::updateReferenceId ),    res.subFind( "id" ) );
185     BOOST_CHECK_EQUAL( res.subFind( sat::SolvAttr::updateReferenceTitle ), res.subFind( "title" ) );
186
187     // repeatedly calling subBegin() is ok:
188     BOOST_CHECK_EQUAL( res.subFind( sat::SolvAttr::updateReferenceType ).subBegin(),  res.subBegin() );
189   }
190
191   // search substructure id without parent-structure works for wellknown structures:
192   q = sat::LookupAttr( sat::SolvAttr::updateReferenceId );
193   BOOST_CHECK_EQUAL( q.size(), 303 );
194
195   // search id in parent-structure:
196   q = sat::LookupAttr( sat::SolvAttr::updateReferenceId, sat::SolvAttr::updateReference );
197   BOOST_CHECK_EQUAL( q.size(), 303 );
198
199   // search id in any parent-structure:
200   q = sat::LookupAttr( sat::SolvAttr::updateReferenceId, sat::SolvAttr::allAttr );
201   BOOST_CHECK_EQUAL( q.size(), 303 );
202
203   // search any id in parent-structure: (4 ids per updateReference)
204   q = sat::LookupAttr( sat::SolvAttr::allAttr, sat::SolvAttr::updateReference );
205   BOOST_CHECK_EQUAL( q.size(), 1212 );
206
207   // search any id in any parent-structure:
208   q = sat::LookupAttr( sat::SolvAttr::allAttr, sat::SolvAttr::allAttr );
209   BOOST_CHECK_EQUAL( q.size(), 10473 );
210 }
211
212 BOOST_AUTO_TEST_CASE(LookupAttr_repoattr)
213 {
214   sat::LookupAttr q( sat::SolvAttr::repositoryAddedFileProvides, sat::LookupAttr::REPO_ATTR );
215   BOOST_CHECK( ! q.empty() );
216   BOOST_CHECK_EQUAL( q.size(), 264 );
217
218   sat::LookupRepoAttr p( sat::SolvAttr::repositoryAddedFileProvides );
219   BOOST_CHECK( ! p.empty() );
220   BOOST_REQUIRE_EQUAL( p.size(), q.size() );
221
222   sat::LookupRepoAttr::iterator pit( p.begin() );
223   for_( qit, q.begin(), q.end() )
224   {
225     BOOST_CHECK_EQUAL( qit, pit );
226     ++pit;
227   }
228 }
229
230 #if 0
231 BOOST_AUTO_TEST_CASE(LookupAttr_)
232 {
233   base::LogControl::TmpLineWriter shutUp( new log::FileLineWriter( "/tmp/YLOG" ) );
234   MIL << "GO" << endl;
235 }
236 #endif