Skip to content

Commit 45eb299

Browse files
authored
Merge pull request #423 from tisnik/lcore-574-unit-tests-for-postgres-configuration
LCORE-574: Unit tests for PostgreSQL configuration + removed unused check
2 parents 19cc14d + 96c341c commit 45eb299

File tree

2 files changed

+63
-2
lines changed

2 files changed

+63
-2
lines changed

src/models/config.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,6 @@ def check_postgres_configuration(self) -> Self:
7272
"""Check PostgreSQL configuration."""
7373
if self.port > 65535:
7474
raise ValueError("Port value should be less than 65536")
75-
if self.ca_cert_path is not None and not self.ca_cert_path.exists():
76-
raise ValueError(f"CA certificate file does not exist: {self.ca_cert_path}")
7775
return self
7876

7977

tests/unit/models/test_config.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
AUTH_MOD_NOOP,
1212
AUTH_MOD_K8S,
1313
AUTH_MOD_JWK_TOKEN,
14+
POSTGRES_DEFAULT_SSL_MODE,
15+
POSTGRES_DEFAULT_GSS_ENCMODE,
1416
)
1517

1618
from models.config import (
@@ -24,6 +26,7 @@
2426
CORSConfiguration,
2527
ModelContextProtocolServer,
2628
InferenceConfiguration,
29+
PostgreSQLDatabaseConfiguration,
2730
)
2831

2932
from utils.checks import InvalidConfigurationError
@@ -867,3 +870,63 @@ def test_authentication_configuration_module_unsupported() -> None:
867870
k8s_ca_cert_path=None,
868871
k8s_cluster_api=None,
869872
)
873+
874+
875+
def test_postgresql_database_configuration() -> None:
876+
"""Test the PostgreSQLDatabaseConfiguration model."""
877+
c = PostgreSQLDatabaseConfiguration(db="db", user="user", password="password")
878+
assert c is not None
879+
assert c.host == "localhost"
880+
assert c.port == 5432
881+
assert c.db == "db"
882+
assert c.user == "user"
883+
assert c.password == "password"
884+
assert c.ssl_mode == POSTGRES_DEFAULT_SSL_MODE
885+
assert c.gss_encmode == POSTGRES_DEFAULT_GSS_ENCMODE
886+
assert c.namespace == "lightspeed-stack"
887+
assert c.ca_cert_path is None
888+
889+
890+
def test_postgresql_database_configuration_port_setting(subtests) -> None:
891+
"""Test the PostgreSQLDatabaseConfiguration model."""
892+
with subtests.test(msg="Correct port value"):
893+
c = PostgreSQLDatabaseConfiguration(
894+
db="db", user="user", password="password", port=1234
895+
)
896+
assert c is not None
897+
assert c.port == 1234
898+
899+
with subtests.test(msg="Negative port value"):
900+
with pytest.raises(ValidationError, match="Input should be greater than 0"):
901+
PostgreSQLDatabaseConfiguration(
902+
db="db", user="user", password="password", port=-1
903+
)
904+
905+
with subtests.test(msg="Too big port value"):
906+
with pytest.raises(ValueError, match="Port value should be less than 65536"):
907+
PostgreSQLDatabaseConfiguration(
908+
db="db", user="user", password="password", port=100000
909+
)
910+
911+
912+
def test_postgresql_database_configuration_ca_cert_path(subtests) -> None:
913+
"""Test the PostgreSQLDatabaseConfiguration model."""
914+
with subtests.test(msg="Path exists"):
915+
c = PostgreSQLDatabaseConfiguration(
916+
db="db",
917+
user="user",
918+
password="password",
919+
port=1234,
920+
ca_cert_path=Path("tests/configuration/server.crt"),
921+
)
922+
assert c.ca_cert_path == Path("tests/configuration/server.crt")
923+
924+
with subtests.test(msg="Path does not exist"):
925+
with pytest.raises(ValidationError, match="Path does not point to a file"):
926+
PostgreSQLDatabaseConfiguration(
927+
db="db",
928+
user="user",
929+
password="password",
930+
port=1234,
931+
ca_cert_path=Path("not a file"),
932+
)

0 commit comments

Comments
 (0)