Skip to content

Commit c38b064

Browse files
authored
Fixes [unit] webserver 03 and [unit] settings-library (both mypy and tests) (#6773)
1 parent 66a492f commit c38b064

File tree

8 files changed

+77
-89
lines changed

8 files changed

+77
-89
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
from enum import StrEnum
2+
3+
4+
class LogLevel(StrEnum):
5+
DEBUG = "DEBUG"
6+
INFO = "INFO"
7+
WARNING = "WARNING"
8+
ERROR = "ERROR"
9+
10+
11+
class BootModeEnum(StrEnum):
12+
"""
13+
Values taken by SC_BOOT_MODE environment variable
14+
set in Dockerfile and used during docker/boot.sh
15+
"""
16+
17+
DEFAULT = "default"
18+
LOCAL = "local-development"
19+
DEBUG = "debug"
20+
PRODUCTION = "production"
21+
DEVELOPMENT = "development"
22+
23+
def is_devel_mode(self) -> bool:
24+
"""returns True if this boot mode is used for development"""
25+
return self in (self.DEBUG, self.DEVELOPMENT, self.LOCAL)
26+
27+
28+
class BuildTargetEnum(StrEnum):
29+
"""
30+
Values taken by SC_BUILD_TARGET environment variable
31+
set in Dockerfile that defines the stage targeted in the
32+
docker image build
33+
"""
34+
35+
BUILD = "build"
36+
CACHE = "cache"
37+
PRODUCTION = "production"
38+
DEVELOPMENT = "development"

packages/models-library/src/models_library/basic_types.py

Lines changed: 13 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
from decimal import Decimal
2-
from enum import StrEnum
2+
from enum import Enum
33
from re import Pattern
44
from typing import Annotated, ClassVar, Final, TypeAlias
55

6+
from common_library.basic_types import BootModeEnum, BuildTargetEnum, LogLevel
67
from pydantic import Field, HttpUrl, PositiveInt, StringConstraints
78
from pydantic_core import core_schema
89

@@ -13,6 +14,17 @@
1314
UUID_RE,
1415
)
1516

17+
assert issubclass(LogLevel, Enum) # nosec
18+
assert issubclass(BootModeEnum, Enum) # nosec
19+
assert issubclass(BuildTargetEnum, Enum) # nosec
20+
21+
__all__: tuple[str, ...] = (
22+
"LogLevel",
23+
"BootModeEnum",
24+
"BuildTargetEnum",
25+
)
26+
27+
1628
NonNegativeDecimal: TypeAlias = Annotated[Decimal, Field(ge=0)]
1729

1830
PositiveDecimal: TypeAlias = Annotated[Decimal, Field(gt=0)]
@@ -145,41 +157,4 @@ class HttpUrlWithCustomMinLength(HttpUrl):
145157
min_length = 0
146158

147159

148-
class LogLevel(StrEnum):
149-
DEBUG = "DEBUG"
150-
INFO = "INFO"
151-
WARNING = "WARNING"
152-
ERROR = "ERROR"
153-
154-
155-
class BootModeEnum(StrEnum):
156-
"""
157-
Values taken by SC_BOOT_MODE environment variable
158-
set in Dockerfile and used during docker/boot.sh
159-
"""
160-
161-
DEFAULT = "default"
162-
LOCAL = "local-development"
163-
DEBUG = "debug"
164-
PRODUCTION = "production"
165-
DEVELOPMENT = "development"
166-
167-
def is_devel_mode(self) -> bool:
168-
"""returns True if this boot mode is used for development"""
169-
return self in (self.DEBUG, self.DEVELOPMENT, self.LOCAL)
170-
171-
172-
class BuildTargetEnum(StrEnum):
173-
"""
174-
Values taken by SC_BUILD_TARGET environment variable
175-
set in Dockerfile that defines the stage targeted in the
176-
docker image build
177-
"""
178-
179-
BUILD = "build"
180-
CACHE = "cache"
181-
PRODUCTION = "production"
182-
DEVELOPMENT = "development"
183-
184-
185160
KeyIDStr = Annotated[str, StringConstraints(pattern=PROPERTY_KEY_RE)]

packages/settings-library/src/settings_library/application.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
from models_library.basic_types import BootModeEnum
21
from pydantic import Field, PositiveInt
32

43
from .base import BaseCustomSettings
5-
from .basic_types import BuildTargetEnum
4+
from .basic_types import BootModeEnum, BuildTargetEnum
65

76

87
class BaseApplicationSettings(BaseCustomSettings):

packages/settings-library/src/settings_library/base.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ def _is_auto_default_from_env_enabled(field: FieldInfo) -> bool:
5858
)
5959

6060

61-
ENABLED: Final = {}
61+
_MARKED_AS_UNSET: Final[dict] = {}
6262

6363

6464
class EnvSettingsWithAutoDefaultSource(EnvSettingsSource):
@@ -89,7 +89,7 @@ def prepare_field_value(
8989
_is_auto_default_from_env_enabled(field)
9090
and field.default_factory
9191
and field.default is None
92-
and prepared_value == ENABLED
92+
and prepared_value == _MARKED_AS_UNSET
9393
):
9494
prepared_value = field.default_factory()
9595
return prepared_value
Lines changed: 14 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,31 @@
1-
#
2-
# NOTE: This file copies some of the types from models_library.basic_types
3-
# This is a minor evil to avoid the maintenance burden that creates
4-
# an extra dependency to a larger models_library (intra-repo library)
5-
6-
from enum import StrEnum
1+
from enum import Enum
72
from typing import Annotated, TypeAlias
83

4+
from common_library.basic_types import BootModeEnum, BuildTargetEnum, LogLevel
95
from pydantic import Field, StringConstraints
106

7+
assert issubclass(LogLevel, Enum) # nosec
8+
assert issubclass(BootModeEnum, Enum) # nosec
9+
assert issubclass(BuildTargetEnum, Enum) # nosec
10+
11+
__all__: tuple[str, ...] = (
12+
"LogLevel",
13+
"BootModeEnum",
14+
"BuildTargetEnum",
15+
)
16+
17+
1118
# port number range
1219
PortInt: TypeAlias = Annotated[int, Field(gt=0, lt=65535)]
20+
RegisteredPortInt: TypeAlias = Annotated[int, Field(gt=1024, lt=65535)]
1321

1422

1523
# e.g. 'v5'
1624
VersionTag: TypeAlias = Annotated[str, StringConstraints(pattern=r"^v\d$")]
1725

1826

19-
class LogLevel(StrEnum):
20-
DEBUG = "DEBUG"
21-
INFO = "INFO"
22-
WARNING = "WARNING"
23-
ERROR = "ERROR"
24-
25-
26-
class BootMode(StrEnum):
27-
"""
28-
Values taken by SC_BOOT_MODE environment variable
29-
set in Dockerfile and used during docker/boot.sh
30-
"""
31-
32-
DEFAULT = "default"
33-
LOCAL = "local-development"
34-
DEBUG = "debug"
35-
PRODUCTION = "production"
36-
DEVELOPMENT = "development"
37-
38-
39-
class BuildTargetEnum(StrEnum):
40-
"""
41-
Values taken by SC_BUILD_TARGET environment variable
42-
set in Dockerfile that defines the stage targeted in the
43-
docker image build
44-
"""
45-
46-
BUILD = "build"
47-
CACHE = "cache"
48-
PRODUCTION = "production"
49-
DEVELOPMENT = "development"
50-
51-
5227
# non-empty bounded string used as identifier
5328
# e.g. "123" or "name_123" or "fa327c73-52d8-462a-9267-84eeaf0f90e3" but NOT ""
5429
IDStr: TypeAlias = Annotated[
5530
str, StringConstraints(strip_whitespace=True, min_length=1, max_length=50)
5631
]
57-
RegisteredPortInt: TypeAlias = Annotated[int, Field(gt=1024, lt=65535)]

packages/settings-library/tests/test_utils_logging.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from pydantic import AliasChoices, Field, field_validator
44
from settings_library.base import BaseCustomSettings
5-
from settings_library.basic_types import BootMode
5+
from settings_library.basic_types import BootModeEnum
66
from settings_library.utils_logging import MixinLoggingSettings
77

88

@@ -14,7 +14,7 @@ def test_mixin_logging(monkeypatch):
1414

1515
class Settings(BaseCustomSettings, MixinLoggingSettings):
1616
# DOCKER
17-
SC_BOOT_MODE: BootMode | None = None
17+
SC_BOOT_MODE: BootModeEnum | None = None
1818

1919
# LOGGING
2020
LOG_LEVEL: str = Field(

services/web/server/tests/unit/with_dbs/03/resource_usage/test_usage_services__export.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,5 +115,7 @@ async def test_list_service_usage(
115115
assert mock_export_usage_services.called
116116
args = mock_export_usage_services.call_args[1]
117117

118-
assert args["order_by"] == OrderBy.model_validate(_order_by)
118+
assert (
119+
args["order_by"].model_dump() == OrderBy.model_validate(_order_by).model_dump()
120+
)
119121
assert args["filters"] == ServiceResourceUsagesFilters.model_validate(_filter)

services/web/server/tests/unit/with_dbs/03/resource_usage/test_usage_services__list.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ async def test_list_service_usage_with_order_by_query_param(
221221
_, error = await assert_status(resp, status.HTTP_422_UNPROCESSABLE_ENTITY)
222222
assert mock_list_usage_services.called
223223
assert error["status"] == status.HTTP_422_UNPROCESSABLE_ENTITY
224-
assert error["errors"][0]["message"].startswith("Invalid JSON")
224+
assert "Invalid JSON" in error["errors"][0]["message"]
225225

226226
# with order by without direction
227227
_filter = {"field": "started_at"}
@@ -249,7 +249,7 @@ async def test_list_service_usage_with_order_by_query_param(
249249
errors = {(e["code"], e["field"]) for e in error["errors"]}
250250
assert {
251251
("value_error", "order_by.field"),
252-
("type_error.enum", "order_by.direction"),
252+
("enum", "order_by.direction"),
253253
} == errors
254254
assert len(errors) == 2
255255

@@ -264,8 +264,8 @@ async def test_list_service_usage_with_order_by_query_param(
264264
_, error = await assert_status(resp, status.HTTP_422_UNPROCESSABLE_ENTITY)
265265
assert mock_list_usage_services.called
266266
assert error["status"] == status.HTTP_422_UNPROCESSABLE_ENTITY
267-
assert error["errors"][0]["message"].startswith("field required")
268-
assert error["errors"][0]["code"] == "value_error.missing"
267+
assert error["errors"][0]["message"].startswith("Field required")
268+
assert error["errors"][0]["code"] == "missing"
269269
assert error["errors"][0]["field"] == "order_by.field"
270270

271271

0 commit comments

Comments
 (0)