Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

๐Ÿ› Fix *Urls serialization #6852

Merged
merged 8 commits into from
Nov 28, 2024
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
from uuid import UUID

import orjson
from pydantic import NameEmail, SecretBytes, SecretStr
from pydantic import AnyHttpUrl, AnyUrl, HttpUrl, NameEmail, SecretBytes, SecretStr
from pydantic_core import Url
from pydantic_extra_types.color import Color

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


ENCODERS_BY_TYPE: dict[type[Any], Callable[[Any], Any]] = {
AnyHttpUrl: str,
AnyUrl: str,
giancarloromeo marked this conversation as resolved.
Show resolved Hide resolved
bytes: lambda o: o.decode(),
Color: str,
datetime.date: isoformat,
Expand All @@ -73,6 +75,7 @@ def decimal_encoder(dec_value: Decimal) -> int | float:
frozenset: list,
deque: list,
GeneratorType: list,
HttpUrl: str,
IPv4Address: str,
IPv4Interface: str,
IPv4Network: str,
Expand Down
17 changes: 16 additions & 1 deletion packages/common-library/tests/test_json_serialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
json_loads,
)
from faker import Faker
from pydantic import Field, TypeAdapter
from pydantic import AnyHttpUrl, AnyUrl, BaseModel, Field, HttpUrl, TypeAdapter
from pydantic.json import pydantic_encoder


Expand Down Expand Up @@ -95,3 +95,18 @@ def test_compatiblity_with_json_interface(

# NOTE: cannot compare dumps directly because orjson compacts it more
assert json_loads(orjson_dump) == json_loads(json_dump)


def test_serialized_model_with_urls(faker: Faker):
giancarloromeo marked this conversation as resolved.
Show resolved Hide resolved
# See: https://github.com/ITISFoundation/osparc-simcore/pull/6852
class M(BaseModel):
any_http_url: AnyHttpUrl
any_url: AnyUrl
http_url: HttpUrl

obj = M(
any_http_url=faker.url(),
any_url=faker.url(),
http_url=faker.url(),
)
json_dumps(obj)
giancarloromeo marked this conversation as resolved.
Show resolved Hide resolved
37 changes: 30 additions & 7 deletions services/dynamic-scheduler/tests/unit/test_cli.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
# pylint:disable=unused-argument

import os
import traceback

import pytest
from click.testing import Result
from pytest_simcore.helpers.monkeypatch_envs import load_dotenv, setenvs_from_dict
from pytest_simcore.helpers.typing_env import EnvVarsDict
from simcore_service_dynamic_scheduler._meta import API_VERSION
Expand All @@ -11,20 +13,26 @@
from typer.testing import CliRunner


def _format_cli_error(result: Result) -> str:
assert result.exception
tb_message = "\n".join(traceback.format_tb(result.exception.__traceback__))
return f"Below exception was raised by the cli:\n{tb_message}"


def test_cli_help_and_version(cli_runner: CliRunner):
# simcore-service-dynamic-scheduler --help
result = cli_runner.invoke(cli_main, "--help")
assert result.exit_code == os.EX_OK, result.output
assert result.exit_code == os.EX_OK, _format_cli_error(result)

result = cli_runner.invoke(cli_main, "--version")
assert result.exit_code == os.EX_OK, result.output
assert result.exit_code == os.EX_OK, _format_cli_error(result)
assert result.stdout.strip() == API_VERSION


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

environs = load_dotenv(result.stdout)

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


def test_list_settings(cli_runner: CliRunner, app_environment: EnvVarsDict):
# simcore-service-dynamic-scheduler settings --show-secrets --as-json
result = cli_runner.invoke(cli_main, ["settings", "--show-secrets", "--as-json"])
assert result.exit_code == os.EX_OK, result.output
def test_list_settings(
cli_runner: CliRunner, app_environment: EnvVarsDict, monkeypatch: pytest.MonkeyPatch
):
with monkeypatch.context() as patch:
setenvs_from_dict(
patch,
{
**app_environment,
"DYNAMIC_SCHEDULER_TRACING": "{}",
"TRACING_OPENTELEMETRY_COLLECTOR_ENDPOINT": "http://replace-with-opentelemetry-collector",
"TRACING_OPENTELEMETRY_COLLECTOR_PORT": "4318",
},
)

# simcore-service-dynamic-scheduler settings --show-secrets --as-json
result = cli_runner.invoke(
cli_main, ["settings", "--show-secrets", "--as-json"]
)
assert result.exit_code == os.EX_OK, _format_cli_error(result)

print(result.output)
settings = ApplicationSettings(result.output)
Expand Down
Loading