mirror of
https://github.com/Horizontal42/netsleuth.git
synced 2026-11-11 01:02:10 +03:00
test: add parameterized test for to_jsonable types
Adds a comprehensive test checking all type handling branches inside models.to_jsonable, importantly including support for verifying correct encoding of nested dataclasses. Co-authored-by: Horizontal42 <75383519+Horizontal42@users.noreply.github.com>
This commit is contained in:
parent
8c99ac78b7
commit
e9e9730ce0
1 changed files with 46 additions and 0 deletions
|
|
@ -1,6 +1,10 @@
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
import json
|
import json
|
||||||
|
from dataclasses import dataclass
|
||||||
|
from datetime import date, datetime
|
||||||
|
from enum import Enum
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
|
|
@ -373,3 +377,45 @@ def test_prefix_benchmark_nests_prefix_probes_and_ranks():
|
||||||
out = to_jsonable(bench)
|
out = to_jsonable(bench)
|
||||||
assert out["results"][0]["avg_ms"] == 15.0
|
assert out["results"][0]["avg_ms"] == 15.0
|
||||||
assert out["best"] == "203.0.113.0/24"
|
assert out["best"] == "203.0.113.0/24"
|
||||||
|
|
||||||
|
|
||||||
|
class DummyEnum(Enum):
|
||||||
|
A = "a"
|
||||||
|
B = 2
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class InnerData:
|
||||||
|
x: int
|
||||||
|
y: str
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class OuterData:
|
||||||
|
inner: InnerData
|
||||||
|
z: bool
|
||||||
|
|
||||||
|
|
||||||
|
class CustomObj:
|
||||||
|
def __str__(self):
|
||||||
|
return "custom_str"
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
"input_val, expected_val",
|
||||||
|
[
|
||||||
|
(OuterData(inner=InnerData(x=1, y="2"), z=True), {"inner": {"x": 1, "y": "2"}, "z": True}),
|
||||||
|
(DummyEnum.A, "a"),
|
||||||
|
(DummyEnum.B, 2),
|
||||||
|
(datetime(2023, 1, 1, 12, 0, 0), "2023-01-01T12:00:00"),
|
||||||
|
(date(2023, 1, 1), "2023-01-01"),
|
||||||
|
(Path("/path/to/file.txt"), "/path/to/file.txt"),
|
||||||
|
(b"bytes", "bytes"),
|
||||||
|
(b"\xff\xfe", "\ufffd\ufffd"), # test invalid utf-8 replace
|
||||||
|
({1, 2, 3}, [1, 2, 3]),
|
||||||
|
(frozenset([3, 1, 2]), [1, 2, 3]),
|
||||||
|
(CustomObj(), "custom_str"),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
def test_to_jsonable_handles_additional_types(input_val, expected_val):
|
||||||
|
assert to_jsonable(input_val) == expected_val
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue