Added configuration tests & improved json parsing by converting into target types

This commit is contained in:
Sven Heidemann 2023-04-09 12:31:31 +02:00
parent 0378f8944a
commit 1117735f2e
6 changed files with 71 additions and 4 deletions

View File

@ -12,11 +12,11 @@ class DatabaseSettings(ConfigurationModelABC):
port: int = None, port: int = None,
user: str = None, user: str = None,
password: str = None, password: str = None,
databse: str = None, database: str = None,
charset: str = None, charset: str = None,
use_unicode: bool = None, use_unicode: bool = None,
buffered: bool = None, buffered: bool = None,
auth_plugin: bool = None, auth_plugin: str = None,
): ):
ConfigurationModelABC.__init__(self) ConfigurationModelABC.__init__(self)
@ -24,7 +24,7 @@ class DatabaseSettings(ConfigurationModelABC):
self._port: Optional[int] = port self._port: Optional[int] = port
self._user: Optional[str] = user self._user: Optional[str] = user
self._password: Optional[str] = password self._password: Optional[str] = password
self._databse: Optional[str] = databse self._databse: Optional[str] = database
self._charset: Optional[str] = charset self._charset: Optional[str] = charset
self._use_unicode: Optional[bool] = use_unicode self._use_unicode: Optional[bool] = use_unicode
self._buffered: Optional[bool] = buffered self._buffered: Optional[bool] = buffered

View File

@ -30,6 +30,9 @@ class JSONProcessor:
if issubclass(parameter.annotation, enum.Enum): if issubclass(parameter.annotation, enum.Enum):
value = parameter.annotation[value] value = parameter.annotation[value]
if type(value) != parameter.annotation:
value = parameter.annotation(value)
args.append(value) args.append(value)
elif parameter.default != Parameter.empty: elif parameter.default != Parameter.empty:

View File

@ -0,0 +1,37 @@
import os
import unittest
from cpl_core.configuration import Configuration
from cpl_core.database import DatabaseSettings
from cpl_core.mailing import EMailClientSettings
class ConfigurationTestCase(unittest.TestCase):
def setUp(self):
self._config = Configuration()
def test_env_vars(self):
os.environ["CPLT_TESTVAR"] = "Hello World"
os.environ["CPL_NOT_EXISTING"] = "Hello World"
self._config.add_environment_variables("CPLT_")
self.assertEqual(self._config.get_configuration("TESTVAR"), "Hello World")
self.assertEqual(self._config.get_configuration("TESTVAR"), "Hello World")
self.assertEqual(self._config.get_configuration("NOT_EXISTING"), None)
def test_add_json_file(self):
self._config.add_json_file("unittests_core/configuration/test-settings.json")
db = self._config.get_configuration(DatabaseSettings)
self.assertIsNotNone(db)
self.assertEqual("localhost", db.host)
self.assertEqual("local", db.user)
self.assertEqual("bG9jYWw=", db.password)
self.assertEqual("local", db.database)
self.assertEqual(int, type(db.port))
self.assertEqual(3306, db.port)
self.assertEqual("utf8mb4", db.charset)
self.assertTrue(db.use_unicode)
self.assertTrue(db.buffered)
self.assertEqual("mysql_native_password", db.auth_plugin)
self.assertIsNone(self._config.get_configuration(EMailClientSettings))

View File

@ -0,0 +1,25 @@
{
"TimeFormatSettings": {
"DateFormat": "%Y-%m-%d",
"TimeFormat": "%H:%M:%S",
"DateTimeFormat": "%Y-%m-%d %H:%M:%S.%f",
"DateTimeLogFormat": "%Y-%m-%d_%H-%M-%S"
},
"LoggingSettings": {
"Path": "logs/$date_now/",
"Filename": "bot.log",
"ConsoleLogLevel": "TRACE",
"FileLogLevel": "TRACE"
},
"DatabaseSettings": {
"Host": "localhost",
"User": "local",
"Password": "bG9jYWw=",
"Database": "local",
"Port": "3306",
"Charset": "utf8mb4",
"UseUnicode": "true",
"Buffered": "true",
"AuthPlugin": "mysql_native_password"
}
}

View File

@ -1,5 +1,6 @@
import unittest import unittest
from unittests_core.configuration.configuration_test_case import ConfigurationTestCase
from unittests_core.configuration.environment_test_case import EnvironmentTestCase from unittests_core.configuration.environment_test_case import EnvironmentTestCase
from unittests_core.pipes.bool_pipe_test_case import BoolPipeTestCase from unittests_core.pipes.bool_pipe_test_case import BoolPipeTestCase
from unittests_core.pipes.ip_address_pipe_test_case import IPAddressTestCase from unittests_core.pipes.ip_address_pipe_test_case import IPAddressTestCase
@ -16,6 +17,7 @@ class CoreTestSuite(unittest.TestSuite):
loader = unittest.TestLoader() loader = unittest.TestLoader()
tests = [ tests = [
# config # config
ConfigurationTestCase,
EnvironmentTestCase, EnvironmentTestCase,
# pipes # pipes
BoolPipeTestCase, BoolPipeTestCase,

View File

@ -26,7 +26,7 @@ class JSONProcessorTestCase(unittest.TestCase):
"i": 10, "i": 10,
"s": "Hello World", "s": "Hello World",
"d": {"test": "Test"}, "d": {"test": "Test"},
"l": range(0, 11), "l": list(range(0, 11)),
"value": {"value": "Hello World"}, "value": {"value": "Hello World"},
} }
test: TestClass = JSONProcessor.process(TestClass, test_dict) test: TestClass = JSONProcessor.process(TestClass, test_dict)