Blame tests/test_tasks.py

Slavek Kabrda 5ab53d
from mock import patch, MagicMock, Mock
Lubomír Sedlář d6299f
from collections import namedtuple
Lubomír Sedlář d6299f
import os
Lubomír Sedlář d6299f
import unittest
Lubomír Sedlář d6299f
Lubomír Sedlář d6299f
from pagure.lib import tasks
Lubomír Sedlář d6299f
Lubomír Sedlář d6299f
Lubomír Sedlář d6299f
MockUser = namedtuple('MockUser', ['fullname', 'default_email'])
Lubomír Sedlář d6299f
Lubomír Sedlář d6299f
Lubomír Sedlář d6299f
class MockCommit(object):
Lubomír Sedlář d6299f
    def __init__(self, name, email, time='1970-01-01 00:00'):
Lubomír Sedlář d6299f
        self.author = Mock(email=email)
Lubomír Sedlář d6299f
        self.author.name = name
Lubomír Sedlář d6299f
        self.commit_time = time
Lubomír Sedlář d6299f
Lubomír Sedlář d6299f
Lubomír Sedlář d6299f
@patch('pagure.lib.create_session', new=Mock())
Lubomír Sedlář d6299f
class TestCommitsAuthorStats(unittest.TestCase):
Lubomír Sedlář d6299f
Lubomír Sedlář d6299f
    def setUp(self):
Lubomír Sedlář d6299f
        self.search_user_patcher = patch('pagure.lib.search_user')
Lubomír Sedlář d6299f
        mock_search_user = self.search_user_patcher.start()
Lubomír Sedlář d6299f
        mock_search_user.side_effect = lambda _, email: self.authors.get(email)
Lubomír Sedlář d6299f
Lubomír Sedlář d6299f
        self.pygit_patcher = patch('pygit2.Repository')
Lubomír Sedlář d6299f
        mock_repo = self.pygit_patcher.start().return_value
Lubomír Sedlář d6299f
Lubomír Sedlář d6299f
        def mock_walk_impl(*args, **kwargs):
Lubomír Sedlář d6299f
            for commit in self.commits:
Lubomír Sedlář d6299f
                yield commit
Lubomír Sedlář d6299f
Lubomír Sedlář d6299f
        mock_repo.walk.side_effect = mock_walk_impl
Lubomír Sedlář d6299f
Lubomír Sedlář d6299f
        self.repopath = Mock()
Lubomír Sedlář d6299f
        exists = os.path.exists
Lubomír Sedlář d6299f
Lubomír Sedlář d6299f
        def mock_exists_impl(path):
Lubomír Sedlář d6299f
            if path == self.repopath:
Lubomír Sedlář d6299f
                return True
Lubomír Sedlář d6299f
            return exists(path)
Lubomír Sedlář d6299f
Lubomír Sedlář d6299f
        self.exists_patcher = patch('os.path.exists')
Lubomír Sedlář d6299f
        mock_exists = self.exists_patcher.start()
Lubomír Sedlář d6299f
        mock_exists.side_effect = mock_exists_impl
Lubomír Sedlář d6299f
Lubomír Sedlář d6299f
    def tearDown(self):
Lubomír Sedlář d6299f
        self.search_user_patcher.stop()
Lubomír Sedlář d6299f
        self.pygit_patcher.stop()
Lubomír Sedlář d6299f
        self.exists_patcher.stop()
Lubomír Sedlář d6299f
Lubomír Sedlář d6299f
    def test_no_change(self):
Lubomír Sedlář d6299f
        self.commits = [
Lubomír Sedlář d6299f
            MockCommit('Alice', 'alice@example.com', '2018-01-01 00:00'),
Lubomír Sedlář d6299f
        ]
Lubomír Sedlář d6299f
        self.authors = {
Lubomír Sedlář d6299f
            'alice@example.com': MockUser('Alice', 'alice@example.com'),
Lubomír Sedlář d6299f
        }
Lubomír Sedlář d6299f
Lubomír Sedlář d6299f
        num_commits, authors, num_authors, last_time = \
Lubomír Sedlář d6299f
            tasks.commits_author_stats(self.repopath)
Lubomír Sedlář d6299f
Lubomír Sedlář d6299f
        self.assertEqual(num_commits, 1)
Lubomír Sedlář d6299f
        self.assertEqual(num_authors, 1)
Lubomír Sedlář d6299f
        self.assertEqual(last_time, '2018-01-01 00:00')
Lubomír Sedlář d6299f
        self.assertEqual(authors, [(1, [('Alice', 'alice@example.com')])])
Lubomír Sedlář d6299f
Lubomír Sedlář d6299f
    def test_rename_user_and_merge(self):
Lubomír Sedlář d6299f
        self.commits = [
Lubomír Sedlář d6299f
            MockCommit('Alice', 'alice@example.com'),
Lubomír Sedlář d6299f
            MockCommit('Bad name', 'alice@example.com', '2018-01-01 00:00'),
Lubomír Sedlář d6299f
        ]
Lubomír Sedlář d6299f
        self.authors = {
Lubomír Sedlář d6299f
            'alice@example.com': MockUser('Alice', 'alice@example.com'),
Lubomír Sedlář d6299f
        }
Lubomír Sedlář d6299f
Lubomír Sedlář d6299f
        num_commits, authors, num_authors, last_time = \
Lubomír Sedlář d6299f
            tasks.commits_author_stats(self.repopath)
Lubomír Sedlář d6299f
Lubomír Sedlář d6299f
        self.assertEqual(num_commits, 2)
Lubomír Sedlář d6299f
        self.assertEqual(num_authors, 1)
Lubomír Sedlář d6299f
        self.assertEqual(last_time, '2018-01-01 00:00')
Lubomír Sedlář d6299f
        self.assertEqual(authors, [(2, [('Alice', 'alice@example.com')])])
Lubomír Sedlář d6299f
Lubomír Sedlář d6299f
    def test_preserve_unknown_author(self):
Lubomír Sedlář d6299f
        self.commits = [
Lubomír Sedlář d6299f
            MockCommit('Alice', 'alice@example.com', '2018-01-01 00:00'),
Lubomír Sedlář d6299f
        ]
Lubomír Sedlář d6299f
        self.authors = {}
Lubomír Sedlář d6299f
Lubomír Sedlář d6299f
        num_commits, authors, num_authors, last_time = \
Lubomír Sedlář d6299f
            tasks.commits_author_stats(self.repopath)
Lubomír Sedlář d6299f
Lubomír Sedlář d6299f
        self.assertEqual(num_commits, 1)
Lubomír Sedlář d6299f
        self.assertEqual(num_authors, 1)
Lubomír Sedlář d6299f
        self.assertEqual(last_time, '2018-01-01 00:00')
Lubomír Sedlář d6299f
        self.assertEqual(authors, [(1, [('Alice', 'alice@example.com')])])
Lubomír Sedlář d6299f
Lubomír Sedlář d6299f
    def test_handle_empty_email(self):
Lubomír Sedlář d6299f
        self.commits = [
Lubomír Sedlář d6299f
            # Two commits for Alice to ensure order of the result.
Lubomír Sedlář d6299f
            MockCommit('Alice', None),
Lubomír Sedlář d6299f
            MockCommit('Alice', None),
Lubomír Sedlář d6299f
            MockCommit('Bob', '', '2018-01-01 00:00'),
Lubomír Sedlář d6299f
        ]
Lubomír Sedlář d6299f
        self.authors = {}
Lubomír Sedlář d6299f
Lubomír Sedlář d6299f
        num_commits, authors, num_authors, last_time = \
Lubomír Sedlář d6299f
            tasks.commits_author_stats(self.repopath)
Lubomír Sedlář d6299f
Lubomír Sedlář d6299f
        self.assertEqual(num_commits, 3)
Lubomír Sedlář d6299f
        self.assertEqual(num_authors, 2)
Lubomír Sedlář d6299f
        self.assertEqual(last_time, '2018-01-01 00:00')
Lubomír Sedlář d6299f
        self.assertEqual(authors, [(2, [('Alice', None)]),
Lubomír Sedlář d6299f
                                   (1, [('Bob', '')])])
Slavek Kabrda 5ab53d
Slavek Kabrda 5ab53d
Slavek Kabrda 5ab53d
class TestGitolitePostCompileOnly(object):
Slavek Kabrda 5ab53d
    @patch('pagure.lib.git_auth.get_git_auth_helper')
Slavek Kabrda 5ab53d
    def test_backend_has_post_compile_only(self, get_helper):
Slavek Kabrda 5ab53d
        helper = MagicMock()
Slavek Kabrda 5ab53d
        get_helper.return_value = helper
Slavek Kabrda 5ab53d
        helper.post_compile_only = MagicMock()
Slavek Kabrda 5ab53d
        tasks.gitolite_post_compile_only()
Slavek Kabrda 5ab53d
        helper.post_compile_only.assert_called_once()
Slavek Kabrda 5ab53d
Slavek Kabrda 5ab53d
    @patch('pagure.lib.git_auth.get_git_auth_helper')
Slavek Kabrda 5ab53d
    def test_backend_doesnt_have_post_compile_only(self, get_helper):
Slavek Kabrda 5ab53d
        helper = MagicMock()
Slavek Kabrda 5ab53d
        get_helper.return_value = helper
Slavek Kabrda 5ab53d
        helper.generate_acls = MagicMock()
Slavek Kabrda 5ab53d
        del helper.post_compile_only
Slavek Kabrda 5ab53d
        tasks.gitolite_post_compile_only()
Slavek Kabrda 5ab53d
        helper.generate_acls.assert_called_once_with(project=None)