Skip to content

Add tests for TestCase.assertDictEqual() #11154

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

Merged
merged 3 commits into from
Dec 12, 2023
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
2 changes: 2 additions & 0 deletions stdlib/unittest/case.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,8 @@ class TestCase:
def assertListEqual(self, list1: list[Any], list2: list[Any], msg: Any = None) -> None: ...
def assertTupleEqual(self, tuple1: tuple[Any, ...], tuple2: tuple[Any, ...], msg: Any = None) -> None: ...
def assertSetEqual(self, set1: AbstractSet[object], set2: AbstractSet[object], msg: Any = None) -> None: ...
# assertDictEqual accepts only true dict instances. We can't use that here, since that would make
# assertDictEqual incompatible with TypedDict.
def assertDictEqual(self, d1: Mapping[Any, object], d2: Mapping[Any, object], msg: Any = None) -> None: ...
def fail(self, msg: Any = None) -> NoReturn: ...
def countTestCases(self) -> int: ...
Expand Down
51 changes: 50 additions & 1 deletion test_cases/stdlib/check_unittest.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
from __future__ import annotations

import unittest
from collections.abc import Iterator, Mapping
from datetime import datetime, timedelta
from decimal import Decimal
from fractions import Fraction
from typing_extensions import assert_type
from typing_extensions import TypedDict, assert_type
from unittest.mock import MagicMock, Mock, patch

case = unittest.TestCase()
Expand Down Expand Up @@ -89,6 +90,54 @@ def __gt__(self, other: Bacon) -> bool:
case.assertGreater(Ham(), Bacon()) # type: ignore
case.assertGreater(Bacon(), Ham()) # type: ignore


###
# Tests for assertDictEqual
###


class TD1(TypedDict):
x: int
y: str


class TD2(TypedDict):
a: bool
b: bool


class MyMapping(Mapping[str, int]):
def __getitem__(self, __key: str) -> int:
return 42

def __iter__(self) -> Iterator[str]:
return iter([])

def __len__(self) -> int:
return 0


td1: TD1 = {"x": 1, "y": "foo"}
td2: TD2 = {"a": True, "b": False}
m = MyMapping()

case.assertDictEqual({}, {})
case.assertDictEqual({"x": 1, "y": 2}, {"x": 1, "y": 2})
case.assertDictEqual({"x": 1, "y": "foo"}, {"y": "foo", "x": 1})
case.assertDictEqual({"x": 1}, {})
case.assertDictEqual({}, {"x": 1})
case.assertDictEqual({1: "x"}, {"y": 222})
case.assertDictEqual({1: "x"}, td1)
case.assertDictEqual(td1, {1: "x"})
case.assertDictEqual(td1, td2)

case.assertDictEqual(1, {}) # type: ignore
case.assertDictEqual({}, 1) # type: ignore

# These should fail, but don't due to TypedDict limitations:
# case.assertDictEqual(m, {"": 0}) # xtype: ignore
# case.assertDictEqual({"": 0}, m) # xtype: ignore

###
# Tests for mock.patch
###
Expand Down