test_pagure_lib_drop_issue.py 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. # -*- coding: utf-8 -*-
  2. """
  3. (c) 2017 - Copyright Red Hat Inc
  4. Authors:
  5. Pierre-Yves Chibon <pingou@pingoured.fr>
  6. """
  7. from __future__ import unicode_literals, absolute_import
  8. import unittest
  9. import shutil
  10. import sys
  11. import os
  12. from mock import patch, MagicMock
  13. sys.path.insert(0, os.path.join(os.path.dirname(
  14. os.path.abspath(__file__)), '..'))
  15. import pagure.lib.query
  16. import pagure.lib.model
  17. import tests
  18. class PagureLibDropIssuetests(tests.Modeltests):
  19. """ Tests for pagure.lib.query.drop_issue """
  20. @patch('pagure.lib.git.update_git')
  21. @patch('pagure.lib.notify.send_email')
  22. def setUp(self, p_send_email, p_ugt):
  23. """ Create a couple of tickets and add tag to the project so we can
  24. play with them later.
  25. """
  26. super(PagureLibDropIssuetests, self).setUp()
  27. p_send_email.return_value = True
  28. p_ugt.return_value = True
  29. tests.create_projects(self.session)
  30. repo = pagure.lib.query.get_authorized_project(self.session, 'test')
  31. # Before
  32. issues = pagure.lib.query.search_issues(self.session, repo)
  33. self.assertEqual(len(issues), 0)
  34. self.assertEqual(repo.open_tickets, 0)
  35. self.assertEqual(repo.open_tickets_public, 0)
  36. # Create two issues to play with
  37. msg = pagure.lib.query.new_issue(
  38. session=self.session,
  39. repo=repo,
  40. title='Test issue',
  41. content='We should work on this',
  42. user='pingou',
  43. )
  44. self.session.commit()
  45. self.assertEqual(msg.title, 'Test issue')
  46. self.assertEqual(repo.open_tickets, 1)
  47. self.assertEqual(repo.open_tickets_public, 1)
  48. msg = pagure.lib.query.new_issue(
  49. session=self.session,
  50. repo=repo,
  51. title='Test issue #2',
  52. content='We should work on this for the second time',
  53. user='foo',
  54. status='Open',
  55. )
  56. self.session.commit()
  57. self.assertEqual(msg.title, 'Test issue #2')
  58. self.assertEqual(repo.open_tickets, 2)
  59. self.assertEqual(repo.open_tickets_public, 2)
  60. # After
  61. issues = pagure.lib.query.search_issues(self.session, repo)
  62. self.assertEqual(len(issues), 2)
  63. # Add tag to the project
  64. pagure.lib.query.new_tag(
  65. self.session,
  66. 'red',
  67. 'red tag',
  68. '#ff0000',
  69. repo.id
  70. )
  71. self.session.commit()
  72. repo = pagure.lib.query.get_authorized_project(self.session, 'test')
  73. self.assertEqual(
  74. str(repo.tags_colored),
  75. '[TagColored(id: 1, tag:red, tag_description:red tag, color:#ff0000)]'
  76. )
  77. @patch('pagure.lib.git.update_git')
  78. @patch('pagure.lib.notify.send_email')
  79. @patch('pagure.lib.git._maybe_wait', tests.definitely_wait)
  80. def test_drop_issue(self, p_send_email, p_ugt):
  81. """ Test the drop_issue of pagure.lib.query.
  82. We had an issue where we could not delete issue that had been tagged
  83. with this test, we create two issues, tag one of them and delete
  84. it, ensuring it all goes well.
  85. """
  86. p_send_email.return_value = True
  87. p_ugt.return_value = True
  88. repo = pagure.lib.query.get_authorized_project(self.session, 'test')
  89. # Add tag to the second issue
  90. issue = pagure.lib.query.search_issues(self.session, repo, issueid=2)
  91. msgs = pagure.lib.query.update_tags(
  92. self.session,
  93. issue,
  94. tags=['red'],
  95. username='pingou',
  96. )
  97. self.session.commit()
  98. self.assertEqual(msgs, ['Issue tagged with: red'])
  99. repo = pagure.lib.query.get_authorized_project(self.session, 'test')
  100. self.assertEqual(len(repo.issues), 2)
  101. issue = pagure.lib.query.search_issues(self.session, repo, issueid=2)
  102. self.assertEqual(
  103. str(issue.tags),
  104. '[TagColored(id: 1, tag:red, tag_description:red tag, color:#ff0000)]'
  105. )
  106. # Drop the issue #2
  107. issue = pagure.lib.query.search_issues(self.session, repo, issueid=2)
  108. pagure.lib.query.drop_issue(
  109. self.session, issue, user='pingou')
  110. self.session.commit()
  111. repo = pagure.lib.query.get_authorized_project(self.session, 'test')
  112. self.assertEqual(len(repo.issues), 1)
  113. @patch('pagure.lib.git.update_git')
  114. @patch('pagure.lib.notify.send_email')
  115. @patch('pagure.lib.git._maybe_wait', tests.definitely_wait)
  116. def test_drop_issue_two_issues_one_tag(self, p_send_email, p_ugt):
  117. """ Test the drop_issue of pagure.lib.query.
  118. We had an issue where we could not delete issue that had been tagged
  119. with this test, we create two issues, tag them both and delete one
  120. then we check that the other issue is still tagged.
  121. """
  122. p_send_email.return_value = True
  123. p_ugt.return_value = True
  124. repo = pagure.lib.query.get_authorized_project(self.session, 'test')
  125. # Add the tag to both issues
  126. issue = pagure.lib.query.search_issues(self.session, repo, issueid=1)
  127. msgs = pagure.lib.query.update_tags(
  128. self.session,
  129. issue,
  130. tags=['red'],
  131. username='pingou',
  132. )
  133. self.session.commit()
  134. self.assertEqual(msgs, ['Issue tagged with: red'])
  135. issue = pagure.lib.query.search_issues(self.session, repo, issueid=2)
  136. msgs = pagure.lib.query.update_tags(
  137. self.session,
  138. issue,
  139. tags=['red'],
  140. username='pingou',
  141. )
  142. self.session.commit()
  143. self.assertEqual(msgs, ['Issue tagged with: red'])
  144. repo = pagure.lib.query.get_authorized_project(self.session, 'test')
  145. self.assertEqual(len(repo.issues), 2)
  146. issue = pagure.lib.query.search_issues(self.session, repo, issueid=1)
  147. self.assertEqual(
  148. str(issue.tags),
  149. '[TagColored(id: 1, tag:red, tag_description:red tag, color:#ff0000)]'
  150. )
  151. issue = pagure.lib.query.search_issues(self.session, repo, issueid=2)
  152. self.assertEqual(
  153. str(issue.tags),
  154. '[TagColored(id: 1, tag:red, tag_description:red tag, color:#ff0000)]'
  155. )
  156. # Drop the issue #2
  157. issue = pagure.lib.query.search_issues(self.session, repo, issueid=2)
  158. pagure.lib.query.drop_issue(
  159. self.session, issue, user='pingou')
  160. self.session.commit()
  161. repo = pagure.lib.query.get_authorized_project(self.session, 'test')
  162. self.assertEqual(len(repo.issues), 1)
  163. issue = pagure.lib.query.search_issues(self.session, repo, issueid=1)
  164. self.assertEqual(
  165. str(issue.tags),
  166. '[TagColored(id: 1, tag:red, tag_description:red tag, color:#ff0000)]'
  167. )
  168. issue = pagure.lib.query.search_issues(self.session, repo, issueid=2)
  169. self.assertIsNone(issue)
  170. if __name__ == '__main__':
  171. unittest.main(verbosity=2)