dbtest_contents.py 9.4 KB

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