|
| 1 | +"""Test dataclasses_json handling of Literal types.""" |
| 2 | +import sys |
| 3 | +import pytest |
| 4 | + |
| 5 | +if sys.version_info < (3, 8): |
| 6 | + pytest.skip("Literal types are only supported in Python 3.8+", allow_module_level=True) |
| 7 | + |
| 8 | +import json |
| 9 | +from typing import Literal, Optional, List, Dict |
| 10 | + |
| 11 | +from dataclasses import dataclass |
| 12 | + |
| 13 | +from dataclasses_json import dataclass_json, DataClassJsonMixin |
| 14 | +from marshmallow.exceptions import ValidationError # type: ignore |
| 15 | + |
| 16 | + |
| 17 | +@dataclass_json |
| 18 | +@dataclass |
| 19 | +class DataClassWithLiteral(DataClassJsonMixin): |
| 20 | + numeric_literals: Literal[0, 1] |
| 21 | + string_literals: Literal["one", "two", "three"] |
| 22 | + mixed_literals: Literal[0, "one", 2] |
| 23 | + |
| 24 | + |
| 25 | +with_valid_literal_json = '{"numeric_literals": 0, "string_literals": "one", "mixed_literals": 2}' |
| 26 | +with_valid_literal_data = DataClassWithLiteral(numeric_literals=0, string_literals="one", mixed_literals=2) |
| 27 | +with_invalid_literal_json = '{"numeric_literals": 9, "string_literals": "four", "mixed_literals": []}' |
| 28 | +with_invalid_literal_data = DataClassWithLiteral(numeric_literals=9, string_literals="four", mixed_literals=[]) # type: ignore |
| 29 | + |
| 30 | +@dataclass_json |
| 31 | +@dataclass |
| 32 | +class DataClassWithNestedLiteral(DataClassJsonMixin): |
| 33 | + list_of_literals: List[Literal[0, 1]] |
| 34 | + dict_of_literals: Dict[Literal["one", "two", "three"], Literal[0, 1]] |
| 35 | + optional_literal: Optional[Literal[0, 1]] |
| 36 | + |
| 37 | +with_valid_nested_literal_json = '{"list_of_literals": [0, 1], "dict_of_literals": {"one": 0, "two": 1}, "optional_literal": 1}' |
| 38 | +with_valid_nested_literal_data = DataClassWithNestedLiteral(list_of_literals=[0, 1], dict_of_literals={"one": 0, "two": 1}, optional_literal=1) |
| 39 | +with_invalid_nested_literal_json = '{"list_of_literals": [0, 2], "dict_of_literals": {"one": 0, "four": 2}, "optional_literal": 2}' |
| 40 | +with_invalid_nested_literal_data = DataClassWithNestedLiteral(list_of_literals=[0, 2], dict_of_literals={"one": 0, "four": 2}, optional_literal=2) # type: ignore |
| 41 | + |
| 42 | +class TestEncoder: |
| 43 | + def test_valid_literal(self): |
| 44 | + assert with_valid_literal_data.to_dict(encode_json=True) == json.loads(with_valid_literal_json) |
| 45 | + |
| 46 | + def test_invalid_literal(self): |
| 47 | + assert with_invalid_literal_data.to_dict(encode_json=True) == json.loads(with_invalid_literal_json) |
| 48 | + |
| 49 | + def test_valid_nested_literal(self): |
| 50 | + assert with_valid_nested_literal_data.to_dict(encode_json=True) == json.loads(with_valid_nested_literal_json) |
| 51 | + |
| 52 | + def test_invalid_nested_literal(self): |
| 53 | + assert with_invalid_nested_literal_data.to_dict(encode_json=True) == json.loads(with_invalid_nested_literal_json) |
| 54 | + |
| 55 | + |
| 56 | +class TestSchemaEncoder: |
| 57 | + def test_valid_literal(self): |
| 58 | + actual = DataClassWithLiteral.schema().dumps(with_valid_literal_data) |
| 59 | + assert json.loads(actual) == json.loads(with_valid_literal_json) |
| 60 | + |
| 61 | + def test_invalid_literal(self): |
| 62 | + actual = DataClassWithLiteral.schema().dumps(with_invalid_literal_data) |
| 63 | + assert json.loads(actual) == json.loads(with_invalid_literal_json) |
| 64 | + |
| 65 | + def test_valid_nested_literal(self): |
| 66 | + actual = DataClassWithNestedLiteral.schema().dumps(with_valid_nested_literal_data) |
| 67 | + assert json.loads(actual) == json.loads(with_valid_nested_literal_json) |
| 68 | + |
| 69 | + def test_invalid_nested_literal(self): |
| 70 | + actual = DataClassWithNestedLiteral.schema().dumps(with_invalid_nested_literal_data) |
| 71 | + assert json.loads(actual) == json.loads(with_invalid_nested_literal_json) |
| 72 | + |
| 73 | +class TestDecoder: |
| 74 | + def test_valid_literal(self): |
| 75 | + actual = DataClassWithLiteral.from_json(with_valid_literal_json) |
| 76 | + assert actual == with_valid_literal_data |
| 77 | + |
| 78 | + def test_invalid_literal(self): |
| 79 | + expected = DataClassWithLiteral(numeric_literals=9, string_literals="four", mixed_literals=[]) # type: ignore |
| 80 | + actual = DataClassWithLiteral.from_json(with_invalid_literal_json) |
| 81 | + assert actual == expected |
| 82 | + |
| 83 | + |
| 84 | +class TestSchemaDecoder: |
| 85 | + def test_valid_literal(self): |
| 86 | + actual = DataClassWithLiteral.schema().loads(with_valid_literal_json) |
| 87 | + assert actual == with_valid_literal_data |
| 88 | + |
| 89 | + def test_invalid_literal(self): |
| 90 | + with pytest.raises(ValidationError): |
| 91 | + DataClassWithLiteral.schema().loads(with_invalid_literal_json) |
| 92 | + |
| 93 | + def test_valid_nested_literal(self): |
| 94 | + actual = DataClassWithNestedLiteral.schema().loads(with_valid_nested_literal_json) |
| 95 | + assert actual == with_valid_nested_literal_data |
| 96 | + |
| 97 | + def test_invalid_nested_literal(self): |
| 98 | + with pytest.raises(ValidationError): |
| 99 | + DataClassWithNestedLiteral.schema().loads(with_invalid_nested_literal_json) |
0 commit comments