def test_branch_helper_local_branch_does_not_exist_anywhere(self): mock_repo = mock.MagicMock() # Our mock repo should act like this branch does not exist anywhere mock_repo.branches = object() # Will not have an attribute named 'branch_name' origin = mock_repo.remotes.origin class just_has_parent: pass origin.refs = ( just_has_parent() ) # Will not have an attribute named 'branch_name' mock_parent_branch = mock.MagicMock() setattr(origin.refs, settings.OFFICIAL_GIT_BRANCH, mock_parent_branch) result = branch_status_helper(mock_repo, self.translation_branch) mock_repo.iter_commits.return_value = [] self.assertEqual( { "branch": self.translation_branch, "commits": [], "last_commit": None, "official_git_branch": settings.OFFICIAL_GIT_BRANCH, }, result, ) mock_repo.iter_commits.assert_called_with( f"origin/{self.translation_branch.branch_name}", max_count=4 )
def test_branch_helper_branch_only_upstream(self): branch_name = self.translation_branch.branch_name mock_repo = mock.MagicMock() # Our mock repo should act like this branch does not exist here mock_repo.branches = object() # Will not have an attribute named 'branch_name' # But it does exist upstream origin = mock_repo.remotes.origin class has_branch: pass origin.refs = has_branch() mock_upstream_branch = mock.MagicMock() setattr(origin.refs, branch_name, mock_upstream_branch) result = branch_status_helper(mock_repo, self.translation_branch) mock_repo.iter_commits.return_value = [] self.assertEqual( { "branch": self.translation_branch, "commits": [], "last_commit": None, "official_git_branch": settings.OFFICIAL_GIT_BRANCH, }, result, ) mock_repo.iter_commits.assert_called_with(f"origin/{branch_name}", max_count=4)
def test_branch_helper_local_branch_exists(self): mock_repo = mock.MagicMock() mock_commit = mock.MagicMock( hexsha="0123456789abcdef", message="A message", committed_datetime=timezone.now(), committer="John Q. Committer", ) mock_commits = [ mock_commit, mock_commit, mock_commit, mock_commit, ] mock_repo.iter_commits.return_value = mock_commits # Something like this will be returned for each commit # Most will have a "previous" added, though. massaged_commit = { "committed_datetime": mock_commit.committed_datetime, "committer": "John Q. Committer", "hexsha": "0123456789abcdef", "message": "A message", "shorthash": "0123456", } expected_commits = [ dict(massaged_commit) for i in range(min(NUM_COMMITS + 1, len(mock_commits))) ] for i, commit in enumerate(expected_commits): if (i + 1) < len(expected_commits): commit["previous"] = expected_commits[i + 1] expected_commits = expected_commits[:NUM_COMMITS] last_commit = expected_commits[0] result = branch_status_helper(mock_repo, self.translation_branch) self.assertEqual( { "branch": self.translation_branch, "commits": expected_commits, "last_commit": last_commit, "official_git_branch": settings.OFFICIAL_GIT_BRANCH, }, result, ) mock_repo.iter_commits.assert_called_with( f"origin/{self.translation_branch.branch_name}", max_count=4 )