]> git.decadent.org.uk Git - dak.git/blob - tests/dbtest_contents.py
dak rm: warn when working on oldstable as on stable
[dak.git] / tests / dbtest_contents.py
1 #!/usr/bin/env python
2
3 from db_test import DBDakTestCase, fixture
4
5 from daklib.dbconn import *
6 from daklib.contents import BinaryContentsWriter, BinaryContentsScanner, \
7     UnpackedSource, SourceContentsScanner, SourceContentsWriter
8
9 from os.path import normpath
10 from sqlalchemy.exc import FlushError, IntegrityError
11 from subprocess import CalledProcessError
12 import unittest
13
14 class ContentsTestCase(DBDakTestCase):
15     """
16     This TestCase checks the behaviour of contents generation.
17     """
18
19     def test_duplicates1(self):
20         '''
21         Test the BinContents class for duplication problems.
22         '''
23         self.setup_binaries()
24         contents1 = BinContents(file = 'usr/bin/hello', \
25             binary = self.binary['hello_2.2-1_i386'])
26         self.session.add(contents1)
27         self.session.flush()
28         # test duplicates
29         contents2 = BinContents(file = 'usr/bin/hello', \
30             binary = self.binary['hello_2.2-1_i386'])
31         self.session.add(contents2)
32         self.assertRaises(FlushError, self.session.flush)
33
34     def test_duplicates2(self):
35         '''
36         Test the BinContents class for more duplication problems.
37         '''
38         self.setup_binaries()
39         contents1 = BinContents(file = 'usr/bin/hello', \
40             binary = self.binary['hello_2.2-1_i386'])
41         self.session.add(contents1)
42         contents2 = BinContents(file = 'usr/bin/gruezi', \
43             binary = self.binary['hello_2.2-1_i386'])
44         self.session.add(contents2)
45         self.session.flush()
46         # test duplicates
47         contents2.file = 'usr/bin/hello'
48         self.assertRaises(IntegrityError, self.session.flush)
49
50     def test_duplicates3(self):
51         '''
52         Test the BinContents class even more.
53         '''
54         self.setup_binaries()
55         contents1 = BinContents(file = 'usr/bin/hello', \
56             binary = self.binary['hello_2.2-1_i386'])
57         self.session.add(contents1)
58         # same file in different binary packages should be okay
59         contents2 = BinContents(file = 'usr/bin/hello', \
60             binary = self.binary['gnome-hello_2.2-1_i386'])
61         self.session.add(contents2)
62         self.session.flush()
63
64     def test_overridetype(self):
65         '''
66         Test the OverrideType class.
67         '''
68         self.setup_overridetypes()
69         self.assertEqual('deb', self.otype['deb'].overridetype)
70         self.assertEqual(0, self.otype['deb'].overrides.count())
71         self.assertEqual(self.otype['deb'], get_override_type('deb', self.session))
72
73     def test_section(self):
74         '''
75         Test Section class.
76         '''
77         self.setup_sections()
78         self.assertEqual('python', self.section['python'].section)
79         self.assertEqual('python', self.section['python'])
80         self.assertTrue(self.section['python'] != 'java')
81         self.assertEqual(self.section['python'], get_section('python', self.session))
82         all_sections = get_sections(self.session)
83         self.assertEqual(self.section['python'].section_id, all_sections['python'])
84         self.assertEqual(0, self.section['python'].overrides.count())
85
86     def test_priority(self):
87         '''
88         Test Priority class.
89         '''
90         self.setup_priorities()
91         self.assertEqual('standard', self.prio['standard'].priority)
92         self.assertEqual(7, self.prio['standard'].level)
93         self.assertEqual('standard', self.prio['standard'])
94         self.assertTrue(self.prio['standard'] != 'extra')
95         self.assertEqual(self.prio['standard'], get_priority('standard', self.session))
96         all_priorities = get_priorities(self.session)
97         self.assertEqual(self.prio['standard'].priority_id, all_priorities['standard'])
98         self.assertEqual(0, self.prio['standard'].overrides.count())
99
100     def test_override(self):
101         '''
102         Test Override class.
103         '''
104         self.setup_overrides()
105         list = get_override('hello', session = self.session)
106         self.assertEqual(3, len(list))
107         self.assertTrue(self.override['hello_sid_main_udeb'] in list)
108         self.assertTrue(self.override['hello_squeeze_main_deb'] in list)
109         list = get_override('hello', suite = 'sid', session = self.session)
110         self.assertEqual([self.override['hello_sid_main_udeb']], list)
111         list = get_override('hello', suite = ['sid'], session = self.session)
112         self.assertEqual([self.override['hello_sid_main_udeb']], list)
113         list = get_override('hello', component = 'contrib', session = self.session)
114         self.assertEqual([self.override['hello_lenny_contrib_deb']], list)
115         list = get_override('hello', component = ['contrib'], session = self.session)
116         self.assertEqual([self.override['hello_lenny_contrib_deb']], list)
117         list = get_override('hello', overridetype = 'deb', session = self.session)
118         self.assertEqual(2, len(list))
119         self.assertTrue(self.override['hello_sid_main_udeb'] not in list)
120         self.assertTrue(self.override['hello_squeeze_main_deb'] in list)
121         list = get_override('hello', overridetype = ['deb'], session = self.session)
122         self.assertEqual(2, len(list))
123         self.assertTrue(self.override['hello_sid_main_udeb'] not in list)
124         self.assertTrue(self.override['hello_squeeze_main_deb'] in list)
125         # test the backrefs
126         self.assertEqual(self.override['hello_sid_main_udeb'], \
127             self.suite['sid'].overrides.one())
128         self.assertEqual(2, self.comp['main'].overrides.count())
129         self.assertEqual(self.override['hello_sid_main_udeb'], \
130             self.comp['main'].overrides.filter_by(suite = self.suite['sid']).one())
131         self.assertEqual(self.override['hello_sid_main_udeb'], \
132             self.otype['udeb'].overrides.one())
133
134     def test_binarycontentswriter(self):
135         '''
136         Test the BinaryContentsWriter class.
137         '''
138         self.setup_suites()
139         self.setup_architectures()
140         self.setup_overridetypes()
141         self.setup_binaries()
142         self.setup_overrides()
143         self.binary['hello_2.2-1_i386'].contents.append(BinContents(file = '/usr/bin/hello'))
144         self.session.commit()
145         cw = BinaryContentsWriter(self.suite['squeeze'], self.arch['i386'], \
146             self.otype['deb'], self.comp['main'])
147         self.assertEqual(['/usr/bin/hello                                          python/hello\n'], \
148             cw.get_list())
149         # test formatline and sort order
150         self.assertEqual('/usr/bin/hello                                          python/hello\n', \
151             cw.formatline('/usr/bin/hello', 'python/hello'))
152         # test unicode support
153         self.binary['hello_2.2-1_i386'].contents.append(BinContents(file = '\xc3\xb6'))
154         self.session.commit()
155         # test delete cascading
156         self.session.delete(self.binary['hello_2.2-1_i386'])
157         self.session.commit()
158
159     def test_binary_scan_contents(self):
160         '''
161         Tests the BinaryContentsScanner.
162         '''
163         self.setup_binaries()
164         filelist = [f for f in self.binary['hello_2.2-1_i386'].scan_contents()]
165         self.assertEqual(['usr/bin/hello', 'usr/share/doc/hello/copyright'],
166             filelist)
167         self.session.commit()
168         BinaryContentsScanner(self.binary['hello_2.2-1_i386'].binary_id).scan()
169         bin_contents_list = self.binary['hello_2.2-1_i386'].contents.order_by('file').all()
170         self.assertEqual(2, len(bin_contents_list))
171         self.assertEqual('usr/bin/hello', bin_contents_list[0].file)
172         self.assertEqual('usr/share/doc/hello/copyright', bin_contents_list[1].file)
173
174     def test_unpack(self):
175         '''
176         Tests the UnpackedSource class and the SourceContentsScanner.
177         '''
178         self.setup_sources()
179         source = self.source['hello_2.2-1']
180         dscfilename = fixture('ftp/pool/' + source.poolfile.filename)
181         unpacked = UnpackedSource(dscfilename)
182         self.assertTrue(len(unpacked.get_root_directory()) > 0)
183         self.assertEqual('hello (2.2-1) unstable; urgency=low\n',
184             unpacked.get_changelog_file().readline())
185         all_filenames = set(unpacked.get_all_filenames())
186         self.assertEqual(8, len(all_filenames))
187         self.assertTrue('debian/rules' in all_filenames)
188         # method scan_contents()
189         self.assertEqual(all_filenames, source.scan_contents())
190         # exception with invalid files
191         self.assertRaises(CalledProcessError, lambda: UnpackedSource('invalidname'))
192         # SourceContentsScanner
193         self.session.commit()
194         self.assertTrue(source.contents.count() == 0)
195         SourceContentsScanner(source.source_id).scan()
196         self.assertTrue(source.contents.count() > 0)
197
198     def test_sourcecontentswriter(self):
199         '''
200         Test the SourceContentsWriter class.
201         '''
202         self.setup_sources()
203         self.session.flush()
204         # remove newer package from sid because it disturbs the test
205         self.source['hello_2.2-2'].suites = []
206         self.session.commit()
207         source = self.source['hello_2.2-1']
208         SourceContentsScanner(source.source_id).scan()
209         cw = SourceContentsWriter(source.suites[0], source.poolfile.location.component)
210         result = cw.get_list()
211         self.assertEqual(8, len(result))
212         self.assertTrue('debian/changelog\thello\n' in result)
213
214     def classes_to_clean(self):
215         return [Override, Suite, BinContents, DBBinary, DBSource, Architecture, Section, \
216             OverrideType, Maintainer, Component, Priority, PoolFile]
217
218 if __name__ == '__main__':
219     unittest.main()