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 an assertDictContainsSubset method. #317

Merged
merged 1 commit into from
Feb 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ Nothing notable unreleased.
tests equality of Mapping objects not requiring them to be dicts. Similar
to `assertSequenceEqual` but for mappings.

* (testing) Added a new method `absltest.assertDictContainsSubset` that
checks that a dictionary contains a subset of keys and values. Similar
to a removed method `unittest.assertDictContainsSubset` (existed until Python 3.11).

### Fixed

* (testing) Fixed an issue where the test reporter crashes with exceptions with
Expand Down
19 changes: 19 additions & 0 deletions absl/testing/absltest.py
Original file line number Diff line number Diff line change
Expand Up @@ -1656,6 +1656,25 @@ def CheckEqual(a, b):
for a, b in itertools.product(group, group):
CheckEqual(a, b)

def assertDictContainsSubset(
self, subset: Mapping[Any, Any], dictionary: Mapping[Any, Any], msg=None
):
"""Raises AssertionError if dictionary is not a superset of subset.

Args:
subset: A dict, the expected subset of the `dictionary`.
dictionary: A dict, the actual value.
msg: An optional str, the associated message.

Raises:
AssertionError: if dictionary is not a superset of subset.
"""
if not isinstance(subset, dict):
subset = dict(subset)
if not isinstance(dictionary, dict):
dictionary = dict(dictionary)
self.assertDictEqual(dictionary, {**dictionary, **subset}, msg)

def assertDictEqual(self, a, b, msg=None):
"""Raises AssertionError if a and b are not equal dictionaries.

Expand Down
86 changes: 85 additions & 1 deletion absl/testing/tests/absltest_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import sys
import tempfile
import textwrap
from typing import Optional
from typing import Any, ItemsView, Iterator, KeysView, Mapping, Optional, Type, ValuesView
import unittest

from absl.testing import _bazelize_command
Expand Down Expand Up @@ -1597,6 +1597,90 @@ def test_assert_json_equal_bad_json(self):
with self.assertRaises(ValueError) as error_context:
self.assertJsonEqual('', '')

@parameterized.named_parameters(
dict(testcase_name='empty', subset={}, dictionary={}),
dict(testcase_name='empty_is_a_subset', subset={}, dictionary={'a': 1}),
dict(
testcase_name='equal_one_element',
subset={'a': 1},
dictionary={'a': 1},
),
dict(
testcase_name='subset', subset={'a': 1}, dictionary={'a': 1, 'b': 2}
),
dict(
testcase_name='equal_many_elements',
subset={'a': 1, 'b': 2},
dictionary={'a': 1, 'b': 2},
),
)
def test_assert_dict_contains_subset(
self, subset: Mapping[Any, Any], dictionary: Mapping[Any, Any]
):
self.assertDictContainsSubset(subset, dictionary)

def test_assert_dict_contains_subset_converts_to_dict(self):
class ConvertibleToDict(Mapping):

def __init__(self, **kwargs):
self._data = kwargs

def __getitem__(self, key: Any) -> Any:
return self._data[key]

def __iter__(self) -> Iterator:
return iter(self._data)

def __len__(self) -> int:
return len(self._data)

def keys(self) -> KeysView:
return self._data.keys()

def values(self) -> ValuesView:
return self._data.values()

def items(self) -> ItemsView:
return self._data.items()

self.assertDictContainsSubset(
ConvertibleToDict(name='a', value=1),
ConvertibleToDict(name='a', value=1),
)

@parameterized.named_parameters(
dict(testcase_name='subset_vs_empty', subset={1: 'one'}, dictionary={}),
dict(
testcase_name='value_is_different',
subset={'a': 2},
dictionary={'a': 1},
),
dict(
testcase_name='key_is_different', subset={'c': 1}, dictionary={'a': 1}
),
dict(
testcase_name='subset_is_larger',
subset={'a': 1, 'c': 1},
dictionary={'a': 1},
),
dict(
testcase_name='UnicodeDecodeError_constructing_failure_msg',
subset={'foo': ''.join(chr(i) for i in range(255))},
dictionary={'foo': '\uFFFD'},
),
)
def test_assert_dict_contains_subset_fails(
self, subset: Mapping[Any, Any], dictionary: Mapping[Any, Any]
):
with self.assertRaises(self.failureException):
self.assertDictContainsSubset(subset, dictionary)

def test_assert_dict_contains_subset_fails_with_msg(self):
with self.assertRaisesRegex(
AssertionError, re.compile('custom message', re.DOTALL)
):
self.assertDictContainsSubset({'a': 1}, {'a': 2}, msg='custom message')


class GetCommandStderrTestCase(absltest.TestCase):

Expand Down
Loading