|
| 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