Skip to content

Commit

Permalink
Updated line-length argument for isort, added tests for helloworld an…
Browse files Browse the repository at this point in the history
…d profile, #48
  • Loading branch information
MarcoHuebner committed Sep 5, 2022
1 parent 1f4af9e commit e95835a
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 20 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ repos:
hooks:
- id: isort
name: isort (python)
args: ["--profile", "black", "--filter-files"]
args: ["--profile", "black", "--line-length=80"]
- repo: https://github.com/python-jsonschema/check-jsonschema
rev: 0.14.2
hooks:
Expand Down
15 changes: 7 additions & 8 deletions src/pygenesis/helloworld.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@
from pygenesis.config import load_config
from pygenesis.http_helper import _check_invalid_status_code

config = load_config()


# TODO: Write tests
def whoami() -> str:
"""
Wrapper method which constructs an URL for testing the Destatis API
Expand All @@ -17,34 +14,36 @@ def whoami() -> str:
Returns:
str: text test response from Destatis
"""
config = load_config()
url = f"{config['GENESIS API']['base_url']}" + "helloworld/whoami"

response = requests.get(url)
response = requests.get(url, timeout=(1, 15))

_check_invalid_status_code(response.status_code)

return response.text


def logincheck():
def logincheck() -> str:
"""
Wrapper method which constructs an URL for testing the Destatis API
logincheck method, which tests the login credentials (from the config.ini).
Returns:
str: text logincheck response from Destatis
"""
config = load_config()
url = f"{config['GENESIS API']['base_url']}" + "helloworld/logincheck"

params = {
"username": config["GENESIS API"]["username"],
"password": config["GENESIS API"]["password"],
}

response = requests.get(url, params=params)
response = requests.get(url, params=params, timeout=(1, 15))

# NOTE: Cannot use get_response_from_endpoint due to colliding
# and misleading usage of "Status" key in response
# NOTE: Cannot use get_data_from_endpoint due to colliding
# and misleading usage of "Status" key in API response
_check_invalid_status_code(response.status_code)

return response.text
7 changes: 3 additions & 4 deletions src/pygenesis/profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@
get_config_path_from_settings,
load_config,
)
from pygenesis.http_helper import get_response_from_endpoint
from pygenesis.http_helper import get_data_from_endpoint


# TODO: write tests
def password(new_password: str):
"""
Changes Genesis REST-API password and updates local config.
Expand Down Expand Up @@ -36,12 +35,12 @@ def password(new_password: str):
)

# change remote password
response = get_response_from_endpoint("profile", "password", params)
response_text = get_data_from_endpoint("profile", "password", params)
# change local password
config["GENESIS API"]["password"] = new_password
_write_config(config, get_config_path_from_settings())

return response.text
return response_text


def remove_result():
Expand Down
24 changes: 24 additions & 0 deletions tests/test_helloworld.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from configparser import ConfigParser

from mock import patch

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


@patch("requests.get")
@patch("pygenesis.helloworld.load_config")
def test_whoami(mock_config, mock_requests):
mock_config.return_value = mock_config_dict()
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 = mock_config_dict()
mock_requests.return_value = _generic_request_status()

logincheck()
20 changes: 13 additions & 7 deletions tests/test_http_helper.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import json
import logging
from configparser import ConfigParser

import pytest
import requests
Expand Down Expand Up @@ -57,20 +58,25 @@ def _generic_request_status(
return request_status


def mock_config_dict() -> ConfigParser:
config = ConfigParser()
config["GENESIS API"] = {
"base_url": "mocked_url",
"username": "JaneDoe",
"password": "password",
}

return config


@patch("requests.get")
@patch("pygenesis.http_helper.load_config")
def test_get_response_from_endpoint(mock_config, mock_requests):
"""
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_config.return_value = mock_config_dict()
mock_requests.return_value = _generic_request_status()

get_data_from_endpoint(endpoint="endpoint", method="method", params={})
Expand Down
33 changes: 33 additions & 0 deletions tests/test_profile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import re
from configparser import ConfigParser
from pathlib import Path

import pytest
from mock import patch

from pygenesis.profile import password, remove_result
from tests.test_http_helper import _generic_request_status, mock_config_dict


@pytest.fixture()
def cache_dir(tmp_path_factory):
# remove white-space and non-latin characters (issue fo some user names)
temp_dir = str(tmp_path_factory.mktemp(".pygenesis"))
temp_dir = re.sub(r"[^\x00-\x7f]", r"", temp_dir.replace(" ", ""))

return Path(temp_dir)


@patch("pygenesis.profile.get_config_path_from_settings")
@patch("pygenesis.profile.get_data_from_endpoint")
@patch("pygenesis.profile.load_config")
def test_password(mock_config, mock_requests, mock_config_dir, cache_dir):
mock_config.return_value = mock_config_dict()
mock_requests.return_value = _generic_request_status()
mock_config_dir.return_value = cache_dir

password("new_password")


def test_remove_results():
pass

0 comments on commit e95835a

Please sign in to comment.