Skip to content

Commit

Permalink
Add initial unit tests
Browse files Browse the repository at this point in the history
We now have enough non-network functionality to warrant testing.

Signed-off-by: Stephen Finucane <[email protected]>
  • Loading branch information
stephenfin committed Jun 15, 2018
1 parent 5c0db06 commit fb73c2c
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ htmlcov/
.coverage
.coverage.*
.cache
nosetests.xml
.pytest_cache/
coverage.xml
*,cover
.hypothesis/
Expand Down
73 changes: 73 additions & 0 deletions git_pw/tests/test_api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
"""Unit tests for ``git_pw/api.py``."""

import mock
import pytest

from git_pw import api


@mock.patch.object(api, 'LOG')
@mock.patch.object(api, 'CONF')
def test_get_server_undefined(mock_conf, mock_log):
mock_conf.server = None

with pytest.raises(SystemExit):
api._get_server()

assert mock_log.error.called


@mock.patch.object(api, 'LOG')
@mock.patch.object(api, 'CONF')
def test_get_server_missing_version(mock_conf, mock_log):
mock_conf.server = 'https://example.com/api'

server = api._get_server()

assert mock_log.warning.called
assert server == 'https://example.com/api'


@mock.patch.object(api, 'LOG')
@mock.patch.object(api, 'CONF')
def test_get_server_missing_version_and_path(mock_conf, mock_log):
mock_conf.server = 'https://example.com/'

server = api._get_server()

assert mock_log.warning.called
assert server == 'https://example.com/api'


@mock.patch.object(api, 'LOG')
@mock.patch.object(api, 'CONF')
def test_get_project_undefined(mock_conf, mock_log):
mock_conf.project = None

with pytest.raises(SystemExit):
api._get_project()

assert mock_log.error.called


@mock.patch.object(api, 'CONF')
def test_get_project_wildcard(mock_conf):
mock_conf.project = '*'

project = api._get_project()

assert project == ''


@mock.patch.object(api, '_get_server')
def test_version_missing(mock_server):
mock_server.return_value = 'https://example.com/api'

assert api.version() == (1, 0)


@mock.patch.object(api, '_get_server')
def test_version(mock_server):
mock_server.return_value = 'https://example.com/api/1.1'

assert api.version() == (1, 1)
2 changes: 2 additions & 0 deletions test-requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
mock==2.0.0
pytest==3.6.1
8 changes: 6 additions & 2 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,13 @@ envlist = pep8,mypy,docs
[testenv]
deps =
-r{toxinidir}/requirements.txt
-r{toxinidir}/test-requirements.txt
commands =
pytest -Wall {posargs}

[testenv:pep8]
deps = flake8
deps =
flake8
commands =
flake8 {posargs} git_pw

Expand All @@ -31,7 +35,7 @@ commands =

[testenv:release]
deps =
{[testenv]deps}
-r{toxinidir}/requirements.txt
twine
commands =
python setup.py -q bdist_wheel sdist
Expand Down

0 comments on commit fb73c2c

Please sign in to comment.