Skip to content

Commit

Permalink
refactor all tests to use python-mock (#72)
Browse files Browse the repository at this point in the history
* refactor all tests to use python-mock

* update mocks
  • Loading branch information
pmayd authored Sep 27, 2022
1 parent 2c8d12f commit 547afe1
Show file tree
Hide file tree
Showing 7 changed files with 118 additions and 129 deletions.
72 changes: 20 additions & 52 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ bandit = "^1.7.4"
black = "^22.3.0"
flake8-docstrings = "^1.6.0"
isort = "^5.10.1"
mock = "^4.0.3"
mypy = "^0.942"
pre-commit = "^2.18.1"
pylint = "^2.13.4"
Expand All @@ -24,6 +23,7 @@ pytest-cov = "^3.0.0"
safety = "^2.0.0"
flake8 = "^4.0.1"
jupyter = "^1.0.0"
pytest-mock = "^3.8.2"

[tool.black]
line-length = 80
Expand Down
1 change: 0 additions & 1 deletion tests/test_cache.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import re
from pathlib import Path
from typing import Optional

import pytest

Expand Down
16 changes: 9 additions & 7 deletions tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
from pathlib import Path

import pytest
from mock import patch

import pygenesis.config
from pygenesis.config import (
DEFAULT_SETTINGS_FILE,
_write_config,
Expand Down Expand Up @@ -33,17 +33,19 @@ def restore_settings():
_write_config(old_settings, DEFAULT_SETTINGS_FILE)


def test_settings():
def test_create_settings_is_run_on_import():
assert DEFAULT_SETTINGS_FILE.exists() and DEFAULT_SETTINGS_FILE.is_file()


@patch("pygenesis.config.DEFAULT_CONFIG_DIR")
@patch("pygenesis.config.DEFAULT_SETTINGS_FILE")
def test_create_settings(mock_config, mock_settings, config_dir):
mock_config.return_value = config_dir
mock_settings.return_value = config_dir / "settings.ini"
def test_create_settings(config_dir, mocker):
mocker.patch.object(pygenesis.config, "DEFAULT_CONFIG_DIR", config_dir)
mocker.patch.object(
pygenesis.config, "DEFAULT_SETTINGS_FILE", config_dir / "settings.ini"
)
create_settings()

assert (config_dir / "settings.ini").is_file()


def test_load_settings():
settings = load_settings()
Expand Down
71 changes: 41 additions & 30 deletions tests/test_helloworld.py
Original file line number Diff line number Diff line change
@@ -1,34 +1,45 @@
from mock import patch

from pygenesis.helloworld import logincheck, whoami
from tests.test_http_helper import _generic_request_status


@patch("requests.get")
@patch("pygenesis.helloworld.load_config")
def test_whoami(mock_config, mock_requests):
mock_config.return_value = {
"GENESIS API": {
"base_url": "mocked_url",
"username": "JaneDoe",
"password": "password",
}
}
mock_requests.return_value = _generic_request_status()

whoami()


@patch("requests.get")
@patch("pygenesis.helloworld.load_config")
def test_logincheck(mock_config, mock_requests):
mock_config.return_value = {
"GENESIS API": {
"base_url": "mocked_url",
"username": "JaneDoe",
"password": "password",
}
}
mock_requests.return_value = _generic_request_status()

logincheck()
def test_whoami(mocker):
mocker.patch(
"pygenesis.helloworld.load_config",
return_value={
"GENESIS API": {
"base_url": "mocked_url",
"username": "JaneDoe",
"password": "password",
}
},
)

mocker.patch(
"pygenesis.helloworld.requests.get",
return_value=_generic_request_status(),
)

response = whoami()

assert response == str(_generic_request_status().text)


def test_logincheck(mocker):
mocker.patch(
"pygenesis.helloworld.load_config",
return_value={
"GENESIS API": {
"base_url": "mocked_url",
"username": "JaneDoe",
"password": "password",
}
},
)
mocker.patch(
"pygenesis.helloworld.requests.get",
return_value=_generic_request_status(),
)

response = logincheck()

assert response == str(_generic_request_status().text)
28 changes: 14 additions & 14 deletions tests/test_http_helper.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import json
import logging
import re
from pathlib import Path

import pytest
import requests
from mock import patch

from pygenesis.custom_exceptions import DestatisStatusError
from pygenesis.http_helper import (
Expand Down Expand Up @@ -59,21 +56,24 @@ def _generic_request_status(
return request_status


@patch("requests.get")
@patch("pygenesis.http_helper.load_config")
def test_get_response_from_endpoint(mock_config, mock_requests):
def test_get_response_from_endpoint(mocker):
"""
Test once with generic API response, more detailed tests
of subfunctions and specific cases below.
"""
mock_config.return_value = {
"GENESIS API": {
"base_url": "mocked_url",
"username": "JaneDoe",
"password": "password",
}
}
mock_requests.return_value = _generic_request_status()
mocker.patch(
"pygenesis.http_helper.requests", return_value=_generic_request_status()
)
mocker.patch(
"pygenesis.http_helper.load_config",
return_value={
"GENESIS API": {
"base_url": "mocked_url",
"username": "JaneDoe",
"password": "password",
}
},
)

get_data_from_endpoint(endpoint="endpoint", method="method", params={})

Expand Down
Loading

0 comments on commit 547afe1

Please sign in to comment.