1
0

test_pagure_lib_drop_issue.py 6.9 KB

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