test_pagure_lib_model_delete_project.py 9.2 KB

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