|
5 | 5 | import re |
6 | 6 | from unittest.mock import patch |
7 | 7 | import pytest |
| 8 | +import itertools |
| 9 | +from functools import partial |
| 10 | +import contextlib |
8 | 11 |
|
9 | 12 | import pandas as pd |
10 | 13 | import polars as pl |
@@ -7604,3 +7607,56 @@ def test_get_schema_step_report_25_5(tbl_schema_tests, snapshot): |
7604 | 7607 |
|
7605 | 7608 | # Take snapshot of the report DataFrame |
7606 | 7609 | snapshot.assert_match(str(report_df), "schema_step_report_25-5.txt") |
| 7610 | + |
| 7611 | + |
| 7612 | +@pytest.mark.parametrize(('tbl','should_pass'), itertools.product(TBL_LIST, [True, False])) |
| 7613 | +def test_assert_passing(request, tbl : str , *, should_pass: bool) -> None: |
| 7614 | + tbl = request.getfixturevalue(tbl) |
| 7615 | + |
| 7616 | + if should_pass: |
| 7617 | + val = 0 # should always pass |
| 7618 | + catcher = contextlib.nullcontext |
| 7619 | + else: |
| 7620 | + val = 100 # should always fail |
| 7621 | + catcher = partial(pytest.raises, AssertionError, match = "All tests did not pass") |
| 7622 | + |
| 7623 | + |
| 7624 | + v = Validate(tbl).col_vals_gt(columns="x", value=val).interrogate() |
| 7625 | + |
| 7626 | + |
| 7627 | + try: |
| 7628 | + assert v.all_passed() == should_pass |
| 7629 | + except AssertionError: |
| 7630 | + pytest.mark.skip(reason="Unexpected result invalidating the test. Please review.") |
| 7631 | + |
| 7632 | + with catcher(): |
| 7633 | + v.assert_passing() # should not raise since all passing |
| 7634 | + |
| 7635 | +def test_assert_passing_example() -> None: |
| 7636 | + tbl = pl.DataFrame( |
| 7637 | + { |
| 7638 | + "a": [1, 2, 9, 5], |
| 7639 | + "b": [5, 6, 10, 3], |
| 7640 | + "c": ["a", "b", "a", "a"], |
| 7641 | + } |
| 7642 | + ) |
| 7643 | + |
| 7644 | + validation = ( |
| 7645 | + Validate(data=tbl) |
| 7646 | + .col_vals_gt(columns="a", value=0) |
| 7647 | + .col_vals_lt(columns="b", value=9) # this step will not pass |
| 7648 | + .col_vals_in_set(columns="c", set=["a", "b"]) |
| 7649 | + .interrogate() |
| 7650 | + ) |
| 7651 | + with pytest.raises(AssertionError, match = "All tests did not pass"): |
| 7652 | + validation.assert_passing() |
| 7653 | + |
| 7654 | + passing_validation = ( |
| 7655 | + Validate(data=tbl) |
| 7656 | + .col_vals_gt(columns="a", value=0) |
| 7657 | + # now, the invalid step passes |
| 7658 | + .col_vals_in_set(columns="c", set=["a", "b"]) |
| 7659 | + .interrogate() |
| 7660 | + ) |
| 7661 | + |
| 7662 | + passing_validation.assert_passing() |
0 commit comments