Skip to content

Commit

Permalink
test setter only
Browse files Browse the repository at this point in the history
  • Loading branch information
PythonFZ committed Oct 17, 2024
1 parent f03adc9 commit e4eb4b4
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions tests/test_znfields.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ def stringify_list(self, name):
class SetterGetterNoInit(znfields.Base):
parameter: float = znfields.field(getter=getter_01, setter=setter_01, init=False)

@dataclasses.dataclass
class SetterOnly(znfields.Base):
parameter: float = znfields.field(setter=setter_01)


@dataclasses.dataclass
class Example1(znfields.Base):
Expand Down Expand Up @@ -75,6 +79,9 @@ def test_wrong_metadata():
with pytest.raises(TypeError):
znfields.field(getter=getter_01, metadata="Hello")

with pytest.raises(TypeError):
znfields.field(setter=setter_01, metadata="Hello")


@dataclasses.dataclass
class Example3(znfields.Base):
Expand Down Expand Up @@ -177,6 +184,10 @@ def test_getter_setter_no_init():
example.parameter = 3.14
assert example.parameter == "parameter:3.14"

# test non-field attributes
example.some_attribute = 42
assert example.some_attribute == 42


@dataclasses.dataclass
class ParentClass(znfields.Base):
Expand Down Expand Up @@ -256,3 +267,10 @@ def test_no_dataclass():

with pytest.raises(TypeError, match="is not a dataclass"):
assert x.parameter is None


def test_setter_only():
x = SetterOnly(parameter=5.5)
with pytest.raises(ValueError):
x.parameter = "5"
assert x.parameter == 5.5

0 comments on commit e4eb4b4

Please sign in to comment.