Skip to content

Commit 5b0f875

Browse files
committed
add more tests for dataclass utils
1 parent cb6e04d commit 5b0f875

File tree

2 files changed

+77
-3
lines changed

2 files changed

+77
-3
lines changed

robotcode/utils/dataclasses.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -256,8 +256,10 @@ def from_dict(value: Any, types: Union[Type[_T], Tuple[Type[_T], ...], None] = N
256256
)
257257

258258

259-
def from_json(s: Union[str, bytes], types: Union[Type[_T], Tuple[Type[_T], ...], None] = None) -> _T:
260-
return from_dict(json.loads(s), types)
259+
def from_json(
260+
s: Union[str, bytes], types: Union[Type[_T], Tuple[Type[_T], ...], None] = None, /, *, strict: bool = False
261+
) -> _T:
262+
return from_dict(json.loads(s), types, strict=strict)
261263

262264

263265
def as_dict(value: Any) -> Dict[str, Any]:

tests/robotcode/utils/test_dataclasses.py

+73-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# flake8: noqa E501
22
from dataclasses import dataclass
33
from enum import Enum
4-
from typing import Any, Dict, List, Optional, Union
4+
from typing import Any, Dict, List, Literal, Optional, Union
55

66
import pytest
77

@@ -422,6 +422,78 @@ def test_decode_union_with_unknown_keys_should_raise_typeerror() -> None:
422422
) == ComplexItemWithUnionTypeWithSimpleAndComplexTypes(SimpleItem(1, 2))
423423

424424

425+
@pytest.mark.parametrize(
426+
("expr", "type", "expected"),
427+
[
428+
('{"a":1, "b":2, "c":3}', SimpleItem, SimpleItem(1, 2)),
429+
('{"a":1, "b":2, "c":3}', SimpleItemWithOnlyOptionalFields, SimpleItemWithOnlyOptionalFields(1, 2)),
430+
('{"a":1}', SimpleItemWithOnlyOptionalFields, SimpleItemWithOnlyOptionalFields(1)),
431+
("{}", SimpleItemWithOnlyOptionalFields, SimpleItemWithOnlyOptionalFields()),
432+
("{}", SimpleItemWithNoFields, SimpleItemWithNoFields()),
433+
('{"a": 1}', SimpleItemWithNoFields, SimpleItemWithNoFields()),
434+
('{"a":1, "b":2, "c": 3}', (SimpleItemWithNoFields, SimpleItem), SimpleItem(1, 2)),
435+
],
436+
)
437+
def test_decode_non_strict_should_work(expr: Any, type: Any, expected: str) -> None:
438+
assert from_json(expr, type) == expected
439+
440+
441+
@pytest.mark.parametrize(
442+
("expr", "type", "expected"),
443+
[
444+
('{"a":1, "b":2}', SimpleItem, SimpleItem(1, 2)),
445+
('{"a":1, "b":2}', SimpleItemWithOnlyOptionalFields, SimpleItemWithOnlyOptionalFields(1, 2)),
446+
('{"a":1}', SimpleItemWithOnlyOptionalFields, SimpleItemWithOnlyOptionalFields(1)),
447+
("{}", SimpleItemWithOnlyOptionalFields, SimpleItemWithOnlyOptionalFields()),
448+
("{}", SimpleItemWithNoFields, SimpleItemWithNoFields()),
449+
("{}", (SimpleItemWithNoFields, SimpleItem), SimpleItemWithNoFields()),
450+
('{"a":1, "b":2}', (SimpleItemWithNoFields, SimpleItem), SimpleItem(1, 2)),
451+
],
452+
)
453+
def test_decode_strict_should_work(expr: Any, type: Any, expected: str) -> None:
454+
assert from_json(expr, type, strict=True) == expected
455+
456+
457+
@pytest.mark.parametrize(
458+
("expr", "type"),
459+
[
460+
('{"a":1, "b": 2, "c": 3}', SimpleItem),
461+
('{"a":1, "b": 2, "c": 3}', SimpleItemWithOnlyOptionalFields),
462+
('{"a":1, "c": 3}', SimpleItemWithOnlyOptionalFields),
463+
('{"c": 3}', SimpleItemWithOnlyOptionalFields),
464+
('{"c": 3}', SimpleItemWithNoFields),
465+
],
466+
)
467+
def test_decode_strict_with_invalid_data_should_raise_typeerror(expr: Any, type: Any) -> None:
468+
with pytest.raises(TypeError):
469+
from_json(expr, type, strict=True)
470+
471+
472+
@pytest.mark.parametrize(
473+
("expr", "type", "expected"),
474+
[
475+
('"test"', Literal["test", "blah", "bluff"], "test"),
476+
('"bluff"', Literal["test", "blah", "bluff"], "bluff"),
477+
('"dada"', (Literal["test", "blah", "bluff"], str), "dada"),
478+
("1", (Literal["test", "blah", "bluff"], int), 1),
479+
],
480+
)
481+
def test_literal_should_work(expr: Any, type: Any, expected: str) -> None:
482+
assert from_json(expr, type) == expected
483+
484+
485+
@pytest.mark.parametrize(
486+
("expr", "type"),
487+
[
488+
('"dada"', Literal["test", "blah", "bluff"]),
489+
('"dada"', (Literal["test", "blah", "bluff"], int)),
490+
],
491+
)
492+
def test_literal_with_invalid_args_should_raise_typerror(expr: Any, type: Any) -> None:
493+
with pytest.raises(TypeError):
494+
from_json(expr, type)
495+
496+
425497
def test_really_complex_data() -> None:
426498
data = """\
427499
{

0 commit comments

Comments
 (0)