-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Proof of concept of auto pages for easy override
- Loading branch information
Showing
6 changed files
with
220 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
from copy import copy | ||
|
||
import attrs | ||
import pytest | ||
from web_poet import Returns, field | ||
|
||
from zyte_common_items import AutoProductPage, Product | ||
|
||
from .test_items import _PRODUCT_ALL_KWARGS | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_product_unmodified(): | ||
input_product = Product(**_PRODUCT_ALL_KWARGS) | ||
page = AutoProductPage(product=input_product) | ||
assert await page.to_item() == input_product | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_product_modified(): | ||
class CustomProductPage(AutoProductPage): | ||
@field | ||
async def name(self): | ||
return f"{self.product.brand.name} {self.product.name}" | ||
|
||
input_product = Product(**_PRODUCT_ALL_KWARGS) | ||
page = CustomProductPage(product=input_product) | ||
expected_product = copy(input_product) | ||
expected_product.name = f"{input_product.brand.name} {input_product.name}" | ||
assert await page.to_item() == expected_product | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_product_extended(): | ||
@attrs.define | ||
class ExtendedProduct(Product): | ||
foo: str | ||
|
||
class CustomProductPage(AutoProductPage, Returns[ExtendedProduct]): | ||
@field | ||
async def foo(self): | ||
return "bar" | ||
|
||
input_product = Product(**_PRODUCT_ALL_KWARGS) | ||
page = CustomProductPage(product=input_product) | ||
expected_product = ExtendedProduct(**_PRODUCT_ALL_KWARGS, foo="bar") | ||
assert await page.to_item() == expected_product |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters