Skip to content

Commit a674a30

Browse files
committed
Add tests for test_assert_only_unstructured_passes_for_primitives
1 parent b02f13a commit a674a30

4 files changed

Lines changed: 55 additions & 3 deletions

File tree

tests/asserts.py renamed to tests/helpers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
"""Helpers for assertions."""
1+
"""Helpers for tests."""
22

33
from typing import Any
44

tests/test_converter.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
from cattrs.gen import make_dict_structure_fn, override
3333

3434
from ._compat import is_py310_plus
35-
from .asserts import assert_only_unstructured
35+
from .helpers import assert_only_unstructured
3636
from .typed import (
3737
nested_typed_classes,
3838
simple_typed_attrs,

tests/test_gen_dict.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from cattrs.errors import ClassValidationError, ForbiddenExtraKeysError
1414
from cattrs.gen import make_dict_structure_fn, make_dict_unstructure_fn, override
1515

16-
from .asserts import assert_only_unstructured
16+
from .helpers import assert_only_unstructured
1717
from .typed import nested_typed_classes, simple_typed_classes, simple_typed_dataclasses
1818
from .untyped import nested_classes, simple_classes
1919

tests/test_helpers.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
"""Tests for test helpers."""
2+
3+
import pytest
4+
from attrs import define
5+
6+
from .helpers import assert_only_unstructured
7+
8+
9+
def test_assert_only_unstructured_passes_for_primitives():
10+
"""assert_only_unstructured should pass for basic Python data types."""
11+
# Test primitives
12+
assert_only_unstructured(42)
13+
assert_only_unstructured("hello")
14+
assert_only_unstructured(3.14)
15+
assert_only_unstructured(True)
16+
assert_only_unstructured(None)
17+
18+
# Test collections of primitives
19+
assert_only_unstructured([1, 2, 3])
20+
assert_only_unstructured({"key": "value", "number": 42})
21+
assert_only_unstructured((1, "two", 3.0))
22+
assert_only_unstructured({1, 2, 3})
23+
assert_only_unstructured(frozenset([1, 2, 3]))
24+
25+
# Test nested structures
26+
assert_only_unstructured(
27+
{"list": [1, 2, {"nested": "dict"}], "tuple": (True, None), "number": 42}
28+
)
29+
30+
31+
def test_assert_only_unstructured_fails_for_attrs_classes():
32+
"""assert_only_unstructured should fail for attrs classes."""
33+
34+
@define
35+
class SimpleAttrsClass:
36+
value: int
37+
38+
instance = SimpleAttrsClass(42)
39+
40+
# Should raise AssertionError for attrs class instance
41+
with pytest.raises(AssertionError):
42+
assert_only_unstructured(instance)
43+
44+
# Should also fail when attrs instance is nested in collections
45+
with pytest.raises(AssertionError):
46+
assert_only_unstructured([instance])
47+
48+
with pytest.raises(AssertionError):
49+
assert_only_unstructured({"key": instance})
50+
51+
with pytest.raises(AssertionError):
52+
assert_only_unstructured((1, instance, 3))

0 commit comments

Comments
 (0)