-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_impl_marshaller.py
53 lines (41 loc) · 1.62 KB
/
test_impl_marshaller.py
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
42
43
44
45
46
47
48
49
50
51
52
53
from abc import abstractmethod, ABC
from dataclasses import dataclass
from typing import List
from unittest import TestCase
from injecty import create_injecty_context
from marshy.factory.impl_marshaller_factory import ImplMarshallerFactory
from marshy.marshy_context import create_marshy_context
@dataclass
class PetAbc(ABC):
name: str
@abstractmethod
def vocalize(self) -> str:
"""What sound does this make?"""
class Cat(PetAbc):
def vocalize(self):
return "Meow!"
class Dog(PetAbc):
def vocalize(self) -> str:
return "Woof!"
class TestImplMarshaller(TestCase):
def test_marshall(self):
injecty_context = create_injecty_context()
injecty_context.register_impls(PetAbc, [Cat, Dog])
context = create_marshy_context(injecty_context=injecty_context)
pet = Cat("Felix")
dumped = context.dump(pet, PetAbc)
assert dumped == ["Cat", dict(name="Felix")]
loaded = context.load(PetAbc, dumped)
assert pet == loaded
assert loaded.vocalize() == "Meow!"
def test_marshall_nested(self):
injecty_context = create_injecty_context()
injecty_context.register_impls(PetAbc, [Cat, Dog])
context = create_marshy_context(injecty_context=injecty_context)
pets = [Cat("Felix"), Dog("Rover")]
dumped = context.dump(pets, List[PetAbc])
assert dumped == [["Cat", dict(name="Felix")], ["Dog", dict(name="Rover")]]
loaded = context.load(List[PetAbc], dumped)
assert pets == loaded
vocalizations = [p.vocalize() for p in loaded]
assert ["Meow!", "Woof!"] == vocalizations