def test_true(self):
     self.assertTrue(DataclassAdapter.is_item(DataClassItem()))
     self.assertTrue(
         DataclassAdapter.is_item(DataClassItem(name="asdf", value=1234)))
     # field metadata
     self.assertEqual(get_field_meta_from_class(DataClassItem, "name"),
                      MappingProxyType({"serializer": str}))
     self.assertEqual(
         get_field_meta_from_class(DataClassItem, "value"),
         MappingProxyType({"serializer": int}),
     )
     with self.assertRaises(
             KeyError,
             msg="DataClassItem does not support field: non_existent"):
         get_field_meta_from_class(DataClassItem, "non_existent")
Пример #2
0
 def test_repr_dataclass(self):
     item = DataClassItem(name="asdf", value=1234)
     adapter = ItemAdapter(item)
     self.assertEqual(
         repr(adapter),
         "<ItemAdapter for DataClassItem(name='asdf', value=1234)>",
     )
Пример #3
0
 def test_false(self):
     self.assertFalse(ScrapyItemAdapter.is_item(int))
     self.assertFalse(ScrapyItemAdapter.is_item(sum))
     self.assertFalse(ScrapyItemAdapter.is_item(1234))
     self.assertFalse(ScrapyItemAdapter.is_item(object()))
     self.assertFalse(ScrapyItemAdapter.is_item(AttrsItem()))
     self.assertFalse(ScrapyItemAdapter.is_item(DataClassItem()))
     self.assertFalse(ScrapyItemAdapter.is_item(PydanticModel()))
     self.assertFalse(ScrapyItemAdapter.is_item("a string"))
     self.assertFalse(ScrapyItemAdapter.is_item(b"some bytes"))
     self.assertFalse(ScrapyItemAdapter.is_item({"a": "dict"}))
     self.assertFalse(ScrapyItemAdapter.is_item(["a", "list"]))
     self.assertFalse(ScrapyItemAdapter.is_item(("a", "tuple")))
     self.assertFalse(ScrapyItemAdapter.is_item({"a", "set"}))
     self.assertFalse(ScrapyItemAdapter.is_item(ScrapySubclassedItem))
Пример #4
0
 def test_true_dataclass(self):
     self.assertTrue(is_item(DataClassItem(name="asdf", value=1234)))
     self.assertTrue(ItemAdapter.is_item_class(DataClassItem))
Пример #5
0
 def test_true(self):
     self.assertTrue(is_dataclass_instance(DataClassItem()))
     self.assertTrue(
         is_dataclass_instance(DataClassItem(name="asdf", value=1234)))
Пример #6
0
 def test_module_not_available(self):
     self.assertFalse(
         is_dataclass_instance(DataClassItem(name="asdf", value=1234)))
 def test_module_not_available(self):
     self.assertFalse(
         DataclassAdapter.is_item(DataClassItem(name="asdf", value=1234)))
     with self.assertRaises(TypeError,
                            msg="DataClassItem is not a valid item class"):
         get_field_meta_from_class(DataClassItem, "name")