]> git.decadent.org.uk Git - dak.git/blob - tests/dbtest_packages.py
Refactor get_source_in_suite().
[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_suites(self):
19         "setup a hash of Suite objects in self.suite"
20
21         if 'suite' in self.__dict__:
22             return
23         self.suite = {}
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())
27
28     def setup_architectures(self):
29         "setup Architecture objects in self.arch and connect to suites"
30
31         if 'arch' in self.__dict__:
32             return
33         self.setup_suites()
34         self.arch = {}
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()
39             else:
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())
45
46     def setUp(self):
47         super(PackageTestCase, self).setUp()
48         self.setup_architectures()
49         self.setup_suites()
50
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)
81
82     def setup_locations(self):
83         'create some Location objects, TODO: add component'
84
85         if 'loc' in self.__dict__:
86             return
87         self.loc = {}
88         self.loc['main'] = Location(path = \
89             '/srv/ftp-master.debian.org/ftp/pool/')
90         self.session.add(self.loc['main'])
91
92     def setup_poolfiles(self):
93         'create some PoolFile objects'
94
95         if 'file' in self.__dict__:
96             return
97         self.setup_locations()
98         self.file = {}
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())
109
110     def test_poolfiles(self):
111         '''
112         Test the relation of the classes PoolFile and Location.
113
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:
119
120         somelocation.files.append(somefile)
121         '''
122
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)
132         # test get()
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())
148         # test fullpath
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))
167
168     def setup_maintainers(self):
169         'create some Maintainer objects'
170
171         if 'maintainer' in self.__dict__:
172             return
173         self.maintainer = {}
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())
178
179     def setup_sources(self):
180         'create a DBSource object; but it cannot be stored in the DB yet'
181
182         if 'source' in self.__dict__:
183             return
184         self.setup_maintainers()
185         self.setup_poolfiles()
186         self.setup_suites()
187         self.source = {}
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())
205
206     def test_maintainers(self):
207         '''
208         tests relation between Maintainer and DBSource
209
210         TODO: add relations to changes_pending_source
211         '''
212
213         self.setup_sources()
214         self.session.flush()
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, [])
232
233     def get_source_in_suite_fail(self):
234         '''
235         This function throws the MultipleResultsFound exception because
236         get_source_in_suite is broken.
237
238         TODO: fix get_source_in_suite
239         '''
240
241         return get_source_in_suite('hello', 'sid', self.session)
242
243     def test_sources(self):
244         'test relation between DBSource and PoolFile or Suite'
245
246         self.setup_sources()
247         # test PoolFile
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)
251         # test Suite
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))
269
270
271 if __name__ == '__main__':
272     unittest.main()