test_pagure_lib_model_delete_project.py 9.2 KB

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