From a12f89e31839bd4c4e456c0379d6cc3e134020ca Mon Sep 17 00:00:00 2001 From: Pavel Tisnovsky Date: Wed, 3 Sep 2025 11:39:31 +0200 Subject: [PATCH 1/2] LCORE-574: database configuration unit tests --- tests/unit/models/test_config.py | 61 +++++++++++++++++++++++++++++--- 1 file changed, 57 insertions(+), 4 deletions(-) diff --git a/tests/unit/models/test_config.py b/tests/unit/models/test_config.py index 78944726..82519cef 100644 --- a/tests/unit/models/test_config.py +++ b/tests/unit/models/test_config.py @@ -27,6 +27,8 @@ ModelContextProtocolServer, InferenceConfiguration, PostgreSQLDatabaseConfiguration, + SQLiteDatabaseConfiguration, + DatabaseConfiguration, ) from utils.checks import InvalidConfigurationError @@ -503,9 +505,9 @@ def test_configuration_multiple_mcp_servers() -> None: def test_dump_configuration(tmp_path) -> None: """ - Test that the Configuration object can be serialized to a JSON - file and that the resulting file contains all expected sections - and values. + Test that the Configuration object can be serialized to a JSON file and + that the resulting file contains all expected sections and values. It also + checks if all keys and passwords were redacted. """ cfg = Configuration( name="test_name", @@ -525,6 +527,7 @@ def test_dump_configuration(tmp_path) -> None: llama_stack=LlamaStackConfiguration( use_as_library_client=True, library_client_config_path="tests/configuration/run.yaml", + api_key="whatever", ), user_data_collection=UserDataCollection( feedback_enabled=False, feedback_storage=None @@ -593,7 +596,7 @@ def test_dump_configuration(tmp_path) -> None: }, "llama_stack": { "url": None, - "api_key": None, + "api_key": "whatever", "use_as_library_client": True, "library_client_config_path": "tests/configuration/run.yaml", }, @@ -874,6 +877,56 @@ def test_authentication_configuration_module_unsupported() -> None: ) +def test_database_configuration(subtests) -> None: + """Test the database configuration handling.""" + with subtests.test(msg="PostgresSQL"): + d1 = PostgreSQLDatabaseConfiguration( + db="db", + user="user", + password="password", + port=1234, + ca_cert_path=Path("tests/configuration/server.crt"), + ) + d = DatabaseConfiguration(postgres=d1) + assert d is not None + assert d.sqlite is None + assert d.postgres is not None + assert d.db_type == "postgres" + + with subtests.test(msg="SQLite"): + d1 = SQLiteDatabaseConfiguration( + db_path="/tmp/foo/bar/baz", + ) + d = DatabaseConfiguration(sqlite=d1) + assert d is not None + assert d.sqlite is not None + assert d.postgres is None + assert d.db_type == "sqlite" + + +def test_no_databases_configuration() -> None: + """Test if no databases configuration is checked.""" + d = DatabaseConfiguration() + assert d is not None + + # simulate no DB configuration + d.sqlite = None + d.postgres = None + + with pytest.raises(ValueError, match="No database configuration found"): + d.db_type + + +def test_two_databases_configuration() -> None: + """Test if two databases configuration is checked.""" + d1 = PostgreSQLDatabaseConfiguration(db="db", user="user", password="password") + d2 = SQLiteDatabaseConfiguration(db_path="foo_bar_baz") + with pytest.raises( + ValidationError, match="Only one database configuration can be provided" + ): + DatabaseConfiguration(postgres=d1, sqlite=d2) + + def test_postgresql_database_configuration() -> None: """Test the PostgreSQLDatabaseConfiguration model.""" c = PostgreSQLDatabaseConfiguration(db="db", user="user", password="password") From 8d4fc820283917e469f9e9face3b2972cc037406 Mon Sep 17 00:00:00 2001 From: Pavel Tisnovsky Date: Wed, 3 Sep 2025 11:52:32 +0200 Subject: [PATCH 2/2] Minor fixes --- tests/unit/models/test_config.py | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/tests/unit/models/test_config.py b/tests/unit/models/test_config.py index 82519cef..3c34a6eb 100644 --- a/tests/unit/models/test_config.py +++ b/tests/unit/models/test_config.py @@ -15,6 +15,8 @@ POSTGRES_DEFAULT_GSS_ENCMODE, ) +from utils.checks import InvalidConfigurationError + from models.config import ( AuthenticationConfiguration, Configuration, @@ -31,8 +33,6 @@ DatabaseConfiguration, ) -from utils.checks import InvalidConfigurationError - def test_service_configuration_constructor() -> None: """ @@ -506,8 +506,9 @@ def test_configuration_multiple_mcp_servers() -> None: def test_dump_configuration(tmp_path) -> None: """ Test that the Configuration object can be serialized to a JSON file and - that the resulting file contains all expected sections and values. It also - checks if all keys and passwords were redacted. + that the resulting file contains all expected sections and values. + + Please note that redaction process is not in place. """ cfg = Configuration( name="test_name", @@ -879,7 +880,7 @@ def test_authentication_configuration_module_unsupported() -> None: def test_database_configuration(subtests) -> None: """Test the database configuration handling.""" - with subtests.test(msg="PostgresSQL"): + with subtests.test(msg="PostgreSQL"): d1 = PostgreSQLDatabaseConfiguration( db="db", user="user", @@ -909,12 +910,16 @@ def test_no_databases_configuration() -> None: d = DatabaseConfiguration() assert d is not None + # default should be SQLite when nothing is provided + assert d.db_type == "sqlite" + # simulate no DB configuration d.sqlite = None d.postgres = None with pytest.raises(ValueError, match="No database configuration found"): - d.db_type + # access propery to call it's getter + _ = d.db_type def test_two_databases_configuration() -> None: