test: Add tests for approx changes

Includes a demonstration test for expected behavior if approx is intended to support range
This commit is contained in:
Zach OBrien 2022-05-08 00:35:03 -04:00
parent 18c4449cc0
commit 843ad11bab
No known key found for this signature in database
GPG Key ID: 3FC3457550AF85EB
1 changed files with 42 additions and 0 deletions

View File

@ -2,12 +2,14 @@ import operator
from contextlib import contextmanager
from decimal import Decimal
from fractions import Fraction
from math import sqrt
from operator import eq
from operator import ne
from typing import Optional
import pytest
from _pytest.pytester import Pytester
from _pytest.python_api import _recursive_sequence_map
from pytest import approx
inf, nan = float("inf"), float("nan")
@ -133,6 +135,32 @@ class TestApprox:
],
)
assert_approx_raises_regex(
(1, 2.2, 4),
(1, 3.2, 4),
[
r" comparison failed. Mismatched elements: 1 / 3:",
rf" Max absolute difference: {SOME_FLOAT}",
rf" Max relative difference: {SOME_FLOAT}",
r" Index \| Obtained\s+\| Expected ",
rf" 1 \| {SOME_FLOAT} \| {SOME_FLOAT} ± {SOME_FLOAT}",
],
)
assert_approx_raises_regex(
range(0, 3),
range(1, 4),
[
r" comparison failed. Mismatched elements: 3 / 3:",
rf" Max absolute difference: {SOME_FLOAT}",
rf" Max relative difference: {SOME_FLOAT}",
r" Index \| Obtained\s+\| Expected ",
rf" 0 \| {SOME_FLOAT} \| {SOME_FLOAT} ± {SOME_FLOAT}",
rf" 1 \| {SOME_FLOAT} \| {SOME_FLOAT} ± {SOME_FLOAT}",
rf" 2 \| {SOME_FLOAT} \| {SOME_FLOAT} ± {SOME_FLOAT}",
],
)
# Specific test for comparison with 0.0 (relative diff will be 'inf')
assert_approx_raises_regex(
[0.0],
@ -878,3 +906,17 @@ class TestApprox:
"""pytest.approx() should raise an error on unordered sequences (#9692)."""
with pytest.raises(TypeError, match="only supports ordered sequences"):
assert {1, 2, 3} == approx({1, 2, 3})
class TestRecursiveSequenceMap:
def test_map_over_scalar(self):
assert _recursive_sequence_map(sqrt, 16) == 4
def test_map_over_list(self):
assert _recursive_sequence_map(sqrt, [4, 16, 25, 676]) == [2, 4, 5, 26]
def test_map_over_tuple(self):
assert _recursive_sequence_map(sqrt, (4, 16, 25, 676)) == (2, 4, 5, 26)
def test_map_over_range(self):
assert _recursive_sequence_map(lambda x: 2 * x, range(0, 5)) == range(0, 9, 2)