Skip to content

Commit 63ede9b

Browse files
authored
Prepare test environment (#34)
* Setup test environment As we want to run automated tests we have to implement a environment to solve this task. We refactored existing tests to run with `pytest`. Following things were done: * Add packages for automated testing to requirements ** add pytest, pytest-vcr ** add coverage ** add flake8-docstrings to provide better documentation * Refactor section tests * Refactor connection tests * Refactor makefile for test framework Add new targets to prepare environment for testing and run all tests at once. ** `test-setup` to setup test environment ** `test-all` to run all tests ** `test-<test>` to run a specific test * Add documentation for new targets. * Refactor controller method tests * Refactor address search tests * Refactor search subnets tests * Refactor vlan tests * Refactor nameserver tests * Refactor section search tests * Add coverage target to makefile Add target to output a simple coverage report for `phpypam` and `tests` folder. * run each test in conjunction with coverage * Extend clean target ** Add housekeeping of coverage data to `clean` target. ** Add pytest cache to clean target * Add some more tests for missing code fragments ** Add test for all exceptions ** Add test for custom `user_agent` header * add some badges to readme * fix issues from codacy report
1 parent 884518f commit 63ede9b

17 files changed

+393
-218
lines changed

Makefile

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,17 @@ default: help
1616

1717
help:
1818
@echo "Please use \`make <target>' where <target> is one of:"
19-
@echo " help - to show this message"
20-
@echo " dist - to build the collection archive"
21-
@echo " lint - to run code linting"
19+
@echo " help - to show this message"
20+
@echo " dist - to build the collection archive"
21+
@echo " lint - to run code linting"
22+
@echo " clean - clean workspace"
23+
@echo " doc-setup - prepare environment for creating documentation"
24+
@echo " doc - create documentation"
25+
@echo " test-setup - prepare environment for tests"
26+
@echo " test-all - run all tests"
27+
@echo " test-<test> - run a specifig test"
28+
@echo " coverage - display code coverage"
29+
@echo " coverage-xml - create xml coverage report"
2230

2331
lint:
2432
flake8 phpypam
@@ -33,11 +41,12 @@ publish: check
3341
$(TWINE_CMD) upload $(TWINE_OPTIONS) dist/*
3442

3543
clean:
36-
rm -Rf docs/{build,_build} {build,dist} *.egg-info
44+
rm -Rf docs/{build,_build} {build,dist} *.egg-info coverage.xml .pytest_cache
3745
find . -name '*.pyc' -exec rm -f {} +
3846
find . -name '*.pyo' -exec rm -f {} +
3947
find . -name '*~' -exec rm -f {} +
4048
find . -name '__pycache__' -exec rm -rf {} +
49+
coverage erase
4150

4251
doc-setup:
4352
pip install --upgrade -r docs/requirements.txt
@@ -47,6 +56,21 @@ doc:
4756
sphinx-apidoc -M -f -o docs/plugins/ phpypam
4857
make -C docs html
4958

59+
test-setup:
60+
pip install --upgrade -r requirements-dev.txt
61+
62+
test-all:
63+
coverage run -m pytest tests/test_cases/* -v
64+
65+
test-%:
66+
coverage run -m pytest tests/test_cases/$*.py -v
67+
68+
coverage: test-all
69+
coverage report -m --include 'phpypam/*','phpypam/**/*','tests/**/*'
70+
71+
coverage-xml: test-all
72+
coverage xml --include 'phpypam/*','phpypam/**/*','tests/**/*'
73+
5074
FORCE:
5175

5276
.PHONY: help dist lint publish FORCE

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# phpypam: Python API client library for phpIPAM installation
22

3-
[![Codacy Badge](https://api.codacy.com/project/badge/Grade/6b89ed13b6694197944cddea94c953ed)](https://app.codacy.com/gh/codeaffen/phpypam?utm_source=github.com&utm_medium=referral&utm_content=codeaffen/phpypam&utm_campaign=Badge_Grade_Settings)
3+
[![PyPI version](https://badge.fury.io/py/phpypam.svg)](https://badge.fury.io/py/phpypam)
4+
[![Codacy Badge](https://app.codacy.com/project/badge/Grade/ed3511c33a254bfe942777c9ef3251e3)](https://www.codacy.com/gh/codeaffen/phpypam/dashboard?utm_source=github.com&amp;utm_medium=referral&amp;utm_content=codeaffen/phpypam&amp;utm_campaign=Badge_Grade)
5+
[![Documentation Status](https://readthedocs.org/projects/phpypam/badge/?version=latest)](https://phpypam.readthedocs.io/en/latest/?badge=latest)
46

57
phpypam is intended to be a complete library for speaking with phpIPAM API.
68

requirements-dev.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
wheel
22
flake8
33
flake8-colors
4+
flake8-docstrings
45
requests
56
inflection
67
twine
78
setuptools
89
PyYAML
910
bumpversion
11+
pytest
12+
pytest-vcr
13+
coverage

tests/api_connection.py

Lines changed: 0 additions & 19 deletions
This file was deleted.

tests/ensure_nameserver.py

Lines changed: 0 additions & 47 deletions
This file was deleted.

tests/ensure_section.py

Lines changed: 0 additions & 46 deletions
This file was deleted.

tests/ensure_vlan.py

Lines changed: 0 additions & 49 deletions
This file was deleted.

tests/search_section_by_name.py

Lines changed: 0 additions & 32 deletions
This file was deleted.

tests/test_cases/api_connection.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
"""Tests to check connection to API."""
2+
import phpypam
3+
import pytest
4+
import yaml
5+
6+
from phpypam.core.exceptions import PHPyPAMInvalidCredentials
7+
8+
with open('tests/vars/server.yml') as c:
9+
server = yaml.safe_load(c)
10+
11+
connection_params = dict(
12+
url=server['url'],
13+
app_id=server['app_id'],
14+
username=server['username'],
15+
password=server['password'],
16+
ssl_verify=True
17+
)
18+
19+
20+
def test_connection_success():
21+
"""Test whether a connection to API can be done."""
22+
pi = phpypam.api(**connection_params)
23+
24+
token = pi.get_token()
25+
26+
assert isinstance(pi, phpypam.api)
27+
assert len(token) == 24
28+
29+
30+
def test_connection_failure():
31+
"""Test faulty connection.
32+
33+
Test if failure where reported correctly if there is faulty login data.
34+
"""
35+
connection_kwargs = connection_params.copy()
36+
connection_kwargs.update({'password': 'wrong_password'})
37+
38+
pytest.raises(PHPyPAMInvalidCredentials, phpypam.api, **connection_kwargs)
39+
40+
connection_kwargs = connection_params.copy()
41+
connection_kwargs.update({'username': 'wrong_username'})
42+
43+
pytest.raises(PHPyPAMInvalidCredentials, phpypam.api, **connection_kwargs)
44+
45+
46+
def test_custom_user_agent():
47+
"""Test to set custom user-agent header.
48+
49+
Test to connect to API with a custom user-agent header set.
50+
"""
51+
connection_kwargs = connection_params.copy()
52+
connection_kwargs.update({'user_agent': 'my_test_agent'})
53+
54+
pi = phpypam.api(**connection_kwargs)
55+
56+
assert isinstance(pi, phpypam.api)
57+
assert len(pi.get_token()) == 24
Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
#!/usr/bin/env python
2-
1+
"""Test controller method."""
32
import phpypam
43
import yaml
54

65
with open('tests/vars/server.yml') as c:
76
server = yaml.safe_load(c)
87

9-
if __name__ == '__main__':
8+
9+
def test_controllers():
10+
"""Test if controllers method returns correct datatype."""
1011
pi = phpypam.api(
1112
url=server['url'],
1213
app_id=server['app_id'],
@@ -16,4 +17,4 @@
1617
)
1718

1819
controllers = pi.controllers()
19-
print(controllers)
20+
assert isinstance(controllers, set)

0 commit comments

Comments
 (0)