test_pagure_lib_drop_issue.py 6.7 KB

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