test_pagure_lib_model.py 7.8 KB

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