Skip to content

Commit 0d8a012

Browse files
bkircherbegotxe
authored andcommitted
Add simple unit testing
Add pytest framework and a few simple tests that just check the most basic things. - tests.test_config: checks that config has sane defaults - tests.test_sync_request: check that we can render a basic GET request for /objects/servers - test.conftest: fixture for SyncGridscaleApiClient - pytest.ini: ini file for pytest that just limits test discovery to tests/ directory (much faster) - dev-requirements.txt: contains all dependencies to run the test plus a few other Python dev tools for linting, refactoring, and formatting, notably pylint, rope, black You can run the tests with: $ python -m venv .venv $ source .venv/bin/activate $ python -m pip install -r dev-requirements.txt $ pytest
1 parent 5c5a1f5 commit 0d8a012

File tree

6 files changed

+67
-0
lines changed

6 files changed

+67
-0
lines changed

dev-requirements.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
urllib3>=1.15
2+
six>=1.10
3+
certifi
4+
python-dateutil
5+
pylint
6+
rope
7+
black
8+
pytest
9+
pytest-mock

pytest.ini

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# See https://docs.pytest.org/en/latest/reference.html#configuration-options
2+
3+
[pytest]
4+
testpaths = tests

tests/__init__.py

Whitespace-only changes.

tests/conftest.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from typing import Iterator
2+
3+
import pytest
4+
5+
from gs_api_client import SyncGridscaleApiClient, Configuration
6+
7+
8+
@pytest.fixture
9+
def client() -> Iterator[SyncGridscaleApiClient]:
10+
"""Fixture that creates and configures a synchronous API client."""
11+
12+
c = Configuration()
13+
c.host = "https://example.com"
14+
c.api_key["X-Auth-Token"] = "fake-token"
15+
c.api_key["X-Auth-UserId"] = "not-a-uuid"
16+
17+
yield SyncGridscaleApiClient(configuration=c)

tests/test_config.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from gs_api_client import Configuration
2+
3+
4+
def test_debug_is_disabled_by_default():
5+
config = Configuration()
6+
assert not config.debug
7+
8+
9+
def test_tls_certs_are_verified_by_default():
10+
config = Configuration()
11+
assert config.verify_ssl

tests/test_sync_request.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from gs_api_client import Configuration, SyncGridscaleApiClient
2+
3+
4+
def test_get_servers(client, mocker):
5+
mock_api = mocker.patch.object(client.api_client, "call_api")
6+
mock_api.return_value = (mocker.ANY, 200, None)
7+
8+
client.get_servers()
9+
10+
mock_api.assert_called_once_with(
11+
"/objects/servers",
12+
"GET",
13+
mocker.ANY,
14+
mocker.ANY,
15+
mocker.ANY,
16+
body=mocker.ANY,
17+
post_params=mocker.ANY,
18+
files=mocker.ANY,
19+
response_type=mocker.ANY,
20+
auth_settings=mocker.ANY,
21+
async_req=mocker.ANY,
22+
_return_http_data_only=mocker.ANY,
23+
collection_formats=mocker.ANY,
24+
_preload_content=mocker.ANY,
25+
_request_timeout=mocker.ANY,
26+
)

0 commit comments

Comments
 (0)