-
Notifications
You must be signed in to change notification settings - Fork 155
/
test_str_subclass.py
32 lines (22 loc) · 1023 Bytes
/
test_str_subclass.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
from dataclasses import dataclass
from dataclasses_json import DataClassJsonMixin
class MyStr(str):
def is_even_length(self) -> bool:
return len(self) % 2 == 0
@dataclass(frozen=True)
class DataClassWithStrSubclass(DataClassJsonMixin):
any_str: str
my_str: MyStr
class TestDataClassWithStrSubclass:
def test_encode__no_instantiation_required(self):
model_dict = {"any_str": "str", "my_str": MyStr("str")}
expected = DataClassWithStrSubclass(any_str="str", my_str=MyStr("str"))
actual = DataClassWithStrSubclass.from_dict(model_dict)
assert expected == actual
assert model_dict["my_str"] is actual.my_str
def test_encode__subclass_str_instantiated(self):
model_dict = {"any_str": "str", "my_str": "str"}
expected = DataClassWithStrSubclass(any_str="str", my_str=MyStr("str"))
actual = DataClassWithStrSubclass.from_dict(model_dict)
assert expected == actual
assert model_dict["my_str"] is not actual.my_str