Merge pull request 'Refactor(clan_lib): move serde tests next to serde module' (#3728) from hsjobeki/clan-core:chores-1 into main

Reviewed-on: https://git.clan.lol/clan/clan-core/pulls/3728
This commit is contained in:
hsjobeki
2025-05-21 08:15:11 +00:00
3 changed files with 77 additions and 4 deletions

View File

@@ -1,324 +0,0 @@
from dataclasses import dataclass, field
from pathlib import Path
from typing import Any, Literal
import pytest
from clan_cli.machines import machines
# Functions to test
from clan_lib.api import dataclass_to_dict, from_dict
from clan_lib.errors import ClanError
def test_simple() -> None:
@dataclass
class Person:
name: str
person_dict = {
"name": "John",
}
expected_person = Person(
name="John",
)
assert from_dict(Person, person_dict) == expected_person
def test_nested() -> None:
@dataclass
class Age:
value: str
@dataclass
class Person:
name: str
# deeply nested dataclasses
home: Path | str | None
age: Age
age_list: list[Age]
age_dict: dict[str, Age]
# Optional field
person_dict = {
"name": "John",
"age": {
"value": "99",
},
"age_list": [{"value": "66"}, {"value": "77"}],
"age_dict": {"now": {"value": "55"}, "max": {"value": "100"}},
"home": "/home",
}
expected_person = Person(
name="John",
age=Age("99"),
age_list=[Age("66"), Age("77")],
age_dict={"now": Age("55"), "max": Age("100")},
home=Path("/home"),
)
assert from_dict(Person, person_dict) == expected_person
def test_nested_nullable() -> None:
@dataclass
class SystemConfig:
language: str | None = field(default=None)
keymap: str | None = field(default=None)
ssh_keys_path: list[str] | None = field(default=None)
@dataclass
class FlashOptions:
machine: machines.Machine
mode: str
disks: dict[str, str]
system_config: SystemConfig
dry_run: bool
write_efi_boot_entries: bool
debug: bool
data = {
"machine": {
"name": "flash-installer",
"flake": {"identifier": "git+https://git.clan.lol/clan/clan-core"},
},
"mode": "format",
"disks": {"main": "/dev/sda"},
"system_config": {"language": "en_US.UTF-8", "keymap": "en"},
"dry_run": False,
"write_efi_boot_entries": False,
"debug": False,
"op_key": "jWnTSHwYhSgr7Qz3u4ppD",
}
expected = FlashOptions(
machine=machines.Machine(
name="flash-installer",
flake=machines.Flake("git+https://git.clan.lol/clan/clan-core"),
),
mode="format",
disks={"main": "/dev/sda"},
system_config=SystemConfig(
language="en_US.UTF-8", keymap="en", ssh_keys_path=None
),
dry_run=False,
write_efi_boot_entries=False,
debug=False,
)
assert from_dict(FlashOptions, data) == expected
def test_simple_field_missing() -> None:
@dataclass
class Person:
name: str
person_dict: Any = {}
with pytest.raises(ClanError):
from_dict(Person, person_dict)
def test_nullable() -> None:
@dataclass
class Person:
name: None
person_dict = {
"name": None,
}
from_dict(Person, person_dict)
def test_nullable_non_exist() -> None:
@dataclass
class Person:
name: None
person_dict: Any = {}
with pytest.raises(ClanError):
from_dict(Person, person_dict)
def test_list() -> None:
data = [
{"name": "John"},
{"name": "Sarah"},
]
@dataclass
class Name:
name: str
result = from_dict(list[Name], data)
assert result == [Name("John"), Name("Sarah")]
def test_alias_field() -> None:
@dataclass
class Person:
name: str = field(metadata={"alias": "--user-name--"})
data = {"--user-name--": "John"}
expected = Person(name="John")
person = from_dict(Person, data)
# Deserialize
assert person == expected
# Serialize with alias
assert dataclass_to_dict(person) == data
# Serialize without alias
assert dataclass_to_dict(person, use_alias=False) == {"name": "John"}
def test_alias_field_from_orig_name() -> None:
"""
Field declares an alias. But the data is provided with the field name.
"""
@dataclass
class Person:
name: str = field(metadata={"alias": "--user-name--"})
data = {"user": "John"}
with pytest.raises(ClanError):
from_dict(Person, data)
def test_none_or_string() -> None:
"""
Field declares an alias. But the data is provided with the field name.
"""
data = None
@dataclass
class Person:
name: Path
checked: str | None = from_dict(str | None, data)
assert checked is None
checked2: dict[str, str] | None = from_dict(dict[str, str] | None, data)
assert checked2 is None
checked3: Person | None = from_dict(Person | None, data)
assert checked3 is None
def test_roundtrip_escape() -> None:
assert from_dict(str, "\n") == "\n"
assert dataclass_to_dict("\n") == "\n"
# Test that the functions are inverses of each other
# f(g(x)) == x
# and
# g(f(x)) == x
assert from_dict(str, dataclass_to_dict("\n")) == "\n"
assert dataclass_to_dict(from_dict(str, "\\n")) == "\\n"
def test_path_field() -> None:
@dataclass
class Person:
name: Path
data = {"name": "John"}
expected = Person(name=Path("John"))
assert from_dict(Person, data) == expected
def test_private_public_fields() -> None:
@dataclass
class Person:
name: Path
_name: str | None = None
data = {"name": "John"}
expected = Person(name=Path("John"))
assert from_dict(Person, data) == expected
assert dataclass_to_dict(expected) == data
def test_literal_field() -> None:
@dataclass
class Person:
name: Literal["open_file", "select_folder", "save"]
data = {"name": "open_file"}
expected = Person(name="open_file")
assert from_dict(Person, data) == expected
assert dataclass_to_dict(expected) == data
with pytest.raises(ClanError):
# Not a valid value
from_dict(Person, {"name": "open"})
def test_enum_roundtrip() -> None:
from enum import Enum
class MyEnum(Enum):
FOO = "abc"
BAR = 2
@dataclass
class Person:
name: MyEnum
# Both are equivalent
data = {"name": "abc"} # JSON Representation
expected = Person(name=MyEnum.FOO) # Data representation
assert from_dict(Person, data) == expected
assert dataclass_to_dict(expected) == data
# Same test for integer values
data2 = {"name": 2} # JSON Representation
expected2 = Person(name=MyEnum.BAR) # Data representation
assert from_dict(Person, data2) == expected2
assert dataclass_to_dict(expected2) == data2
# for the test below
# we would import this from the nix_models
class Unknown:
pass
def test_unknown_deserialize() -> None:
@dataclass
class Person:
name: Unknown
data = {"name": ["a", "b"]}
person = from_dict(Person, data)
person.name = ["a", "b"]
def test_unknown_serialize() -> None:
@dataclass
class Person:
name: Unknown
data = Person(["a", "b"]) # type: ignore
person = dataclass_to_dict(data)
assert person == {"name": ["a", "b"]}

View File

@@ -1,139 +0,0 @@
from dataclasses import dataclass, field
# Functions to test
from clan_lib.api import (
dataclass_to_dict,
sanitize_string,
)
#
def test_sanitize_string() -> None:
# Simple strings
assert sanitize_string("Hello World") == "Hello World"
assert sanitize_string("Hello\nWorld") == "Hello\nWorld"
assert sanitize_string("Hello\tWorld") == "Hello\tWorld"
assert sanitize_string("Hello\rWorld") == "Hello\rWorld"
assert sanitize_string("Hello\fWorld") == "Hello\fWorld"
assert sanitize_string("Hello\vWorld") == "Hello\u000bWorld"
assert sanitize_string("Hello\bWorld") == "Hello\bWorld"
assert sanitize_string("Hello\\World") == "Hello\\World"
assert sanitize_string('Hello"World') == 'Hello"World'
assert sanitize_string("Hello'World") == "Hello'World"
assert sanitize_string("Hello\0World") == "Hello\x00World"
# Console escape characters
assert sanitize_string("\033[1mBold\033[0m") == "\033[1mBold\033[0m" # Red
assert sanitize_string("\033[31mRed\033[0m") == "\033[31mRed\033[0m" # Blue
assert sanitize_string("\033[42mGreen\033[0m") == "\033[42mGreen\033[0m" # Green
assert sanitize_string("\033[4mUnderline\033[0m") == "\033[4mUnderline\033[0m"
assert (
sanitize_string("\033[91m\033[1mBold Red\033[0m")
== "\033[91m\033[1mBold Red\033[0m"
)
def test_dataclass_to_dict() -> None:
@dataclass
class Person:
name: str
age: int
person = Person(name="John", age=25)
expected_dict = {"name": "John", "age": 25}
assert dataclass_to_dict(person) == expected_dict
def test_dataclass_to_dict_nested() -> None:
@dataclass
class Address:
city: str = "afghanistan"
zip: str = "01234"
@dataclass
class Person:
name: str
age: int
address: Address = field(default_factory=Address)
person1 = Person(name="John", age=25)
expected_dict1 = {
"name": "John",
"age": 25,
"address": {"city": "afghanistan", "zip": "01234"},
}
# address must be constructed with default values if not passed
assert dataclass_to_dict(person1) == expected_dict1
person2 = Person(name="John", age=25, address=Address(zip="0", city="Anywhere"))
expected_dict2 = {
"name": "John",
"age": 25,
"address": {"zip": "0", "city": "Anywhere"},
}
assert dataclass_to_dict(person2) == expected_dict2
def test_dataclass_to_dict_defaults() -> None:
@dataclass
class Foo:
home: dict[str, str] = field(default_factory=dict)
work: list[str] = field(default_factory=list)
@dataclass
class Person:
name: str = field(default="jon")
age: int = field(default=1)
foo: Foo = field(default_factory=Foo)
default_person = Person()
expected_default = {
"name": "jon",
"age": 1,
"foo": {"home": {}, "work": []},
}
# address must be constructed with default values if not passed
assert dataclass_to_dict(default_person) == expected_default
real_person = Person(name="John", age=25, foo=Foo(home={"a": "b"}, work=["a", "b"]))
expected = {
"name": "John",
"age": 25,
"foo": {"home": {"a": "b"}, "work": ["a", "b"]},
}
assert dataclass_to_dict(real_person) == expected
def test_filters_null_fields() -> None:
@dataclass
class Foo:
home: str | None = None
work: str | None = None
# None fields are filtered out
instance = Foo()
assert instance.home is None
assert dataclass_to_dict(instance) == {}
# fields that are set are not filtered
instance = Foo(home="home")
assert instance.home == "home"
assert instance.work is None
assert dataclass_to_dict(instance) == {"home": "home"}
def test_custom_enum() -> None:
from enum import Enum
class CustomEnum(Enum):
FOO = "foo"
BAR = "bar"
@dataclass
class Foo:
field: CustomEnum
instance = Foo(field=CustomEnum.FOO)
assert dataclass_to_dict(instance) == {"field": "foo"}