test_DirectoryExample.py 4.07 KB
Newer Older
Rolf H. B. van Kleef's avatar
Rolf H. B. van Kleef committed
1
import unittest
2
from typing import List, Optional, Tuple
Rolf H. B. van Kleef's avatar
Rolf H. B. van Kleef committed
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 40 41 42 43 44 45 46 47 48 49

from dict_deserializer.annotations import abstract
from dict_deserializer.deserializer import Deserializable, deserialize, Rule


@abstract
class Object(Deserializable):
    name: str

    def __repr__(self):
        return 'Object(name="{}")'.format(self.name)

    def __eq__(self, other):
        return isinstance(other, Object) and other.name == self.name


class User(Object):
    full_name: str
    calling_name: Optional[str] = 'Unknown'

    def __repr__(self):
        if self.calling_name is None:
            return 'User(super={}, full_name="{}")'\
                .format(super(User, self).__repr__(), self.full_name)
        return 'User(super={}, full_name="{}", calling_name="{}")'\
            .format(super(User, self).__repr__(),
                    self.full_name, self.calling_name)

    def __eq__(self, other):
        return isinstance(other, User) and super(User, self).__eq__(other) and\
            other.full_name == self.full_name and\
            other.calling_name == self.calling_name


class Group(Object):
    members: List[Object]

    def __repr__(self):
        return 'Group(super={}, members=[{}])'\
            .format(super(Group, self).__repr__(),
                    ','.join([m.__repr__() for m in self.members]))

    def __eq__(self, other):
        return isinstance(other, Group) and super(Group, self).__eq__(other)\
               and other.members == self.members


50 51 52 53 54 55 56 57 58 59
class NestedTypingStuff(Deserializable):
    test: List[Tuple[int, int, int]]

    def __str__(self):
        return "NestedTypingStuff(test={})".format(self.test)

    def __eq__(self, other):
        return isinstance(other, NestedTypingStuff) and self.test == other.test


Rolf H. B. van Kleef's avatar
Rolf H. B. van Kleef committed
60 61
class TestLists(unittest.TestCase):
    def test_CorrectDeserializationForNestedWithTypeUnionsAndLists(self):
62
        # noinspection PyArgumentList
Rolf H. B. van Kleef's avatar
Rolf H. B. van Kleef committed
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
        self.assertEqual(
            Group(
                name='IAPC',
                members=[
                    User(name='Rolf', full_name='Rolf van Kleef', calling_name='Unknown'),
                    Group(name='Syscom', members=[
                        User(name='Kevin', full_name='Kevin Alberts', calling_name='Kevin'),
                    ]),
                ],
            ),
            deserialize(Rule(Object), {
                'name': 'IAPC',
                'members': [
                    {'name': 'Rolf', 'full_name': 'Rolf van Kleef'},
                    {'name': 'Syscom', 'members': [
                        {'name': 'Kevin', 'full_name': 'Kevin Alberts', 'calling_name': 'Kevin'},
                    ]},
                ],
            })
        )

    def test_FailDeserializeWithInvalidTypes(self):
        with self.assertRaises(TypeError):
            deserialize(Rule(Object), {
                'name': 'Karel',
                'full_name': 0.0,
            })

        with self.assertRaises(TypeError):
            deserialize(Rule(Object), {
                'name': 'Rolf',
                'full_name': 'Rolf van Kleef',
                'calling_name': False,
            })

    def test_DeserializeIntoAbstract(self):
        with self.assertRaises(TypeError) as ctx:
            deserialize(Rule(Object), {
                'name': 'Test'
            })
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134

    def test_DeserializeTupleCorrect(self):
        # noinspection PyArgumentList
        self.assertEqual(
            NestedTypingStuff(test=[(1, 2, 3)]),
            deserialize(Rule(NestedTypingStuff), {
                "test": [
                    [
                        1, 2, 3
                    ]
                ]
            })
        )

    def test_DeserializeTupleFail(self):
        with self.assertRaises(TypeError) as ctx:
            deserialize(Rule(NestedTypingStuff), {
                "test": [
                    [
                        1, 2
                    ]
                ]
            })

        with self.assertRaises(TypeError) as ctx:
            deserialize(Rule(NestedTypingStuff), {
                "test": [
                    [
                        1, 2, "boo"
                    ]
                ]
            })