Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# -*- coding: utf-8 -*-
#
# This file is part of the LOFAR 2.0 Station Software
#
#
#
# Distributed under the terms of the APACHE license.
# See LICENSE.txt for more info.
import git
from unittest import mock
from util import lofar_git
from test import base
class TestLofarGit(base.TestCase):
def setUp(self):
super(TestLofarGit, self).setUp()
# Clear the cache as this function of lofar_git uses LRU decorator
# This is a good demonstration of how unit tests in Python can have
# permanent effects, typically fixtures are needed to restore these.
lofar_git.get_version.cache_clear()
def test_get_version(self):
"""Test if attributes of get_repo are correctly used by get_version"""
with mock.patch.object(lofar_git, 'get_repo') as m_get_repo:
m_commit = mock.Mock()
m_commit.return_value = "123456"
m_is_dirty = mock.Mock()
m_is_dirty.return_value = True
m_get_repo.return_value = mock.Mock(
active_branch="main", commit=m_commit, tags=[],
is_dirty=m_is_dirty)
# No need for special string equal in Python
self.assertEqual("*main [123456]", lofar_git.get_version())
def test_get_version_tag(self):
"""Test if get_version determines production_ready for tagged commit"""
with mock.patch.object(lofar_git, 'get_repo') as m_get_repo:
m_commit = mock.Mock()
m_commit.return_value = "123456"
m_is_dirty = mock.Mock()
m_is_dirty.return_value = False
m_tag = mock.Mock(commit="123456")
m_tag.__str__ = mock.Mock(return_value= "version-1.2")
m_get_repo.return_value = mock.Mock(
active_branch="main", commit=m_commit,
tags=[m_tag], is_dirty=m_is_dirty)
self.assertEqual("version-1.2", lofar_git.get_version())
@mock.patch.object(lofar_git, 'get_repo')
def test_get_version_tag_dirty(self, m_get_repo):
"""Test if get_version determines dirty tagged commit"""
m_commit = mock.Mock()
m_commit.return_value = "123456"
m_is_dirty = mock.Mock()
m_is_dirty.return_value = False
m_tag = mock.Mock(commit="123456")
m_tag.__str__ = mock.Mock(return_value= "version-1.2")
# Now m_get_repo is mocked using a decorator
m_get_repo.return_value = mock.Mock(
active_branch="main", commit=m_commit,
tags=[m_tag], is_dirty=m_is_dirty)
self.assertEqual("version-1.2", lofar_git.get_version())
def test_catch_repo_error(self):
"""Test if invalid git directories will raise error"""
with mock.patch.object(lofar_git, 'get_repo') as m_get_repo:
# Configure lofar_git.get_repo to raise InvalidGitRepositoryError
m_get_repo.side_effect = git.InvalidGitRepositoryError
# Test that error is raised by get_version
self.assertRaises(
git.InvalidGitRepositoryError, lofar_git.get_version)