mirror of
https://github.com/fhswf/aki_prj23_transparenzregister.git
synced 2025-04-24 22:12:35 +02:00
* Reworked the lint action * Removed the file change requirement * Repaired mypy * Repaired pip-audit
75 lines
2.6 KiB
Python
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"]
|