Skip to content

Commit

Permalink
Add TypedList field
Browse files Browse the repository at this point in the history
  • Loading branch information
Tim020 committed Mar 23, 2024
1 parent b56d8ab commit 06483e0
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 17 deletions.
18 changes: 18 additions & 0 deletions src/couch_potato/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,21 @@ def deserialize(self, value):
ret = datetime.fromisoformat(value)
self.ensure_type(ret)
return ret


class TypedArray(Field):
__type__ = list

def __init__(
self, item_type: Field, nullable: bool = True, read_only: bool = False
):
self.item_type = item_type
super().__init__(nullable=nullable, read_only=read_only)

def serialize(self, value: list):
self.ensure_type(value)
return [self.item_type.serialize(item) for item in value]

def deserialize(self, value):
self.ensure_type(value)
return [self.item_type.deserialize(item) for item in value]
93 changes: 76 additions & 17 deletions tests/test_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,46 +4,105 @@
import pytest

from src.couch_potato._types import Field
from src.couch_potato.fields import Float, String, Integer, Boolean, DateTime
from src.couch_potato.fields import (
Float,
String,
Integer,
Boolean,
DateTime,
TypedArray,
)


@pytest.mark.parametrize(
"field_class, value, expected_result",
"field, value, expected_result",
[
pytest.param(Integer, 1, 1, id="Integer"),
pytest.param(Float, 1.0, 1.0, id="Float"),
pytest.param(String, "foobar", "foobar", id="String"),
pytest.param(Boolean, True, True, id="Boolean"),
pytest.param(Integer(), 1, 1, id="Integer"),
pytest.param(Float(), 1.0, 1.0, id="Float"),
pytest.param(String(), "foobar", "foobar", id="String"),
pytest.param(Boolean(), True, True, id="Boolean"),
pytest.param(
DateTime,
DateTime(),
datetime(2024, 3, 23, 0, 0, 0),
"2024-03-23T00:00:00",
id="DateTime",
),
pytest.param(
TypedArray(Integer()), [1, 2, 3], [1, 2, 3], id="TypedArray[Integer]"
),
pytest.param(
TypedArray(Float()),
[1.0, 2.0, 3.0],
[1.0, 2.0, 3.0],
id="TypedArray[Float]",
),
pytest.param(
TypedArray(String()),
["foo", "bar"],
["foo", "bar"],
id="TypedArray[String]",
),
pytest.param(
TypedArray(Boolean()),
[True, False],
[True, False],
id="TypedArray[Boolean]",
),
pytest.param(
TypedArray(DateTime()),
[datetime(2024, 3, 23, 0, 0, 0), datetime(2024, 3, 23, 1, 0, 0)],
["2024-03-23T00:00:00", "2024-03-23T01:00:00"],
id="TypedArray[Boolean]",
),
],
)
def test_field_serialize(field_class: Type[Field], value: Any, expected_result: Any):
field = field_class()
def test_field_serialize(field: Field, value: Any, expected_result: Any):
serialized_value = field.serialize(value)
assert serialized_value == expected_result


@pytest.mark.parametrize(
"field_class, value, expected_result",
"field, value, expected_result",
[
pytest.param(Integer, 1, 1, id="Integer"),
pytest.param(Float, 1.0, 1.0, id="Float"),
pytest.param(String, "foobar", "foobar", id="String"),
pytest.param(Boolean, True, True, id="Boolean"),
pytest.param(Integer(), 1, 1, id="Integer"),
pytest.param(Float(), 1.0, 1.0, id="Float"),
pytest.param(String(), "foobar", "foobar", id="String"),
pytest.param(Boolean(), True, True, id="Boolean"),
pytest.param(
DateTime,
DateTime(),
"2024-03-23T00:00:00",
datetime(2024, 3, 23, 0, 0, 0),
id="DateTime",
),
pytest.param(
TypedArray(Integer()), [1, 2, 3], [1, 2, 3], id="TypedArray[Integer]"
),
pytest.param(
TypedArray(Float()),
[1.0, 2.0, 3.0],
[1.0, 2.0, 3.0],
id="TypedArray[Float]",
),
pytest.param(
TypedArray(String()),
["foo", "bar"],
["foo", "bar"],
id="TypedArray[String]",
),
pytest.param(
TypedArray(Boolean()),
[True, False],
[True, False],
id="TypedArray[Boolean]",
),
pytest.param(
TypedArray(DateTime()),
["2024-03-23T00:00:00", "2024-03-23T01:00:00"],
[datetime(2024, 3, 23, 0, 0, 0), datetime(2024, 3, 23, 1, 0, 0)],
id="TypedArray[Boolean]",
),
],
)
def test_field_deserialize(field_class: Type[Field], value: Any, expected_result: Any):
field = field_class()
def test_field_deserialize(field: Field, value: Any, expected_result: Any):
serialized_value = field.deserialize(value)
assert serialized_value == expected_result

0 comments on commit 06483e0

Please sign in to comment.