test_pagure_lib_model.py 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. # -*- coding: utf-8 -*-
  2. """
  3. (c) 2015-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 sys
  10. import os
  11. from mock import patch
  12. sys.path.insert(0, os.path.join(os.path.dirname(
  13. os.path.abspath(__file__)), '..'))
  14. import pagure.lib.model
  15. import pagure.lib.query
  16. import tests
  17. class PagureLibModeltests(tests.Modeltests):
  18. """ Tests for pagure.lib.model """
  19. def test_user__repr__(self):
  20. """ Test the User.__repr__ function of pagure.lib.model. """
  21. item = pagure.lib.query.search_user(self.session, email='foo@bar.com')
  22. self.assertEqual(str(item), 'User: 2 - name foo')
  23. self.assertEqual('foo', item.user)
  24. self.assertEqual('foo', item.username)
  25. self.assertEqual([], item.groups)
  26. @patch('pagure.lib.git.update_git')
  27. @patch('pagure.lib.notify.send_email')
  28. def test_issue__repr__(self, p_send_email, p_ugt):
  29. """ Test the Issue.__repr__ function of pagure.lib.model. """
  30. p_send_email.return_value = True
  31. p_ugt.return_value = True
  32. tests.create_projects(self.session)
  33. repo = pagure.lib.query.get_authorized_project(self.session, 'test')
  34. # Create an issue
  35. msg = pagure.lib.query.new_issue(
  36. session=self.session,
  37. repo=repo,
  38. title='Test issue',
  39. content='We should work on this',
  40. user='pingou',
  41. )
  42. self.assertEqual(msg.title, 'Test issue')
  43. issues = pagure.lib.query.search_issues(self.session, repo)
  44. self.assertEqual(len(issues), 1)
  45. self.assertEqual(
  46. str(issues[0]),
  47. 'Issue(1, project:test, user:pingou, title:Test issue)')
  48. @patch('pagure.lib.git.update_git')
  49. @patch('pagure.lib.notify.send_email')
  50. def test_pullrequest__repr__(self, p_send_email, p_ugt):
  51. """ Test the PullRequest.__repr__ function of pagure.lib.model. """
  52. p_send_email.return_value = True
  53. p_ugt.return_value = True
  54. tests.create_projects(self.session)
  55. # Create a forked repo
  56. item = pagure.lib.model.Project(
  57. user_id=1, # pingou
  58. name='test',
  59. description='test project #1',
  60. is_fork=True,
  61. parent_id=1,
  62. hook_token='aaabbbyyy',
  63. )
  64. self.session.commit()
  65. self.session.add(item)
  66. repo = pagure.lib.query.get_authorized_project(self.session, 'test')
  67. forked_repo = pagure.lib.query.get_authorized_project(
  68. self.session, 'test', user='pingou')
  69. # Create an pull-request
  70. req = pagure.lib.query.new_pull_request(
  71. session=self.session,
  72. repo_from=forked_repo,
  73. branch_from='master',
  74. repo_to=repo,
  75. branch_to='master',
  76. title='test pull-request',
  77. user='pingou',
  78. )
  79. self.assertEqual(req.id, 1)
  80. self.assertEqual(req.title, 'test pull-request')
  81. self.assertEqual(
  82. str(req),
  83. 'PullRequest(1, project:test, user:pingou, '
  84. 'title:test pull-request)')
  85. request = pagure.lib.query.search_pull_requests(self.session, requestid=1)
  86. self.assertEqual(
  87. str(request),
  88. 'PullRequest(1, project:test, user:pingou, '
  89. 'title:test pull-request)')
  90. def test_paguregroup__repr__(self):
  91. """ Test the PagureGroup.__repr__ function of pagure.lib.model. """
  92. item = pagure.lib.model.PagureGroup(
  93. group_name='admin',
  94. display_name='admin group',
  95. description='the local admin group',
  96. user_id=1,
  97. )
  98. self.session.add(item)
  99. self.session.commit()
  100. self.assertEqual(str(item), 'Group: 1 - name admin')
  101. def test_tagissue__repr__(self):
  102. """ Test the TagIssue.__repr__ function of pagure.lib.model. """
  103. self.test_issue__repr__()
  104. repo = pagure.lib.query.get_authorized_project(self.session, 'test')
  105. issues = pagure.lib.query.search_issues(self.session, repo)
  106. self.assertEqual(len(issues), 1)
  107. item = pagure.lib.model.Tag(tag='foo')
  108. self.session.add(item)
  109. self.session.commit()
  110. item = pagure.lib.model.TagIssue(
  111. issue_uid=issues[0].uid,
  112. tag='foo'
  113. )
  114. self.session.add(item)
  115. self.session.commit()
  116. self.assertEqual(str(item), 'TagIssue(issue:1, tag:foo)')
  117. def test_tagissuecolor__repr__(self):
  118. """ Test the TagIssue.__repr__ function of pagure.lib.model. """
  119. self.test_issue__repr__()
  120. repo = pagure.lib.query.get_authorized_project(self.session, 'test')
  121. issues = pagure.lib.query.search_issues(self.session, repo)
  122. self.assertEqual(len(issues), 1)
  123. item = pagure.lib.model.TagColored(
  124. tag='foo',
  125. tag_description='bar',
  126. tag_color='DeepSkyBlue',
  127. project_id=repo.id)
  128. self.session.add(item)
  129. self.session.commit()
  130. item = pagure.lib.model.TagIssueColored(
  131. issue_uid=issues[0].uid,
  132. tag_id=item.id
  133. )
  134. self.session.add(item)
  135. self.session.commit()
  136. self.assertEqual(
  137. str(item),
  138. 'TagIssueColored(issue:1, tag:foo, project:test)'
  139. )
  140. def test_group_project_ordering(self):
  141. """ Test the ordering of project.groups. """
  142. # Create three projects
  143. item = pagure.lib.model.Project(
  144. user_id=1, # pingou
  145. name='aaa',
  146. description='Project aaa',
  147. hook_token='aaabbbccc',
  148. )
  149. item.close_status = ['Invalid', 'Fixed', 'Duplicate']
  150. self.session.add(item)
  151. item = pagure.lib.model.Project(
  152. user_id=1, # pingou
  153. name='KKK',
  154. description='project KKK',
  155. hook_token='aaabbbddd',
  156. )
  157. item.close_status = ['Invalid', 'Fixed', 'Duplicate']
  158. self.session.add(item)
  159. item = pagure.lib.model.Project(
  160. user_id=1, # pingou
  161. name='zzz',
  162. description='Namespaced project zzz',
  163. hook_token='aaabbbeee',
  164. namespace='somenamespace',
  165. )
  166. item.close_status = ['Invalid', 'Fixed', 'Duplicate']
  167. self.session.add(item)
  168. # Create a group
  169. group = pagure.lib.model.PagureGroup(
  170. group_name='testgrp',
  171. display_name='Test group',
  172. description=None,
  173. group_type='user',
  174. user_id=1, # pingou
  175. )
  176. item.close_status = ['Invalid', 'Fixed', 'Duplicate']
  177. self.session.add(group)
  178. self.session.commit()
  179. # Add projects to group
  180. for ns, reponame in [
  181. (None, 'aaa'), (None, 'KKK'), ('somenamespace', 'zzz')]:
  182. repo = pagure.lib.query.get_authorized_project(
  183. self.session, reponame, namespace=ns)
  184. msg = pagure.lib.query.add_group_to_project(
  185. self.session,
  186. project=repo,
  187. new_group='testgrp',
  188. user='pingou',
  189. create=False,
  190. is_admin=False
  191. )
  192. self.session.commit()
  193. self.assertEqual(msg, 'Group added')
  194. # Check the ordering
  195. group = pagure.lib.query.search_groups(self.session, group_name='testgrp')
  196. # Default PostgreSQL order
  197. order = ['aaa', 'KKK', 'somenamespace/zzz']
  198. # Odd, SQLite order
  199. if str(self.session.bind.engine.url).startswith('sqlite:'):
  200. order = ['somenamespace/zzz', 'aaa', 'KKK']
  201. self.assertEqual(
  202. [p.fullname for p in group.projects],
  203. order
  204. )
  205. if __name__ == '__main__':
  206. unittest.main(verbosity=2)