3 from db_test import DBDakTestCase
5 from daklib.dbconn import Architecture, Suite, get_suite_architectures, \
6 get_architecture_suites, Maintainer, DBSource, Location, PoolFile, \
7 check_poolfile, get_poolfile_like_name, get_source_in_suite
9 from sqlalchemy.orm.exc import MultipleResultsFound
12 class PackageTestCase(DBDakTestCase):
14 PackageTestCase checks the handling of source and binary packages in dak's
18 def setup_suites(self):
19 "setup a hash of Suite objects in self.suite"
21 if 'suite' in self.__dict__:
24 for suite_name in ('lenny', 'squeeze', 'sid'):
25 self.suite[suite_name] = Suite(suite_name = suite_name, version = '-')
26 self.session.add_all(self.suite.values())
28 def setup_architectures(self):
29 "setup Architecture objects in self.arch and connect to suites"
31 if 'arch' in self.__dict__:
35 for arch_string in ('source', 'all', 'i386', 'amd64', 'kfreebsd-i386'):
36 self.arch[arch_string] = Architecture(arch_string)
37 if arch_string != 'kfreebsd-i386':
38 self.arch[arch_string].suites = self.suite.values()
40 self.arch[arch_string].suites = [self.suite['squeeze'], self.suite['sid']]
41 # hard code ids for source and all
42 self.arch['source'].arch_id = 1
43 self.arch['all'].arch_id = 2
44 self.session.add_all(self.arch.values())
47 super(PackageTestCase, self).setUp()
48 self.setup_architectures()
51 def test_suite_architecture(self):
52 # check the id for architectures source and all
53 self.assertEqual(1, self.arch['source'].arch_id)
54 self.assertEqual(2, self.arch['all'].arch_id)
55 # check the many to many relation between Suite and Architecture
56 self.assertEqual('source', self.suite['lenny'].architectures[0])
57 self.assertEqual(4, len(self.suite['lenny'].architectures))
58 self.assertEqual(3, len(self.arch['i386'].suites))
59 # check the function get_suite_architectures()
60 architectures = get_suite_architectures('lenny', session = self.session)
61 self.assertEqual(4, len(architectures))
62 self.assertTrue(self.arch['source'] in architectures)
63 self.assertTrue(self.arch['all'] in architectures)
64 self.assertTrue(self.arch['kfreebsd-i386'] not in architectures)
65 architectures = get_suite_architectures('sid', session = self.session)
66 self.assertEqual(5, len(architectures))
67 self.assertTrue(self.arch['kfreebsd-i386'] in architectures)
68 architectures = get_suite_architectures('lenny', skipsrc = True, session = self.session)
69 self.assertEqual(3, len(architectures))
70 self.assertTrue(self.arch['source'] not in architectures)
71 architectures = get_suite_architectures('lenny', skipall = True, session = self.session)
72 self.assertEqual(3, len(architectures))
73 self.assertTrue(self.arch['all'] not in architectures)
74 # check the function get_architecture_suites()
75 suites = get_architecture_suites('i386', self.session)
76 self.assertEqual(3, len(suites))
77 self.assertTrue(self.suite['lenny'] in suites)
78 suites = get_architecture_suites('kfreebsd-i386', self.session)
79 self.assertEqual(2, len(suites))
80 self.assertTrue(self.suite['lenny'] not in suites)
82 def setup_locations(self):
83 'create some Location objects, TODO: add component'
85 if 'loc' in self.__dict__:
88 self.loc['main'] = Location(path = \
89 '/srv/ftp-master.debian.org/ftp/pool/')
90 self.session.add(self.loc['main'])
92 def setup_poolfiles(self):
93 'create some PoolFile objects'
95 if 'file' in self.__dict__:
97 self.setup_locations()
99 self.file['hello'] = PoolFile(filename = 'main/h/hello/hello_2.2-2.dsc', \
100 location = self.loc['main'], filesize = 0, md5sum = '')
101 self.file['hello_old'] = PoolFile(filename = 'main/h/hello/hello_2.2-1.dsc', \
102 location = self.loc['main'], filesize = 0, md5sum = '')
103 self.file['sl'] = PoolFile(filename = 'main/s/sl/sl_3.03-16.dsc', \
104 location = self.loc['main'], filesize = 0, md5sum = '')
105 self.file['python'] = PoolFile( \
106 filename = 'main/p/python2.6/python2.6_2.6.6-8.dsc', \
107 location = self.loc['main'], filesize = 0, md5sum = '')
108 self.session.add_all(self.file.values())
110 def test_poolfiles(self):
112 Test the relation of the classes PoolFile and Location.
114 The code needs some explaination. The property Location.files is not a
115 list as in other relations because such a list would become rather
116 huge. It is a query object that can be queried, filtered, and iterated
117 as usual. But list like methods like append() and remove() are
118 supported as well which allows code like:
120 somelocation.files.append(somefile)
123 self.setup_poolfiles()
124 location = self.session.query(Location)[0]
125 self.assertEqual('/srv/ftp-master.debian.org/ftp/pool/', location.path)
126 self.assertEqual(4, location.files.count())
127 poolfile = location.files. \
128 filter(PoolFile.filename.like('%/hello/hello%')). \
129 order_by(PoolFile.filename)[1]
130 self.assertEqual('main/h/hello/hello_2.2-2.dsc', poolfile.filename)
131 self.assertEqual(location, poolfile.location)
133 self.assertEqual(poolfile, \
134 self.session.query(PoolFile).get(poolfile.file_id))
135 self.assertEqual(None, self.session.query(PoolFile).get(-1))
136 # test remove() and append()
137 location.files.remove(self.file['sl'])
138 # TODO: deletion should cascade automatically
139 self.session.delete(self.file['sl'])
140 self.session.refresh(location)
141 self.assertEqual(3, location.files.count())
142 # please note that we intentionally do not specify 'location' here
143 self.file['sl'] = PoolFile(filename = 'main/s/sl/sl_3.03-16.dsc', \
144 filesize = 0, md5sum = '')
145 location.files.append(self.file['sl'])
146 self.session.refresh(location)
147 self.assertEqual(4, location.files.count())
149 self.assertEqual('/srv/ftp-master.debian.org/ftp/pool/main/s/sl/sl_3.03-16.dsc', \
150 self.file['sl'].fullpath)
151 # test check_poolfile()
152 self.assertEqual((True, self.file['sl']), \
153 check_poolfile('main/s/sl/sl_3.03-16.dsc', 0, '', \
154 location.location_id, self.session))
155 self.assertEqual((False, None), \
156 check_poolfile('foobar', 0, '', location.location_id, self.session))
157 self.assertEqual((False, self.file['sl']), \
158 check_poolfile('main/s/sl/sl_3.03-16.dsc', 42, '', \
159 location.location_id, self.session))
160 self.assertEqual((False, self.file['sl']), \
161 check_poolfile('main/s/sl/sl_3.03-16.dsc', 0, 'deadbeef', \
162 location.location_id, self.session))
163 # test get_poolfile_like_name()
164 self.assertEqual([self.file['sl']], \
165 get_poolfile_like_name('sl_3.03-16.dsc', self.session))
166 self.assertEqual([], get_poolfile_like_name('foobar', self.session))
168 def setup_maintainers(self):
169 'create some Maintainer objects'
171 if 'maintainer' in self.__dict__:
174 self.maintainer['maintainer'] = Maintainer(name = 'Mr. Maintainer')
175 self.maintainer['uploader'] = Maintainer(name = 'Mrs. Uploader')
176 self.maintainer['lazyguy'] = Maintainer(name = 'Lazy Guy')
177 self.session.add_all(self.maintainer.values())
179 def setup_sources(self):
180 'create a DBSource object; but it cannot be stored in the DB yet'
182 if 'source' in self.__dict__:
184 self.setup_maintainers()
185 self.setup_poolfiles()
188 self.source['hello'] = DBSource(source = 'hello', version = '2.2-2', \
189 maintainer = self.maintainer['maintainer'], \
190 changedby = self.maintainer['uploader'], \
191 poolfile = self.file['hello'], install_date = self.now())
192 self.source['hello'].suites.append(self.suite['sid'])
193 self.source['hello_old'] = DBSource(source = 'hello', version = '2.2-1', \
194 maintainer = self.maintainer['maintainer'], \
195 changedby = self.maintainer['uploader'], \
196 poolfile = self.file['hello_old'], install_date = self.now())
197 self.source['hello_old'].suites.append(self.suite['sid'])
198 self.source['sl'] = DBSource(source = 'sl', version = '3.03-16', \
199 maintainer = self.maintainer['maintainer'], \
200 changedby = self.maintainer['uploader'], \
201 poolfile = self.file['sl'], install_date = self.now())
202 self.source['sl'].suites.append(self.suite['squeeze'])
203 self.source['sl'].suites.append(self.suite['sid'])
204 self.session.add_all(self.source.values())
206 def test_maintainers(self):
208 tests relation between Maintainer and DBSource
210 TODO: add relations to changes_pending_source
215 maintainer = self.maintainer['maintainer']
216 self.assertEqual(maintainer,
217 self.session.query(Maintainer).get(maintainer.maintainer_id))
218 uploader = self.maintainer['uploader']
219 self.assertEqual(uploader,
220 self.session.query(Maintainer).get(uploader.maintainer_id))
221 lazyguy = self.maintainer['lazyguy']
222 self.assertEqual(lazyguy,
223 self.session.query(Maintainer).get(lazyguy.maintainer_id))
224 self.assertEqual(3, len(maintainer.maintains_sources))
225 self.assertTrue(self.source['hello'] in maintainer.maintains_sources)
226 self.assertEqual(maintainer.changed_sources, [])
227 self.assertEqual(uploader.maintains_sources, [])
228 self.assertEqual(3, len(uploader.changed_sources))
229 self.assertTrue(self.source['sl'] in uploader.changed_sources)
230 self.assertEqual(lazyguy.maintains_sources, [])
231 self.assertEqual(lazyguy.changed_sources, [])
233 def get_source_in_suite_fail(self):
235 This function throws the MultipleResultsFound exception because
236 get_source_in_suite is broken.
238 TODO: fix get_source_in_suite
241 return get_source_in_suite('hello', 'sid', self.session)
243 def test_sources(self):
244 'test relation between DBSource and PoolFile or Suite'
248 self.assertEqual(self.file['hello'], self.source['hello'].poolfile)
249 self.assertEqual(self.source['hello'], self.file['hello'].source)
250 self.assertEqual(None, self.file['python'].source)
252 squeeze = self.session.query(Suite). \
253 filter(Suite.sources.contains(self.source['sl'])). \
254 order_by(Suite.suite_name)[1]
255 self.assertEqual(self.suite['squeeze'], squeeze)
256 self.assertEqual(1, len(squeeze.sources))
257 self.assertEqual(self.source['sl'], squeeze.sources[0])
258 sl = self.session.query(DBSource). \
259 filter(DBSource.suites.contains(self.suite['squeeze'])).one()
260 self.assertEqual(self.source['sl'], sl)
261 self.assertEqual(2, len(sl.suites))
262 self.assertTrue(self.suite['sid'] in sl.suites)
263 # test get_source_in_suite()
264 self.assertRaises(MultipleResultsFound, self.get_source_in_suite_fail)
265 self.assertEqual(None, \
266 get_source_in_suite('hello', 'squeeze', self.session))
267 self.assertEqual(self.source['sl'], \
268 get_source_in_suite('sl', 'sid', self.session))
271 if __name__ == '__main__':