test_pagure_lib_model_delete_project.py 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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 json
  9. import unittest
  10. import sys
  11. import os
  12. import uuid
  13. sys.path.insert(
  14. 0, os.path.join(os.path.dirname(os.path.abspath(__file__)), "..")
  15. )
  16. import pagure.lib.model as model
  17. import pagure.lib.query
  18. import tests
  19. class DeleteProjectTests(tests.Modeltests):
  20. """Tests for flask issues controller of pagure"""
  21. def test_delete_project_with_group(self):
  22. """Test the model when we delete a project with a group."""
  23. # Create a project
  24. item = model.Project(
  25. user_id=1, # pingou
  26. name="test",
  27. description="test project #1",
  28. hook_token="aaabbbiii",
  29. )
  30. self.session.add(item)
  31. self.session.commit()
  32. # Create a group
  33. grp = model.PagureGroup(
  34. group_name="testgrp",
  35. display_name="Test group",
  36. description=None,
  37. group_type="user",
  38. user_id=1, # pingou
  39. )
  40. self.session.add(grp)
  41. self.session.commit()
  42. # Add group to project
  43. project_group = model.ProjectGroup(
  44. project_id=1, group_id=1, access="admin"
  45. )
  46. self.session.add(project_group)
  47. self.session.commit()
  48. self.assertEqual(self.session.query(model.Project).count(), 1)
  49. self.assertEqual(self.session.query(model.ProjectGroup).count(), 1)
  50. project = (
  51. self.session.query(model.Project)
  52. .filter(model.Project.id == 1)
  53. .one()
  54. )
  55. self.session.delete(project)
  56. self.session.commit()
  57. self.assertEqual(self.session.query(model.Project).count(), 0)
  58. self.assertEqual(self.session.query(model.ProjectGroup).count(), 0)
  59. def test_delete_project_with_user(self):
  60. """Test the model when we delete a project with users."""
  61. # Create a project
  62. item = model.Project(
  63. user_id=1, # pingou
  64. name="test",
  65. description="test project #1",
  66. hook_token="aaabbbiii",
  67. )
  68. self.session.add(item)
  69. self.session.commit()
  70. # Add user #2 to project
  71. project_user = model.ProjectUser(
  72. project_id=1, user_id=2, access="admin"
  73. )
  74. self.session.add(project_user)
  75. self.session.commit()
  76. self.assertEqual(self.session.query(model.Project).count(), 1)
  77. self.assertEqual(self.session.query(model.ProjectUser).count(), 1)
  78. self.assertEqual(self.session.query(model.User).count(), 2)
  79. project = (
  80. self.session.query(model.Project)
  81. .filter(model.Project.id == 1)
  82. .one()
  83. )
  84. self.session.delete(project)
  85. self.session.commit()
  86. self.assertEqual(self.session.query(model.Project).count(), 0)
  87. self.assertEqual(self.session.query(model.ProjectUser).count(), 0)
  88. self.assertEqual(self.session.query(model.User).count(), 2)
  89. def test_delete_project_with_coloredtags(self):
  90. """Test the model when we delete a project with Colored tags."""
  91. # Create a project
  92. item = model.Project(
  93. user_id=1, # pingou
  94. name="test",
  95. description="test project #1",
  96. hook_token="aaabbbiii",
  97. )
  98. self.session.add(item)
  99. self.session.commit()
  100. # Create two ColoredTags
  101. tagobj = model.TagColored(tag="Tag#1", project_id=1)
  102. self.session.add(tagobj)
  103. self.session.flush()
  104. tagobj = model.TagColored(tag="Tag#2", project_id=1)
  105. self.session.add(tagobj)
  106. self.session.flush()
  107. self.assertEqual(self.session.query(model.Project).count(), 1)
  108. self.assertEqual(self.session.query(model.TagColored).count(), 2)
  109. project = (
  110. self.session.query(model.Project)
  111. .filter(model.Project.id == 1)
  112. .one()
  113. )
  114. self.session.delete(project)
  115. self.session.commit()
  116. self.assertEqual(self.session.query(model.Project).count(), 0)
  117. self.assertEqual(self.session.query(model.TagColored).count(), 0)
  118. def test_delete_project_with_coloredtags_and_issues(self):
  119. """Test the model when we delete a project with Colored tags and
  120. issues."""
  121. # Create a project
  122. item = model.Project(
  123. user_id=1, # pingou
  124. name="test",
  125. description="test project #1",
  126. hook_token="aaabbbiii",
  127. )
  128. self.session.add(item)
  129. self.session.commit()
  130. # Create two ColoredTags
  131. tagobj = model.TagColored(tag="Tag#1", project_id=1)
  132. self.session.add(tagobj)
  133. self.session.flush()
  134. tagobj = model.TagColored(tag="Tag#2", project_id=1)
  135. self.session.add(tagobj)
  136. self.session.flush()
  137. # Create issues
  138. issue = model.Issue(
  139. id=pagure.lib.query.get_next_id(self.session, 1),
  140. project_id=1,
  141. title="Issue #1",
  142. content="Description #1",
  143. user_id=1,
  144. uid=uuid.uuid4().hex,
  145. private=False,
  146. )
  147. self.session.add(issue)
  148. self.session.commit()
  149. issue = model.Issue(
  150. id=pagure.lib.query.get_next_id(self.session, 1),
  151. project_id=1,
  152. title="Issue #2",
  153. content="Description #2",
  154. user_id=1,
  155. uid=uuid.uuid4().hex,
  156. private=False,
  157. )
  158. self.session.add(issue)
  159. self.session.commit()
  160. self.assertEqual(self.session.query(model.Project).count(), 1)
  161. self.assertEqual(self.session.query(model.TagColored).count(), 2)
  162. self.assertEqual(self.session.query(model.Issue).count(), 2)
  163. project = (
  164. self.session.query(model.Project)
  165. .filter(model.Project.id == 1)
  166. .one()
  167. )
  168. self.session.delete(project)
  169. self.session.commit()
  170. self.assertEqual(self.session.query(model.Project).count(), 0)
  171. self.assertEqual(self.session.query(model.TagColored).count(), 0)
  172. self.assertEqual(self.session.query(model.Issue).count(), 0)
  173. def test_delete_project_with_coloredtags_and_tagged_issues(self):
  174. """Test the model when we delete a project with Colored tags and
  175. tagged issues."""
  176. # Create a project
  177. item = model.Project(
  178. user_id=1, # pingou
  179. name="test",
  180. description="test project #1",
  181. hook_token="aaabbbiii",
  182. )
  183. self.session.add(item)
  184. self.session.commit()
  185. # Create two ColoredTags
  186. tagobj = model.TagColored(tag="Tag#1", project_id=1)
  187. self.session.add(tagobj)
  188. self.session.flush()
  189. tagobj = model.TagColored(tag="Tag#2", project_id=1)
  190. self.session.add(tagobj)
  191. self.session.flush()
  192. # Create issues
  193. issue = model.Issue(
  194. id=pagure.lib.query.get_next_id(self.session, 1),
  195. project_id=1,
  196. title="Issue #1",
  197. content="Description #1",
  198. user_id=1,
  199. uid="Issue#1",
  200. private=False,
  201. )
  202. self.session.add(issue)
  203. self.session.commit()
  204. issue = model.Issue(
  205. id=pagure.lib.query.get_next_id(self.session, 1),
  206. project_id=1,
  207. title="Issue #2",
  208. content="Description #2",
  209. user_id=1,
  210. uid="Issue#2",
  211. private=False,
  212. )
  213. self.session.add(issue)
  214. self.session.commit()
  215. # Tag the issue
  216. tagissue = model.TagIssueColored(issue_uid="Issue#1", tag_id=1)
  217. self.session.add(tagissue)
  218. self.session.commit()
  219. tagissue = model.TagIssueColored(issue_uid="Issue#2", tag_id=2)
  220. self.session.add(tagissue)
  221. self.session.commit()
  222. self.assertEqual(self.session.query(model.Project).count(), 1)
  223. self.assertEqual(self.session.query(model.TagColored).count(), 2)
  224. self.assertEqual(self.session.query(model.Issue).count(), 2)
  225. project = (
  226. self.session.query(model.Project)
  227. .filter(model.Project.id == 1)
  228. .one()
  229. )
  230. self.session.delete(project)
  231. self.session.commit()
  232. self.assertEqual(self.session.query(model.Project).count(), 0)
  233. self.assertEqual(self.session.query(model.TagColored).count(), 0)
  234. self.assertEqual(self.session.query(model.Issue).count(), 0)
  235. if __name__ == "__main__":
  236. unittest.main(verbosity=2)