Skip to content

Commit aa1b51a

Browse files
Fix Urls serialization
1 parent 125a290 commit aa1b51a

File tree

3 files changed

+49
-9
lines changed

3 files changed

+49
-9
lines changed

packages/common-library/src/common_library/json_serialization.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
from uuid import UUID
2424

2525
import orjson
26-
from pydantic import NameEmail, SecretBytes, SecretStr
26+
from pydantic import AnyHttpUrl, AnyUrl, HttpUrl, NameEmail, SecretBytes, SecretStr
2727
from pydantic_core import Url
2828
from pydantic_extra_types.color import Color
2929

@@ -62,6 +62,8 @@ def decimal_encoder(dec_value: Decimal) -> int | float:
6262

6363

6464
ENCODERS_BY_TYPE: dict[type[Any], Callable[[Any], Any]] = {
65+
AnyHttpUrl: str,
66+
AnyUrl: str,
6567
bytes: lambda o: o.decode(),
6668
Color: str,
6769
datetime.date: isoformat,
@@ -73,6 +75,7 @@ def decimal_encoder(dec_value: Decimal) -> int | float:
7375
frozenset: list,
7476
deque: list,
7577
GeneratorType: list,
78+
HttpUrl: str,
7679
IPv4Address: str,
7780
IPv4Interface: str,
7881
IPv4Network: str,

packages/common-library/tests/test_json_serialization.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
json_loads,
1616
)
1717
from faker import Faker
18-
from pydantic import Field, TypeAdapter
18+
from pydantic import AnyHttpUrl, AnyUrl, BaseModel, Field, HttpUrl, TypeAdapter
1919
from pydantic.json import pydantic_encoder
2020

2121

@@ -95,3 +95,17 @@ def test_compatiblity_with_json_interface(
9595

9696
# NOTE: cannot compare dumps directly because orjson compacts it more
9797
assert json_loads(orjson_dump) == json_loads(json_dump)
98+
99+
100+
def test_serialized_model_with_urls(faker: Faker):
101+
class M(BaseModel):
102+
any_http_url: AnyHttpUrl
103+
any_url: AnyUrl
104+
http_url: HttpUrl
105+
106+
obj = M(
107+
any_http_url=faker.url(),
108+
any_url=faker.url(),
109+
http_url=faker.url(),
110+
)
111+
json_dumps(obj)

services/dynamic-scheduler/tests/unit/test_cli.py

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
# pylint:disable=unused-argument
22

33
import os
4+
import traceback
45

56
import pytest
7+
from click.testing import Result
68
from pytest_simcore.helpers.monkeypatch_envs import load_dotenv, setenvs_from_dict
79
from pytest_simcore.helpers.typing_env import EnvVarsDict
810
from simcore_service_dynamic_scheduler._meta import API_VERSION
@@ -11,20 +13,26 @@
1113
from typer.testing import CliRunner
1214

1315

16+
def _format_cli_error(result: Result) -> str:
17+
assert result.exception
18+
tb_message = "\n".join(traceback.format_tb(result.exception.__traceback__))
19+
return f"Below exception was raised by the cli:\n{tb_message}"
20+
21+
1422
def test_cli_help_and_version(cli_runner: CliRunner):
1523
# simcore-service-dynamic-scheduler --help
1624
result = cli_runner.invoke(cli_main, "--help")
17-
assert result.exit_code == os.EX_OK, result.output
25+
assert result.exit_code == os.EX_OK, _format_cli_error(result)
1826

1927
result = cli_runner.invoke(cli_main, "--version")
20-
assert result.exit_code == os.EX_OK, result.output
28+
assert result.exit_code == os.EX_OK, _format_cli_error(result)
2129
assert result.stdout.strip() == API_VERSION
2230

2331

2432
def test_echo_dotenv(cli_runner: CliRunner, monkeypatch: pytest.MonkeyPatch):
2533
# simcore-service-dynamic-scheduler echo-dotenv
2634
result = cli_runner.invoke(cli_main, "echo-dotenv")
27-
assert result.exit_code == os.EX_OK, result.output
35+
assert result.exit_code == os.EX_OK, _format_cli_error(result)
2836

2937
environs = load_dotenv(result.stdout)
3038

@@ -33,10 +41,25 @@ def test_echo_dotenv(cli_runner: CliRunner, monkeypatch: pytest.MonkeyPatch):
3341
ApplicationSettings.create_from_envs()
3442

3543

36-
def test_list_settings(cli_runner: CliRunner, app_environment: EnvVarsDict):
37-
# simcore-service-dynamic-scheduler settings --show-secrets --as-json
38-
result = cli_runner.invoke(cli_main, ["settings", "--show-secrets", "--as-json"])
39-
assert result.exit_code == os.EX_OK, result.output
44+
def test_list_settings(
45+
cli_runner: CliRunner, app_environment: EnvVarsDict, monkeypatch: pytest.MonkeyPatch
46+
):
47+
with monkeypatch.context() as patch:
48+
setenvs_from_dict(
49+
patch,
50+
{
51+
**app_environment,
52+
"DYNAMIC_SCHEDULER_TRACING": "{}",
53+
"TRACING_OPENTELEMETRY_COLLECTOR_ENDPOINT": "http://replace-with-opentelemetry-collector",
54+
"TRACING_OPENTELEMETRY_COLLECTOR_PORT": "4318",
55+
},
56+
)
57+
58+
# simcore-service-dynamic-scheduler settings --show-secrets --as-json
59+
result = cli_runner.invoke(
60+
cli_main, ["settings", "--show-secrets", "--as-json"]
61+
)
62+
assert result.exit_code == os.EX_OK, _format_cli_error(result)
4063

4164
print(result.output)
4265
settings = ApplicationSettings(result.output)

0 commit comments

Comments
 (0)