chore(clan_lib) add api.serde tests for typed_dict

This commit is contained in:
Johannes Kirschbauer
2025-05-21 10:01:27 +02:00
parent ffc82928a7
commit 6c1f8638f5

View File

@@ -1,6 +1,6 @@
from dataclasses import dataclass, field from dataclasses import dataclass, field
from pathlib import Path from pathlib import Path
from typing import Any, Literal from typing import Any, Literal, TypedDict
import pytest import pytest
from clan_cli.machines import machines from clan_cli.machines import machines
@@ -228,6 +228,36 @@ def test_roundtrip_escape() -> None:
assert dataclass_to_dict(from_dict(str, "\\n")) == "\\n" assert dataclass_to_dict(from_dict(str, "\\n")) == "\\n"
def test_roundtrip_typed_dict() -> None:
class Person(TypedDict):
name: str
data = {"name": "John"}
person = Person(name="John")
# Check that the functions are the inverses of each other
# f(g(x)) == x
# and
# g(f(x)) == x
assert from_dict(Person, dataclass_to_dict(person)) == person
assert dataclass_to_dict(from_dict(Person, data)) == person
def test_construct_typed_dict() -> None:
class Person(TypedDict):
name: str
data = {"name": "John"}
person = Person(name="John")
# Check that the from_dict function works with TypedDict
assert from_dict(Person, data) == person
with pytest.raises(ClanError):
# Not a valid value
from_dict(Person, None)
def test_path_field() -> None: def test_path_field() -> None:
@dataclass @dataclass
class Person: class Person: