Blame tests/test_pagure_lib_link.py

Pierre-Yves Chibon 35fa85
# -*- coding: utf-8 -*-
Pierre-Yves Chibon 35fa85
Pierre-Yves Chibon 35fa85
"""
Pierre-Yves Chibon 35fa85
 (c) 2015 - Copyright Red Hat Inc
Pierre-Yves Chibon 35fa85
Pierre-Yves Chibon 35fa85
 Authors:
Pierre-Yves Chibon 35fa85
   Pierre-Yves Chibon <pingou@pingoured.fr></pingou@pingoured.fr>
Pierre-Yves Chibon 35fa85
Pierre-Yves Chibon 35fa85
"""
Pierre-Yves Chibon 35fa85
Pierre-Yves Chibon 35fa85
__requires__ = ['SQLAlchemy >= 0.8']
Pierre-Yves Chibon 35fa85
import pkg_resources
Pierre-Yves Chibon 35fa85
Pierre-Yves Chibon 35fa85
import json
Pierre-Yves Chibon 35fa85
import unittest
Pierre-Yves Chibon 35fa85
import shutil
Pierre-Yves Chibon 35fa85
import sys
Pierre-Yves Chibon 35fa85
import os
Pierre-Yves Chibon 35fa85
Pierre-Yves Chibon 35fa85
import pygit2
Pierre-Yves Chibon 35fa85
from mock import patch
Pierre-Yves Chibon 35fa85
Pierre-Yves Chibon 35fa85
sys.path.insert(0, os.path.join(os.path.dirname(
Pierre-Yves Chibon 35fa85
    os.path.abspath(__file__)), '..'))
Pierre-Yves Chibon 35fa85
Farhaan Bukhsh 72e9db
import pagure
Pierre-Yves Chibon fe5017
import pagure.lib.link
Pierre-Yves Chibon 35fa85
import tests
Pierre-Yves Chibon 35fa85
Pierre-Yves Chibon 35fa85
COMMENTS = [
Pierre-Yves Chibon 35fa85
    'Did you see #1?',
Pierre-Yves Chibon 35fa85
    'This is a duplicate of #2',
Pierre-Yves Chibon 35fa85
    'This is a fixes #3',
Pierre-Yves Chibon fe5017
    'Might be worth looking at https://fedorahosted.org/pagure/tests2/issue/4',
Pierre-Yves Chibon 35fa85
    'This relates to #5',
Pierre-Yves Chibon fe5017
    'Could this be related to https://fedorahosted.org/pagure/tests2/issue/6',
Pierre-Yves Chibon 35fa85
]
Pierre-Yves Chibon 35fa85
Pierre-Yves Chibon 35fa85
Pierre-Yves Chibon fe5017
class PagureLibLinktests(tests.Modeltests):
Pierre-Yves Chibon fe5017
    """ Tests for pagure.lib.link """
Pierre-Yves Chibon 35fa85
Pierre-Yves Chibon 35fa85
    def test_get_relation_relates(self):
Pierre-Yves Chibon fe5017
        """ Test the get_relation function of pagure.lib.link with relates.
Pierre-Yves Chibon 35fa85
        """
Pierre-Yves Chibon 35fa85
Pierre-Yves Chibon 5abfd2
        link = pagure.lib.link.get_relation(
Pierre-Yves Chibon 5abfd2
            self.session,
Pierre-Yves Chibon 5abfd2
            reponame='test',
Pierre-Yves Chibon 5abfd2
            namespace=None,
Pierre-Yves Chibon 5abfd2
            username=None,
Pierre-Yves Chibon 5abfd2
            text=COMMENTS[0],
Pierre-Yves Chibon 5abfd2
            reftype='relates',
Pierre-Yves Chibon 35fa85
        )
Pierre-Yves Chibon 5abfd2
        self.assertEqual(link, [])
Pierre-Yves Chibon 35fa85
Pierre-Yves Chibon 35fa85
        tests.create_projects(self.session)
Pierre-Yves Chibon 35fa85
Pierre-Yves Chibon fe5017
        link = pagure.lib.link.get_relation(
Pierre-Yves Chibon 5abfd2
            self.session,
Pierre-Yves Chibon 5abfd2
            reponame='test',
Pierre-Yves Chibon 5abfd2
            namespace=None,
Pierre-Yves Chibon 5abfd2
            username=None,
Pierre-Yves Chibon 5abfd2
            text=COMMENTS[4],
Pierre-Yves Chibon 5abfd2
            reftype='relates',
Pierre-Yves Chibon 5abfd2
        )
Pierre-Yves Chibon 35fa85
        self.assertEqual(link, [])
Pierre-Yves Chibon 35fa85
Pierre-Yves Chibon 35fa85
        # Create the issue
Pierre-Yves Chibon b130e5
        repo = pagure.lib.get_authorized_project(self.session, 'test')
Pierre-Yves Chibon fe5017
        pagure.lib.new_issue(
Pierre-Yves Chibon 35fa85
            self.session,
Pierre-Yves Chibon 35fa85
            repo,
Pierre-Yves Chibon 35fa85
            title='foo',
Pierre-Yves Chibon 35fa85
            content='bar',
Pierre-Yves Chibon 35fa85
            user='pingou',
Pierre-Yves Chibon 35fa85
            ticketfolder=None,
Pierre-Yves Chibon 35fa85
            issue_id=5,
Pierre-Yves Chibon 35fa85
            notify=False)
Pierre-Yves Chibon 35fa85
        self.session.commit()
Pierre-Yves Chibon 35fa85
Pierre-Yves Chibon 35fa85
        for idx, comment in enumerate(COMMENTS):
Pierre-Yves Chibon fe5017
            link = pagure.lib.link.get_relation(
Pierre-Yves Chibon 5abfd2
                self.session,
Pierre-Yves Chibon 5abfd2
                reponame='test',
Pierre-Yves Chibon 5abfd2
                namespace=None,
Pierre-Yves Chibon 5abfd2
                username=None,
Pierre-Yves Chibon 5abfd2
                text=comment,
Pierre-Yves Chibon 5abfd2
                reftype='relates')
Pierre-Yves Chibon 35fa85
            if idx == 4:
Pierre-Yves Chibon 35fa85
                self.assertEqual(
Pierre-Yves Chibon 35fa85
                    str(link),
Pierre-Yves Chibon 35fa85
                    '[Issue(5, project:test, user:pingou, title:foo)]')
Pierre-Yves Chibon 35fa85
            else:
Pierre-Yves Chibon 35fa85
                self.assertEqual(link, [])
Pierre-Yves Chibon 35fa85
Pierre-Yves Chibon fe5017
        link = pagure.lib.link.get_relation(
Pierre-Yves Chibon 5abfd2
            self.session,
Pierre-Yves Chibon 5abfd2
            reponame='test',
Pierre-Yves Chibon 5abfd2
            namespace=None,
Pierre-Yves Chibon 5abfd2
            username=None,
Pierre-Yves Chibon 5abfd2
            text=COMMENTS[5],
Pierre-Yves Chibon 5abfd2
            reftype='relates',
Pierre-Yves Chibon 5abfd2
        )
Pierre-Yves Chibon 35fa85
        self.assertEqual(link, [])
Pierre-Yves Chibon 35fa85
Pierre-Yves Chibon 35fa85
        # Create the issue
Pierre-Yves Chibon b130e5
        repo = pagure.lib.get_authorized_project(self.session, 'test')
Pierre-Yves Chibon fe5017
        pagure.lib.new_issue(
Pierre-Yves Chibon 35fa85
            self.session,
Pierre-Yves Chibon 35fa85
            repo,
Pierre-Yves Chibon 35fa85
            title='another foo',
Pierre-Yves Chibon 35fa85
            content='another bar',
Pierre-Yves Chibon 35fa85
            user='pingou',
Pierre-Yves Chibon 35fa85
            ticketfolder=None,
Pierre-Yves Chibon 35fa85
            issue_id=6,
Pierre-Yves Chibon 35fa85
            notify=False)
Pierre-Yves Chibon 35fa85
        self.session.commit()
Pierre-Yves Chibon 35fa85
Pierre-Yves Chibon 35fa85
        for idx, comment in enumerate(COMMENTS):
Pierre-Yves Chibon fe5017
            link = pagure.lib.link.get_relation(
Pierre-Yves Chibon 5abfd2
                self.session,
Pierre-Yves Chibon 5abfd2
                reponame='test',
Pierre-Yves Chibon 5abfd2
                namespace=None,
Pierre-Yves Chibon 5abfd2
                username=None,
Pierre-Yves Chibon 5abfd2
                text=comment,
Pierre-Yves Chibon 5abfd2
                reftype='relates')
Pierre-Yves Chibon 35fa85
            if idx == 4:
Pierre-Yves Chibon 35fa85
                self.assertEqual(
Pierre-Yves Chibon 35fa85
                    str(link),
Pierre-Yves Chibon 35fa85
                    '[Issue(5, project:test, user:pingou, title:foo)]')
Pierre-Yves Chibon 35fa85
            elif idx == 5:
Pierre-Yves Chibon 35fa85
                self.assertEqual(
Pierre-Yves Chibon 35fa85
                    str(link),
Pierre-Yves Chibon 35fa85
                    '[Issue(6, project:test, user:pingou, title:another foo)]')
Pierre-Yves Chibon 35fa85
            else:
Pierre-Yves Chibon 35fa85
                self.assertEqual(link, [])
Pierre-Yves Chibon 35fa85
Pierre-Yves Chibon 35fa85
    def test_get_relation_fixes(self):
Pierre-Yves Chibon fe5017
        """ Test the get_relation function of pagure.lib.link with fixes.
Pierre-Yves Chibon 35fa85
        """
Pierre-Yves Chibon 35fa85
Pierre-Yves Chibon 5abfd2
        link = pagure.lib.link.get_relation(
Pierre-Yves Chibon 5abfd2
            self.session,
Pierre-Yves Chibon 5abfd2
            reponame='test',
Pierre-Yves Chibon 5abfd2
            namespace=None,
Pierre-Yves Chibon 5abfd2
            username=None,
Pierre-Yves Chibon 5abfd2
            text=COMMENTS[0],
Pierre-Yves Chibon 5abfd2
            reftype='fixes',
Pierre-Yves Chibon 35fa85
        )
Pierre-Yves Chibon 5abfd2
        self.assertEqual(link, [])
Pierre-Yves Chibon 35fa85
Pierre-Yves Chibon 35fa85
        tests.create_projects(self.session)
Pierre-Yves Chibon 35fa85
Pierre-Yves Chibon fe5017
        link = pagure.lib.link.get_relation(
Pierre-Yves Chibon 5abfd2
            self.session,
Pierre-Yves Chibon 5abfd2
            reponame='test',
Pierre-Yves Chibon 5abfd2
            namespace=None,
Pierre-Yves Chibon 5abfd2
            username=None,
Pierre-Yves Chibon 5abfd2
            text=COMMENTS[2],
Pierre-Yves Chibon 5abfd2
            reftype='fixes',
Pierre-Yves Chibon 5abfd2
        )
Pierre-Yves Chibon 35fa85
        self.assertEqual(link, [])
Pierre-Yves Chibon 35fa85
Pierre-Yves Chibon 35fa85
        # Create the issue
Pierre-Yves Chibon b130e5
        repo = pagure.lib.get_authorized_project(self.session, 'test')
Pierre-Yves Chibon fe5017
        pagure.lib.new_issue(
Pierre-Yves Chibon 35fa85
            self.session,
Pierre-Yves Chibon 35fa85
            repo,
Pierre-Yves Chibon 35fa85
            title='issue 3',
Pierre-Yves Chibon 35fa85
            content='content issue 3',
Pierre-Yves Chibon 35fa85
            user='pingou',
Pierre-Yves Chibon 35fa85
            ticketfolder=None,
Pierre-Yves Chibon 35fa85
            issue_id=3,
Pierre-Yves Chibon 35fa85
            notify=False)
Pierre-Yves Chibon 35fa85
        self.session.commit()
Pierre-Yves Chibon 35fa85
Pierre-Yves Chibon 35fa85
        for idx, comment in enumerate(COMMENTS):
Pierre-Yves Chibon fe5017
            link = pagure.lib.link.get_relation(
Pierre-Yves Chibon 5abfd2
                self.session,
Pierre-Yves Chibon 5abfd2
                reponame='test',
Pierre-Yves Chibon 5abfd2
                namespace=None,
Pierre-Yves Chibon 5abfd2
                username=None,
Pierre-Yves Chibon 5abfd2
                text=comment,
Pierre-Yves Chibon 5abfd2
                reftype='fixes')
Pierre-Yves Chibon 35fa85
            if idx == 2:
Pierre-Yves Chibon 35fa85
                self.assertEqual(
Pierre-Yves Chibon 35fa85
                    str(link),
Pierre-Yves Chibon 35fa85
                    '[Issue(3, project:test, user:pingou, title:issue 3)]')
Pierre-Yves Chibon 35fa85
            else:
Pierre-Yves Chibon 35fa85
                self.assertEqual(link, [])
Pierre-Yves Chibon 35fa85
Pierre-Yves Chibon 29b095
    def test_relates_regex(self):
Pierre-Yves Chibon fe5017
        ''' Test the relates regex present in pagure.lib.link. '''
Pierre-Yves Chibon 604159
        text = 'relates  to   http://localhost/fork/pingou/test/issue/1'
Pierre-Yves Chibon fe5017
        for index, regex in enumerate(pagure.lib.link.RELATES):
Pierre-Yves Chibon 604159
            if index == 2:
Pierre-Yves Chibon 604159
                self.assertNotEqual(regex.match(text), None)
Pierre-Yves Chibon 604159
            else:
Pierre-Yves Chibon 604159
                self.assertEqual(regex.match(text), None)
Pierre-Yves Chibon 604159
Pierre-Yves Chibon b940a6
        text = 'relates http://209.132.184.222/fork/pingou/test/issue/1'
Pierre-Yves Chibon fe5017
        for index, regex in enumerate(pagure.lib.link.RELATES):
Pierre-Yves Chibon b940a6
            if index == 2:
Pierre-Yves Chibon b940a6
                self.assertNotEqual(regex.match(text), None)
Pierre-Yves Chibon b940a6
            else:
Pierre-Yves Chibon b940a6
                self.assertEqual(regex.match(text), None)
Pierre-Yves Chibon b940a6
Pierre-Yves Chibon 604159
        text = 'This relates  to  #5'
Pierre-Yves Chibon fe5017
        for index, regex in enumerate(pagure.lib.link.RELATES):
Pierre-Yves Chibon 604159
            if index == 0:
Pierre-Yves Chibon 604159
                self.assertNotEqual(regex.match(text), None)
Pierre-Yves Chibon 604159
            else:
Pierre-Yves Chibon 604159
                self.assertEqual(regex.match(text), None)
Pierre-Yves Chibon 604159
Pierre-Yves Chibon 604159
        text = 'Could this be related to  '\
Pierre-Yves Chibon fe5017
            ' https://fedorahosted.org/pagure/tests2/issue/6'
Pierre-Yves Chibon fe5017
        for index, regex in enumerate(pagure.lib.link.RELATES):
Pierre-Yves Chibon 604159
            if index == 2:
Pierre-Yves Chibon 604159
                self.assertNotEqual(regex.match(text), None)
Pierre-Yves Chibon 604159
            else:
Pierre-Yves Chibon 604159
                self.assertEqual(regex.match(text), None)
Pierre-Yves Chibon 35fa85
Pierre-Yves Chibon b3a3f6
    def test_fixes_regex(self):
Pierre-Yves Chibon fe5017
        ''' Test the fixes regex present in pagure.lib.link. '''
Pierre-Yves Chibon b3a3f6
Mike McLean fa0f93
        # project/issue matches
Mike McLean fa0f93
        def project_match(text, groups):
Mike McLean fa0f93
            match = None
Mike McLean fa0f93
            for regex in pagure.lib.link.FIXES:
Mike McLean fa0f93
                match = regex.match(text)
Mike McLean fa0f93
                if match:
Mike McLean fa0f93
                    break
Mike McLean fa0f93
            self.assertNotEqual(match, None)
Mike McLean fa0f93
            self.assertEqual(len(match.groups()), 2)
Mike McLean fa0f93
            self.assertEqual(match.groups(), groups)
Mike McLean fa0f93
Mike McLean fa0f93
        data = [
Mike McLean fa0f93
            # [string, groups]
Mike McLean fa0f93
        ]
Mike McLean fa0f93
Mike McLean fa0f93
        project_match('fixes     http://localhost/fork/pingou/test/issue/1',
Mike McLean fa0f93
            ('test', '1'))
Mike McLean fa0f93
        project_match('fix http://209.132.184.222/fork/pingou/test/issue/1',
Mike McLean fa0f93
            ('test', '1'))
Mike McLean fa0f93
        project_match('Could this be fixes  '
Mike McLean fa0f93
            ' https://fedorahosted.org/pagure/tests2/issue/6',
Mike McLean fa0f93
            ('tests2', '6'))
Mike McLean fa0f93
        project_match('merged https://pagure.io/myproject/pull-request/70',
Mike McLean fa0f93
            ('myproject', '70'))
Mike McLean fa0f93
        project_match('Now we merge https://pagure.io/myproject/pull-request/99',
Mike McLean fa0f93
            ('myproject', '99'))
Pierre-Yves Chibon 7369ac
        project_match('Merges     http://localhost/fork/pingou/test/issue/1',
Pierre-Yves Chibon 7369ac
            ('test', '1'))
Pierre-Yves Chibon 7369ac
        project_match('Merges: http://localhost/fork/pingou/test/issue/12',
Pierre-Yves Chibon 7369ac
            ('test', '12'))
Pierre-Yves Chibon 7369ac
        project_match('Merged http://localhost/fork/pingou/test/issue/123#',
Pierre-Yves Chibon 7369ac
            ('test', '123'))
Pierre-Yves Chibon 7369ac
        project_match('Merge: http://localhost/fork/pingou/test/issue/1234#foo',
Pierre-Yves Chibon 7369ac
            ('test', '1234'))
Mike McLean fa0f93
Mike McLean fa0f93
        # issue matches
Mike McLean fa0f93
        def issue_match(text, issue):
Mike McLean fa0f93
            match = None
Mike McLean fa0f93
            for regex in pagure.lib.link.FIXES:
Mike McLean fa0f93
                match = regex.match(text)
Mike McLean fa0f93
                if match:
Mike McLean fa0f93
                    break
Mike McLean fa0f93
            self.assertNotEqual(match, None)
Mike McLean fa0f93
            self.assertEqual(len(match.groups()), 1)
Mike McLean fa0f93
            self.assertEqual(match.group(1), issue)
Mike McLean fa0f93
Mike McLean fa0f93
        issue_match('This fixed  #5', '5')
Mike McLean fa0f93
        issue_match('Merged  #17', '17')
Mike McLean fa0f93
        issue_match('Fixed:  #23', '23')
Mike McLean fa0f93
        issue_match('This commit fixes:  #42', '42')
Mike McLean fa0f93
        issue_match('Merge #137', '137')
Pierre-Yves Chibon f9fd15
        issue_match('Merges #137', '137')
Pierre-Yves Chibon f9fd15
        issue_match('Merges: #137', '137')
Mike McLean fa0f93
Mike McLean fa0f93
        # no match
Mike McLean fa0f93
        def no_match(text):
Mike McLean fa0f93
            match = None
Mike McLean fa0f93
            for regex in pagure.lib.link.FIXES:
Mike McLean fa0f93
                match = regex.match(text)
Mike McLean fa0f93
                if match:
Mike McLean fa0f93
                    break
Mike McLean fa0f93
            self.assertEqual(match, None)
Mike McLean fa0f93
Mike McLean fa0f93
        no_match('nowhitespacemerge: #47')
Mike McLean fa0f93
        no_match('This commit unmerges #45')
Mike McLean fa0f93
        no_match('Fixed 45 typos')
Mike McLean fa0f93
        no_match('Fixed 4 typos')
Mike McLean fa0f93
        no_match("Merge branch 'work'")
Pierre-Yves Chibon b3a3f6
Pierre-Yves Chibon 35fa85
Pierre-Yves Chibon 35fa85
if __name__ == '__main__':
Pierre-Yves Chibon 393f31
    unittest.main(verbosity=2)