1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
|
from __future__ import annotations
from typing import Any
import pytest
from itemloaders import ItemLoader
def _test_item(item: Any) -> None:
il = ItemLoader()
il.add_value("item_list", item)
assert il.load_item() == {"item_list": [item]}
def test_attrs():
try:
import attr # noqa: PLC0415
except ImportError:
pytest.skip("Cannot import attr")
@attr.s
class TestItem:
foo = attr.ib()
_test_item(TestItem(foo="bar"))
def test_dataclass():
try:
from dataclasses import dataclass # noqa: PLC0415
except ImportError:
pytest.skip("Cannot import dataclasses.dataclass")
@dataclass
class TestItem:
foo: str
_test_item(TestItem(foo="bar"))
def test_dict():
_test_item({"foo": "bar"})
def test_scrapy_item():
try:
from scrapy import Field, Item # noqa: PLC0415
except ImportError:
pytest.skip("Cannot import Field or Item from scrapy")
class TestItem(Item):
foo = Field()
_test_item(TestItem(foo="bar"))
|