test_pagure_lib_link.py 7.91 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
# -*- coding: utf-8 -*-

"""
 (c) 2015 - Copyright Red Hat Inc

 Authors:
   Pierre-Yves Chibon <pingou@pingoured.fr>

"""

__requires__ = ['SQLAlchemy >= 0.8']
import pkg_resources

import json
import unittest
import shutil
import sys
import os

import pygit2
from mock import patch

sys.path.insert(0, os.path.join(os.path.dirname(
    os.path.abspath(__file__)), '..'))

Pierre-Yves Chibon's avatar
Pierre-Yves Chibon committed
26
import pagure.lib.link
27 28 29 30 31 32
import tests

COMMENTS = [
    'Did you see #1?',
    'This is a duplicate of #2',
    'This is a fixes #3',
Pierre-Yves Chibon's avatar
Pierre-Yves Chibon committed
33
    'Might be worth looking at https://fedorahosted.org/pagure/tests2/issue/4',
34
    'This relates to #5',
Pierre-Yves Chibon's avatar
Pierre-Yves Chibon committed
35
    'Could this be related to https://fedorahosted.org/pagure/tests2/issue/6',
36 37 38
]


Pierre-Yves Chibon's avatar
Pierre-Yves Chibon committed
39 40
class PagureLibLinktests(tests.Modeltests):
    """ Tests for pagure.lib.link """
41 42

    def test_get_relation_relates(self):
Pierre-Yves Chibon's avatar
Pierre-Yves Chibon committed
43
        """ Test the get_relation function of pagure.lib.link with relates.
44 45 46
        """

        self.assertEqual(
Pierre-Yves Chibon's avatar
Pierre-Yves Chibon committed
47
            pagure.lib.link.get_relation(
48 49 50 51 52 53 54 55 56 57 58
                self.session,
                'test',
                None,
                COMMENTS[0],
                'relates',
            ),
            []
        )

        tests.create_projects(self.session)

Pierre-Yves Chibon's avatar
Pierre-Yves Chibon committed
59
        link = pagure.lib.link.get_relation(
60 61 62 63
            self.session, 'test', None, COMMENTS[4], 'relates')
        self.assertEqual(link, [])

        # Create the issue
Pierre-Yves Chibon's avatar
Pierre-Yves Chibon committed
64 65
        repo = pagure.lib.get_project(self.session, 'test')
        pagure.lib.new_issue(
66 67 68 69 70 71 72 73 74 75 76
            self.session,
            repo,
            title='foo',
            content='bar',
            user='pingou',
            ticketfolder=None,
            issue_id=5,
            notify=False)
        self.session.commit()

        for idx, comment in enumerate(COMMENTS):
Pierre-Yves Chibon's avatar
Pierre-Yves Chibon committed
77
            link = pagure.lib.link.get_relation(
78 79 80 81 82 83 84 85
                self.session, 'test', None, comment, 'relates')
            if idx == 4:
                self.assertEqual(
                    str(link),
                    '[Issue(5, project:test, user:pingou, title:foo)]')
            else:
                self.assertEqual(link, [])

Pierre-Yves Chibon's avatar
Pierre-Yves Chibon committed
86
        link = pagure.lib.link.get_relation(
87 88 89 90
            self.session, 'test', None, COMMENTS[5], 'relates')
        self.assertEqual(link, [])

        # Create the issue
Pierre-Yves Chibon's avatar
Pierre-Yves Chibon committed
91 92
        repo = pagure.lib.get_project(self.session, 'test')
        pagure.lib.new_issue(
93 94 95 96 97 98 99 100 101 102 103
            self.session,
            repo,
            title='another foo',
            content='another bar',
            user='pingou',
            ticketfolder=None,
            issue_id=6,
            notify=False)
        self.session.commit()

        for idx, comment in enumerate(COMMENTS):
Pierre-Yves Chibon's avatar
Pierre-Yves Chibon committed
104
            link = pagure.lib.link.get_relation(
105 106 107 108 109 110 111 112 113 114 115 116 117
                self.session, 'test', None, comment, 'relates')
            if idx == 4:
                self.assertEqual(
                    str(link),
                    '[Issue(5, project:test, user:pingou, title:foo)]')
            elif idx == 5:
                self.assertEqual(
                    str(link),
                    '[Issue(6, project:test, user:pingou, title:another foo)]')
            else:
                self.assertEqual(link, [])

    def test_get_relation_fixes(self):
Pierre-Yves Chibon's avatar
Pierre-Yves Chibon committed
118
        """ Test the get_relation function of pagure.lib.link with fixes.
119 120 121
        """

        self.assertEqual(
Pierre-Yves Chibon's avatar
Pierre-Yves Chibon committed
122
            pagure.lib.link.get_relation(
123 124 125 126 127 128 129 130 131 132 133
                self.session,
                'test',
                None,
                COMMENTS[0],
                'fixes',
            ),
            []
        )

        tests.create_projects(self.session)

Pierre-Yves Chibon's avatar
Pierre-Yves Chibon committed
134
        link = pagure.lib.link.get_relation(
135 136 137 138
            self.session, 'test', None, COMMENTS[2], 'fixes')
        self.assertEqual(link, [])

        # Create the issue
Pierre-Yves Chibon's avatar
Pierre-Yves Chibon committed
139 140
        repo = pagure.lib.get_project(self.session, 'test')
        pagure.lib.new_issue(
141 142 143 144 145 146 147 148 149 150 151
            self.session,
            repo,
            title='issue 3',
            content='content issue 3',
            user='pingou',
            ticketfolder=None,
            issue_id=3,
            notify=False)
        self.session.commit()

        for idx, comment in enumerate(COMMENTS):
Pierre-Yves Chibon's avatar
Pierre-Yves Chibon committed
152
            link = pagure.lib.link.get_relation(
153 154 155 156 157 158 159 160
                self.session, 'test', None, comment, 'fixes')
            if idx == 2:
                self.assertEqual(
                    str(link),
                    '[Issue(3, project:test, user:pingou, title:issue 3)]')
            else:
                self.assertEqual(link, [])

161
    def test_relates_regex(self):
Pierre-Yves Chibon's avatar
Pierre-Yves Chibon committed
162
        ''' Test the relates regex present in pagure.lib.link. '''
163
        text = 'relates  to   http://localhost/fork/pingou/test/issue/1'
Pierre-Yves Chibon's avatar
Pierre-Yves Chibon committed
164
        for index, regex in enumerate(pagure.lib.link.RELATES):
165 166 167 168 169
            if index == 2:
                self.assertNotEqual(regex.match(text), None)
            else:
                self.assertEqual(regex.match(text), None)

170
        text = 'relates http://209.132.184.222/fork/pingou/test/issue/1'
Pierre-Yves Chibon's avatar
Pierre-Yves Chibon committed
171
        for index, regex in enumerate(pagure.lib.link.RELATES):
172 173 174 175 176
            if index == 2:
                self.assertNotEqual(regex.match(text), None)
            else:
                self.assertEqual(regex.match(text), None)

177
        text = 'This relates  to  #5'
Pierre-Yves Chibon's avatar
Pierre-Yves Chibon committed
178
        for index, regex in enumerate(pagure.lib.link.RELATES):
179 180 181 182 183 184
            if index == 0:
                self.assertNotEqual(regex.match(text), None)
            else:
                self.assertEqual(regex.match(text), None)

        text = 'Could this be related to  '\
Pierre-Yves Chibon's avatar
Pierre-Yves Chibon committed
185 186
            ' https://fedorahosted.org/pagure/tests2/issue/6'
        for index, regex in enumerate(pagure.lib.link.RELATES):
187 188 189 190
            if index == 2:
                self.assertNotEqual(regex.match(text), None)
            else:
                self.assertEqual(regex.match(text), None)
191

192
    def test_fixes_regex(self):
Pierre-Yves Chibon's avatar
Pierre-Yves Chibon committed
193
        ''' Test the fixes regex present in pagure.lib.link. '''
194

195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
        # project/issue matches
        def project_match(text, groups):
            match = None
            for regex in pagure.lib.link.FIXES:
                match = regex.match(text)
                if match:
                    break
            self.assertNotEqual(match, None)
            self.assertEqual(len(match.groups()), 2)
            self.assertEqual(match.groups(), groups)

        data = [
            # [string, groups]
        ]

        project_match('fixes     http://localhost/fork/pingou/test/issue/1',
            ('test', '1'))
        project_match('fix http://209.132.184.222/fork/pingou/test/issue/1',
            ('test', '1'))
        project_match('Could this be fixes  '
            ' https://fedorahosted.org/pagure/tests2/issue/6',
            ('tests2', '6'))
        project_match('merged https://pagure.io/myproject/pull-request/70',
            ('myproject', '70'))
        project_match('Now we merge https://pagure.io/myproject/pull-request/99',
            ('myproject', '99'))

        # issue matches
        def issue_match(text, issue):
            match = None
            for regex in pagure.lib.link.FIXES:
                match = regex.match(text)
                if match:
                    break
            self.assertNotEqual(match, None)
            self.assertEqual(len(match.groups()), 1)
            self.assertEqual(match.group(1), issue)

        issue_match('This fixed  #5', '5')
        issue_match('Merged  #17', '17')
        issue_match('Fixed:  #23', '23')
        issue_match('This commit fixes:  #42', '42')
        issue_match('Merge #137', '137')

        # no match
        def no_match(text):
            match = None
            for regex in pagure.lib.link.FIXES:
                match = regex.match(text)
                if match:
                    break
            self.assertEqual(match, None)

        no_match('nowhitespacemerge: #47')
        no_match('This commit unmerges #45')
        no_match('Fixed 45 typos')
        no_match('Fixed 4 typos')
        no_match("Merge branch 'work'")
253

254 255

if __name__ == '__main__':
Pierre-Yves Chibon's avatar
Pierre-Yves Chibon committed
256
    SUITE = unittest.TestLoader().loadTestsFromTestCase(PagureLibLinktests)
257
    unittest.TextTestRunner(verbosity=2).run(SUITE)