tvalentyn commented on code in PR #39443:
URL: https://github.com/apache/beam/pull/39443#discussion_r3638069610
##########
sdks/python/apache_beam/testing/util_test.py:
##########
@@ -93,6 +94,50 @@ def test_assert_with_custom_comparator(self):
p | Create([1, 2, 3]),
equal_to(['1', '2', '3'], equals_fn=lambda e, a: int(e) == int(a)))
+ def test_equal_to_approx(self):
+ with TestPipeline() as p:
+ assert_that(
+ p | Create([1.0, 2.0, 3.0]),
Review Comment:
Just in case to show explicitly that passing ints is acceptable
```suggestion
p | Create([1.0, 2, 3.0]),
```
##########
sdks/python/apache_beam/testing/util.py:
##########
@@ -231,6 +234,33 @@ def row_namedtuple_equals_fn(expected, actual,
fallback_equals_fn=None):
return True
+def equal_to_approx(expected, rel_tol=1e-09, abs_tol=0.0):
+ """Matcher used by assert_that that compares numeric elements approximately.
Review Comment:
Let's add args to the docstring. consider:
```
"""Matcher used by assert_that that compares numeric elements
approximately.
Behaves similarly to `equal_to` for sequence ordering and membership, but
any
real number elements (integers and floats) are compared using
`math.isclose`
instead of exact equality.
Approximate comparisons are also applied to real numbers nested within
lists
and tuples. Note that `equal_to`'s advanced handling for Beam `Row` and
`NamedTuple` is not supported here. All other non-numeric elements are
compared using standard `==` equality.
Args:
expected: The expected output or sequence to compare against.
rel_tol: The relative tolerance used by `math.isclose` (default:
1e-09).
abs_tol: The absolute tolerance used by `math.isclose` (default: 0.0).
Example:
assert_that(
pipeline | beam.Create([1.000000001, 2.0]),
equal_to_approx([1.0, 2.0])
)
"""
```
##########
sdks/python/apache_beam/testing/util_test.py:
##########
@@ -93,6 +94,50 @@ def test_assert_with_custom_comparator(self):
p | Create([1, 2, 3]),
equal_to(['1', '2', '3'], equals_fn=lambda e, a: int(e) == int(a)))
+ def test_equal_to_approx(self):
+ with TestPipeline() as p:
+ assert_that(
+ p | Create([1.0, 2.0, 3.0]),
+ equal_to_approx([3.0000000001, 2.0, 1.0]))
+
+ def test_equal_to_approx_nested(self):
+ with TestPipeline() as p:
+ assert_that(
+ p | Create([('a', 1.0), ('b', 2.0)]),
+ equal_to_approx([('b', 2.0000000001), ('a', 1.0)]))
Review Comment:
```suggestion
equal_to_approx([('b', 2.0000000001), ('a', 1)]))
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]