]> git.decadent.org.uk Git - dak.git/blob - tests/dbtest_packages.py
Make a bidirectional relation DBSource - PoolFile.
[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
8
9 import unittest
10
11 class PackageTestCase(DBDakTestCase):
12     """
13     PackageTestCase checks the handling of source and binary packages in dak's
14     database.
15     """
16
17     def setup_architectures(self):
18         "setup a hash of Architecture objects in self.arch"
19
20         self.arch = {}
21         for arch_string in ('source', 'all', 'i386', 'amd64', 'kfreebsd-i386'):
22             self.arch[arch_string] = Architecture(arch_string)
23         # hard code ids for source and all
24         self.arch['source'].arch_id = 1
25         self.arch['all'].arch_id = 2
26         for _, architecture in self.arch.items():
27             self.session.add(architecture)
28             self.session.flush()
29             self.session.refresh(architecture)
30
31     def setup_suites(self):
32         "setup a hash of Suite objects in self.suite"
33
34         self.suite = {}
35         for suite_name in ('lenny', 'squeeze', 'sid'):
36             suite = Suite(suite_name = suite_name, version = '-')
37             self.suite[suite_name] = suite
38             self.session.add(suite)
39             self.session.flush()
40             self.session.refresh(suite)
41
42     def setUp(self):
43         super(PackageTestCase, self).setUp()
44         self.setup_architectures()
45         self.setup_suites()
46
47     def connect_suite_architectures(self):
48         """
49         Gonnect all suites and all architectures except for kfreebsd-i386 which
50         should not be in lenny.
51         """
52
53         for arch_string, architecture in self.arch.items():
54             if arch_string != 'kfreebsd-i386':
55                 architecture.suites = self.suite.values()
56             else:
57                 architecture.suites = [self.suite['squeeze'], self.suite['sid']]
58
59     def test_suite_architecture(self):
60         # check the id for architectures source and all
61         self.assertEqual(1, self.arch['source'].arch_id)
62         self.assertEqual(2, self.arch['all'].arch_id)
63         # check the many to many relation between Suite and Architecture
64         self.arch['source'].suites.append(self.suite['lenny'])
65         self.assertEqual('source', self.suite['lenny'].architectures[0])
66         self.arch['source'].suites = []
67         self.assertEqual([], self.suite['lenny'].architectures)
68         self.connect_suite_architectures()
69         self.assertEqual(4, len(self.suite['lenny'].architectures))
70         self.assertEqual(3, len(self.arch['i386'].suites))
71         # check the function get_suite_architectures()
72         architectures = get_suite_architectures('lenny', session = self.session)
73         self.assertEqual(4, len(architectures))
74         self.assertTrue(self.arch['source'] in architectures)
75         self.assertTrue(self.arch['all'] in architectures)
76         self.assertTrue(self.arch['kfreebsd-i386'] not in architectures)
77         architectures = get_suite_architectures('sid', session = self.session)
78         self.assertEqual(5, len(architectures))
79         self.assertTrue(self.arch['kfreebsd-i386'] in architectures)
80         architectures = get_suite_architectures('lenny', skipsrc = True, session = self.session)
81         self.assertEqual(3, len(architectures))
82         self.assertTrue(self.arch['source'] not in architectures)
83         architectures = get_suite_architectures('lenny', skipall = True, session = self.session)
84         self.assertEqual(3, len(architectures))
85         self.assertTrue(self.arch['all'] not in architectures)
86         # check the function get_architecture_suites()
87         suites = get_architecture_suites('i386', self.session)
88         self.assertEqual(3, len(suites))
89         self.assertTrue(self.suite['lenny'] in suites)
90         suites = get_architecture_suites('kfreebsd-i386', self.session)
91         self.assertEqual(2, len(suites))
92         self.assertTrue(self.suite['lenny'] not in suites)
93
94     def setup_locations(self):
95         'create some Location objects, TODO: add component'
96
97         self.loc = {}
98         self.loc['main'] = Location(path = \
99             '/srv/ftp-master.debian.org/ftp/pool/')
100         self.session.add(self.loc['main'])
101
102     def setup_poolfiles(self):
103         'create some PoolFile objects'
104
105         self.setup_locations()
106         self.file = {}
107         self.file['hello'] = PoolFile(filename = 'main/h/hello/hello_2.2-2.dsc', \
108             location = self.loc['main'], filesize = 0, md5sum = '')
109         self.file['sl'] = PoolFile(filename = 'main/s/sl/sl_3.03-16.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(2, location.files.count())
130         poolfile = location.files. \
131                 filter(PoolFile.filename.like('%/hello/hello%')).one()
132         self.assertEqual('main/h/hello/hello_2.2-2.dsc', poolfile.filename)
133         self.assertEqual(location, poolfile.location)
134         # test get()
135         self.assertEqual(poolfile, \
136                 self.session.query(PoolFile).get(poolfile.file_id))
137         self.assertEqual(None, self.session.query(PoolFile).get(-1))
138         # test remove() and append()
139         location.files.remove(self.file['sl'])
140         # TODO: deletion should cascade automatically
141         self.session.delete(self.file['sl'])
142         self.session.refresh(location)
143         self.assertEqual(1, location.files.count())
144         # please note that we intentionally do not specify 'location' here
145         self.file['sl'] = PoolFile(filename = 'main/s/sl/sl_3.03-16.dsc', \
146             filesize = 0, md5sum = '')
147         location.files.append(self.file['sl'])
148         self.session.refresh(location)
149         self.assertEqual(2, location.files.count())
150         # test fullpath
151         self.assertEqual('/srv/ftp-master.debian.org/ftp/pool/main/s/sl/sl_3.03-16.dsc', \
152             self.file['sl'].fullpath)
153         # test check_poolfile()
154         self.assertEqual((True, self.file['sl']), \
155             check_poolfile('main/s/sl/sl_3.03-16.dsc', 0, '', \
156                 location.location_id, self.session))
157         self.assertEqual((False, None), \
158             check_poolfile('foobar', 0, '', location.location_id, self.session))
159         self.assertEqual((False, self.file['sl']), \
160             check_poolfile('main/s/sl/sl_3.03-16.dsc', 42, '', \
161                 location.location_id, self.session))
162         self.assertEqual((False, self.file['sl']), \
163             check_poolfile('main/s/sl/sl_3.03-16.dsc', 0, 'deadbeef', \
164                 location.location_id, self.session))
165         # test get_poolfile_like_name()
166         self.assertEqual([self.file['sl']], \
167             get_poolfile_like_name('sl_3.03-16.dsc', self.session))
168         self.assertEqual([], get_poolfile_like_name('foobar', self.session))
169
170     def setup_maintainers(self):
171         'create some Maintainer objects'
172
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         self.session.flush()
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.source = DBSource(source = 'hello', version = '2.2-2', \
186             maintainer = self.maintainer['maintainer'], \
187             changedby = self.maintainer['uploader'], \
188             poolfile = self.file['hello'], install_date = self.now())
189
190     def test_maintainers(self):
191         '''
192         tests relation between Maintainer and DBSource
193
194         TODO: add relations to changes_pending_source
195         '''
196
197         self.setup_sources()
198         maintainer = self.maintainer['maintainer']
199         self.assertEqual(maintainer,
200             self.session.query(Maintainer).get(maintainer.maintainer_id))
201         uploader = self.maintainer['uploader']
202         self.assertEqual(uploader,
203             self.session.query(Maintainer).get(uploader.maintainer_id))
204         lazyguy = self.maintainer['lazyguy']
205         self.assertEqual(lazyguy,
206             self.session.query(Maintainer).get(lazyguy.maintainer_id))
207         self.assertEqual(maintainer.maintains_sources, [self.source])
208         self.assertEqual(maintainer.changed_sources, [])
209         self.assertEqual(uploader.maintains_sources, [])
210         self.assertEqual(uploader.changed_sources, [self.source])
211         self.assertEqual(lazyguy.maintains_sources, [])
212         self.assertEqual(lazyguy.changed_sources, [])
213
214     def test_sources(self):
215         'test relation between DBSource and PoolFile'
216
217         self.setup_sources()
218         poolfile_hello = self.session.query(DBSource)[0].poolfile
219         self.assertEqual(self.file['hello'], poolfile_hello)
220         self.assertEqual(self.source, poolfile_hello.source)
221         poolfile_sl = self.session.query(PoolFile). \
222             filter(PoolFile.filename.like('%/sl/%'))[0]
223         self.assertEqual(None, poolfile_sl.source)
224
225
226 if __name__ == '__main__':
227     unittest.main()