]> git.decadent.org.uk Git - dak.git/blob - tests/dbtest_packages.py
Merge remote branch 'odyx/win32-loader-autobyhand' into merge
[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.file = {}
106         self.file['hello'] = PoolFile(filename = 'main/h/hello/hello_2.2-2.dsc', \
107             location = self.loc['main'], filesize = 0, md5sum = '')
108         self.file['sl'] = PoolFile(filename = 'main/s/sl/sl_3.03-16.dsc', \
109             location = self.loc['main'], filesize = 0, md5sum = '')
110         self.session.add_all(self.file.values())
111
112     def test_poolfiles(self):
113         '''
114         Test the relation of the classes PoolFile and Location.
115
116         The code needs some explaination. The property Location.files is not a
117         list as in other relations because such a list would become rather
118         huge. It is a query object that can be queried, filtered, and iterated
119         as usual.  But list like methods like append() and remove() are
120         supported as well which allows code like:
121
122         somelocation.files.append(somefile)
123         '''
124         self.setup_locations()
125         self.setup_poolfiles()
126         location = self.session.query(Location)[0]
127         self.assertEqual('/srv/ftp-master.debian.org/ftp/pool/', location.path)
128         self.assertEqual(2, location.files.count())
129         poolfile = location.files. \
130                 filter(PoolFile.filename.like('%/hello/hello%')).one()
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(1, 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(2, 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         self.maintainer = Maintainer(name = 'Mr. Maintainer')
173         self.uploader = Maintainer(name = 'Mrs. Uploader')
174         self.lazyguy = Maintainer(name = 'Lazy Guy')
175         self.session.add_all([self.maintainer, self.uploader, self.lazyguy])
176
177     def setup_sources(self):
178         'create a DBSource object; but it cannot be stored in the DB yet'
179
180         self.source = DBSource(maintainer = self.maintainer,
181             changedby = self.uploader)
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_maintainers()
191         self.assertEqual('Mr. Maintainer',
192                 self.session.query(Maintainer)[0].name)
193         self.assertEqual('Mrs. Uploader',
194                 self.session.query(Maintainer)[1].name)
195         self.assertEqual('Lazy Guy',
196                 self.session.query(Maintainer)[2].name)
197         self.setup_sources()
198         #TODO: needs File and Location
199         #self.assertEqual(self.maintainer.maintains_sources, [self.source])
200         #self.assertEqual(self.maintainer.changed_sources, [])
201         #self.assertEqual(self.uploader.maintains_sources, [])
202         #self.assertEqual(self.uploader.changed_sources, [self.source])
203         #self.assertEqual(self.lazyguy.maintains_sources, [])
204         #self.assertEqual(self.lazyguy.changed_sources, [])
205
206
207 if __name__ == '__main__':
208     unittest.main()