Skip to content

Commit

Permalink
Add additional tests for basic fields
Browse files Browse the repository at this point in the history
  • Loading branch information
subhashb committed Jun 9, 2024
1 parent 353b840 commit efdc1d1
Show file tree
Hide file tree
Showing 7 changed files with 139 additions and 7 deletions.
7 changes: 5 additions & 2 deletions src/protean/fields/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ def __init__(
messages.update(error_messages or {})
self.error_messages = messages

def _generic_param_values_for_repr(self):
def _generic_param_values_for_repr(self) -> list[str]:
"""Return the generic parameter values for the Field's repr"""
values = []
if self.description:
Expand All @@ -117,7 +117,10 @@ def _generic_param_values_for_repr(self):
if callable(self.default):
values.append(f"default={self.default.__name__}")
else:
values.append(f"default='{self.default}'")
if isinstance(self.default, str):
values.append(f"default='{self.default}'")
else:
values.append(f"default={self.default}")
return values

def __repr__(self):
Expand Down
18 changes: 14 additions & 4 deletions src/protean/fields/basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,8 @@ def __repr__(self):
# Generate repr values specific to this field
values = self._generic_param_values_for_repr()
if self.max_value:
values.append(f"max_value={self.max_length}")
if self.min_value:
values.append(f"max_value={self.max_value}")
if self.min_value or self.min_value == 0:
values.append(f"min_value={self.min_value}")

return f"{self.__class__.__name__}(" + ", ".join(values) + ")"
Expand Down Expand Up @@ -173,8 +173,8 @@ def __repr__(self):
# Generate repr values specific to this field
values = self._generic_param_values_for_repr()
if self.max_value:
values.append(f"max_value={self.max_length}")
if self.min_value:
values.append(f"max_value={self.max_value}")
if self.min_value or self.min_value == 0.0:
values.append(f"min_value={self.min_value}")

return f"{self.__class__.__name__}(" + ", ".join(values) + ")"
Expand Down Expand Up @@ -462,6 +462,16 @@ def as_dict(self, value):
"""Return JSON-compatible value of self"""
return value

def __repr__(self):
# Generate repr values specific to this field
values = []
if self.schema_name:
values.append(f"'{self.schema_name}'")

values.extend(self._generic_param_values_for_repr())

return f"{self.__class__.__name__}(" + ", ".join(values) + ")"


class Date(Field):
"""Concrete field implementation for the Date type."""
Expand Down
23 changes: 23 additions & 0 deletions tests/field/test_boolean.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import pytest

from protean.fields.basic import Boolean


@pytest.mark.parametrize(
"value,expected",
[
(True, True),
(False, False),
(1, True),
(0, False),
("t", True),
("f", False),
("True", True),
("False", False),
(1.0, True),
(0.0, False),
],
)
def test_boolean_cast_to_type(value, expected):
field = Boolean()
assert field._cast_to_type(value) == expected
4 changes: 4 additions & 0 deletions tests/field/test_identifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,3 +136,7 @@ def test_that_default_is_picked_from_domain_config(self):
assert identifier._load(uuid_val) == uuid_val
assert identifier.identity_type == IdentityType.UUID.value
assert identifier.as_dict(uuid_val) == str(uuid_val)

def test_invalid_identity_type(self):
with pytest.raises(ValidationError):
Identifier(identity_type="invalid")
21 changes: 21 additions & 0 deletions tests/field/test_method.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from datetime import datetime, timezone

from protean.fields import Method


def utc_now():
return datetime.now(timezone.utc)


def test_method_repr_and_str():
method_obj1 = Method("fake_method")
method_obj2 = Method("fake_method", required=True)
method_obj4 = Method("fake_method", required=True, default=utc_now)

assert repr(method_obj1) == str(method_obj1) == "Method()"
assert repr(method_obj2) == str(method_obj2) == "Method(required=True)"
assert (
repr(method_obj4)
== str(method_obj4)
== "Method(required=True, default=utc_now)"
)
21 changes: 21 additions & 0 deletions tests/field/test_nested.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from protean.fields import Nested


def test_nested_field_repr_and_str():
nested_obj1 = Nested("schema1")
nested_obj2 = Nested("schema1", many=True, required=True)
nested_obj3 = Nested("schema1", default={"name": "John Doe"})
nested_obj4 = Nested("schema1", required=True, default={"name": "John Doe"})

assert repr(nested_obj1) == str(nested_obj1) == "Nested('schema1')"
assert repr(nested_obj2) == str(nested_obj2) == "Nested('schema1', required=True)"
assert (
repr(nested_obj3)
== str(nested_obj3)
== "Nested('schema1', default={'name': 'John Doe'})"
)
assert (
repr(nested_obj4)
== str(nested_obj4)
== "Nested('schema1', required=True, default={'name': 'John Doe'})"
)
52 changes: 51 additions & 1 deletion tests/field/test_repr.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from protean.fields import String, Text
from protean.fields import Auto, Float, Integer, String, Text


def test_identifier_in_repr():
Expand Down Expand Up @@ -85,3 +85,53 @@ def test_text_repr_and_str():
== str(text_obj4)
== "Text(required=True, default='John Doe', sanitize=False)"
)


def test_integer_repr_and_str():
int_obj1 = Integer(required=True)
int_obj2 = Integer(required=True, default=100)
int_obj3 = Integer(required=True, min_value=0, max_value=100)
int_obj4 = Integer(required=True, default=100, min_value=0, max_value=100)

assert repr(int_obj1) == str(int_obj1) == "Integer(required=True)"
assert repr(int_obj2) == str(int_obj2) == "Integer(required=True, default=100)"
assert (
repr(int_obj3)
== str(int_obj3)
== "Integer(required=True, max_value=100, min_value=0)"
)
assert (
repr(int_obj4)
== str(int_obj4)
== "Integer(required=True, default=100, max_value=100, min_value=0)"
)


def test_float_repr_and_str():
float_obj1 = Float(required=True)
float_obj2 = Float(required=True, default=100.0)
float_obj3 = Float(required=True, min_value=0.0, max_value=100.0)
float_obj4 = Float(required=True, default=100.0, min_value=0.0, max_value=100.0)

assert repr(float_obj1) == str(float_obj1) == "Float(required=True)"
assert repr(float_obj2) == str(float_obj2) == "Float(required=True, default=100.0)"
assert (
repr(float_obj3)
== str(float_obj3)
== "Float(required=True, max_value=100.0, min_value=0.0)"
)
assert (
repr(float_obj4)
== str(float_obj4)
== "Float(required=True, default=100.0, max_value=100.0, min_value=0.0)"
)


def test_auto_repr_and_str():
auto_obj1 = Auto()
auto_obj2 = Auto(required=True)
auto_obj3 = Auto(required=True, increment=True)

assert repr(auto_obj1) == str(auto_obj1) == "Auto()"
assert repr(auto_obj2) == str(auto_obj2) == "Auto(required=True)"
assert repr(auto_obj3) == str(auto_obj3) == "Auto(required=True, increment=True)"

0 comments on commit efdc1d1

Please sign in to comment.