mirror of
https://github.com/fhswf/aki_prj23_transparenzregister.git
synced 2025-04-24 04:52:33 +02:00
test: Add missing unit tests
This commit is contained in:
parent
86001a19e9
commit
4e7f6bca1d
@ -83,7 +83,7 @@ class JsonFileConfigProvider(ConfigProvider):
|
||||
"""
|
||||
details = self.__data__["mongo"]
|
||||
return MongoConnection(
|
||||
details["hostname"],
|
||||
details["host"],
|
||||
details["database"],
|
||||
details["port"],
|
||||
details["username"],
|
||||
|
1
tests/config/__init__.py
Normal file
1
tests/config/__init__.py
Normal file
@ -0,0 +1 @@
|
||||
"""Tests for config module."""
|
74
tests/config/config_providers_test.py
Normal file
74
tests/config/config_providers_test.py
Normal file
@ -0,0 +1,74 @@
|
||||
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():
|
||||
with pytest.raises(FileNotFoundError):
|
||||
JsonFileConfigProvider("file-that-does-not-exist")
|
||||
|
||||
|
||||
def test_json_provider_init_no_json():
|
||||
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():
|
||||
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_postgre():
|
||||
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():
|
||||
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"]
|
1
tests/utils/postgres/__init__.py
Normal file
1
tests/utils/postgres/__init__.py
Normal file
@ -0,0 +1 @@
|
||||
"""Tests for utils.postgres module."""
|
14
tests/utils/postgres/connector_test.py
Normal file
14
tests/utils/postgres/connector_test.py
Normal file
@ -0,0 +1,14 @@
|
||||
from unittest.mock import patch
|
||||
|
||||
from aki_prj23_transparenzregister.config.config_template import PostgreConnectionString
|
||||
from aki_prj23_transparenzregister.utils.postgres.connector import get_engine
|
||||
|
||||
|
||||
def test_get_engine():
|
||||
conn_args = PostgreConnectionString("", "", "", "", 42)
|
||||
with patch(
|
||||
"aki_prj23_transparenzregister.utils.postgres.connector.create_engine"
|
||||
) as mock_create_engine:
|
||||
result = "someThing"
|
||||
mock_create_engine.return_value = result
|
||||
assert get_engine(conn_args) == result
|
0
tests/utils/postgres/entities_test.py
Normal file
0
tests/utils/postgres/entities_test.py
Normal file
Loading…
x
Reference in New Issue
Block a user