Completed
Push — master ( d5feb1...7c9762 )
by Charles
02:37
created

test_getters()   F

Complexity

Conditions 11

Size

Total Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 11
dl 0
loc 25
rs 3.1764
c 2
b 0
f 0

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_message() == commit.message.strip()
117
118
    assert handler_local.get_author_name() == name
119
    assert handler_local.get_author_email() == email
120
121
    assert handler_local.get_committer_name() == 'User Test'
122
    assert handler_local.get_committer_email() == '[email protected]'
123
124
    assert handler_local.get_author_date() == author_dt_tz
125
    assert handler_local.get_commit_date() == author_dt_tz
126
127
128
def test_get_version_no_commit(handler):
129
    default = '8fa82b6'
130
    assert handler.get_version() == ''
131
    assert handler.get_version(default=default) == default
132
133
134
def test_get_version_no_tags(git_repo, handler):
135
    default = '8fa82b6'
136
    commit = git_utils.commit(repo=git_repo, message='test 1')
137
138
    assert handler.get_version(default=default) == commit.hexsha[0:7]
139
140
141
def test_get_version_on_tag(git_repo, handler):
142
    default = '8fa82b6'
143
    git_utils.commit(repo=git_repo, message='test 1')
144
    git_utils.tag(repo=git_repo, version='v0.1.3')
145
146
    assert handler.get_version(default=default) == 'v0.1.3'
147
148
149
def test_get_version_after_tag(git_repo, handler):
150
    default = '8fa82b6'
151
    git_utils.commit(repo=git_repo, message='test 1')
152
    git_utils.tag(repo=git_repo, version='v0.1.3')
153
    commit = git_utils.commit(repo=git_repo, message='test 2')
154
155
    assert handler.get_version(
156
        default=default) == 'v0.1.3-1-g' + commit.hexsha[0:7]
157
158
159
def test_get_branches(git_repo_local, handler_local):
160
    expected = ['origin/feature/my_feature', 'origin/master']
161
    assert handler_local.get_branches() == expected
162
163
164
def test_get_top_branches(git_repo_local, handler_local):
165
    branches = ['origin/feature/my_feature', 'origin/release', 'origin/master']
166
    expected = ['origin/feature/my_feature', 'origin/master']
167
    abbrev_commit = git_repo_local.commit('HEAD').hexsha[0:7]
168
169
    assert handler_local.get_top_branches(
170
        branches=branches,
171
        abbrev_commit=abbrev_commit) == expected
172
173
174
@pytest.mark.parametrize("branches,expected", [
175
    (['origin/master', 'origin/feature/my_feature'],
176
     ['master', 'feature/my_feature']),
177
])
178
def test_remove_remote_prefix(branches, expected, handler):
179
    assert handler.remove_remote_prefix(branches=branches) == expected
180
181
182
@patch('git_app_version.helper.date.datetime')
183
def test_get_infos(mock_dt, git_repo_local, handler_local):
184
    commit = git_repo_local.commit('HEAD')
185
    now = datetime(2016, 12, 20, 11, 33, 45)
186
187
    tz = pytz.utc
188
    now_tz = tz.localize(now)
189
    mock_dt.now.return_value = now_tz
190
    mock_dt.side_effect = lambda *args, **kw: datetime(*args, **kw)
191
192
    expected = {
193
        'message': 'commit 3',
194
        'abbrev_commit': commit.hexsha[0:7],
195
        'author_date': '2016-12-17T06:40:21+0100',
196
        'author_email': '[email protected]',
197
        'author_name': 'Paul Dupond',
198
        'author_timestamp': '1481953221',
199
        'branches': ['feature/my_feature', 'master'],
200
        'commit_date': '2016-12-17T06:40:21+0100',
201
        'commit_timestamp': '1481953221',
202
        'committer_email': '[email protected]',
203
        'committer_name': 'User Test',
204
        'deploy_date': now_tz.strftime('%Y-%m-%dT%H:%M:%S%z'),
205
        'deploy_timestamp': '1482233625',
206
        'full_commit': commit.hexsha,
207
        'top_branches': ['feature/my_feature', 'master'],
208
        'version': 'v0.1.2-1-g' + commit.hexsha[0:7]
209
    }
210
211
    assert handler_local.get_infos() == expected
212