-
Notifications
You must be signed in to change notification settings - Fork 154
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
67 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,83 @@ | ||
from dataclasses import dataclass | ||
from datetime import datetime | ||
from typing import Generic, TypeVar | ||
|
||
import pytest | ||
|
||
from dataclasses_json import dataclass_json | ||
|
||
S = TypeVar("S") | ||
T = TypeVar("T") | ||
|
||
|
||
@dataclass_json | ||
@dataclass | ||
class NestedClass(Generic[T]): | ||
value: T | ||
class Bar: | ||
value: int | ||
|
||
|
||
@dataclass_json | ||
@dataclass | ||
class Foo(Generic[T]): | ||
bar: T | ||
|
||
|
||
@dataclass_json | ||
@dataclass | ||
class Baz(Generic[T]): | ||
foo: Foo[T] | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"instance_of_t, decodes_successfully", | ||
[ | ||
pytest.param(1, True, id="literal"), | ||
pytest.param([1], True, id="literal_list"), | ||
pytest.param({"a": 1}, True, id="map_of_literal"), | ||
pytest.param(datetime(2021, 1, 1), False, id="extended_type"), | ||
pytest.param(Bar(1), False, id="object"), | ||
] | ||
) | ||
def test_dataclass_with_generic_dataclass_field(instance_of_t, decodes_successfully): | ||
foo = Foo(bar=instance_of_t) | ||
baz = Baz(foo=foo) | ||
decoded = Baz[type(instance_of_t)].from_json(baz.to_json()) | ||
assert decoded.foo == Foo.from_json(foo.to_json()) | ||
if decodes_successfully: | ||
assert decoded == baz | ||
else: | ||
assert decoded != baz | ||
|
||
|
||
@dataclass_json | ||
@dataclass | ||
class Foo2(Generic[T, S]): | ||
bar1: T | ||
bar2: S | ||
|
||
|
||
@dataclass_json | ||
@dataclass | ||
class MyClass(Generic[T]): | ||
nested: NestedClass[T] | ||
class Baz2(Generic[T, S]): | ||
foo2: Foo2[T, S] | ||
|
||
|
||
def test_dataclass_with_generic_dataclass_field(): | ||
a = MyClass(nested=NestedClass(value="value")) | ||
assert MyClass.from_json(a.to_json()) == a | ||
@pytest.mark.parametrize( | ||
"instance_of_t, decodes_successfully", | ||
[ | ||
pytest.param(1, True, id="literal"), | ||
pytest.param([1], True, id="literal_list"), | ||
pytest.param({"a": 1}, True, id="map_of_literal"), | ||
pytest.param(datetime(2021, 1, 1), False, id="extended_type"), | ||
pytest.param(Bar(1), False, id="object"), | ||
] | ||
) | ||
def test_dataclass_with_multiple_generic_dataclass_fields(instance_of_t, decodes_successfully): | ||
foo2 = Foo2(bar1=instance_of_t, bar2=instance_of_t) | ||
baz = Baz2(foo2=foo2) | ||
decoded = Baz2[type(instance_of_t), type(instance_of_t)].from_json(baz.to_json()) | ||
assert decoded.foo2 == Foo2.from_json(foo2.to_json()) | ||
if decodes_successfully: | ||
assert decoded == baz | ||
else: | ||
assert decoded != baz |