test_tasks.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. from mock import patch, MagicMock, Mock
  2. from collections import namedtuple
  3. import os
  4. import unittest
  5. from pagure.lib import tasks
  6. MockUser = namedtuple('MockUser', ['fullname', 'default_email'])
  7. class MockCommit(object):
  8. def __init__(self, name, email, time='1970-01-01 00:00'):
  9. self.author = Mock(email=email)
  10. self.author.name = name
  11. self.commit_time = time
  12. @patch('pagure.lib.create_session', new=Mock())
  13. class TestCommitsAuthorStats(unittest.TestCase):
  14. def setUp(self):
  15. self.search_user_patcher = patch('pagure.lib.search_user')
  16. mock_search_user = self.search_user_patcher.start()
  17. mock_search_user.side_effect = lambda _, email: self.authors.get(email)
  18. self.pygit_patcher = patch('pygit2.Repository')
  19. mock_repo = self.pygit_patcher.start().return_value
  20. def mock_walk_impl(*args, **kwargs):
  21. for commit in self.commits:
  22. yield commit
  23. mock_repo.walk.side_effect = mock_walk_impl
  24. self.repopath = Mock()
  25. exists = os.path.exists
  26. def mock_exists_impl(path):
  27. if path == self.repopath:
  28. return True
  29. return exists(path)
  30. self.exists_patcher = patch('os.path.exists')
  31. mock_exists = self.exists_patcher.start()
  32. mock_exists.side_effect = mock_exists_impl
  33. def tearDown(self):
  34. self.search_user_patcher.stop()
  35. self.pygit_patcher.stop()
  36. self.exists_patcher.stop()
  37. def test_no_change(self):
  38. self.commits = [
  39. MockCommit('Alice', 'alice@example.com', '2018-01-01 00:00'),
  40. ]
  41. self.authors = {
  42. 'alice@example.com': MockUser('Alice', 'alice@example.com'),
  43. }
  44. num_commits, authors, num_authors, last_time = \
  45. tasks.commits_author_stats(self.repopath)
  46. self.assertEqual(num_commits, 1)
  47. self.assertEqual(num_authors, 1)
  48. self.assertEqual(last_time, '2018-01-01 00:00')
  49. self.assertEqual(authors, [(1, [('Alice', 'alice@example.com')])])
  50. def test_rename_user_and_merge(self):
  51. self.commits = [
  52. MockCommit('Alice', 'alice@example.com'),
  53. MockCommit('Bad name', 'alice@example.com', '2018-01-01 00:00'),
  54. ]
  55. self.authors = {
  56. 'alice@example.com': MockUser('Alice', 'alice@example.com'),
  57. }
  58. num_commits, authors, num_authors, last_time = \
  59. tasks.commits_author_stats(self.repopath)
  60. self.assertEqual(num_commits, 2)
  61. self.assertEqual(num_authors, 1)
  62. self.assertEqual(last_time, '2018-01-01 00:00')
  63. self.assertEqual(authors, [(2, [('Alice', 'alice@example.com')])])
  64. def test_preserve_unknown_author(self):
  65. self.commits = [
  66. MockCommit('Alice', 'alice@example.com', '2018-01-01 00:00'),
  67. ]
  68. self.authors = {}
  69. num_commits, authors, num_authors, last_time = \
  70. tasks.commits_author_stats(self.repopath)
  71. self.assertEqual(num_commits, 1)
  72. self.assertEqual(num_authors, 1)
  73. self.assertEqual(last_time, '2018-01-01 00:00')
  74. self.assertEqual(authors, [(1, [('Alice', 'alice@example.com')])])
  75. def test_handle_empty_email(self):
  76. self.commits = [
  77. # Two commits for Alice to ensure order of the result.
  78. MockCommit('Alice', None),
  79. MockCommit('Alice', None),
  80. MockCommit('Bob', '', '2018-01-01 00:00'),
  81. ]
  82. self.authors = {}
  83. num_commits, authors, num_authors, last_time = \
  84. tasks.commits_author_stats(self.repopath)
  85. self.assertEqual(num_commits, 3)
  86. self.assertEqual(num_authors, 2)
  87. self.assertEqual(last_time, '2018-01-01 00:00')
  88. self.assertEqual(authors, [(2, [('Alice', None)]),
  89. (1, [('Bob', '')])])
  90. class TestGitolitePostCompileOnly(object):
  91. @patch('pagure.lib.git_auth.get_git_auth_helper')
  92. def test_backend_has_post_compile_only(self, get_helper):
  93. helper = MagicMock()
  94. get_helper.return_value = helper
  95. helper.post_compile_only = MagicMock()
  96. tasks.gitolite_post_compile_only()
  97. helper.post_compile_only.assert_called_once()
  98. @patch('pagure.lib.git_auth.get_git_auth_helper')
  99. def test_backend_doesnt_have_post_compile_only(self, get_helper):
  100. helper = MagicMock()
  101. get_helper.return_value = helper
  102. helper.generate_acls = MagicMock()
  103. del helper.post_compile_only
  104. tasks.gitolite_post_compile_only()
  105. helper.generate_acls.assert_called_once_with(project=None)