]> git.decadent.org.uk Git - dak.git/blob - tests/dbtest_packages.py
Merge branch 'master' into dbtests
[dak.git] / tests / dbtest_packages.py
1 #!/usr/bin/env python
2
3 from db_test import DBDakTestCase
4
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, \
8     get_suites_source_in, add_dsc_to_db
9
10 from sqlalchemy.orm.exc import MultipleResultsFound
11 import unittest
12
13 class Pkg():
14     'fake package class used for testing'
15
16     def __init__(self):
17         self.dsc = {}
18         self.files = {}
19         self.changes = {}
20
21 class Upload():
22     'fake Upload class used for testing'
23
24     def __init__(self, pkg):
25         self.pkg = pkg
26
27 class PackageTestCase(DBDakTestCase):
28     """
29     PackageTestCase checks the handling of source and binary packages in dak's
30     database.
31     """
32
33     def setup_suites(self):
34         "setup a hash of Suite objects in self.suite"
35
36         if 'suite' in self.__dict__:
37             return
38         self.suite = {}
39         for suite_name in ('lenny', 'squeeze', 'sid'):
40             self.suite[suite_name] = Suite(suite_name = suite_name, version = '-')
41         self.session.add_all(self.suite.values())
42
43     def setup_architectures(self):
44         "setup Architecture objects in self.arch and connect to suites"
45
46         if 'arch' in self.__dict__:
47             return
48         self.setup_suites()
49         self.arch = {}
50         for arch_string in ('source', 'all', 'i386', 'amd64', 'kfreebsd-i386'):
51             self.arch[arch_string] = Architecture(arch_string)
52             if arch_string != 'kfreebsd-i386':
53                 self.arch[arch_string].suites = self.suite.values()
54             else:
55                 self.arch[arch_string].suites = [self.suite['squeeze'], self.suite['sid']]
56         # hard code ids for source and all
57         self.arch['source'].arch_id = 1
58         self.arch['all'].arch_id = 2
59         self.session.add_all(self.arch.values())
60
61     def setUp(self):
62         super(PackageTestCase, self).setUp()
63         self.setup_architectures()
64         self.setup_suites()
65
66     def test_suite_architecture(self):
67         # check the id for architectures source and all
68         self.assertEqual(1, self.arch['source'].arch_id)
69         self.assertEqual(2, self.arch['all'].arch_id)
70         # check the many to many relation between Suite and Architecture
71         self.assertEqual('source', self.suite['lenny'].architectures[0])
72         self.assertEqual(4, len(self.suite['lenny'].architectures))
73         self.assertEqual(3, len(self.arch['i386'].suites))
74         # check the function get_suite_architectures()
75         architectures = get_suite_architectures('lenny', session = self.session)
76         self.assertEqual(4, len(architectures))
77         self.assertTrue(self.arch['source'] in architectures)
78         self.assertTrue(self.arch['all'] in architectures)
79         self.assertTrue(self.arch['kfreebsd-i386'] not in architectures)
80         architectures = get_suite_architectures('sid', session = self.session)
81         self.assertEqual(5, len(architectures))
82         self.assertTrue(self.arch['kfreebsd-i386'] in architectures)
83         architectures = get_suite_architectures('lenny', skipsrc = True, session = self.session)
84         self.assertEqual(3, len(architectures))
85         self.assertTrue(self.arch['source'] not in architectures)
86         architectures = get_suite_architectures('lenny', skipall = True, session = self.session)
87         self.assertEqual(3, len(architectures))
88         self.assertTrue(self.arch['all'] not in architectures)
89         # check the function get_architecture_suites()
90         suites = get_architecture_suites('i386', self.session)
91         self.assertEqual(3, len(suites))
92         self.assertTrue(self.suite['lenny'] in suites)
93         suites = get_architecture_suites('kfreebsd-i386', self.session)
94         self.assertEqual(2, len(suites))
95         self.assertTrue(self.suite['lenny'] not in suites)
96
97     def setup_locations(self):
98         'create some Location objects, TODO: add component'
99
100         if 'loc' in self.__dict__:
101             return
102         self.loc = {}
103         self.loc['main'] = Location(path = \
104             '/srv/ftp-master.debian.org/ftp/pool/')
105         self.session.add(self.loc['main'])
106
107     def setup_poolfiles(self):
108         'create some PoolFile objects'
109
110         if 'file' in self.__dict__:
111             return
112         self.setup_locations()
113         self.file = {}
114         self.file['hello'] = PoolFile(filename = 'main/h/hello/hello_2.2-2.dsc', \
115             location = self.loc['main'], filesize = 0, md5sum = '')
116         self.file['hello_old'] = PoolFile(filename = 'main/h/hello/hello_2.2-1.dsc', \
117             location = self.loc['main'], filesize = 0, md5sum = '')
118         self.file['sl'] = PoolFile(filename = 'main/s/sl/sl_3.03-16.dsc', \
119             location = self.loc['main'], filesize = 0, md5sum = '')
120         self.file['python'] = PoolFile( \
121             filename = 'main/p/python2.6/python2.6_2.6.6-8.dsc', \
122             location = self.loc['main'], filesize = 0, md5sum = '')
123         self.session.add_all(self.file.values())
124
125     def test_poolfiles(self):
126         '''
127         Test the relation of the classes PoolFile and Location.
128
129         The code needs some explaination. The property Location.files is not a
130         list as in other relations because such a list would become rather
131         huge. It is a query object that can be queried, filtered, and iterated
132         as usual.  But list like methods like append() and remove() are
133         supported as well which allows code like:
134
135         somelocation.files.append(somefile)
136         '''
137
138         self.setup_poolfiles()
139         location = self.session.query(Location)[0]
140         self.assertEqual('/srv/ftp-master.debian.org/ftp/pool/', location.path)
141         self.assertEqual(4, location.files.count())
142         poolfile = location.files. \
143                 filter(PoolFile.filename.like('%/hello/hello%')). \
144                 order_by(PoolFile.filename)[1]
145         self.assertEqual('main/h/hello/hello_2.2-2.dsc', poolfile.filename)
146         self.assertEqual(location, poolfile.location)
147         # test get()
148         self.assertEqual(poolfile, \
149                 self.session.query(PoolFile).get(poolfile.file_id))
150         self.assertEqual(None, self.session.query(PoolFile).get(-1))
151         # test remove() and append()
152         location.files.remove(self.file['sl'])
153         # TODO: deletion should cascade automatically
154         self.session.delete(self.file['sl'])
155         self.session.refresh(location)
156         self.assertEqual(3, location.files.count())
157         # please note that we intentionally do not specify 'location' here
158         self.file['sl'] = PoolFile(filename = 'main/s/sl/sl_3.03-16.dsc', \
159             filesize = 0, md5sum = '')
160         location.files.append(self.file['sl'])
161         self.session.refresh(location)
162         self.assertEqual(4, location.files.count())
163         # test fullpath
164         self.assertEqual('/srv/ftp-master.debian.org/ftp/pool/main/s/sl/sl_3.03-16.dsc', \
165             self.file['sl'].fullpath)
166         # test check_poolfile()
167         self.assertEqual((True, self.file['sl']), \
168             check_poolfile('main/s/sl/sl_3.03-16.dsc', 0, '', \
169                 location.location_id, self.session))
170         self.assertEqual((False, None), \
171             check_poolfile('foobar', 0, '', location.location_id, self.session))
172         self.assertEqual((False, self.file['sl']), \
173             check_poolfile('main/s/sl/sl_3.03-16.dsc', 42, '', \
174                 location.location_id, self.session))
175         self.assertEqual((False, self.file['sl']), \
176             check_poolfile('main/s/sl/sl_3.03-16.dsc', 0, 'deadbeef', \
177                 location.location_id, self.session))
178         # test get_poolfile_like_name()
179         self.assertEqual([self.file['sl']], \
180             get_poolfile_like_name('sl_3.03-16.dsc', self.session))
181         self.assertEqual([], get_poolfile_like_name('foobar', self.session))
182
183     def setup_maintainers(self):
184         'create some Maintainer objects'
185
186         if 'maintainer' in self.__dict__:
187             return
188         self.maintainer = {}
189         self.maintainer['maintainer'] = Maintainer(name = 'Mr. Maintainer')
190         self.maintainer['uploader'] = Maintainer(name = 'Mrs. Uploader')
191         self.maintainer['lazyguy'] = Maintainer(name = 'Lazy Guy')
192         self.session.add_all(self.maintainer.values())
193
194     def setup_sources(self):
195         'create a DBSource object; but it cannot be stored in the DB yet'
196
197         if 'source' in self.__dict__:
198             return
199         self.setup_maintainers()
200         self.setup_poolfiles()
201         self.setup_suites()
202         self.source = {}
203         self.source['hello'] = DBSource(source = 'hello', version = '2.2-2', \
204             maintainer = self.maintainer['maintainer'], \
205             changedby = self.maintainer['uploader'], \
206             poolfile = self.file['hello'], install_date = self.now())
207         self.source['hello'].suites.append(self.suite['sid'])
208         self.source['hello_old'] = DBSource(source = 'hello', version = '2.2-1', \
209             maintainer = self.maintainer['maintainer'], \
210             changedby = self.maintainer['uploader'], \
211             poolfile = self.file['hello_old'], install_date = self.now())
212         self.source['hello_old'].suites.append(self.suite['sid'])
213         self.source['sl'] = DBSource(source = 'sl', version = '3.03-16', \
214             maintainer = self.maintainer['maintainer'], \
215             changedby = self.maintainer['uploader'], \
216             poolfile = self.file['sl'], install_date = self.now())
217         self.source['sl'].suites.append(self.suite['squeeze'])
218         self.source['sl'].suites.append(self.suite['sid'])
219         self.session.add_all(self.source.values())
220
221     def test_maintainers(self):
222         '''
223         tests relation between Maintainer and DBSource
224
225         TODO: add relations to changes_pending_source
226         '''
227
228         self.setup_sources()
229         self.session.flush()
230         maintainer = self.maintainer['maintainer']
231         self.assertEqual(maintainer,
232             self.session.query(Maintainer).get(maintainer.maintainer_id))
233         uploader = self.maintainer['uploader']
234         self.assertEqual(uploader,
235             self.session.query(Maintainer).get(uploader.maintainer_id))
236         lazyguy = self.maintainer['lazyguy']
237         self.assertEqual(lazyguy,
238             self.session.query(Maintainer).get(lazyguy.maintainer_id))
239         self.assertEqual(3, len(maintainer.maintains_sources))
240         self.assertTrue(self.source['hello'] in maintainer.maintains_sources)
241         self.assertEqual(maintainer.changed_sources, [])
242         self.assertEqual(uploader.maintains_sources, [])
243         self.assertEqual(3, len(uploader.changed_sources))
244         self.assertTrue(self.source['sl'] in uploader.changed_sources)
245         self.assertEqual(lazyguy.maintains_sources, [])
246         self.assertEqual(lazyguy.changed_sources, [])
247
248     def get_source_in_suite_fail(self):
249         '''
250         This function throws the MultipleResultsFound exception because
251         get_source_in_suite is broken.
252
253         TODO: fix get_source_in_suite
254         '''
255
256         return get_source_in_suite('hello', 'sid', self.session)
257
258     def test_sources(self):
259         'test relation between DBSource and PoolFile or Suite'
260
261         self.setup_sources()
262         # test PoolFile
263         self.assertEqual(self.file['hello'], self.source['hello'].poolfile)
264         self.assertEqual(self.source['hello'], self.file['hello'].source)
265         self.assertEqual(None, self.file['python'].source)
266         # test Suite
267         squeeze = self.session.query(Suite). \
268             filter(Suite.sources.contains(self.source['sl'])). \
269             order_by(Suite.suite_name)[1]
270         self.assertEqual(self.suite['squeeze'], squeeze)
271         self.assertEqual(1, len(squeeze.sources))
272         self.assertEqual(self.source['sl'], squeeze.sources[0])
273         sl = self.session.query(DBSource). \
274             filter(DBSource.suites.contains(self.suite['squeeze'])).one()
275         self.assertEqual(self.source['sl'], sl)
276         self.assertEqual(2, len(sl.suites))
277         self.assertTrue(self.suite['sid'] in sl.suites)
278         # test get_source_in_suite()
279         self.assertRaises(MultipleResultsFound, self.get_source_in_suite_fail)
280         self.assertEqual(None, \
281             get_source_in_suite('hello', 'squeeze', self.session))
282         self.assertEqual(self.source['sl'], \
283             get_source_in_suite('sl', 'sid', self.session))
284         # test get_suites_source_in()
285         self.assertEqual([self.suite['sid']], \
286             get_suites_source_in('hello', self.session))
287         self.assertEqual(2, len(get_suites_source_in('sl', self.session)))
288         self.assertTrue(self.suite['squeeze'] in \
289             get_suites_source_in('sl', self.session))
290
291     def test_upload(self):
292         'tests function add_dsc_to_db()'
293
294         self.setup_maintainers()
295         self.setup_locations()
296         self.setup_poolfiles()
297         pkg = Pkg()
298         pkg.dsc['source'] = 'hello'
299         pkg.dsc['version'] = '2.2-2'
300         pkg.dsc['maintainer'] = self.maintainer['maintainer'].name
301         pkg.changes['changed-by'] = self.maintainer['uploader'].name
302         pkg.changes['fingerprint'] = 'deadbeef'
303         pkg.changes['distribution'] = { 'sid': '' }
304         self.session.flush()
305         self.session.refresh(self.file['hello'])
306         pkg.files['hello_2.2-2.dsc'] = { \
307             'component': 'main',
308             'location id': self.loc['main'].component_id,
309             'files id': self.file['hello'].file_id }
310         pkg.dsc_files = {}
311         upload = Upload(pkg)
312         (source, dsc_component, dsc_location_id, pfs) = \
313             add_dsc_to_db(upload, 'hello_2.2-2.dsc', self.session)
314         self.session.refresh(source)
315         self.assertEqual('hello', source.source)
316         self.assertEqual('2.2-2', source.version)
317         self.assertEqual('sid', source.suites[0].suite_name)
318         self.assertEqual('main', dsc_component)
319         # no dsc files defined above
320         self.assertEqual(None, dsc_location_id)
321         self.assertEqual([], pfs)
322
323
324 if __name__ == '__main__':
325     unittest.main()