chore(clan_lib) add api.serde tests for typed_dict
This commit is contained in:
@@ -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:
|
||||||
|
|||||||
Reference in New Issue
Block a user