Skip to content

Commit

Permalink
Make the fields of Auto- classes sync (#75)
Browse files Browse the repository at this point in the history
  • Loading branch information
Gallaecio authored Feb 5, 2024
1 parent f812da2 commit ff3a4c2
Show file tree
Hide file tree
Showing 3 changed files with 167 additions and 159 deletions.
4 changes: 2 additions & 2 deletions docs/usage/pages.rst
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,11 @@ extra fields. For example:
class ExtendedProductPage(AutoProductPage, Returns[ExtendedProduct]):
@field
async def name(self):
def name(self):
return f"{self.product.brand.name} {self.product.name}"
@field
async def foo(self):
def foo(self):
return "bar"
.. _extractors:
Expand Down
18 changes: 13 additions & 5 deletions tests/test_pages_auto.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import attrs
import pytest
from itemadapter import ItemAdapter
from web_poet import RequestUrl, Returns, field

from zyte_common_items import (
Expand Down Expand Up @@ -79,6 +80,13 @@
)


async def assert_expected_item(page, item):
assert await page.to_item() == item
# Ensure that all fields are sync.
for k, v in ItemAdapter(item).items():
assert getattr(page, k) == v


@pytest.mark.parametrize(*PARAMS)
@pytest.mark.asyncio
async def test_unmodified(
Expand All @@ -90,7 +98,7 @@ async def test_unmodified(
"request_url": RequestUrl("https://example.com"),
}
page = cls(**kwargs)
assert await page.to_item() == item
await assert_expected_item(page, item)


@pytest.mark.parametrize(*PARAMS)
Expand All @@ -102,7 +110,7 @@ async def test_modified(

class CustomPage(cls):
@field
async def url(self):
def url(self):
return modified_url

item = item_cls(**item_kwargs)
Expand All @@ -113,7 +121,7 @@ async def url(self):
page = CustomPage(**kwargs)
expected_item = copy(item)
expected_item.url = modified_url
assert await page.to_item() == expected_item
await assert_expected_item(page, expected_item)


@pytest.mark.parametrize(*PARAMS)
Expand All @@ -127,7 +135,7 @@ class ExtendedItem(item_cls):

class ExtendedPage(cls, Returns[ExtendedItem]):
@field
async def foo(self):
def foo(self):
return "bar"

kwargs = {
Expand All @@ -136,4 +144,4 @@ async def foo(self):
}
page = ExtendedPage(**kwargs)
expected_item = ExtendedItem(**item_kwargs, foo="bar")
assert await page.to_item() == expected_item
await assert_expected_item(page, expected_item)
Loading

0 comments on commit ff3a4c2

Please sign in to comment.