Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more test cases for subclasses using @field #83

Merged
merged 2 commits into from
Jan 25, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions tests/test_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,58 @@ def field3(self):
assert list(get_fields_dict(Page2)) == ["field1", "field3", "field2"]


def test_field_subclassing_from_to_item() -> None:
# to_item() should be the same since it was not overridden from the
# subclass.
class PageToItem(ItemPage):
def to_item(self):
return {"field1": 1, "field2": 2, "field3": 3, "field4": 4}

class Page1(PageToItem):
@field
def field1(self):
return 0

page_1 = Page1()
assert page_1.field1 == 0
assert page_1.to_item() == {"field1": 1, "field2": 2, "field3": 3, "field4": 4}

# to_item() only reflects the field that was decorated.
class Page2(PageToItem):
@field
def field2(self):
return 0

def to_item(self):
return item_from_fields_sync(self)

page_2 = Page2()
assert page_2.field2 == 0
assert page_2.to_item() == {"field2": 0}

# to_item() raises an error if there are some required fields from the item_cls
# that doesn't have a corresponding field value.
@attrs.define
class SomeItem:
field1: int
field2: int
field3: int
field4: int

class Page3(PageToItem):
@field
def field3(self):
return 0

def to_item(self):
return item_from_fields_sync(self, item_cls=SomeItem)

page_3 = Page3()
assert page_3.field3 == 0
with pytest.raises(TypeError):
page_3.to_item()


def test_field_with_other_decorators() -> None:
def clean_str(method):
def wrapper(*args, **kwargs):
Expand Down