test_abc.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. from collections.abc import Mapping, MutableMapping
  2. import pytest
  3. from multidict import MultiMapping, MutableMultiMapping
  4. def test_abc_inheritance():
  5. assert issubclass(MultiMapping, Mapping)
  6. assert not issubclass(MultiMapping, MutableMapping)
  7. assert issubclass(MutableMultiMapping, Mapping)
  8. assert issubclass(MutableMultiMapping, MutableMapping)
  9. class A(MultiMapping):
  10. def __getitem__(self, key):
  11. pass
  12. def __iter__(self):
  13. pass
  14. def __len__(self):
  15. pass
  16. def getall(self, key, default=None):
  17. super().getall(key, default)
  18. def getone(self, key, default=None):
  19. super().getone(key, default)
  20. def test_abc_getall():
  21. with pytest.raises(KeyError):
  22. A().getall("key")
  23. def test_abc_getone():
  24. with pytest.raises(KeyError):
  25. A().getone("key")
  26. class B(A, MutableMultiMapping):
  27. def __setitem__(self, key, value):
  28. pass
  29. def __delitem__(self, key):
  30. pass
  31. def add(self, key, value):
  32. super().add(key, value)
  33. def extend(self, *args, **kwargs):
  34. super().extend(*args, **kwargs)
  35. def popall(self, key, default=None):
  36. super().popall(key, default)
  37. def popone(self, key, default=None):
  38. super().popone(key, default)
  39. def test_abc_add():
  40. with pytest.raises(NotImplementedError):
  41. B().add("key", "val")
  42. def test_abc_extend():
  43. with pytest.raises(NotImplementedError):
  44. B().extend()
  45. def test_abc_popone():
  46. with pytest.raises(KeyError):
  47. B().popone("key")
  48. def test_abc_popall():
  49. with pytest.raises(KeyError):
  50. B().popall("key")
  51. def test_multidict_inheritance(any_multidict_class):
  52. assert issubclass(any_multidict_class, MultiMapping)
  53. assert issubclass(any_multidict_class, MutableMultiMapping)
  54. def test_proxy_inheritance(any_multidict_proxy_class):
  55. assert issubclass(any_multidict_proxy_class, MultiMapping)
  56. assert not issubclass(any_multidict_proxy_class, MutableMultiMapping)
  57. def test_generic_type_in_runtime():
  58. MultiMapping[str]
  59. MutableMultiMapping[str]