def test_true(self): self.assertTrue(ScrapyItemAdapter.is_item(ScrapyItem())) self.assertTrue(ScrapyItemAdapter.is_item(ScrapySubclassedItem())) self.assertTrue( ScrapyItemAdapter.is_item( ScrapySubclassedItem(name="asdf", value=1234))) # field metadata self.assertEqual( get_field_meta_from_class(ScrapySubclassedItem, "name"), MappingProxyType({"serializer": str}), ) self.assertEqual( get_field_meta_from_class(ScrapySubclassedItem, "value"), MappingProxyType({"serializer": int}), )
def test_repr_scrapy_item(self): item = ScrapySubclassedItem(name="asdf", value=1234) adapter = ItemAdapter(item) # Scrapy fields are stored in a dict, which is not guarantied to be sorted in py35 self.assertTrue( repr(adapter) == "<ItemAdapter for ScrapySubclassedItem(name='asdf', value=1234)>" or repr(adapter) == "<ItemAdapter for ScrapySubclassedItem(value=1234, name='asdf')>", )
def test_module_not_available(self): self.assertFalse( ScrapyItemAdapter.is_item( ScrapySubclassedItem(name="asdf", value=1234))) with self.assertRaises( TypeError, msg="ScrapySubclassedItem is not a valid item class"): get_field_meta_from_class(ScrapySubclassedItem, "name")
def test_module_import_error(self): with clear_itemadapter_imports(): from itemadapter.adapter import ScrapyItemAdapter self.assertFalse( ScrapyItemAdapter.is_item( ScrapySubclassedItem(name="asdf", value=1234))) with self.assertRaises( TypeError, msg="ScrapySubclassedItem is not a valid item class"): get_field_meta_from_class(ScrapySubclassedItem, "name")
def test_false(self): self.assertFalse(is_attrs_instance(int)) self.assertFalse(is_attrs_instance(sum)) self.assertFalse(is_attrs_instance(1234)) self.assertFalse(is_attrs_instance(object())) self.assertFalse(is_attrs_instance(ScrapyItem())) self.assertFalse(is_attrs_instance(ScrapySubclassedItem())) self.assertFalse(is_attrs_instance("a string")) self.assertFalse(is_attrs_instance(b"some bytes")) self.assertFalse(is_attrs_instance({"a": "dict"})) self.assertFalse(is_attrs_instance(["a", "list"])) self.assertFalse(is_attrs_instance(("a", "tuple"))) self.assertFalse(is_attrs_instance({"a", "set"})) self.assertFalse(is_attrs_instance(AttrsItem))
def test_false(self): self.assertFalse(PydanticAdapter.is_item(int)) self.assertFalse(PydanticAdapter.is_item(sum)) self.assertFalse(PydanticAdapter.is_item(1234)) self.assertFalse(PydanticAdapter.is_item(object())) self.assertFalse(PydanticAdapter.is_item(ScrapyItem())) self.assertFalse(PydanticAdapter.is_item(AttrsItem())) self.assertFalse(PydanticAdapter.is_item(DataClassItem())) self.assertFalse(PydanticAdapter.is_item(ScrapySubclassedItem())) self.assertFalse(PydanticAdapter.is_item("a string")) self.assertFalse(PydanticAdapter.is_item(b"some bytes")) self.assertFalse(PydanticAdapter.is_item({"a": "dict"})) self.assertFalse(PydanticAdapter.is_item(["a", "list"])) self.assertFalse(PydanticAdapter.is_item(("a", "tuple"))) self.assertFalse(PydanticAdapter.is_item({"a", "set"})) self.assertFalse(PydanticAdapter.is_item(PydanticModel))
def test_true_scrapy(self): self.assertTrue(is_item(ScrapyItem())) self.assertTrue(is_item(ScrapySubclassedItem(name="asdf", value=1234))) self.assertTrue(ItemAdapter.is_item_class(ScrapyItem)) self.assertTrue(ItemAdapter.is_item_class(ScrapySubclassedItem))
def test_true(self): self.assertTrue(is_scrapy_item(ScrapyItem())) self.assertTrue(is_scrapy_item(ScrapySubclassedItem())) self.assertTrue( is_scrapy_item(ScrapySubclassedItem(name="asdf", value=1234)))
def test_module_not_available(self): self.assertFalse( is_scrapy_item(ScrapySubclassedItem(name="asdf", value=1234)))
def test_repr_scrapy_item(self): item = ScrapySubclassedItem(name="asdf", value=1234) adapter = ItemAdapter(item) self.assertEqual( repr(adapter), "<ItemAdapter for ScrapySubclassedItem(name='asdf', value=1234)>")