Skip to content

Commit fb73c2c

Browse files
committed
Add initial unit tests
We now have enough non-network functionality to warrant testing. Signed-off-by: Stephen Finucane <stephen@that.guru>
1 parent 5c0db06 commit fb73c2c

File tree

4 files changed

+82
-3
lines changed

4 files changed

+82
-3
lines changed

.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ htmlcov/
4444
.coverage
4545
.coverage.*
4646
.cache
47-
nosetests.xml
47+
.pytest_cache/
4848
coverage.xml
4949
*,cover
5050
.hypothesis/

git_pw/tests/test_api.py

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
"""Unit tests for ``git_pw/api.py``."""
2+
3+
import mock
4+
import pytest
5+
6+
from git_pw import api
7+
8+
9+
@mock.patch.object(api, 'LOG')
10+
@mock.patch.object(api, 'CONF')
11+
def test_get_server_undefined(mock_conf, mock_log):
12+
mock_conf.server = None
13+
14+
with pytest.raises(SystemExit):
15+
api._get_server()
16+
17+
assert mock_log.error.called
18+
19+
20+
@mock.patch.object(api, 'LOG')
21+
@mock.patch.object(api, 'CONF')
22+
def test_get_server_missing_version(mock_conf, mock_log):
23+
mock_conf.server = 'https://example.com/api'
24+
25+
server = api._get_server()
26+
27+
assert mock_log.warning.called
28+
assert server == 'https://example.com/api'
29+
30+
31+
@mock.patch.object(api, 'LOG')
32+
@mock.patch.object(api, 'CONF')
33+
def test_get_server_missing_version_and_path(mock_conf, mock_log):
34+
mock_conf.server = 'https://example.com/'
35+
36+
server = api._get_server()
37+
38+
assert mock_log.warning.called
39+
assert server == 'https://example.com/api'
40+
41+
42+
@mock.patch.object(api, 'LOG')
43+
@mock.patch.object(api, 'CONF')
44+
def test_get_project_undefined(mock_conf, mock_log):
45+
mock_conf.project = None
46+
47+
with pytest.raises(SystemExit):
48+
api._get_project()
49+
50+
assert mock_log.error.called
51+
52+
53+
@mock.patch.object(api, 'CONF')
54+
def test_get_project_wildcard(mock_conf):
55+
mock_conf.project = '*'
56+
57+
project = api._get_project()
58+
59+
assert project == ''
60+
61+
62+
@mock.patch.object(api, '_get_server')
63+
def test_version_missing(mock_server):
64+
mock_server.return_value = 'https://example.com/api'
65+
66+
assert api.version() == (1, 0)
67+
68+
69+
@mock.patch.object(api, '_get_server')
70+
def test_version(mock_server):
71+
mock_server.return_value = 'https://example.com/api/1.1'
72+
73+
assert api.version() == (1, 1)

test-requirements.txt

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
mock==2.0.0
2+
pytest==3.6.1

tox.ini

+6-2
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,13 @@ envlist = pep8,mypy,docs
55
[testenv]
66
deps =
77
-r{toxinidir}/requirements.txt
8+
-r{toxinidir}/test-requirements.txt
9+
commands =
10+
pytest -Wall {posargs}
811

912
[testenv:pep8]
10-
deps = flake8
13+
deps =
14+
flake8
1115
commands =
1216
flake8 {posargs} git_pw
1317

@@ -31,7 +35,7 @@ commands =
3135

3236
[testenv:release]
3337
deps =
34-
{[testenv]deps}
38+
-r{toxinidir}/requirements.txt
3539
twine
3640
commands =
3741
python setup.py -q bdist_wheel sdist

0 commit comments

Comments
 (0)