Approx decimal sequence mapping (#8422)
This commit is contained in:
parent
146eda93e7
commit
af9f27a874
|
@ -0,0 +1 @@
|
|||
:func:`pytest.approx` now works on :class:`~decimal.Decimal` within mappings/dicts and sequences/lists.
|
|
@ -72,6 +72,8 @@ class ApproxBase:
|
|||
return not (actual == self)
|
||||
|
||||
def _approx_scalar(self, x) -> "ApproxScalar":
|
||||
if isinstance(x, Decimal):
|
||||
return ApproxDecimal(x, rel=self.rel, abs=self.abs, nan_ok=self.nan_ok)
|
||||
return ApproxScalar(x, rel=self.rel, abs=self.abs, nan_ok=self.nan_ok)
|
||||
|
||||
def _yield_comparisons(self, actual):
|
||||
|
|
|
@ -313,6 +313,12 @@ class TestApprox:
|
|||
assert approx(expected, rel=5e-7, abs=0) == actual
|
||||
assert approx(expected, rel=5e-8, abs=0) != actual
|
||||
|
||||
def test_list_decimal(self):
|
||||
actual = [Decimal("1.000001"), Decimal("2.000001")]
|
||||
expected = [Decimal("1"), Decimal("2")]
|
||||
|
||||
assert actual == approx(expected)
|
||||
|
||||
def test_list_wrong_len(self):
|
||||
assert [1, 2] != approx([1])
|
||||
assert [1, 2] != approx([1, 2, 3])
|
||||
|
@ -346,6 +352,14 @@ class TestApprox:
|
|||
assert approx(expected, rel=5e-7, abs=0) == actual
|
||||
assert approx(expected, rel=5e-8, abs=0) != actual
|
||||
|
||||
def test_dict_decimal(self):
|
||||
actual = {"a": Decimal("1.000001"), "b": Decimal("2.000001")}
|
||||
# Dictionaries became ordered in python3.6, so switch up the order here
|
||||
# to make sure it doesn't matter.
|
||||
expected = {"b": Decimal("2"), "a": Decimal("1")}
|
||||
|
||||
assert actual == approx(expected)
|
||||
|
||||
def test_dict_wrong_len(self):
|
||||
assert {"a": 1, "b": 2} != approx({"a": 1})
|
||||
assert {"a": 1, "b": 2} != approx({"a": 1, "c": 2})
|
||||
|
|
Loading…
Reference in New Issue