File: test_nested.py

package info (click to toggle)
dataclasses-json 0.6.7-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 568 kB
  • sloc: python: 3,757; makefile: 7
file content (39 lines) | stat: -rw-r--r-- 1,488 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import pytest
from tests.entities import (DataClassMappingBadDecode,
                            DataClassWithDataClass,
                            DataClassWithList,
                            DataClassWithNestedDictWithTupleKeys,
                            DataClassX,
                            DataClassXs)


class TestEncoder:
    def test_nested_dataclass(self):
        assert (DataClassWithDataClass(DataClassWithList([1])).to_json() ==
                '{"dc_with_list": {"xs": [1]}}')

    def test_nested_list_of_dataclasses(self):
        assert (DataClassXs([DataClassX(0), DataClassX(1)]).to_json() ==
                '{"xs": [{"x": 0}, {"x": 1}]}')


class TestDecoder:
    def test_nested_dataclass(self):
        assert (DataClassWithDataClass.from_json(
            '{"dc_with_list": {"xs": [1]}}') ==
                DataClassWithDataClass(DataClassWithList([1])))

    def test_nested_list_of_dataclasses(self):
        assert (DataClassXs.from_json('{"xs": [{"x": 0}, {"x": 1}]}') ==
                DataClassXs([DataClassX(0), DataClassX(1)]))

    def test_nested_mapping_of_dataclasses(self):
        with pytest.raises(TypeError, match="positional arguments"):
            DataClassMappingBadDecode.from_dict(dict(map=dict(test=dict(id="irrelevant"))))


class TestNested:
    def test_tuple_dict_key(self):
        assert (DataClassWithNestedDictWithTupleKeys.from_dict({'a': {(0,): 2}}) ==
                DataClassWithNestedDictWithTupleKeys(a={(0,): 2}))