aki_prj23_transparenzregister/tests/config/config_providers_test.py
Philipp Horstenkamp e4a57c9136
Chore/rework workflow (#52)
* Reworked the lint action
* Removed the file change requirement
* Repaired mypy
* Repaired pip-audit
2023-08-17 22:05:12 +02:00

75 lines
2.6 KiB
Python

import json
from unittest.mock import mock_open, patch
import pytest
from aki_prj23_transparenzregister.config.config_providers import JsonFileConfigProvider
def test_json_provider_init_fail() -> None:
with pytest.raises(FileNotFoundError):
JsonFileConfigProvider("file-that-does-not-exist")
def test_json_provider_init_no_json() -> None:
with patch("os.path.isfile") as mock_isfile, patch(
"builtins.open", mock_open(read_data="fhdaofhdoas")
):
mock_isfile.return_value = True
with pytest.raises(TypeError):
JsonFileConfigProvider("non-json-file")
def test_json_provider_init() -> None:
data = {"hello": "world"}
input_data = json.dumps(data)
with patch("os.path.isfile") as mock_isfile:
mock_isfile.return_value = True
with patch("builtins.open", mock_open(read_data=input_data)):
provider = JsonFileConfigProvider("someWhere")
assert provider.__data__ == data
def test_json_provider_get_postgres() -> None:
data = {
"postgres": {
"username": "user",
"password": "pass",
"host": "locahost",
"database": "postgres",
"port": 420,
}
}
input_data = json.dumps(data)
with patch("os.path.isfile") as mock_isfile:
mock_isfile.return_value = True
with patch("builtins.open", mock_open(read_data=input_data)):
config = JsonFileConfigProvider("someWhere").get_postgre_connection_string()
assert config.username == data["postgres"]["username"]
assert config.password == data["postgres"]["password"]
assert config.host == data["postgres"]["host"]
assert config.database == data["postgres"]["database"]
assert config.port == data["postgres"]["port"]
def test_json_provider_get_mongo() -> None:
data = {
"mongo": {
"username": "user",
"password": "pass",
"host": "locahost",
"database": "postgres",
"port": 420,
}
}
input_data = json.dumps(data)
with patch("os.path.isfile") as mock_isfile:
mock_isfile.return_value = True
with patch("builtins.open", mock_open(read_data=input_data)):
config = JsonFileConfigProvider("someWhere").get_mongo_connection_string()
assert config.username == data["mongo"]["username"]
assert config.password == data["mongo"]["password"]
assert config.hostname == data["mongo"]["host"]
assert config.database == data["mongo"]["database"]
assert config.port == data["mongo"]["port"]