]> git.decadent.org.uk Git - dak.git/blob - tests/dbtest_packages.py
Merge branch 'master' into dbtests
[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         self.session.add_all(self.arch.values())
27
28     def setup_suites(self):
29         "setup a hash of Suite objects in self.suite"
30
31         self.suite = {}
32         for suite_name in ('lenny', 'squeeze', 'sid'):
33             self.suite[suite_name] = Suite(suite_name = suite_name, version = '-')
34         self.session.add_all(self.suite.values())
35
36     def setUp(self):
37         super(PackageTestCase, self).setUp()
38         self.setup_architectures()
39         self.setup_suites()
40
41     def connect_suite_architectures(self):
42         """
43         Gonnect all suites and all architectures except for kfreebsd-i386 which
44         should not be in lenny.
45         """
46
47         for arch_string, architecture in self.arch.items():
48             if arch_string != 'kfreebsd-i386':
49                 architecture.suites = self.suite.values()
50             else:
51                 architecture.suites = [self.suite['squeeze'], self.suite['sid']]
52
53     def test_suite_architecture(self):
54         # check the id for architectures source and all
55         self.assertEqual(1, self.arch['source'].arch_id)
56         self.assertEqual(2, self.arch['all'].arch_id)
57         # check the many to many relation between Suite and Architecture
58         self.arch['source'].suites.append(self.suite['lenny'])
59         self.assertEqual('source', self.suite['lenny'].architectures[0])
60         self.arch['source'].suites = []
61         self.assertEqual([], self.suite['lenny'].architectures)
62         self.connect_suite_architectures()
63         self.assertEqual(4, len(self.suite['lenny'].architectures))
64         self.assertEqual(3, len(self.arch['i386'].suites))
65         # check the function get_suite_architectures()
66         architectures = get_suite_architectures('lenny', session = self.session)
67         self.assertEqual(4, len(architectures))
68         self.assertTrue(self.arch['source'] in architectures)
69         self.assertTrue(self.arch['all'] in architectures)
70         self.assertTrue(self.arch['kfreebsd-i386'] not in architectures)
71         architectures = get_suite_architectures('sid', session = self.session)
72         self.assertEqual(5, len(architectures))
73         self.assertTrue(self.arch['kfreebsd-i386'] in architectures)
74         architectures = get_suite_architectures('lenny', skipsrc = True, session = self.session)
75         self.assertEqual(3, len(architectures))
76         self.assertTrue(self.arch['source'] not in architectures)
77         architectures = get_suite_architectures('lenny', skipall = True, session = self.session)
78         self.assertEqual(3, len(architectures))
79         self.assertTrue(self.arch['all'] not in architectures)
80         # check the function get_architecture_suites()
81         suites = get_architecture_suites('i386', self.session)
82         self.assertEqual(3, len(suites))
83         self.assertTrue(self.suite['lenny'] in suites)
84         suites = get_architecture_suites('kfreebsd-i386', self.session)
85         self.assertEqual(2, len(suites))
86         self.assertTrue(self.suite['lenny'] not in suites)
87
88     def setup_locations(self):
89         'create some Location objects, TODO: add component'
90
91         self.loc = {}
92         self.loc['main'] = Location(path = \
93             '/srv/ftp-master.debian.org/ftp/pool/')
94         self.session.add(self.loc['main'])
95
96     def setup_poolfiles(self):
97         'create some PoolFile objects'
98
99         self.setup_locations()
100         self.file = {}
101         self.file['hello'] = PoolFile(filename = 'main/h/hello/hello_2.2-2.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.session.add_all(self.file.values())
106
107     def test_poolfiles(self):
108         '''
109         Test the relation of the classes PoolFile and Location.
110
111         The code needs some explaination. The property Location.files is not a
112         list as in other relations because such a list would become rather
113         huge. It is a query object that can be queried, filtered, and iterated
114         as usual.  But list like methods like append() and remove() are
115         supported as well which allows code like:
116
117         somelocation.files.append(somefile)
118         '''
119
120         self.setup_poolfiles()
121         location = self.session.query(Location)[0]
122         self.assertEqual('/srv/ftp-master.debian.org/ftp/pool/', location.path)
123         self.assertEqual(2, location.files.count())
124         poolfile = location.files. \
125                 filter(PoolFile.filename.like('%/hello/hello%')).one()
126         self.assertEqual('main/h/hello/hello_2.2-2.dsc', poolfile.filename)
127         self.assertEqual(location, poolfile.location)
128         # test get()
129         self.assertEqual(poolfile, \
130                 self.session.query(PoolFile).get(poolfile.file_id))
131         self.assertEqual(None, self.session.query(PoolFile).get(-1))
132         # test remove() and append()
133         location.files.remove(self.file['sl'])
134         # TODO: deletion should cascade automatically
135         self.session.delete(self.file['sl'])
136         self.session.refresh(location)
137         self.assertEqual(1, location.files.count())
138         # please note that we intentionally do not specify 'location' here
139         self.file['sl'] = PoolFile(filename = 'main/s/sl/sl_3.03-16.dsc', \
140             filesize = 0, md5sum = '')
141         location.files.append(self.file['sl'])
142         self.session.refresh(location)
143         self.assertEqual(2, location.files.count())
144         # test fullpath
145         self.assertEqual('/srv/ftp-master.debian.org/ftp/pool/main/s/sl/sl_3.03-16.dsc', \
146             self.file['sl'].fullpath)
147         # test check_poolfile()
148         self.assertEqual((True, self.file['sl']), \
149             check_poolfile('main/s/sl/sl_3.03-16.dsc', 0, '', \
150                 location.location_id, self.session))
151         self.assertEqual((False, None), \
152             check_poolfile('foobar', 0, '', location.location_id, self.session))
153         self.assertEqual((False, self.file['sl']), \
154             check_poolfile('main/s/sl/sl_3.03-16.dsc', 42, '', \
155                 location.location_id, self.session))
156         self.assertEqual((False, self.file['sl']), \
157             check_poolfile('main/s/sl/sl_3.03-16.dsc', 0, 'deadbeef', \
158                 location.location_id, self.session))
159         # test get_poolfile_like_name()
160         self.assertEqual([self.file['sl']], \
161             get_poolfile_like_name('sl_3.03-16.dsc', self.session))
162         self.assertEqual([], get_poolfile_like_name('foobar', self.session))
163
164     def setup_maintainers(self):
165         'create some Maintainer objects'
166
167         self.maintainer = {}
168         self.maintainer['maintainer'] = Maintainer(name = 'Mr. Maintainer')
169         self.maintainer['uploader'] = Maintainer(name = 'Mrs. Uploader')
170         self.maintainer['lazyguy'] = Maintainer(name = 'Lazy Guy')
171         self.session.add_all(self.maintainer.values())
172
173     def setup_sources(self):
174         'create a DBSource object; but it cannot be stored in the DB yet'
175
176         self.setup_maintainers()
177         self.setup_poolfiles()
178         self.source = DBSource(source = 'hello', version = '2.2-2', \
179             maintainer = self.maintainer['maintainer'], \
180             changedby = self.maintainer['uploader'], \
181             poolfile = self.file['hello'], install_date = self.now())
182
183     def test_maintainers(self):
184         '''
185         tests relation between Maintainer and DBSource
186
187         TODO: add relations to changes_pending_source
188         '''
189
190         self.setup_sources()
191         self.session.flush()
192         maintainer = self.maintainer['maintainer']
193         self.assertEqual(maintainer,
194             self.session.query(Maintainer).get(maintainer.maintainer_id))
195         uploader = self.maintainer['uploader']
196         self.assertEqual(uploader,
197             self.session.query(Maintainer).get(uploader.maintainer_id))
198         lazyguy = self.maintainer['lazyguy']
199         self.assertEqual(lazyguy,
200             self.session.query(Maintainer).get(lazyguy.maintainer_id))
201         self.assertEqual(maintainer.maintains_sources, [self.source])
202         self.assertEqual(maintainer.changed_sources, [])
203         self.assertEqual(uploader.maintains_sources, [])
204         self.assertEqual(uploader.changed_sources, [self.source])
205         self.assertEqual(lazyguy.maintains_sources, [])
206         self.assertEqual(lazyguy.changed_sources, [])
207
208     def test_sources(self):
209         'test relation between DBSource and PoolFile'
210
211         self.setup_sources()
212         self.assertEqual(self.file['hello'], self.source.poolfile)
213         self.assertEqual(self.source, self.file['hello'].source)
214         self.assertEqual(None, self.file['sl'].source)
215
216
217 if __name__ == '__main__':
218     unittest.main()