test_pagure_lib_model_delete_project.py 9.1 KB

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