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