dbtest_packages.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. #! /usr/bin/env python3
  2. from db_test import DBDakTestCase
  3. from base_test import fixture
  4. import os
  5. from os.path import join
  6. from daklib.dbconn import *
  7. from daklib.queue import get_suite_version_by_source, get_suite_version_by_package
  8. from sqlalchemy.orm.exc import MultipleResultsFound
  9. import unittest
  10. DAKBASE = os.environ['DAKBASE']
  11. class Pkg():
  12. 'fake package class used for testing'
  13. def __init__(self):
  14. self.dsc = {}
  15. self.files = {}
  16. self.changes = {}
  17. class Upload():
  18. 'fake Upload class used for testing'
  19. def __init__(self, pkg):
  20. self.pkg = pkg
  21. class PackageTestCase(DBDakTestCase):
  22. """
  23. PackageTestCase checks the handling of source and binary packages in dak's
  24. database.
  25. """
  26. def setUp(self):
  27. super(PackageTestCase, self).setUp()
  28. self.setup_binaries()
  29. # flush to make sure that the setup is correct
  30. self.session.flush()
  31. def test_suite_architecture(self):
  32. # check the id for architectures source and all
  33. self.assertEqual(1, self.arch['source'].arch_id)
  34. self.assertEqual(2, self.arch['all'].arch_id)
  35. # check the many to many relation between Suite and Architecture
  36. self.assertEqual('all', self.suite['lenny'].get_architectures()[0])
  37. self.assertEqual(4, len(self.suite['lenny'].architectures))
  38. self.assertEqual(3, len(self.arch['i386'].suites))
  39. # check the function get_suite_architectures()
  40. architectures = get_suite_architectures('lenny', session=self.session)
  41. self.assertEqual(4, len(architectures))
  42. self.assertTrue(self.arch['source'] in architectures)
  43. self.assertTrue(self.arch['all'] in architectures)
  44. self.assertTrue(self.arch['kfreebsd-i386'] not in architectures)
  45. architectures = get_suite_architectures('sid', session=self.session)
  46. self.assertEqual(5, len(architectures))
  47. self.assertTrue(self.arch['kfreebsd-i386'] in architectures)
  48. architectures = get_suite_architectures(
  49. 'lenny', skipsrc=True, session=self.session)
  50. self.assertEqual(3, len(architectures))
  51. self.assertTrue(self.arch['source'] not in architectures)
  52. architectures = get_suite_architectures(
  53. 'lenny', skipall=True, session=self.session)
  54. self.assertEqual(3, len(architectures))
  55. self.assertTrue(self.arch['all'] not in architectures)
  56. # check overrides
  57. self.assertEqual(0, self.suite['lenny'].overrides.count())
  58. def test_poolfiles(self):
  59. main = self.comp['main']
  60. contrib = self.comp['contrib']
  61. poolfile = self.session.query(PoolFile).filter(
  62. PoolFile.filename.like('%/hello/hello%')). \
  63. order_by(PoolFile.filename)[0]
  64. self.assertEqual('h/hello/hello_2.2-1.dsc', poolfile.filename)
  65. self.assertEqual(main, poolfile.component)
  66. # test get()
  67. self.assertEqual(poolfile,
  68. self.session.query(PoolFile).get(poolfile.file_id))
  69. self.assertEqual(None, self.session.query(PoolFile).get(-1))
  70. # test fullpath
  71. self.assertEqual(join(DAKBASE, 'ftp/pool/main/s/sl/sl_3.03-16.dsc'),
  72. self.file['sl_3.03-16.dsc'].fullpath)
  73. def test_maintainers(self):
  74. '''
  75. tests relation between Maintainer and DBSource
  76. TODO: add relations to changes_pending_source
  77. '''
  78. maintainer = self.maintainer['maintainer']
  79. self.assertEqual(maintainer,
  80. self.session.query(Maintainer).get(maintainer.maintainer_id))
  81. uploader = self.maintainer['uploader']
  82. self.assertEqual(uploader,
  83. self.session.query(Maintainer).get(uploader.maintainer_id))
  84. lazyguy = self.maintainer['lazyguy']
  85. self.assertEqual(lazyguy,
  86. self.session.query(Maintainer).get(lazyguy.maintainer_id))
  87. self.assertEqual(4, len(maintainer.maintains_sources))
  88. self.assertTrue(
  89. self.source['hello_2.2-2'] in maintainer.maintains_sources)
  90. self.assertEqual(maintainer.changed_sources, [])
  91. self.assertEqual(uploader.maintains_sources, [])
  92. self.assertEqual(4, len(uploader.changed_sources))
  93. self.assertTrue(self.source['sl_3.03-16'] in uploader.changed_sources)
  94. self.assertEqual(lazyguy.maintains_sources, [])
  95. self.assertEqual(lazyguy.changed_sources, [])
  96. def get_source_in_suite_fail(self):
  97. '''
  98. This function throws the MultipleResultsFound exception because
  99. get_source_in_suite is broken.
  100. TODO: fix get_source_in_suite
  101. '''
  102. return get_source_in_suite('hello', 'sid', self.session)
  103. def test_sources(self):
  104. 'test relation between DBSource and PoolFile or Suite'
  105. # test PoolFile
  106. self.assertEqual(
  107. self.file['hello_2.2-2.dsc'], self.source['hello_2.2-2'].poolfile)
  108. self.assertEqual(
  109. self.file['hello_2.2-2.dsc'].component, self.comp['main'])
  110. self.assertEqual(
  111. self.file['gnome-hello_3.0-1.dsc'].component, self.comp['contrib'])
  112. # test Suite
  113. squeeze = self.session.query(Suite). \
  114. filter(Suite.sources.contains(self.source['sl_3.03-16'])). \
  115. order_by(Suite.suite_name)[1]
  116. self.assertEqual(self.suite['squeeze'], squeeze)
  117. self.assertEqual(1, squeeze.sources.count())
  118. self.assertEqual(self.source['sl_3.03-16'], squeeze.sources[0])
  119. sl = self.session.query(DBSource). \
  120. filter(DBSource.suites.contains(self.suite['squeeze'])).one()
  121. self.assertEqual(self.source['sl_3.03-16'], sl)
  122. self.assertEqual(2, len(sl.suites))
  123. self.assertTrue(self.suite['sid'] in sl.suites)
  124. # test get_source_in_suite()
  125. self.assertRaises(MultipleResultsFound, self.get_source_in_suite_fail)
  126. self.assertEqual(None,
  127. get_source_in_suite('hello', 'squeeze', self.session))
  128. self.assertEqual(self.source['sl_3.03-16'],
  129. get_source_in_suite('sl', 'sid', self.session))
  130. # test get_suites_source_in()
  131. self.assertEqual([self.suite['sid']],
  132. get_suites_source_in('hello', self.session))
  133. self.assertEqual(2, len(get_suites_source_in('sl', self.session)))
  134. self.assertTrue(self.suite['squeeze'] in
  135. get_suites_source_in('sl', self.session))
  136. def test_get_suite_version_by_source(self):
  137. 'test function get_suite_version_by_source()'
  138. result = get_suite_version_by_source('hello', self.session)
  139. self.assertEqual(2, len(result))
  140. self.assertTrue(('sid', '2.2-1') in result)
  141. self.assertTrue(('sid', '2.2-2') in result)
  142. result = get_suite_version_by_source('sl', self.session)
  143. self.assertEqual(2, len(result))
  144. self.assertTrue(('squeeze', '3.03-16') in result)
  145. self.assertTrue(('sid', '3.03-16') in result)
  146. def test_binaries(self):
  147. '''
  148. tests class DBBinary; TODO: test relation with Architecture, Maintainer,
  149. PoolFile, and Fingerprint
  150. '''
  151. # test Suite relation
  152. self.assertEqual(3, self.suite['sid'].binaries.count())
  153. self.assertTrue(self.binary['hello_2.2-1_i386'] in
  154. self.suite['sid'].binaries.all())
  155. self.assertEqual(0, self.suite['lenny'].binaries.count())
  156. # test DBSource relation
  157. self.assertEqual(3, len(self.source['hello_2.2-1'].binaries))
  158. self.assertTrue(self.binary['hello_2.2-1_i386'] in
  159. self.source['hello_2.2-1'].binaries)
  160. self.assertEqual(0, len(self.source['hello_2.2-2'].binaries))
  161. # test get_suites_binary_in()
  162. self.assertEqual(2, len(get_suites_binary_in('hello', self.session)))
  163. self.assertTrue(self.suite['sid'] in
  164. get_suites_binary_in('hello', self.session))
  165. self.assertEqual(
  166. 2, len(get_suites_binary_in('gnome-hello', self.session)))
  167. self.assertTrue(self.suite['squeeze'] in
  168. get_suites_binary_in('gnome-hello', self.session))
  169. self.assertEqual(0, len(get_suites_binary_in('sl', self.session)))
  170. def test_get_suite_version_by_package(self):
  171. 'test function get_suite_version_by_package()'
  172. result = get_suite_version_by_package('hello', 'i386', self.session)
  173. self.assertEqual(2, len(result))
  174. self.assertTrue(('sid', '2.2-1') in result)
  175. result = get_suite_version_by_package('hello', 'amd64', self.session)
  176. self.assertEqual(0, len(result))
  177. result = get_suite_version_by_package(
  178. 'python-hello', 'i386', self.session)
  179. self.assertEqual([('squeeze', '2.2-1')], result)
  180. result = get_suite_version_by_package(
  181. 'python-hello', 'amd64', self.session)
  182. self.assertEqual([('squeeze', '2.2-1')], result)
  183. def test_components(self):
  184. 'test class Component'
  185. self.assertEqual(0, self.comp['main'].overrides.count())
  186. def test_get_component_by_package_suite(self):
  187. 'test get_component_by_package_suite()'
  188. result = get_component_by_package_suite('hello', ['sid'],
  189. session=self.session)
  190. self.assertEqual('main', result)
  191. result = get_component_by_package_suite('hello', ['hamm'],
  192. session=self.session)
  193. self.assertEqual(None, result)
  194. result = get_component_by_package_suite('foobar', ['sid'],
  195. session=self.session)
  196. self.assertEqual(None, result)
  197. # test that the newest version is returned
  198. result = get_component_by_package_suite('gnome-hello', ['squeeze'],
  199. session=self.session)
  200. self.assertEqual('main', result)
  201. result = get_component_by_package_suite('gnome-hello', ['sid'],
  202. session=self.session)
  203. self.assertEqual('contrib', result)
  204. # test arch_list
  205. result = get_component_by_package_suite('hello', ['sid'],
  206. arch_list=['i386'], session=self.session)
  207. self.assertEqual('main', result)
  208. result = get_component_by_package_suite('hello', ['sid'],
  209. arch_list=['amd64'], session=self.session)
  210. self.assertEqual(None, result)
  211. if __name__ == '__main__':
  212. unittest.main()