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