Skip to content

Commit

Permalink
Add test for dataclass json encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
DCsunset committed Sep 4, 2022
1 parent 1aa1f89 commit ed92464
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions tests/test_jsonable_encoder.py
@@ -1,3 +1,4 @@
from dataclasses import dataclass
from datetime import datetime, timezone
from enum import Enum
from pathlib import PurePath, PurePosixPath, PureWindowsPath
Expand All @@ -19,6 +20,12 @@ def __init__(self, owner: Person, name: str):
self.name = name


@dataclass
class Item:
name: str
count: int


class DictablePerson(Person):
def __iter__(self):
return ((k, v) for k, v in self.__dict__.items())
Expand Down Expand Up @@ -131,6 +138,15 @@ def test_encode_dictable():
}


def test_encode_dataclass():
item = Item(name="foo", count=100)
assert jsonable_encoder(item) == {"name": "foo", "count": 100}
assert jsonable_encoder(item, include={"name"}) == {"name": "foo"}
assert jsonable_encoder(item, exclude={"count"}) == {"name": "foo"}
assert jsonable_encoder(item, include={}) == {}
assert jsonable_encoder(item, exclude={}) == {"name": "foo", "count": 100}


def test_encode_unsupported():
unserializable = Unserializable()
with pytest.raises(ValueError):
Expand Down

0 comments on commit ed92464

Please sign in to comment.