]> git.decadent.org.uk Git - dak.git/blob - tests/dbtest_packages.py
Merge branch 'master' of ssh://franck.debian.org/srv/ftp.debian.org/git/dak
[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
8 import unittest
9
10 class PackageTestCase(DBDakTestCase):
11     """
12     PackageTestCase checks the handling of source and binary packages in dak's
13     database.
14     """
15
16     def setup_architectures(self):
17         "setup a hash of Architecture objects in self.arch"
18
19         self.arch = {}
20         for arch_string in ('source', 'all', 'i386', 'amd64', 'kfreebsd-i386'):
21             self.arch[arch_string] = Architecture(arch_string)
22         # hard code ids for source and all
23         self.arch['source'].arch_id = 1
24         self.arch['all'].arch_id = 2
25         for _, architecture in self.arch.items():
26             self.session.add(architecture)
27             self.session.flush()
28             self.session.refresh(architecture)
29
30     def setup_suites(self):
31         "setup a hash of Suite objects in self.suite"
32
33         self.suite = {}
34         for suite_name in ('lenny', 'squeeze', 'sid'):
35             suite = Suite(suite_name = suite_name, version = '-')
36             self.suite[suite_name] = suite
37             self.session.add(suite)
38             self.session.flush()
39             self.session.refresh(suite)
40
41     def setUp(self):
42         super(PackageTestCase, self).setUp()
43         self.setup_architectures()
44         self.setup_suites()
45
46     def connect_suite_architectures(self):
47         """
48         Gonnect all suites and all architectures except for kfreebsd-i386 which
49         should not be in lenny.
50         """
51
52         for arch_string, architecture in self.arch.items():
53             if arch_string != 'kfreebsd-i386':
54                 architecture.suites = self.suite.values()
55             else:
56                 architecture.suites = [self.suite['squeeze'], self.suite['sid']]
57
58     def test_suite_architecture(self):
59         # check the id for architectures source and all
60         self.assertEqual(1, self.arch['source'].arch_id)
61         self.assertEqual(2, self.arch['all'].arch_id)
62         # check the many to many relation between Suite and Architecture
63         self.arch['source'].suites.append(self.suite['lenny'])
64         self.assertEqual('source', self.suite['lenny'].architectures[0])
65         self.arch['source'].suites = []
66         self.assertEqual([], self.suite['lenny'].architectures)
67         self.connect_suite_architectures()
68         self.assertEqual(4, len(self.suite['lenny'].architectures))
69         self.assertEqual(3, len(self.arch['i386'].suites))
70         # check the function get_suite_architectures()
71         architectures = get_suite_architectures('lenny', session = self.session)
72         self.assertEqual(4, len(architectures))
73         self.assertTrue(self.arch['source'] in architectures)
74         self.assertTrue(self.arch['all'] in architectures)
75         self.assertTrue(self.arch['kfreebsd-i386'] not in architectures)
76         architectures = get_suite_architectures('sid', session = self.session)
77         self.assertEqual(5, len(architectures))
78         self.assertTrue(self.arch['kfreebsd-i386'] in architectures)
79         architectures = get_suite_architectures('lenny', skipsrc = True, session = self.session)
80         self.assertEqual(3, len(architectures))
81         self.assertTrue(self.arch['source'] not in architectures)
82         architectures = get_suite_architectures('lenny', skipall = True, session = self.session)
83         self.assertEqual(3, len(architectures))
84         self.assertTrue(self.arch['all'] not in architectures)
85         # check the function get_architecture_suites()
86         suites = get_architecture_suites('i386', self.session)
87         self.assertEqual(3, len(suites))
88         self.assertTrue(self.suite['lenny'] in suites)
89         suites = get_architecture_suites('kfreebsd-i386', self.session)
90         self.assertEqual(2, len(suites))
91         self.assertTrue(self.suite['lenny'] not in suites)
92
93     def setup_locations(self):
94         'create some Location objects, TODO: add component'
95
96         self.loc = {}
97         self.loc['main'] = Location(path = \
98             '/srv/ftp-master.debian.org/ftp/pool/')
99         self.session.add(self.loc['main'])
100
101     def setup_poolfiles(self):
102         'create some PoolFile objects'
103
104         self.file = {}
105         self.file['hello'] = PoolFile(filename = 'main/h/hello/hello_2.2-2.dsc', \
106             location = self.loc['main'], filesize = 0, md5sum = '')
107         self.file['sl'] = PoolFile(filename = 'main/s/sl/sl_3.03-16.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         self.setup_locations()
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(2, location.files.count())
128         poolfile = location.files. \
129                 filter(PoolFile.filename.like('%/hello/hello%')).one()
130         self.assertEqual('main/h/hello/hello_2.2-2.dsc', poolfile.filename)
131         self.assertEqual(location, poolfile.location)
132         location.files.remove(self.file['sl'])
133         # TODO: deletion should cascade automatically
134         self.session.delete(self.file['sl'])
135         self.session.refresh(location)
136         self.assertEqual(1, location.files.count())
137         # please note that we intentionally do not specify 'location' here
138         self.file['sl'] = PoolFile(filename = 'main/s/sl/sl_3.03-16.dsc', \
139             filesize = 0, md5sum = '')
140         location.files.append(self.file['sl'])
141         self.session.refresh(location)
142         self.assertEqual(2, location.files.count())
143
144     def setup_maintainers(self):
145         'create some Maintainer objects'
146
147         self.maintainer = Maintainer(name = 'Mr. Maintainer')
148         self.uploader = Maintainer(name = 'Mrs. Uploader')
149         self.lazyguy = Maintainer(name = 'Lazy Guy')
150         self.session.add_all([self.maintainer, self.uploader, self.lazyguy])
151
152     def setup_sources(self):
153         'create a DBSource object; but it cannot be stored in the DB yet'
154
155         self.source = DBSource(maintainer = self.maintainer,
156             changedby = self.uploader)
157
158     def test_maintainers(self):
159         '''
160         tests relation between Maintainer and DBSource
161
162         TODO: add relations to changes_pending_source
163         '''
164
165         self.setup_maintainers()
166         self.assertEqual('Mr. Maintainer',
167                 self.session.query(Maintainer)[0].name)
168         self.assertEqual('Mrs. Uploader',
169                 self.session.query(Maintainer)[1].name)
170         self.assertEqual('Lazy Guy',
171                 self.session.query(Maintainer)[2].name)
172         self.setup_sources()
173         #TODO: needs File and Location
174         #self.assertEqual(self.maintainer.maintains_sources, [self.source])
175         #self.assertEqual(self.maintainer.changed_sources, [])
176         #self.assertEqual(self.uploader.maintains_sources, [])
177         #self.assertEqual(self.uploader.changed_sources, [self.source])
178         #self.assertEqual(self.lazyguy.maintains_sources, [])
179         #self.assertEqual(self.lazyguy.changed_sources, [])
180
181
182 if __name__ == '__main__':
183     unittest.main()