]> git.decadent.org.uk Git - dak.git/blob - tests/dbtest_packages.py
Remove obsolete code.
[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         # test get_poolfile_like_name()
117         self.assertEqual([self.file['sl_3.03-16.dsc']], \
118             get_poolfile_like_name('sl_3.03-16.dsc', self.session))
119         self.assertEqual([], get_poolfile_like_name('foobar', self.session))
120
121     def test_maintainers(self):
122         '''
123         tests relation between Maintainer and DBSource
124
125         TODO: add relations to changes_pending_source
126         '''
127
128         maintainer = self.maintainer['maintainer']
129         self.assertEqual(maintainer,
130             self.session.query(Maintainer).get(maintainer.maintainer_id))
131         uploader = self.maintainer['uploader']
132         self.assertEqual(uploader,
133             self.session.query(Maintainer).get(uploader.maintainer_id))
134         lazyguy = self.maintainer['lazyguy']
135         self.assertEqual(lazyguy,
136             self.session.query(Maintainer).get(lazyguy.maintainer_id))
137         self.assertEqual(4, len(maintainer.maintains_sources))
138         self.assertTrue(self.source['hello_2.2-2'] in maintainer.maintains_sources)
139         self.assertEqual(maintainer.changed_sources, [])
140         self.assertEqual(uploader.maintains_sources, [])
141         self.assertEqual(4, len(uploader.changed_sources))
142         self.assertTrue(self.source['sl_3.03-16'] in uploader.changed_sources)
143         self.assertEqual(lazyguy.maintains_sources, [])
144         self.assertEqual(lazyguy.changed_sources, [])
145
146     def get_source_in_suite_fail(self):
147         '''
148         This function throws the MultipleResultsFound exception because
149         get_source_in_suite is broken.
150
151         TODO: fix get_source_in_suite
152         '''
153
154         return get_source_in_suite('hello', 'sid', self.session)
155
156     def test_sources(self):
157         'test relation between DBSource and PoolFile or Suite'
158
159         # test PoolFile
160         self.assertEqual(self.file['hello_2.2-2.dsc'], self.source['hello_2.2-2'].poolfile)
161         self.assertEqual(self.source['hello_2.2-2'], self.file['hello_2.2-2.dsc'].source)
162         self.assertEqual(None, self.file['python2.6_2.6.6-8.dsc'].source)
163         # test Suite
164         squeeze = self.session.query(Suite). \
165             filter(Suite.sources.contains(self.source['sl_3.03-16'])). \
166             order_by(Suite.suite_name)[1]
167         self.assertEqual(self.suite['squeeze'], squeeze)
168         self.assertEqual(1, squeeze.sources.count())
169         self.assertEqual(self.source['sl_3.03-16'], squeeze.sources[0])
170         sl = self.session.query(DBSource). \
171             filter(DBSource.suites.contains(self.suite['squeeze'])).one()
172         self.assertEqual(self.source['sl_3.03-16'], sl)
173         self.assertEqual(2, len(sl.suites))
174         self.assertTrue(self.suite['sid'] in sl.suites)
175         # test get_source_in_suite()
176         self.assertRaises(MultipleResultsFound, self.get_source_in_suite_fail)
177         self.assertEqual(None, \
178             get_source_in_suite('hello', 'squeeze', self.session))
179         self.assertEqual(self.source['sl_3.03-16'], \
180             get_source_in_suite('sl', 'sid', self.session))
181         # test get_suites_source_in()
182         self.assertEqual([self.suite['sid']], \
183             get_suites_source_in('hello', self.session))
184         self.assertEqual(2, len(get_suites_source_in('sl', self.session)))
185         self.assertTrue(self.suite['squeeze'] in \
186             get_suites_source_in('sl', self.session))
187
188     def test_add_dsc_to_db(self):
189         'tests function add_dsc_to_db()'
190
191         pkg = Pkg()
192         pkg.dsc['source'] = 'hello'
193         pkg.dsc['version'] = '2.2-3'
194         pkg.dsc['maintainer'] = self.maintainer['maintainer'].name
195         pkg.changes['changed-by'] = self.maintainer['uploader'].name
196         pkg.changes['fingerprint'] = 'deadbeef'
197         pkg.changes['distribution'] = { 'sid': '' }
198         pkg.files['hello_2.2-3.dsc'] = { \
199             'component': 'main',
200             'location id': self.loc['main'].location_id,
201             'files id': self.file['hello_2.2-3.dsc'].file_id }
202         pkg.dsc_files = {}
203         upload = Upload(pkg)
204         (source, dsc_component, dsc_location_id, pfs) = \
205             add_dsc_to_db(upload, 'hello_2.2-3.dsc', self.session)
206         self.assertEqual('hello', source.source)
207         self.assertEqual('2.2-3', source.version)
208         self.assertEqual('sid', source.suites[0].suite_name)
209         self.assertEqual('main', dsc_component)
210         self.assertEqual(self.loc['main'].location_id, dsc_location_id)
211         self.assertEqual([], pfs)
212
213     def test_source_exists(self):
214         'test function source_exists()'
215
216         hello = self.source['hello_2.2-2']
217         self.assertTrue(source_exists(hello.source, hello.version, \
218             suites = ['sid'], session = self.session))
219         # binNMU
220         self.assertTrue(source_exists(hello.source, hello.version + '+b7', \
221             suites = ['sid'], session = self.session))
222         self.assertTrue(not source_exists(hello.source, hello.version, \
223             suites = ['lenny', 'squeeze'], session = self.session))
224         self.assertTrue(not source_exists(hello.source, hello.version, \
225             suites = ['lenny', 'sid'], session = self.session))
226         self.assertTrue(not source_exists(hello.source, hello.version, \
227             suites = ['sid', 'lenny'], session = self.session))
228         self.assertTrue(not source_exists(hello.source, '0815', \
229             suites = ['sid'], session = self.session))
230         # 'any' suite
231         self.assertTrue(source_exists(hello.source, hello.version, \
232             session = self.session))
233
234     def test_get_suite_version_by_source(self):
235         'test function get_suite_version_by_source()'
236
237         result = get_suite_version_by_source('hello', self.session)
238         self.assertEqual(2, len(result))
239         self.assertTrue(('sid', '2.2-1') in result)
240         self.assertTrue(('sid', '2.2-2') in result)
241         result = get_suite_version_by_source('sl', self.session)
242         self.assertEqual(2, len(result))
243         self.assertTrue(('squeeze', '3.03-16') in result)
244         self.assertTrue(('sid', '3.03-16') in result)
245
246     def test_binaries(self):
247         '''
248         tests class DBBinary; TODO: test relation with Architecture, Maintainer,
249         PoolFile, and Fingerprint
250         '''
251
252         # test Suite relation
253         self.assertEqual(3, self.suite['sid'].binaries.count())
254         self.assertTrue(self.binary['hello_2.2-1_i386'] in \
255             self.suite['sid'].binaries.all())
256         self.assertEqual(0, self.suite['lenny'].binaries.count())
257         # test DBSource relation
258         self.assertEqual(3, len(self.source['hello_2.2-1'].binaries))
259         self.assertTrue(self.binary['hello_2.2-1_i386'] in \
260             self.source['hello_2.2-1'].binaries)
261         self.assertEqual(0, len(self.source['hello_2.2-2'].binaries))
262         # test get_suites_binary_in()
263         self.assertEqual(2, len(get_suites_binary_in('hello', self.session)))
264         self.assertTrue(self.suite['sid'] in \
265             get_suites_binary_in('hello', self.session))
266         self.assertEqual(2, len(get_suites_binary_in('gnome-hello', self.session)))
267         self.assertTrue(self.suite['squeeze'] in \
268             get_suites_binary_in('gnome-hello', self.session))
269         self.assertEqual(0, len(get_suites_binary_in('sl', self.session)))
270
271     def test_add_deb_to_db(self):
272         'tests function add_deb_to_db()'
273
274         pkg = Pkg()
275         pkg.changes['fingerprint'] = 'deadbeef'
276         pkg.changes['distribution'] = { 'sid': '' }
277         pkg.files['hello_2.2-2_i386.deb'] = { \
278             'package': 'hello',
279             'version': '2.2-2',
280             'maintainer': self.maintainer['maintainer'].name,
281             'architecture': 'i386',
282             'dbtype': 'deb',
283             'pool name': 'main/h/hello/',
284             'location id': self.loc['main'].location_id,
285             'source package': 'hello',
286             'source version': '2.2-2',
287             'size': 0,
288             'md5sum': 'deadbeef',
289             'sha1sum': 'deadbeef',
290             'sha256sum': 'deadbeef'}
291         upload = Upload(pkg)
292         bin, poolfile = add_deb_to_db(upload, 'hello_2.2-2_i386.deb', self.session)
293         self.session.refresh(poolfile)
294         self.session.refresh(poolfile.binary)
295         self.assertEqual('main/h/hello/hello_2.2-2_i386.deb', poolfile.filename)
296         self.assertEqual('hello', poolfile.binary.package)
297         self.assertEqual('2.2-2', poolfile.binary.version)
298         self.assertEqual(['sid'], poolfile.binary.suites)
299         self.assertEqual('Mr. Maintainer', poolfile.binary.maintainer.name)
300         self.assertEqual('i386', poolfile.binary.architecture.arch_string)
301         self.assertEqual('deb', poolfile.binary.binarytype)
302         self.assertEqual(self.loc['main'], poolfile.location)
303         self.assertEqual(self.source['hello_2.2-2'], poolfile.binary.source)
304         self.assertEqual(0, poolfile.filesize)
305         self.assertEqual('deadbeef', poolfile.md5sum)
306         self.assertEqual('deadbeef', poolfile.sha1sum)
307         self.assertEqual('deadbeef', poolfile.sha256sum)
308
309     def test_get_suite_version_by_package(self):
310         'test function get_suite_version_by_package()'
311
312         result = get_suite_version_by_package('hello', 'i386', self.session)
313         self.assertEqual(2, len(result))
314         self.assertTrue(('sid', '2.2-1') in result)
315         result = get_suite_version_by_package('hello', 'amd64', self.session)
316         self.assertEqual(0, len(result))
317         result = get_suite_version_by_package('python-hello', 'i386', self.session)
318         self.assertEqual([('squeeze', '2.2-1')], result)
319         result = get_suite_version_by_package('python-hello', 'amd64', self.session)
320         self.assertEqual([('squeeze', '2.2-1')], result)
321
322     def test_components(self):
323         'test class Component'
324
325         self.assertEqual([self.loc['main']], self.comp['main'].location)
326         self.assertEqual([self.loc['contrib']], self.comp['contrib'].location)
327         self.assertEqual(0, self.comp['main'].overrides.count())
328
329     def test_get_component_by_package_suite(self):
330         'test get_component_by_package_suite()'
331
332         result = get_component_by_package_suite('hello', ['sid'], \
333             session = self.session)
334         self.assertEqual('main', result)
335         result = get_component_by_package_suite('hello', ['hamm'], \
336             session = self.session)
337         self.assertEqual(None, result)
338         result = get_component_by_package_suite('foobar', ['sid'], \
339             session = self.session)
340         self.assertEqual(None, result)
341         # test that the newest version is returned
342         result = get_component_by_package_suite('gnome-hello', ['squeeze'], \
343             session = self.session)
344         self.assertEqual('main', result)
345         result = get_component_by_package_suite('gnome-hello', ['sid'], \
346             session = self.session)
347         self.assertEqual('contrib', result)
348         # test arch_list
349         result = get_component_by_package_suite('hello', ['sid'], \
350             arch_list = ['i386'], session = self.session)
351         self.assertEqual('main', result)
352         result = get_component_by_package_suite('hello', ['sid'], \
353             arch_list = ['amd64'], session = self.session)
354         self.assertEqual(None, result)
355
356 if __name__ == '__main__':
357     unittest.main()