Completed
Push — master ( 5e51a6...52387f )
by Charles
01:50
created

test_getters()   D

Complexity

Conditions 10

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 10
c 1
b 0
f 0
dl 0
loc 23
rs 4.0396

How to fix   Complexity   

Complexity

Complex classes like test_getters() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
# -*- coding: utf-8 -*-
2
3
import os
4
from datetime import datetime
5
6
import pytest
7
import pytz
8
from mock import patch
9
10
from git_app_version.githandler import GitHandler
11
from test_helpers import git_utils
12
13
14
@pytest.fixture()
15
def git_repo(tmpdir_factory):
16
    cwd = os.getcwd()
17
    new_cwd = tmpdir_factory.mktemp('git_repo')
18
    new_cwd_path = str(new_cwd)
19
    os.chdir(new_cwd_path)
20
    repo = git_utils.init(repo_dir=new_cwd_path)
21
    yield repo
22
    os.chdir(cwd)
23
24
25
@pytest.fixture()
26
def git_repo_remote(tmpdir_factory):
27
    cwd = os.getcwd()
28
    new_cwd = tmpdir_factory.mktemp('git_repo_remote')
29
    new_cwd_path = str(new_cwd)
30
    os.chdir(new_cwd_path)
31
32
    name = 'Paul Dupond'
33
    email = '[email protected]'
34
35
    author_tz = 'Europe/Paris'
36
    tz = pytz.timezone(author_tz)
37
38
    author_dt1 = datetime(2016, 12, 10, 0, 33, 33)
39
    author_dt2 = datetime(2016, 12, 12, 2, 33, 33)
40
    author_dt3 = datetime(2016, 12, 17, 6, 40, 21)
41
42
    repo = git_utils.init(repo_dir=new_cwd_path)
43
    git_utils.commit(repo, message='commit 1',
44
                                   author='{} <{}>'.format(name, email),
45
                                   date=tz.localize(author_dt1).isoformat())
46
    git_utils.commit(repo, message='commit 2',
47
                                   author='{} <{}>'.format(name, email),
48
                                   date=tz.localize(author_dt2).isoformat())
49
    git_utils.tag(repo, 'v0.1.2')
50
    git_utils.branch(repo, 'release', 'master')
51
    git_utils.commit(repo, message='commit 3',
52
                                   author='{} <{}>'.format(name, email),
53
                                   date=tz.localize(author_dt3).isoformat())
54
    git_utils.branch(repo, 'feature/my_feature', 'master')
55
56
    yield repo
57
    os.chdir(cwd)
58
59
60
@pytest.fixture()
61
def git_repo_local(tmpdir_factory, git_repo_remote):
62
    cwd = os.getcwd()
63
    new_cwd = tmpdir_factory.mktemp('git_repo_local')
64
    new_cwd_path = str(new_cwd)
65
    os.chdir(new_cwd_path)
66
    repo = git_utils.clone(git_repo_remote, new_cwd_path)
67
    yield repo
68
    os.chdir(cwd)
69
70
71
@pytest.fixture()
72
def handler(git_repo):
73
    return GitHandler(git_repo.working_dir)
74
75
76
@pytest.fixture()
77
def handler_local(git_repo_local):
78
    return GitHandler(git_repo_local.working_dir)
79
80
81
def test_not_git_repository(tmpdir):
82
    not_git_dir = tmpdir.mkdir('not_git')
83
84
    with pytest.raises(ValueError):
85
        GitHandler(str(not_git_dir))
86
87
88
@patch('git_app_version.helper.date.datetime')
89
@pytest.mark.parametrize("mock_dt_now,expected", [
90
    (datetime(2015, 12, 21, 11, 33, 45), datetime(2015, 12, 21, 11, 33, 45)),
91
])
92
def test_get_deploy_date(mock_dt, mock_dt_now, expected, handler):
93
    tz = pytz.utc
94
95
    expectedDate = tz.localize(expected)
96
    mock_dt.now.return_value = tz.localize(mock_dt_now)
97
98
    assert expectedDate == handler.get_deploy_date()
99
100
101
def test_getters(git_repo_local, handler_local):
102
    name = 'Paul Dupond'
103
    email = '[email protected]'
104
105
    author_dt = datetime(2016, 12, 17, 6, 40, 21)
106
    author_tz = 'Europe/Paris'
107
    tz = pytz.timezone(author_tz)
108
    author_dt_tz = tz.localize(author_dt)
109
110
    commit = git_repo_local.commit('HEAD')
111
112
    assert handler_local.get_full_commit() == commit.hexsha
113
    assert handler_local.get_abbrev_commit() == commit.hexsha[0:7]
114
    assert handler_local.get_version() == 'v0.1.2-1-g' + commit.hexsha[0:7]
115
116
    assert handler_local.get_author_name() == name
117
    assert handler_local.get_author_email() == email
118
119
    assert handler_local.get_committer_name() == 'User Test'
120
    assert handler_local.get_committer_email() == '[email protected]'
121
122
    assert handler_local.get_author_date() == author_dt_tz
123
    assert handler_local.get_commit_date() == author_dt_tz
124
125
126
def test_get_version_no_commit(handler):
127
    default = '8fa82b6'
128
    assert handler.get_version() == ''
129
    assert handler.get_version(default=default) == default
130
131
132
def test_get_version_no_tags(git_repo, handler):
133
    default = '8fa82b6'
134
    commit = git_utils.commit(repo=git_repo, message='test 1')
135
136
    assert handler.get_version(default=default) == commit.hexsha[0:7]
137
138
139
def test_get_version_on_tag(git_repo, handler):
140
    default = '8fa82b6'
141
    git_utils.commit(repo=git_repo, message='test 1')
142
    git_utils.tag(repo=git_repo, version='v0.1.3')
143
144
    assert handler.get_version(default=default) == 'v0.1.3'
145
146
147
def test_get_version_after_tag(git_repo, handler):
148
    default = '8fa82b6'
149
    git_utils.commit(repo=git_repo, message='test 1')
150
    git_utils.tag(repo=git_repo, version='v0.1.3')
151
    commit = git_utils.commit(repo=git_repo, message='test 2')
152
153
    assert handler.get_version(
154
        default=default) == 'v0.1.3-1-g' + commit.hexsha[0:7]
155
156
157
def test_get_branches(git_repo_local, handler_local):
158
    expected = ['origin/feature/my_feature', 'origin/master']
159
    assert handler_local.get_branches() == expected
160
161
162
def test_get_top_branches(git_repo_local, handler_local):
163
    branches = ['origin/feature/my_feature', 'origin/release', 'origin/master']
164
    expected = ['origin/feature/my_feature', 'origin/master']
165
    abbrev_commit = git_repo_local.commit('HEAD').hexsha[0:7]
166
167
    assert handler_local.get_top_branches(
168
        branches=branches,
169
        abbrev_commit=abbrev_commit) == expected
170
171
172
@pytest.mark.parametrize("branches,expected", [
173
    (['origin/master', 'origin/feature/my_feature'],
174
     ['master', 'feature/my_feature']),
175
])
176
def test_remove_remote_prefix(branches, expected, handler):
177
    assert handler.remove_remote_prefix(branches=branches) == expected
178
179
180
@patch('git_app_version.helper.date.datetime')
181
def test_get_infos(mock_dt, git_repo_local, handler_local):
182
    commit = git_repo_local.commit('HEAD')
183
    now = datetime(2016, 12, 20, 11, 33, 45)
184
185
    tz = pytz.utc
186
    now_tz = tz.localize(now)
187
    mock_dt.now.return_value = now_tz
188
    mock_dt.side_effect = lambda *args, **kw: datetime(*args, **kw)
189
190
    expected = {
191
        'abbrev_commit': commit.hexsha[0:7],
192
        'author_date': '2016-12-17T06:40:21+0100',
193
        'author_email': '[email protected]',
194
        'author_name': 'Paul Dupond',
195
        'author_timestamp': '1481953221',
196
        'branches': ['feature/my_feature', 'master'],
197
        'commit_date': '2016-12-17T06:40:21+0100',
198
        'commit_timestamp': '1481953221',
199
        'committer_email': '[email protected]',
200
        'committer_name': 'User Test',
201
        'deploy_date': now_tz.strftime('%Y-%m-%dT%H:%M:%S%z'),
202
        'deploy_timestamp': '1482233625',
203
        'full_commit': commit.hexsha,
204
        'top_branches': ['feature/my_feature', 'master'],
205
        'version': 'v0.1.2-1-g' + commit.hexsha[0:7]
206
    }
207
208
    assert handler_local.get_infos() == expected
209