|
| 1 | +"""Pyyaml-specific tests.""" |
| 2 | +from datetime import date, datetime, timezone |
| 3 | + |
| 4 | +from attrs import define |
| 5 | +from hypothesis import given |
| 6 | +from pytest import raises |
| 7 | + |
| 8 | +from cattrs._compat import FrozenSetSubscriptable |
| 9 | +from cattrs.errors import ClassValidationError |
| 10 | +from cattrs.preconf.pyyaml import make_converter |
| 11 | + |
| 12 | +from .._compat import is_py38 |
| 13 | +from ..test_preconf import Everything, everythings, native_unions |
| 14 | + |
| 15 | + |
| 16 | +@given(everythings()) |
| 17 | +def test_pyyaml(everything: Everything): |
| 18 | + from yaml import safe_dump, safe_load |
| 19 | + |
| 20 | + converter = make_converter() |
| 21 | + unstructured = converter.unstructure(everything) |
| 22 | + raw = safe_dump(unstructured) |
| 23 | + assert converter.structure(safe_load(raw), Everything) == everything |
| 24 | + |
| 25 | + |
| 26 | +@given(everythings()) |
| 27 | +def test_pyyaml_converter(everything: Everything): |
| 28 | + converter = make_converter() |
| 29 | + raw = converter.dumps(everything) |
| 30 | + assert converter.loads(raw, Everything) == everything |
| 31 | + |
| 32 | + |
| 33 | +@given(everythings()) |
| 34 | +def test_pyyaml_converter_unstruct_collection_overrides(everything: Everything): |
| 35 | + converter = make_converter( |
| 36 | + unstruct_collection_overrides={FrozenSetSubscriptable: sorted} |
| 37 | + ) |
| 38 | + raw = converter.unstructure(everything) |
| 39 | + assert raw["a_frozenset"] == sorted(raw["a_frozenset"]) |
| 40 | + |
| 41 | + |
| 42 | +@given( |
| 43 | + union_and_val=native_unions(include_bools=not is_py38), # Literal issues on 3.8 |
| 44 | + detailed_validation=..., |
| 45 | +) |
| 46 | +def test_pyyaml_unions(union_and_val: tuple, detailed_validation: bool): |
| 47 | + """Native union passthrough works.""" |
| 48 | + converter = make_converter(detailed_validation=detailed_validation) |
| 49 | + type, val = union_and_val |
| 50 | + |
| 51 | + assert converter.structure(val, type) == val |
| 52 | + |
| 53 | + |
| 54 | +@given(detailed_validation=...) |
| 55 | +def test_pyyaml_dates(detailed_validation: bool): |
| 56 | + """Pyyaml dates work.""" |
| 57 | + converter = make_converter(detailed_validation=detailed_validation) |
| 58 | + |
| 59 | + @define |
| 60 | + class A: |
| 61 | + datetime: datetime |
| 62 | + date: date |
| 63 | + |
| 64 | + data = """ |
| 65 | + datetime: 1970-01-01T00:00:00Z |
| 66 | + date: 1970-01-01""" |
| 67 | + assert converter.loads(data, A) == A( |
| 68 | + datetime(1970, 1, 1, tzinfo=timezone.utc), date(1970, 1, 1) |
| 69 | + ) |
| 70 | + |
| 71 | + bad_data = """ |
| 72 | + datetime: 1 |
| 73 | + date: 1 |
| 74 | + """ |
| 75 | + |
| 76 | + with raises(ClassValidationError if detailed_validation else Exception) as exc_info: |
| 77 | + converter.loads(bad_data, A) |
| 78 | + |
| 79 | + if detailed_validation: |
| 80 | + assert ( |
| 81 | + repr(exc_info.value.exceptions[0]) |
| 82 | + == "Exception('Expected datetime, got 1')" |
| 83 | + ) |
| 84 | + assert ( |
| 85 | + repr(exc_info.value.exceptions[1]) == "ValueError('Expected date, got 1')" |
| 86 | + ) |
0 commit comments