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