test_abc.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. from collections.abc import Mapping, MutableMapping
  2. import pytest
  3. from multidict import MultiMapping, MutableMultiMapping
  4. from multidict._compat import USE_EXTENSIONS
  5. from multidict._multidict_py import CIMultiDict as PyCIMultiDict
  6. from multidict._multidict_py import CIMultiDictProxy as PyCIMultiDictProxy
  7. from multidict._multidict_py import MultiDict as PyMultiDict # noqa: E402
  8. from multidict._multidict_py import MultiDictProxy as PyMultiDictProxy
  9. if USE_EXTENSIONS:
  10. from multidict._multidict import ( # type: ignore
  11. CIMultiDict,
  12. CIMultiDictProxy,
  13. MultiDict,
  14. MultiDictProxy,
  15. )
  16. @pytest.fixture(
  17. params=([MultiDict, CIMultiDict] if USE_EXTENSIONS else [])
  18. + [PyMultiDict, PyCIMultiDict],
  19. ids=(["MultiDict", "CIMultiDict"] if USE_EXTENSIONS else [])
  20. + ["PyMultiDict", "PyCIMultiDict"],
  21. )
  22. def cls(request):
  23. return request.param
  24. @pytest.fixture(
  25. params=(
  26. [(MultiDictProxy, MultiDict), (CIMultiDictProxy, CIMultiDict)]
  27. if USE_EXTENSIONS
  28. else []
  29. )
  30. + [(PyMultiDictProxy, PyMultiDict), (PyCIMultiDictProxy, PyCIMultiDict)],
  31. ids=(["MultiDictProxy", "CIMultiDictProxy"] if USE_EXTENSIONS else [])
  32. + ["PyMultiDictProxy", "PyCIMultiDictProxy"],
  33. )
  34. def proxy_classes(request):
  35. return request.param
  36. def test_abc_inheritance():
  37. assert issubclass(MultiMapping, Mapping)
  38. assert not issubclass(MultiMapping, MutableMapping)
  39. assert issubclass(MutableMultiMapping, Mapping)
  40. assert issubclass(MutableMultiMapping, MutableMapping)
  41. class A(MultiMapping):
  42. def __getitem__(self, key):
  43. pass
  44. def __iter__(self):
  45. pass
  46. def __len__(self):
  47. pass
  48. def getall(self, key, default=None):
  49. super().getall(key, default)
  50. def getone(self, key, default=None):
  51. super().getone(key, default)
  52. def test_abc_getall():
  53. with pytest.raises(KeyError):
  54. A().getall("key")
  55. def test_abc_getone():
  56. with pytest.raises(KeyError):
  57. A().getone("key")
  58. class B(A, MutableMultiMapping):
  59. def __setitem__(self, key, value):
  60. pass
  61. def __delitem__(self, key):
  62. pass
  63. def add(self, key, value):
  64. super().add(key, value)
  65. def extend(self, *args, **kwargs):
  66. super().extend(*args, **kwargs)
  67. def popall(self, key, default=None):
  68. super().popall(key, default)
  69. def popone(self, key, default=None):
  70. super().popone(key, default)
  71. def test_abc_add():
  72. with pytest.raises(NotImplementedError):
  73. B().add("key", "val")
  74. def test_abc_extend():
  75. with pytest.raises(NotImplementedError):
  76. B().extend()
  77. def test_abc_popone():
  78. with pytest.raises(KeyError):
  79. B().popone("key")
  80. def test_abc_popall():
  81. with pytest.raises(KeyError):
  82. B().popall("key")
  83. def test_multidict_inheritance(cls):
  84. assert issubclass(cls, MultiMapping)
  85. assert issubclass(cls, MutableMultiMapping)
  86. def test_proxy_inheritance(proxy_classes):
  87. proxy, _ = proxy_classes
  88. assert issubclass(proxy, MultiMapping)
  89. assert not issubclass(proxy, MutableMultiMapping)
  90. def test_generic_type_in_runtime():
  91. MultiMapping[str]
  92. MutableMultiMapping[str]