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