test_pagure_lib_model.py 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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(
  13. 0, os.path.join(os.path.dirname(os.path.abspath(__file__)), "..")
  14. )
  15. import pagure.lib.model
  16. import pagure.lib.query
  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.query.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.query.get_authorized_project(self.session, "test")
  35. # Create an issue
  36. msg = pagure.lib.query.new_issue(
  37. session=self.session,
  38. repo=repo,
  39. title="Test issue",
  40. content="We should work on this",
  41. user="pingou",
  42. )
  43. self.assertEqual(msg.title, "Test issue")
  44. issues = pagure.lib.query.search_issues(self.session, repo)
  45. self.assertEqual(len(issues), 1)
  46. self.assertEqual(
  47. str(issues[0]),
  48. "Issue(1, project:test, user:pingou, title:Test issue)",
  49. )
  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. )
  72. # Create an pull-request
  73. req = pagure.lib.query.new_pull_request(
  74. session=self.session,
  75. repo_from=forked_repo,
  76. branch_from="master",
  77. repo_to=repo,
  78. branch_to="master",
  79. title="test pull-request",
  80. user="pingou",
  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. )
  89. request = pagure.lib.query.search_pull_requests(
  90. self.session, requestid=1
  91. )
  92. self.assertEqual(
  93. str(request),
  94. "PullRequest(1, project:test, user:pingou, "
  95. "title:test pull-request)",
  96. )
  97. def test_paguregroup__repr__(self):
  98. """Test the PagureGroup.__repr__ function of pagure.lib.model."""
  99. item = pagure.lib.model.PagureGroup(
  100. group_name="admin",
  101. display_name="admin group",
  102. description="the local admin group",
  103. user_id=1,
  104. )
  105. self.session.add(item)
  106. self.session.commit()
  107. self.assertEqual(str(item), "Group: 1 - name admin")
  108. def test_tagissue__repr__(self):
  109. """Test the TagIssue.__repr__ function of pagure.lib.model."""
  110. self.test_issue__repr__()
  111. repo = pagure.lib.query.get_authorized_project(self.session, "test")
  112. issues = pagure.lib.query.search_issues(self.session, repo)
  113. self.assertEqual(len(issues), 1)
  114. item = pagure.lib.model.Tag(tag="foo")
  115. self.session.add(item)
  116. self.session.commit()
  117. item = pagure.lib.model.TagIssue(issue_uid=issues[0].uid, tag="foo")
  118. self.session.add(item)
  119. self.session.commit()
  120. self.assertEqual(str(item), "TagIssue(issue:1, tag:foo)")
  121. def test_tagissuecolor__repr__(self):
  122. """Test the TagIssue.__repr__ function of pagure.lib.model."""
  123. self.test_issue__repr__()
  124. repo = pagure.lib.query.get_authorized_project(self.session, "test")
  125. issues = pagure.lib.query.search_issues(self.session, repo)
  126. self.assertEqual(len(issues), 1)
  127. item = pagure.lib.model.TagColored(
  128. tag="foo",
  129. tag_description="bar",
  130. tag_color="DeepSkyBlue",
  131. project_id=repo.id,
  132. )
  133. self.session.add(item)
  134. self.session.commit()
  135. item = pagure.lib.model.TagIssueColored(
  136. issue_uid=issues[0].uid, tag_id=item.id
  137. )
  138. self.session.add(item)
  139. self.session.commit()
  140. self.assertEqual(
  141. str(item), "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"),
  185. (None, "KKK"),
  186. ("somenamespace", "zzz"),
  187. ]:
  188. repo = pagure.lib.query.get_authorized_project(
  189. self.session, reponame, namespace=ns
  190. )
  191. msg = pagure.lib.query.add_group_to_project(
  192. self.session,
  193. project=repo,
  194. new_group="testgrp",
  195. user="pingou",
  196. create=False,
  197. is_admin=False,
  198. )
  199. self.session.commit()
  200. self.assertEqual(msg, "Group added")
  201. # Check the ordering
  202. group = pagure.lib.query.search_groups(
  203. self.session, group_name="testgrp"
  204. )
  205. # Default PostgreSQL order
  206. order = ["aaa", "KKK", "somenamespace/zzz"]
  207. # Odd, SQLite order
  208. if str(self.session.bind.engine.url).startswith("sqlite:"):
  209. order = ["somenamespace/zzz", "aaa", "KKK"]
  210. self.assertEqual([p.fullname for p in group.projects], order)
  211. if __name__ == "__main__":
  212. unittest.main(verbosity=2)