Blame tests/test_progit_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
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 35fa85
        self.assertEqual(
Pierre-Yves Chibon fe5017
            pagure.lib.link.get_relation(
Pierre-Yves Chibon 35fa85
                self.session,
Pierre-Yves Chibon 35fa85
                'test',
Pierre-Yves Chibon 35fa85
                None,
Pierre-Yves Chibon 35fa85
                COMMENTS[0],
Pierre-Yves Chibon 35fa85
                'relates',
Pierre-Yves Chibon 35fa85
            ),
Pierre-Yves Chibon 35fa85
            []
Pierre-Yves Chibon 35fa85
        )
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 35fa85
            self.session, 'test', None, COMMENTS[4], 'relates')
Pierre-Yves Chibon 35fa85
        self.assertEqual(link, [])
Pierre-Yves Chibon 35fa85
Pierre-Yves Chibon 35fa85
        # Create the issue
Pierre-Yves Chibon fe5017
        repo = pagure.lib.get_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 35fa85
                self.session, 'test', None, comment, '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 35fa85
            self.session, 'test', None, COMMENTS[5], 'relates')
Pierre-Yves Chibon 35fa85
        self.assertEqual(link, [])
Pierre-Yves Chibon 35fa85
Pierre-Yves Chibon 35fa85
        # Create the issue
Pierre-Yves Chibon fe5017
        repo = pagure.lib.get_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 35fa85
                self.session, 'test', None, comment, '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 35fa85
        self.assertEqual(
Pierre-Yves Chibon fe5017
            pagure.lib.link.get_relation(
Pierre-Yves Chibon 35fa85
                self.session,
Pierre-Yves Chibon 35fa85
                'test',
Pierre-Yves Chibon 35fa85
                None,
Pierre-Yves Chibon 35fa85
                COMMENTS[0],
Pierre-Yves Chibon 35fa85
                'fixes',
Pierre-Yves Chibon 35fa85
            ),
Pierre-Yves Chibon 35fa85
            []
Pierre-Yves Chibon 35fa85
        )
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 35fa85
            self.session, 'test', None, COMMENTS[2], 'fixes')
Pierre-Yves Chibon 35fa85
        self.assertEqual(link, [])
Pierre-Yves Chibon 35fa85
Pierre-Yves Chibon 35fa85
        # Create the issue
Pierre-Yves Chibon fe5017
        repo = pagure.lib.get_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 35fa85
                self.session, 'test', None, comment, '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
        text = 'fixes     http://localhost/fork/pingou/test/issue/1'
Pierre-Yves Chibon fe5017
        for index, regex in enumerate(pagure.lib.link.FIXES):
Pierre-Yves Chibon b3a3f6
            if index in [2, 3]:
Pierre-Yves Chibon b3a3f6
                self.assertNotEqual(regex.match(text), None)
Pierre-Yves Chibon b3a3f6
            else:
Pierre-Yves Chibon b3a3f6
                self.assertEqual(regex.match(text), None)
Pierre-Yves Chibon b3a3f6
Pierre-Yves Chibon b3a3f6
        text = 'fix http://209.132.184.222/fork/pingou/test/issue/1'
Pierre-Yves Chibon fe5017
        for index, regex in enumerate(pagure.lib.link.FIXES):
Pierre-Yves Chibon b3a3f6
            if index in [2, 3]:
Pierre-Yves Chibon b3a3f6
                self.assertNotEqual(regex.match(text), None)
Pierre-Yves Chibon b3a3f6
            else:
Pierre-Yves Chibon b3a3f6
                self.assertEqual(regex.match(text), None)
Pierre-Yves Chibon b3a3f6
Pierre-Yves Chibon b3a3f6
        text = 'This fixed  #5'
Pierre-Yves Chibon fe5017
        for index, regex in enumerate(pagure.lib.link.FIXES):
Pierre-Yves Chibon b3a3f6
            if index == 1:
Pierre-Yves Chibon b3a3f6
                self.assertNotEqual(regex.match(text), None)
Pierre-Yves Chibon b3a3f6
            else:
Pierre-Yves Chibon b3a3f6
                self.assertEqual(regex.match(text), None)
Pierre-Yves Chibon b3a3f6
Pierre-Yves Chibon b3a3f6
        text = 'Could this be fixes  '\
Pierre-Yves Chibon fe5017
            ' https://fedorahosted.org/pagure/tests2/issue/6'
Pierre-Yves Chibon fe5017
        for index, regex in enumerate(pagure.lib.link.FIXES):
Pierre-Yves Chibon b3a3f6
            if index == 3:
Pierre-Yves Chibon b3a3f6
                self.assertNotEqual(regex.match(text), None)
Pierre-Yves Chibon b3a3f6
            else:
Pierre-Yves Chibon b3a3f6
                self.assertEqual(regex.match(text), None)
Pierre-Yves Chibon b3a3f6
Pierre-Yves Chibon 35fa85
Pierre-Yves Chibon 35fa85
if __name__ == '__main__':
Pierre-Yves Chibon fe5017
    SUITE = unittest.TestLoader().loadTestsFromTestCase(PagureLibLinktests)
Pierre-Yves Chibon 35fa85
    unittest.TextTestRunner(verbosity=2).run(SUITE)