Skip to content

Commit

Permalink
✅ Started unit testing with tox/pytest
Browse files Browse the repository at this point in the history
Include system test

TODO: Needs more tests (coverage at 12% now)
  • Loading branch information
MarcelWaldvogel committed Nov 18, 2021
1 parent 54468f2 commit c2520ea
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 0 deletions.
14 changes: 14 additions & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[paths]
source =
src
*/site-packages

[run]
branch = true
parallel = true
source =
dnstemple

[report]
show_missing = true
precision = 2
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ __pycache__/
build/
dist/
.idea
.tox/
.coverage
16 changes: 16 additions & 0 deletions tests/test_basics.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from mock import patch, call
import time

import dnstemple


@patch('dnstemple.time.gmtime')
def test_today(mock_gmtime):
mock_gmtime.return_value = (2021, 11, 18, 11, 21, 17, 4, 322, False)
assert dnstemple.soa_serial_for_today() == 2021111800


@patch('dnstemple.time.time')
def test_now(mock_time):
mock_time.return_value = 1637231137
assert dnstemple.soa_serial_for_now() == 1637231137
8 changes: 8 additions & 0 deletions tests/test_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from mock import patch, call, mock_open

import dnstemple


@patch('builtins.open', new_callable=mock_open, read_data='../some/path')
def test_config(mock_file):
pass
25 changes: 25 additions & 0 deletions tests/test_variables.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from mock import patch, call
from pytest import raises

import dnstemple


def test_expand_none():
assert dnstemple.expand_variables(
'a.zone', '@\tTXT\t"test"', {'variables': {}}) == '@\tTXT\t"test"'


def test_expand_one():
assert dnstemple.expand_variables(
'a.zone', '{one}\tTXT\t"test"', {'variables': {'one': 'eins'}}) == 'eins\tTXT\t"test"'


def test_expand_two():
assert dnstemple.expand_variables(
'a.zone', '{one}\tTXT\t"{two}"', {'variables': {'one': 'eins', 'two': 'zwei'}}) == 'eins\tTXT\t"zwei"'


def test_expand_bad():
with raises(SystemExit, match=r'Unknown variable'):
assert dnstemple.expand_variables(
'a.zone', '{one}\tTXT\t"{three}"', {'variables': {'one': 'eins', 'two': 'zwei'}}) == 'eins\tTXT\t"zwei"'
38 changes: 38 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# tox (https://tox.readthedocs.io/) is a tool for running tests
# in multiple virtualenvs. This configuration file will run the
# test suite on all supported python versions. To use it, "pip install tox"
# and then run "tox" from this directory.

[tox]
envlist = py36, py37, py38, py39, py310, pypy3, system
isolated_build = True

[testenv]
deps =
pytest
mock
importlib_metadata
dnspython
PyYAML
commands =
pytest

[testenv:py39]
deps =
pytest
pytest-cov
mock
importlib_metadata
dnspython
PyYAML
commands =
pytest --cov

[testenv:system]
deps =
importlib_metadata
dnspython
PyYAML
allowlist_externals = make
commands =
make test

0 comments on commit c2520ea

Please sign in to comment.