test_pagure_lib_link.py 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. # -*- coding: utf-8 -*-
  2. """
  3. (c) 2015 - 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 shutil
  12. import sys
  13. import os
  14. import pygit2
  15. from mock import patch
  16. sys.path.insert(0, os.path.join(os.path.dirname(
  17. os.path.abspath(__file__)), '..'))
  18. import pagure.lib.link
  19. import tests
  20. COMMENTS = [
  21. 'Did you see #1?',
  22. 'This is a duplicate of #2',
  23. 'This is a fixes #3',
  24. 'Might be worth looking at https://fedorahosted.org/pagure/tests2/issue/4',
  25. 'This relates to #5',
  26. 'Could this be related to https://fedorahosted.org/pagure/tests2/issue/6',
  27. ]
  28. class PagureLibLinktests(tests.Modeltests):
  29. """ Tests for pagure.lib.link """
  30. def test_get_relation_relates(self):
  31. """ Test the get_relation function of pagure.lib.link with relates.
  32. """
  33. self.assertEqual(
  34. pagure.lib.link.get_relation(
  35. self.session,
  36. 'test',
  37. None,
  38. COMMENTS[0],
  39. 'relates',
  40. ),
  41. []
  42. )
  43. tests.create_projects(self.session)
  44. link = pagure.lib.link.get_relation(
  45. self.session, 'test', None, COMMENTS[4], 'relates')
  46. self.assertEqual(link, [])
  47. # Create the issue
  48. repo = pagure.lib.get_project(self.session, 'test')
  49. pagure.lib.new_issue(
  50. self.session,
  51. repo,
  52. title='foo',
  53. content='bar',
  54. user='pingou',
  55. ticketfolder=None,
  56. issue_id=5,
  57. notify=False)
  58. self.session.commit()
  59. for idx, comment in enumerate(COMMENTS):
  60. link = pagure.lib.link.get_relation(
  61. self.session, 'test', None, comment, 'relates')
  62. if idx == 4:
  63. self.assertEqual(
  64. str(link),
  65. '[Issue(5, project:test, user:pingou, title:foo)]')
  66. else:
  67. self.assertEqual(link, [])
  68. link = pagure.lib.link.get_relation(
  69. self.session, 'test', None, COMMENTS[5], 'relates')
  70. self.assertEqual(link, [])
  71. # Create the issue
  72. repo = pagure.lib.get_project(self.session, 'test')
  73. pagure.lib.new_issue(
  74. self.session,
  75. repo,
  76. title='another foo',
  77. content='another bar',
  78. user='pingou',
  79. ticketfolder=None,
  80. issue_id=6,
  81. notify=False)
  82. self.session.commit()
  83. for idx, comment in enumerate(COMMENTS):
  84. link = pagure.lib.link.get_relation(
  85. self.session, 'test', None, comment, 'relates')
  86. if idx == 4:
  87. self.assertEqual(
  88. str(link),
  89. '[Issue(5, project:test, user:pingou, title:foo)]')
  90. elif idx == 5:
  91. self.assertEqual(
  92. str(link),
  93. '[Issue(6, project:test, user:pingou, title:another foo)]')
  94. else:
  95. self.assertEqual(link, [])
  96. def test_get_relation_fixes(self):
  97. """ Test the get_relation function of pagure.lib.link with fixes.
  98. """
  99. self.assertEqual(
  100. pagure.lib.link.get_relation(
  101. self.session,
  102. 'test',
  103. None,
  104. COMMENTS[0],
  105. 'fixes',
  106. ),
  107. []
  108. )
  109. tests.create_projects(self.session)
  110. link = pagure.lib.link.get_relation(
  111. self.session, 'test', None, COMMENTS[2], 'fixes')
  112. self.assertEqual(link, [])
  113. # Create the issue
  114. repo = pagure.lib.get_project(self.session, 'test')
  115. pagure.lib.new_issue(
  116. self.session,
  117. repo,
  118. title='issue 3',
  119. content='content issue 3',
  120. user='pingou',
  121. ticketfolder=None,
  122. issue_id=3,
  123. notify=False)
  124. self.session.commit()
  125. for idx, comment in enumerate(COMMENTS):
  126. link = pagure.lib.link.get_relation(
  127. self.session, 'test', None, comment, 'fixes')
  128. if idx == 2:
  129. self.assertEqual(
  130. str(link),
  131. '[Issue(3, project:test, user:pingou, title:issue 3)]')
  132. else:
  133. self.assertEqual(link, [])
  134. def test_relates_regex(self):
  135. ''' Test the relates regex present in pagure.lib.link. '''
  136. text = 'relates to http://localhost/fork/pingou/test/issue/1'
  137. for index, regex in enumerate(pagure.lib.link.RELATES):
  138. if index == 2:
  139. self.assertNotEqual(regex.match(text), None)
  140. else:
  141. self.assertEqual(regex.match(text), None)
  142. text = 'relates http://209.132.184.222/fork/pingou/test/issue/1'
  143. for index, regex in enumerate(pagure.lib.link.RELATES):
  144. if index == 2:
  145. self.assertNotEqual(regex.match(text), None)
  146. else:
  147. self.assertEqual(regex.match(text), None)
  148. text = 'This relates to #5'
  149. for index, regex in enumerate(pagure.lib.link.RELATES):
  150. if index == 0:
  151. self.assertNotEqual(regex.match(text), None)
  152. else:
  153. self.assertEqual(regex.match(text), None)
  154. text = 'Could this be related to '\
  155. ' https://fedorahosted.org/pagure/tests2/issue/6'
  156. for index, regex in enumerate(pagure.lib.link.RELATES):
  157. if index == 2:
  158. self.assertNotEqual(regex.match(text), None)
  159. else:
  160. self.assertEqual(regex.match(text), None)
  161. def test_fixes_regex(self):
  162. ''' Test the fixes regex present in pagure.lib.link. '''
  163. # project/issue matches
  164. def project_match(text, groups):
  165. match = None
  166. for regex in pagure.lib.link.FIXES:
  167. match = regex.match(text)
  168. if match:
  169. break
  170. self.assertNotEqual(match, None)
  171. self.assertEqual(len(match.groups()), 2)
  172. self.assertEqual(match.groups(), groups)
  173. data = [
  174. # [string, groups]
  175. ]
  176. project_match('fixes http://localhost/fork/pingou/test/issue/1',
  177. ('test', '1'))
  178. project_match('fix http://209.132.184.222/fork/pingou/test/issue/1',
  179. ('test', '1'))
  180. project_match('Could this be fixes '
  181. ' https://fedorahosted.org/pagure/tests2/issue/6',
  182. ('tests2', '6'))
  183. project_match('merged https://pagure.io/myproject/pull-request/70',
  184. ('myproject', '70'))
  185. project_match('Now we merge https://pagure.io/myproject/pull-request/99',
  186. ('myproject', '99'))
  187. # issue matches
  188. def issue_match(text, issue):
  189. match = None
  190. for regex in pagure.lib.link.FIXES:
  191. match = regex.match(text)
  192. if match:
  193. break
  194. self.assertNotEqual(match, None)
  195. self.assertEqual(len(match.groups()), 1)
  196. self.assertEqual(match.group(1), issue)
  197. issue_match('This fixed #5', '5')
  198. issue_match('Merged #17', '17')
  199. issue_match('Fixed: #23', '23')
  200. issue_match('This commit fixes: #42', '42')
  201. issue_match('Merge #137', '137')
  202. # no match
  203. def no_match(text):
  204. match = None
  205. for regex in pagure.lib.link.FIXES:
  206. match = regex.match(text)
  207. if match:
  208. break
  209. self.assertEqual(match, None)
  210. no_match('nowhitespacemerge: #47')
  211. no_match('This commit unmerges #45')
  212. no_match('Fixed 45 typos')
  213. no_match('Fixed 4 typos')
  214. no_match("Merge branch 'work'")
  215. if __name__ == '__main__':
  216. SUITE = unittest.TestLoader().loadTestsFromTestCase(PagureLibLinktests)
  217. unittest.TextTestRunner(verbosity=2).run(SUITE)