Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test coverage for _multidict_base.py #936

Merged
merged 13 commits into from
Feb 1, 2024
9 changes: 9 additions & 0 deletions CHANGES/936.contrib.rst
a5r0n marked this conversation as resolved.
Show resolved Hide resolved
a5r0n marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Added test coverage for the :ref:`and <python:and>`, :ref:`or
<python:or>`, :py:obj:`sub <python:object.__sub__>`, and
:py:obj:`xor <python:object.__xor__>` operators in the
:file:`multidict/_multidict_base.py` module. It also covers
:py:data:`NotImplemented` and
":py:class:`~typing.Iterable`-but-not-:py:class:`~typing.Set`"
cases there.

-- by :user:`a5r0n`
77 changes: 77 additions & 0 deletions tests/test_multidict.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
Dict,
Iterable,
Iterator,
KeysView,
List,
Mapping,
Set,
Expand Down Expand Up @@ -387,6 +388,27 @@ def test_and2(self, cls: Type[MutableMultiMapping[str]]) -> None:

assert {"key"} == {"key", "key2"} & d.keys()

def test_bitwise_and_not_implemented(
self, cls: Type[MutableMultiMapping[str]]
) -> None:
d = cls([("key", "value1")])

sentinel_operation_result = object()

class RightOperand:
def __rand__(self, other: KeysView[str]) -> object:
assert isinstance(other, KeysView)
return sentinel_operation_result

assert d.keys() & RightOperand() is sentinel_operation_result

def test_bitwise_and_iterable_not_set(
self, cls: Type[MutableMultiMapping[str]]
) -> None:
d = cls([("key", "value1")])

assert {"key"} == d.keys() & ["key", "key2"]

def test_or(self, cls: Type[MutableMultiMapping[str]]) -> None:
d = cls([("key", "value1")])

Expand All @@ -397,6 +419,27 @@ def test_or2(self, cls: Type[MutableMultiMapping[str]]) -> None:

assert {"key", "key2"} == {"key2"} | d.keys()

def test_bitwise_or_not_implemented(
self, cls: Type[MutableMultiMapping[str]]
) -> None:
d = cls([("key", "value1")])

sentinel_operation_result = object()

class RightOperand:
def __ror__(self, other: KeysView[str]) -> object:
assert isinstance(other, KeysView)
return sentinel_operation_result

assert d.keys() | RightOperand() is sentinel_operation_result

def test_bitwise_or_iterable_not_set(
self, cls: Type[MutableMultiMapping[str]]
) -> None:
d = cls([("key", "value1")])

assert {"key", "key2"} == d.keys() | ["key2"]

def test_sub(self, cls: Type[MutableMultiMapping[str]]) -> None:
d = cls([("key", "value1"), ("key2", "value2")])

Expand All @@ -407,6 +450,23 @@ def test_sub2(self, cls: Type[MutableMultiMapping[str]]) -> None:

assert {"key3"} == {"key", "key2", "key3"} - d.keys()

def test_sub_not_implemented(self, cls: Type[MutableMultiMapping[str]]) -> None:
d = cls([("key", "value1"), ("key2", "value2")])

sentinel_operation_result = object()

class RightOperand:
def __rsub__(self, other: KeysView[str]) -> object:
assert isinstance(other, KeysView)
return sentinel_operation_result

assert d.keys() - RightOperand() is sentinel_operation_result

def test_sub_iterable_not_set(self, cls: Type[MutableMultiMapping[str]]) -> None:
d = cls([("key", "value1"), ("key2", "value2")])

assert {"key"} == d.keys() - ["key2"]

def test_xor(self, cls: Type[MutableMultiMapping[str]]) -> None:
d = cls([("key", "value1"), ("key2", "value2")])

Expand All @@ -417,6 +477,23 @@ def test_xor2(self, cls: Type[MutableMultiMapping[str]]) -> None:

assert {"key", "key3"} == {"key2", "key3"} ^ d.keys()

def test_xor_not_implemented(self, cls: Type[MutableMultiMapping[str]]) -> None:
d = cls([("key", "value1"), ("key2", "value2")])

sentinel_operation_result = object()

class RightOperand:
def __rxor__(self, other: KeysView[str]) -> object:
assert isinstance(other, KeysView)
return sentinel_operation_result

assert d.keys() ^ RightOperand() is sentinel_operation_result

def test_xor_iterable_not_set(self, cls: Type[MutableMultiMapping[str]]) -> None:
d = cls([("key", "value1"), ("key2", "value2")])

assert {"key", "key3"} == d.keys() ^ ["key2", "key3"]

@pytest.mark.parametrize(
("key", "value", "expected"),
(("key2", "v", True), ("key", "value1", False)),
Expand Down
Loading