File: test_adapter_dataclasses.py

package info (click to toggle)
python-itemadapter 0.13.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 296 kB
  • sloc: python: 3,384; makefile: 4
file content (66 lines) | stat: -rw-r--r-- 2,373 bytes parent folder | download
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
56
57
58
59
60
61
62
63
64
65
66
from types import MappingProxyType
from unittest import TestCase

import pytest

from itemadapter.utils import get_field_meta_from_class
from tests import (
    AttrsItem,
    DataClassItem,
    PydanticModel,
    PydanticV1Model,
    ScrapyItem,
    ScrapySubclassedItem,
)


class DataclassTestCase(TestCase):
    def test_false(self):
        from itemadapter.adapter import DataclassAdapter

        assert not DataclassAdapter.is_item(int)
        assert not DataclassAdapter.is_item(sum)
        assert not DataclassAdapter.is_item(1234)
        assert not DataclassAdapter.is_item(object())
        assert not DataclassAdapter.is_item("a string")
        assert not DataclassAdapter.is_item(b"some bytes")
        assert not DataclassAdapter.is_item({"a": "dict"})
        assert not DataclassAdapter.is_item(["a", "list"])
        assert not DataclassAdapter.is_item(("a", "tuple"))
        assert not DataclassAdapter.is_item({"a", "set"})
        assert not DataclassAdapter.is_item(DataClassItem)

        try:
            import attrs  # noqa: F401  # pylint: disable=unused-import
        except ImportError:
            pass
        else:
            assert not DataclassAdapter.is_item(AttrsItem())

        if PydanticModel is not None:
            assert not DataclassAdapter.is_item(PydanticModel())
        if PydanticV1Model is not None:
            assert not DataclassAdapter.is_item(PydanticV1Model())

        try:
            import scrapy  # noqa: F401  # pylint: disable=unused-import
        except ImportError:
            pass
        else:
            assert not DataclassAdapter.is_item(ScrapyItem())
            assert not DataclassAdapter.is_item(ScrapySubclassedItem())

    def test_true(self):
        from itemadapter.adapter import DataclassAdapter

        assert DataclassAdapter.is_item(DataClassItem())
        assert DataclassAdapter.is_item(DataClassItem(name="asdf", value=1234))
        # field metadata
        assert get_field_meta_from_class(DataClassItem, "name") == MappingProxyType(
            {"serializer": str}
        )
        assert get_field_meta_from_class(DataClassItem, "value") == MappingProxyType(
            {"serializer": int}
        )
        with pytest.raises(KeyError, match="DataClassItem does not support field: non_existent"):
            get_field_meta_from_class(DataClassItem, "non_existent")