diff --git a/docs/config.png b/docs/config.png index 0c60626b..a5e468d6 100644 Binary files a/docs/config.png and b/docs/config.png differ diff --git a/docs/config.puml b/docs/config.puml index 636d2268..17d14e02 100644 --- a/docs/config.puml +++ b/docs/config.puml @@ -73,7 +73,7 @@ class "PostgreSQLDatabaseConfiguration" as src.models.config.PostgreSQLDatabaseC host : str namespace : Optional[str] password : str - port : int + port : Annotated ssl_mode : str user : str check_postgres_configuration() -> Self @@ -87,9 +87,9 @@ class "ServiceConfiguration" as src.models.config.ServiceConfiguration { color_log : bool cors host : str - port : int + port : Annotated tls_config - workers : int + workers : Annotated check_service_configuration() -> Self } class "TLSConfiguration" as src.models.config.TLSConfiguration { diff --git a/src/models/config.py b/src/models/config.py index bda9699a..5010a7ba 100644 --- a/src/models/config.py +++ b/src/models/config.py @@ -3,7 +3,7 @@ from pathlib import Path from typing import Optional -from pydantic import BaseModel, model_validator, FilePath, AnyHttpUrl +from pydantic import BaseModel, model_validator, FilePath, AnyHttpUrl, PositiveInt from typing_extensions import Self, Literal import constants @@ -58,7 +58,7 @@ class PostgreSQLDatabaseConfiguration(BaseModel): """PostgreSQL database configuration.""" host: str = "localhost" - port: int = 5432 + port: PositiveInt = 5432 db: str user: str password: str @@ -70,8 +70,6 @@ class PostgreSQLDatabaseConfiguration(BaseModel): @model_validator(mode="after") def check_postgres_configuration(self) -> Self: """Check PostgreSQL configuration.""" - if self.port <= 0: - raise ValueError("Port value should not be negative") if self.port > 65535: raise ValueError("Port value should be less than 65536") if self.ca_cert_path is not None and not self.ca_cert_path.exists(): @@ -124,9 +122,9 @@ class ServiceConfiguration(BaseModel): """Service configuration.""" host: str = "localhost" - port: int = 8080 + port: PositiveInt = 8080 auth_enabled: bool = False - workers: int = 1 + workers: PositiveInt = 1 color_log: bool = True access_log: bool = True tls_config: TLSConfiguration = TLSConfiguration() @@ -135,12 +133,8 @@ class ServiceConfiguration(BaseModel): @model_validator(mode="after") def check_service_configuration(self) -> Self: """Check service configuration.""" - if self.port <= 0: - raise ValueError("Port value should not be negative") if self.port > 65535: raise ValueError("Port value should be less than 65536") - if self.workers < 1: - raise ValueError("Workers must be set to at least 1") return self diff --git a/tests/unit/models/test_config.py b/tests/unit/models/test_config.py index 02a976c6..76e64bc9 100644 --- a/tests/unit/models/test_config.py +++ b/tests/unit/models/test_config.py @@ -48,7 +48,7 @@ def test_service_configuration_constructor() -> None: def test_service_configuration_port_value() -> None: """Test the ServiceConfiguration port value validation.""" - with pytest.raises(ValueError, match="Port value should not be negative"): + with pytest.raises(ValidationError, match="Input should be greater than 0"): ServiceConfiguration(port=-1) with pytest.raises(ValueError, match="Port value should be less than 65536"): @@ -57,7 +57,7 @@ def test_service_configuration_port_value() -> None: def test_service_configuration_workers_value() -> None: """Test the ServiceConfiguration workers value validation.""" - with pytest.raises(ValueError, match="Workers must be set to at least 1"): + with pytest.raises(ValidationError, match="Input should be greater than 0"): ServiceConfiguration(workers=-1)