test_Property.py 1.1 KB
Newer Older
Rolf H. B. van Kleef's avatar
Rolf H. B. van Kleef committed
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 40 41
import unittest

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


class Object(Deserializable):
    name: str

    @validated()
    def name(self, value):
        if len(value) > 10:
            raise TypeError("Maximum name length is 20 characters")

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

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


class TestLists(unittest.TestCase):
    def test_SetWrongTypeShouldFail(self):
        with self.assertRaises(TypeError):
            deserialize(Rule(Object), {
                'name': 8
            })

    def test_SetWrongValueShouldFail(self):
        with self.assertRaises(TypeError):
            deserialize(Rule(Object), {
                'name': 'abcdefghijklmnopqrstuvwxyz'
            })

    def test_SetCorrectValueShouldSucceed(self):
        self.assertEqual(
            Object(name='Rolf'),
            deserialize(Rule(Object), {
               'name': 'Rolf'
            }, try_all=False)
        )