dbtest_packages.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. #!/usr/bin/env python
  2. from db_test import DBDakTestCase
  3. from base_test import fixture
  4. from daklib.dbconn import *
  5. from daklib.queue import get_suite_version_by_source, get_suite_version_by_package
  6. from sqlalchemy.orm.exc import MultipleResultsFound
  7. import unittest
  8. class Pkg():
  9. 'fake package class used for testing'
  10. def __init__(self):
  11. self.dsc = {}
  12. self.files = {}
  13. self.changes = {}
  14. class Upload():
  15. 'fake Upload class used for testing'
  16. def __init__(self, pkg):
  17. self.pkg = pkg
  18. class PackageTestCase(DBDakTestCase):
  19. """
  20. PackageTestCase checks the handling of source and binary packages in dak's
  21. database.
  22. """
  23. def setUp(self):
  24. super(PackageTestCase, self).setUp()
  25. self.setup_binaries()
  26. # flush to make sure that the setup is correct
  27. self.session.flush()
  28. def test_suite_architecture(self):
  29. # check the id for architectures source and all
  30. self.assertEqual(1, self.arch['source'].arch_id)
  31. self.assertEqual(2, self.arch['all'].arch_id)
  32. # check the many to many relation between Suite and Architecture
  33. self.assertEqual('source', self.suite['lenny'].architectures[0])
  34. self.assertEqual(4, len(self.suite['lenny'].architectures))
  35. self.assertEqual(3, len(self.arch['i386'].suites))
  36. # check the function get_suite_architectures()
  37. architectures = get_suite_architectures('lenny', session = self.session)
  38. self.assertEqual(4, len(architectures))
  39. self.assertTrue(self.arch['source'] in architectures)
  40. self.assertTrue(self.arch['all'] in architectures)
  41. self.assertTrue(self.arch['kfreebsd-i386'] not in architectures)
  42. architectures = get_suite_architectures('sid', session = self.session)
  43. self.assertEqual(5, len(architectures))
  44. self.assertTrue(self.arch['kfreebsd-i386'] in architectures)
  45. architectures = get_suite_architectures('lenny', skipsrc = True, session = self.session)
  46. self.assertEqual(3, len(architectures))
  47. self.assertTrue(self.arch['source'] not in architectures)
  48. architectures = get_suite_architectures('lenny', skipall = True, session = self.session)
  49. self.assertEqual(3, len(architectures))
  50. self.assertTrue(self.arch['all'] not in architectures)
  51. # check overrides
  52. self.assertEqual(0, self.suite['lenny'].overrides.count())
  53. def test_poolfiles(self):
  54. '''
  55. Test the relation of the classes PoolFile and Location.
  56. The code needs some explaination. The property Location.files is not a
  57. list as in other relations because such a list would become rather
  58. huge. It is a query object that can be queried, filtered, and iterated
  59. as usual. But list like methods like append() and remove() are
  60. supported as well which allows code like:
  61. somelocation.files.append(somefile)
  62. '''
  63. main = self.loc['main']
  64. contrib = self.loc['contrib']
  65. self.assertEqual(fixture('ftp/pool/'), main.path)
  66. count = len(self.file.keys()) - 2
  67. self.assertEqual(count, main.files.count())
  68. self.assertEqual(2, contrib.files.count())
  69. poolfile = main.files. \
  70. filter(PoolFile.filename.like('%/hello/hello%')). \
  71. order_by(PoolFile.filename)[0]
  72. self.assertEqual('main/h/hello/hello_2.2-1.dsc', poolfile.filename)
  73. self.assertEqual(main, poolfile.location)
  74. # test get()
  75. self.assertEqual(poolfile, \
  76. self.session.query(PoolFile).get(poolfile.file_id))
  77. self.assertEqual(None, self.session.query(PoolFile).get(-1))
  78. # test remove() and append()
  79. main.files.remove(self.file['sl_3.03-16.dsc'])
  80. contrib.files.append(self.file['sl_3.03-16.dsc'])
  81. self.assertEqual(count - 1, main.files.count())
  82. self.assertEqual(3, contrib.files.count())
  83. # test fullpath
  84. self.assertEqual(fixture('ftp/pool/main/s/sl/sl_3.03-16.dsc'), \
  85. self.file['sl_3.03-16.dsc'].fullpath)
  86. # test check_poolfile()
  87. self.assertEqual((True, self.file['sl_3.03-16.dsc']), \
  88. check_poolfile('main/s/sl/sl_3.03-16.dsc', 0, '', \
  89. contrib.location_id, self.session))
  90. # test string value of 2nd argument
  91. self.assertEqual((True, self.file['sl_3.03-16.dsc']), \
  92. check_poolfile('main/s/sl/sl_3.03-16.dsc', '0', '', \
  93. contrib.location_id, self.session))
  94. self.assertEqual((False, None), \
  95. check_poolfile('foobar', 0, '', contrib.location_id, self.session))
  96. self.assertEqual((False, self.file['sl_3.03-16.dsc']), \
  97. check_poolfile('main/s/sl/sl_3.03-16.dsc', 42, '', \
  98. contrib.location_id, self.session))
  99. self.assertEqual((False, self.file['sl_3.03-16.dsc']), \
  100. check_poolfile('main/s/sl/sl_3.03-16.dsc', 0, 'deadbeef', \
  101. contrib.location_id, self.session))
  102. def test_maintainers(self):
  103. '''
  104. tests relation between Maintainer and DBSource
  105. TODO: add relations to changes_pending_source
  106. '''
  107. maintainer = self.maintainer['maintainer']
  108. self.assertEqual(maintainer,
  109. self.session.query(Maintainer).get(maintainer.maintainer_id))
  110. uploader = self.maintainer['uploader']
  111. self.assertEqual(uploader,
  112. self.session.query(Maintainer).get(uploader.maintainer_id))
  113. lazyguy = self.maintainer['lazyguy']
  114. self.assertEqual(lazyguy,
  115. self.session.query(Maintainer).get(lazyguy.maintainer_id))
  116. self.assertEqual(4, len(maintainer.maintains_sources))
  117. self.assertTrue(self.source['hello_2.2-2'] in maintainer.maintains_sources)
  118. self.assertEqual(maintainer.changed_sources, [])
  119. self.assertEqual(uploader.maintains_sources, [])
  120. self.assertEqual(4, len(uploader.changed_sources))
  121. self.assertTrue(self.source['sl_3.03-16'] in uploader.changed_sources)
  122. self.assertEqual(lazyguy.maintains_sources, [])
  123. self.assertEqual(lazyguy.changed_sources, [])
  124. def get_source_in_suite_fail(self):
  125. '''
  126. This function throws the MultipleResultsFound exception because
  127. get_source_in_suite is broken.
  128. TODO: fix get_source_in_suite
  129. '''
  130. return get_source_in_suite('hello', 'sid', self.session)
  131. def test_sources(self):
  132. 'test relation between DBSource and PoolFile or Suite'
  133. # test PoolFile
  134. self.assertEqual(self.file['hello_2.2-2.dsc'], self.source['hello_2.2-2'].poolfile)
  135. self.assertEqual(self.source['hello_2.2-2'], self.file['hello_2.2-2.dsc'].source)
  136. self.assertEqual(None, self.file['python2.6_2.6.6-8.dsc'].source)
  137. # test Suite
  138. squeeze = self.session.query(Suite). \
  139. filter(Suite.sources.contains(self.source['sl_3.03-16'])). \
  140. order_by(Suite.suite_name)[1]
  141. self.assertEqual(self.suite['squeeze'], squeeze)
  142. self.assertEqual(1, squeeze.sources.count())
  143. self.assertEqual(self.source['sl_3.03-16'], squeeze.sources[0])
  144. sl = self.session.query(DBSource). \
  145. filter(DBSource.suites.contains(self.suite['squeeze'])).one()
  146. self.assertEqual(self.source['sl_3.03-16'], sl)
  147. self.assertEqual(2, len(sl.suites))
  148. self.assertTrue(self.suite['sid'] in sl.suites)
  149. # test get_source_in_suite()
  150. self.assertRaises(MultipleResultsFound, self.get_source_in_suite_fail)
  151. self.assertEqual(None, \
  152. get_source_in_suite('hello', 'squeeze', self.session))
  153. self.assertEqual(self.source['sl_3.03-16'], \
  154. get_source_in_suite('sl', 'sid', self.session))
  155. # test get_suites_source_in()
  156. self.assertEqual([self.suite['sid']], \
  157. get_suites_source_in('hello', self.session))
  158. self.assertEqual(2, len(get_suites_source_in('sl', self.session)))
  159. self.assertTrue(self.suite['squeeze'] in \
  160. get_suites_source_in('sl', self.session))
  161. def test_add_dsc_to_db(self):
  162. 'tests function add_dsc_to_db()'
  163. pkg = Pkg()
  164. pkg.dsc['source'] = 'hello'
  165. pkg.dsc['version'] = '2.2-3'
  166. pkg.dsc['maintainer'] = self.maintainer['maintainer'].name
  167. pkg.changes['changed-by'] = self.maintainer['uploader'].name
  168. pkg.changes['fingerprint'] = 'deadbeef'
  169. pkg.changes['distribution'] = { 'sid': '' }
  170. pkg.files['hello_2.2-3.dsc'] = { \
  171. 'component': 'main',
  172. 'location id': self.loc['main'].location_id,
  173. 'files id': self.file['hello_2.2-3.dsc'].file_id }
  174. pkg.dsc_files = {}
  175. upload = Upload(pkg)
  176. (source, dsc_component, dsc_location_id, pfs) = \
  177. add_dsc_to_db(upload, 'hello_2.2-3.dsc', self.session)
  178. self.assertEqual('hello', source.source)
  179. self.assertEqual('2.2-3', source.version)
  180. self.assertEqual('sid', source.suites[0].suite_name)
  181. self.assertEqual('main', dsc_component)
  182. self.assertEqual(self.loc['main'].location_id, dsc_location_id)
  183. self.assertEqual([], pfs)
  184. def test_get_suite_version_by_source(self):
  185. 'test function get_suite_version_by_source()'
  186. result = get_suite_version_by_source('hello', self.session)
  187. self.assertEqual(2, len(result))
  188. self.assertTrue(('sid', '2.2-1') in result)
  189. self.assertTrue(('sid', '2.2-2') in result)
  190. result = get_suite_version_by_source('sl', self.session)
  191. self.assertEqual(2, len(result))
  192. self.assertTrue(('squeeze', '3.03-16') in result)
  193. self.assertTrue(('sid', '3.03-16') in result)
  194. def test_binaries(self):
  195. '''
  196. tests class DBBinary; TODO: test relation with Architecture, Maintainer,
  197. PoolFile, and Fingerprint
  198. '''
  199. # test Suite relation
  200. self.assertEqual(3, self.suite['sid'].binaries.count())
  201. self.assertTrue(self.binary['hello_2.2-1_i386'] in \
  202. self.suite['sid'].binaries.all())
  203. self.assertEqual(0, self.suite['lenny'].binaries.count())
  204. # test DBSource relation
  205. self.assertEqual(3, len(self.source['hello_2.2-1'].binaries))
  206. self.assertTrue(self.binary['hello_2.2-1_i386'] in \
  207. self.source['hello_2.2-1'].binaries)
  208. self.assertEqual(0, len(self.source['hello_2.2-2'].binaries))
  209. # test get_suites_binary_in()
  210. self.assertEqual(2, len(get_suites_binary_in('hello', self.session)))
  211. self.assertTrue(self.suite['sid'] in \
  212. get_suites_binary_in('hello', self.session))
  213. self.assertEqual(2, len(get_suites_binary_in('gnome-hello', self.session)))
  214. self.assertTrue(self.suite['squeeze'] in \
  215. get_suites_binary_in('gnome-hello', self.session))
  216. self.assertEqual(0, len(get_suites_binary_in('sl', self.session)))
  217. def test_add_deb_to_db(self):
  218. 'tests function add_deb_to_db()'
  219. pkg = Pkg()
  220. pkg.changes['fingerprint'] = 'deadbeef'
  221. pkg.changes['distribution'] = { 'sid': '' }
  222. pkg.files['hello_2.2-2_i386.deb'] = { \
  223. 'package': 'hello',
  224. 'version': '2.2-2',
  225. 'maintainer': self.maintainer['maintainer'].name,
  226. 'architecture': 'i386',
  227. 'dbtype': 'deb',
  228. 'pool name': 'main/h/hello/',
  229. 'location id': self.loc['main'].location_id,
  230. 'source package': 'hello',
  231. 'source version': '2.2-2',
  232. 'size': 0,
  233. 'md5sum': 'deadbeef',
  234. 'sha1sum': 'deadbeef',
  235. 'sha256sum': 'deadbeef'}
  236. upload = Upload(pkg)
  237. bin, poolfile = add_deb_to_db(upload, 'hello_2.2-2_i386.deb', self.session)
  238. self.session.refresh(poolfile)
  239. self.session.refresh(poolfile.binary)
  240. self.assertEqual('main/h/hello/hello_2.2-2_i386.deb', poolfile.filename)
  241. self.assertEqual('hello', poolfile.binary.package)
  242. self.assertEqual('2.2-2', poolfile.binary.version)
  243. self.assertEqual(['sid'], poolfile.binary.suites)
  244. self.assertEqual('Mr. Maintainer', poolfile.binary.maintainer.name)
  245. self.assertEqual('i386', poolfile.binary.architecture.arch_string)
  246. self.assertEqual('deb', poolfile.binary.binarytype)
  247. self.assertEqual(self.loc['main'], poolfile.location)
  248. self.assertEqual(self.source['hello_2.2-2'], poolfile.binary.source)
  249. self.assertEqual(0, poolfile.filesize)
  250. self.assertEqual('deadbeef', poolfile.md5sum)
  251. self.assertEqual('deadbeef', poolfile.sha1sum)
  252. self.assertEqual('deadbeef', poolfile.sha256sum)
  253. def test_get_suite_version_by_package(self):
  254. 'test function get_suite_version_by_package()'
  255. result = get_suite_version_by_package('hello', 'i386', self.session)
  256. self.assertEqual(2, len(result))
  257. self.assertTrue(('sid', '2.2-1') in result)
  258. result = get_suite_version_by_package('hello', 'amd64', self.session)
  259. self.assertEqual(0, len(result))
  260. result = get_suite_version_by_package('python-hello', 'i386', self.session)
  261. self.assertEqual([('squeeze', '2.2-1')], result)
  262. result = get_suite_version_by_package('python-hello', 'amd64', self.session)
  263. self.assertEqual([('squeeze', '2.2-1')], result)
  264. def test_components(self):
  265. 'test class Component'
  266. self.assertEqual([self.loc['main']], self.comp['main'].location)
  267. self.assertEqual([self.loc['contrib']], self.comp['contrib'].location)
  268. self.assertEqual(0, self.comp['main'].overrides.count())
  269. def test_get_component_by_package_suite(self):
  270. 'test get_component_by_package_suite()'
  271. result = get_component_by_package_suite('hello', ['sid'], \
  272. session = self.session)
  273. self.assertEqual('main', result)
  274. result = get_component_by_package_suite('hello', ['hamm'], \
  275. session = self.session)
  276. self.assertEqual(None, result)
  277. result = get_component_by_package_suite('foobar', ['sid'], \
  278. session = self.session)
  279. self.assertEqual(None, result)
  280. # test that the newest version is returned
  281. result = get_component_by_package_suite('gnome-hello', ['squeeze'], \
  282. session = self.session)
  283. self.assertEqual('main', result)
  284. result = get_component_by_package_suite('gnome-hello', ['sid'], \
  285. session = self.session)
  286. self.assertEqual('contrib', result)
  287. # test arch_list
  288. result = get_component_by_package_suite('hello', ['sid'], \
  289. arch_list = ['i386'], session = self.session)
  290. self.assertEqual('main', result)
  291. result = get_component_by_package_suite('hello', ['sid'], \
  292. arch_list = ['amd64'], session = self.session)
  293. self.assertEqual(None, result)
  294. if __name__ == '__main__':
  295. unittest.main()