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