tests.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2013 The Distro Tracker Developers
  3. # See the COPYRIGHT file at the top-level directory of this distribution and
  4. # at http://deb.li/DTAuthors
  5. #
  6. # This file is part of Distro Tracker. It is subject to the license terms
  7. # in the LICENSE file found in the top-level directory of this
  8. # distribution and at http://deb.li/DTLicense. No part of Distro Tracker,
  9. # including this file, may be copied, modified, propagated, or distributed
  10. # except according to the terms contained in the LICENSE file.
  11. """
  12. Tests for the :mod:`distro_tracker.stdver_warnings` app.
  13. """
  14. from __future__ import unicode_literals
  15. from distro_tracker.test import TestCase
  16. from django.utils.six.moves import mock
  17. from distro_tracker.stdver_warnings.tracker_tasks \
  18. import UpdateStandardsVersionWarnings
  19. from distro_tracker.core.tasks import Event, Job, JobState
  20. from distro_tracker.core.models import SourcePackageName
  21. from distro_tracker.core.models import SourcePackage
  22. from distro_tracker.core.models import ActionItem, ActionItemType
  23. class StandardsVersionActionItemTests(TestCase):
  24. """
  25. Tests for the
  26. :class:`distro_tracker.stdver_warnings.tracker_tasks.UpdateStandardsVersionWarnings`
  27. task.
  28. """
  29. def setUp(self):
  30. self.package_name = \
  31. SourcePackageName.objects.create(name='dummy-package')
  32. self.package = SourcePackage.objects.create(
  33. source_package_name=self.package_name, version='1.0.0')
  34. self.default_policy_version = '3.9.4.0'
  35. self.debian_policy_name = SourcePackageName.objects.create(
  36. name='debian-policy')
  37. self.debian_policy = SourcePackage.objects.create(
  38. source_package_name=self.debian_policy_name,
  39. version=self.default_policy_version)
  40. self.job_state = mock.create_autospec(JobState)
  41. self.job_state.events_for_task.return_value = []
  42. self.job = mock.create_autospec(Job)
  43. self.job.job_state = self.job_state
  44. self.task = UpdateStandardsVersionWarnings()
  45. self.task.job = self.job
  46. def add_mock_event(self, name, arguments):
  47. """
  48. Helper method adding mock events which the news generation task will
  49. see when it runs.
  50. """
  51. self.job_state.events_for_task.return_value.append(
  52. Event(name=name, arguments=arguments)
  53. )
  54. def run_task(self, initial_task=False):
  55. """
  56. Initiates the task run.
  57. :param initial_task: An optional flag which if ``True`` means that the
  58. task should be ran as if it were directly passed to the
  59. :func:`distro_tracker.core.tasks.run_task` function.
  60. :type initial_task: Boolean
  61. """
  62. if initial_task:
  63. self.job_state.events_for_task.return_value = []
  64. self.job_state.processed_tasks = []
  65. else:
  66. # If it is not the initial task, add a dummy task to make it look
  67. # like that.
  68. self.job_state.processed_tasks = ['sometask']
  69. self.task.execute()
  70. def set_debian_policy_version(self, policy_version):
  71. """
  72. Set the version of the debian-policy package to the given version.
  73. """
  74. self.debian_policy.version = policy_version
  75. self.debian_policy.standards_version = policy_version.rsplit('.', 1)[0]
  76. self.debian_policy.save()
  77. def get_action_type(self):
  78. """
  79. Returns the :class:`distro_tracker.core.models.ActionItemType` for
  80. Standards-Version warnings.
  81. """
  82. return ActionItemType.objects.get_or_create(
  83. type_name=UpdateStandardsVersionWarnings.ACTION_ITEM_TYPE)[0]
  84. def add_mock_new_source_version_event(self, package):
  85. """
  86. Helper method adding mock 'new-source-package-version' events where
  87. the newly created source package should be the one given in the
  88. ``package`` parameter.
  89. """
  90. self.add_mock_event('new-source-package-version', {
  91. 'pk': package.pk,
  92. })
  93. def test_action_item_outdated_policy(self):
  94. """
  95. Tests that an action item is created when the package's standards
  96. version is outdated.
  97. """
  98. policy_version = '3.9.4.0'
  99. self.set_debian_policy_version(policy_version)
  100. # Set the std-ver below the policy version
  101. self.package.standards_version = '3.9.3'
  102. self.package.save()
  103. # Sanity check: no action item
  104. self.assertEqual(0, ActionItem.objects.count())
  105. self.add_mock_new_source_version_event(self.package)
  106. self.run_task()
  107. # Action item created.
  108. self.assertEqual(1, ActionItem.objects.count())
  109. # Correct type?
  110. item = ActionItem.objects.all()[0]
  111. self.assertEqual(
  112. item.item_type.type_name,
  113. self.get_action_type().type_name)
  114. # Contains the correct package standards version in extra data
  115. self.assertEqual(
  116. self.package.standards_version,
  117. item.extra_data['standards_version'])
  118. self.assertFalse(item.extra_data['severely_outdated'])
  119. # This is a wishlist severity issue
  120. self.assertEqual('wishlist', item.get_severity_display())
  121. def test_action_item_severely_outdated_policy(self):
  122. """
  123. Tests that an action item is created when the package's standards
  124. version is severely outdated (major version number differs from the
  125. major version number of debian-policy).
  126. """
  127. policy_version = '3.9.4.0'
  128. self.set_debian_policy_version(policy_version)
  129. # Set the std-ver below the policy version
  130. self.package.standards_version = '2.9.3'
  131. self.package.save()
  132. # Sanity check: no action item
  133. self.assertEqual(0, ActionItem.objects.count())
  134. self.add_mock_new_source_version_event(self.package)
  135. self.run_task()
  136. # Action item created.
  137. self.assertEqual(1, ActionItem.objects.count())
  138. # Contains the correct package standards version in extra data
  139. item = ActionItem.objects.all()[0]
  140. self.assertTrue(item.extra_data['severely_outdated'])
  141. # This is a high severity issue
  142. self.assertEqual('high', item.get_severity_display())
  143. def test_no_action_item_policy_up_to_date(self):
  144. """
  145. Tests that no action item is created when the package's
  146. Standards-Version is up to date.
  147. """
  148. policy_version = '3.9.4.0'
  149. self.set_debian_policy_version(policy_version)
  150. # Set the std-ver to be equal to the policy version.
  151. self.package.standards_version = '3.9.4'
  152. self.package.save()
  153. # Sanity check: no action item
  154. self.assertEqual(0, ActionItem.objects.count())
  155. self.add_mock_new_source_version_event(self.package)
  156. self.run_task()
  157. # Still no action items.
  158. self.assertEqual(0, ActionItem.objects.count())
  159. def test_action_item_policy_outdated_full_version(self):
  160. """
  161. Tests that an action item is created when the package's standards
  162. version is outdated and set by giving all 4 version numbers.
  163. """
  164. policy_version = '3.9.4.0'
  165. self.set_debian_policy_version(policy_version)
  166. # Set the std-ver below the policy version
  167. self.package.standards_version = '3.9.3.1'
  168. self.package.save()
  169. # Sanity check: no action item
  170. self.assertEqual(0, ActionItem.objects.count())
  171. self.add_mock_new_source_version_event(self.package)
  172. self.run_task()
  173. # Action item created.
  174. self.assertEqual(1, ActionItem.objects.count())
  175. item = ActionItem.objects.all()[0]
  176. self.assertEqual(
  177. item.item_type.type_name,
  178. self.get_action_type().type_name)
  179. def test_no_action_item_policy_up_to_date_full_version(self):
  180. """
  181. Tests that no action item is created when the package's
  182. Standards-Version is up to date and set by giving all 4 version
  183. numbers.
  184. """
  185. policy_version = '3.9.4.0'
  186. self.set_debian_policy_version(policy_version)
  187. # Set the std-ver to be equal to the policy version.
  188. self.package.standards_version = policy_version
  189. self.package.save()
  190. # Sanity check: no action item
  191. self.assertEqual(0, ActionItem.objects.count())
  192. self.add_mock_new_source_version_event(self.package)
  193. self.run_task()
  194. # Still no action items.
  195. self.assertEqual(0, ActionItem.objects.count())
  196. def test_action_item_removed(self):
  197. """
  198. Tests that an existing action item is removed when there is a new
  199. package version with a non-outdated Std-Ver.
  200. """
  201. policy_version = '3.9.4.0'
  202. self.set_debian_policy_version(policy_version)
  203. action_type = self.get_action_type()
  204. ActionItem.objects.create(
  205. package=self.package_name,
  206. item_type=action_type,
  207. short_description="Desc")
  208. self.package.standards_version = '3.9.3'
  209. self.package.save()
  210. # Create a new package with a higher Std-Ver
  211. new_package = SourcePackage.objects.create(
  212. source_package_name=self.package_name,
  213. version='4.0.0',
  214. standards_version='3.9.4.0')
  215. self.add_mock_new_source_version_event(new_package)
  216. self.run_task()
  217. # The action item has been removed.
  218. self.assertEqual(0, self.package_name.action_items.count())
  219. def test_action_item_updated(self):
  220. """
  221. Tests that an existing action item is updated when there is a new
  222. package version which still has an outdated Std-Ver.
  223. """
  224. policy_version = '3.9.4.0'
  225. self.set_debian_policy_version(policy_version)
  226. action_type = self.get_action_type()
  227. ActionItem.objects.create(
  228. package=self.package_name,
  229. item_type=action_type,
  230. short_description="Desc")
  231. self.package.standards_version = '3.9.2'
  232. self.package.save()
  233. # Create a new package with a higher Std-Ver
  234. new_package = SourcePackage.objects.create(
  235. source_package_name=self.package_name,
  236. version='4.0.0',
  237. standards_version='3.9.3.0')
  238. self.add_mock_new_source_version_event(new_package)
  239. self.run_task()
  240. # Still only one action item
  241. self.assertEqual(1, self.package_name.action_items.count())
  242. # The standards version in the extra data has been updated
  243. item = self.package_name.action_items.all()[0]
  244. self.assertEqual('3.9.3', item.extra_data['standards_version'])
  245. def test_task_directly_called(self):
  246. """
  247. Tests that when the task is directly called, the Standards-Version of
  248. all packages is checked.
  249. """
  250. policy_version = '3.9.4.0'
  251. self.set_debian_policy_version(policy_version)
  252. self.package.standards_version = '3.9.3'
  253. self.package.save()
  254. # Create another package with an outdated standards version
  255. outdated_package_name = \
  256. SourcePackageName.objects.create(name='outdated')
  257. SourcePackage.objects.create(
  258. source_package_name=outdated_package_name,
  259. version='4.0.0',
  260. standards_version='3.9.1.0')
  261. # Create a package with an up to date standards version
  262. up_to_date_package_name = \
  263. SourcePackageName.objects.create(name='uptodate')
  264. SourcePackage.objects.create(
  265. source_package_name=up_to_date_package_name,
  266. version='4.0.0',
  267. standards_version='3.9.4')
  268. # No events received by the task in this case.
  269. # Sanity check: No action items in the beginning
  270. self.assertEqual(0, ActionItem.objects.count())
  271. self.run_task(initial_task=True)
  272. # An action item is created for the two packages with out dated std-ver.
  273. self.assertEqual(2, ActionItem.objects.count())
  274. self.assertEqual(1, outdated_package_name.action_items.count())
  275. self.assertEqual(1, self.package_name.action_items.count())