Best Python code snippet using pandera_python
test_checks_builtin.py
Source:test_checks_builtin.py
...26 # Assert that the failure cases are correct27 assert set(check_result.failure_cases) == set(28 expected_failure_cases29 ), "Unexpected failure cases returned by Check.__call__()"30def check_none_failures(values, check) -> None:31 """Like check_values but expects a failure and due to Null values.32 Asserts that the check fails on the given values and that the only33 failures are Null values.34 """35 series = pd.Series(values)36 check_result = check(series)37 assert not check_result.check_passed, "Check should fail due to None value"38 assert (39 check_result.checked_object is series40 ), "Wrong checked_object returned"41 assert (42 check_result.failure_cases.isnull().all()43 ), "Only null values should be failure cases"44def check_raise_error_or_warning(failure_values, check) -> None:45 """46 Check that Series and DataFrameSchemas raise warnings instead of exceptions47 NOTE: it's not ideal that we have to import schema and schemaa_components48 modules into this test module to test this functionality, this doesn't49 separate the units under test very well.50 """51 failure_series = pd.Series(failure_values)52 failure_df = pd.DataFrame({"failure_column": failure_values})53 check.raise_warning = False54 with pytest.raises(SchemaError):55 SeriesSchema(checks=check)(failure_series)56 DataFrameSchema({"failure_column": Column(checks=check)})(failure_df)57 check.raise_warning = True58 with pytest.warns(UserWarning):59 SeriesSchema(checks=check)(failure_series)60 DataFrameSchema({"failure_column": Column(checks=check)})(failure_df)61class TestGreaterThan:62 """Tests for Check.greater_than"""63 @staticmethod64 @pytest.mark.parametrize("check_fn", [Check.greater_than, Check.gt])65 def test_argument_check(check_fn):66 """Test if None is accepted as boundary"""67 with pytest.raises(ValueError):68 check_fn(min_value=None)69 @staticmethod70 @pytest.mark.parametrize("check_fn", [Check.greater_than, Check.gt])71 @pytest.mark.parametrize(72 "values, min_val",73 [74 ((1, 2, 3), 0),75 ((1, 2, 3), -1),76 (77 (78 pd.Timestamp("2015-02-01"),79 pd.Timestamp("2015-02-02"),80 pd.Timestamp("2015-02-03"),81 ),82 pd.Timestamp("2015-01-01"),83 ),84 (("b", "c"), "a"),85 ],86 )87 def test_succeeding(check_fn, values, min_val):88 """Run checks which should succeed"""89 check_values(values, check_fn(min_val), {})90 @staticmethod91 @pytest.mark.parametrize("check_fn", [Check.greater_than, Check.gt])92 @pytest.mark.parametrize(93 "values, min_val, failure_cases",94 [95 ((1, 2, 3), 1, {1}),96 ((3, 2, 1), 1, {1}),97 ((1, 2, 3), 2, {1, 2}),98 (99 (100 pd.Timestamp("2015-02-01"),101 pd.Timestamp("2015-02-02"),102 pd.Timestamp("2015-02-03"),103 ),104 pd.Timestamp("2015-02-02"),105 {pd.Timestamp("2015-02-01"), pd.Timestamp("2015-02-02")},106 ),107 (("b", "c"), "b", {"b"}),108 ],109 )110 def test_failing(check_fn, values, min_val, failure_cases):111 """Run checks which should fail"""112 check_values(values, check_fn(min_val), failure_cases)113 check_raise_error_or_warning(values, check_fn(min_val))114 @staticmethod115 @pytest.mark.parametrize("check_fn", [Check.greater_than, Check.gt])116 @pytest.mark.parametrize(117 "values, min_val",118 [119 [(2, None), 1],120 [(pd.Timestamp("2015-02-02"), None), pd.Timestamp("2015-02-01")],121 [("b", None), "a"],122 ],123 )124 def test_failing_with_none(check_fn, values, min_val):125 """Validate the check works also on dataframes with None values"""126 check_none_failures(values, check_fn(min_val, ignore_na=False))127class TestGreaterThanOrEqualTo:128 """Tests for Check.greater_than_or_equal_to"""129 @staticmethod130 @pytest.mark.parametrize(131 "check_fn", [Check.greater_than_or_equal_to, Check.ge]132 )133 def test_argument_check(check_fn):134 """Test if None is accepted as boundary"""135 with pytest.raises(ValueError):136 check_fn(min_value=None)137 @staticmethod138 @pytest.mark.parametrize(139 "check_fn", [Check.greater_than_or_equal_to, Check.ge]140 )141 @pytest.mark.parametrize(142 "values, min_val",143 [144 ((1, 2, 3), 1),145 ((1, 2, 3), -1),146 (147 (148 pd.Timestamp("2015-02-01"),149 pd.Timestamp("2015-02-02"),150 pd.Timestamp("2015-02-03"),151 ),152 pd.Timestamp("2015-02-01"),153 ),154 (("b", "a"), "a"),155 ],156 )157 def test_succeeding(check_fn, values, min_val):158 """Run checks which should succeed"""159 check_values(values, check_fn(min_val), {})160 @staticmethod161 @pytest.mark.parametrize(162 "check_fn", [Check.greater_than_or_equal_to, Check.ge]163 )164 @pytest.mark.parametrize(165 "values, min_val, failure_cases",166 [167 ((1, 2, 3), 2, {1}),168 ((3, 2, 1), 2, {1}),169 ((1, 2, 3), 3, {1, 2}),170 (171 (172 pd.Timestamp("2015-02-01"),173 pd.Timestamp("2015-02-02"),174 pd.Timestamp("2015-02-03"),175 ),176 pd.Timestamp("2015-02-02"),177 {pd.Timestamp("2015-02-01")},178 ),179 (("b", "c"), "c", {"b"}),180 ],181 )182 def test_failing(check_fn, values, min_val, failure_cases):183 """Run checks which should fail"""184 check_values(values, check_fn(min_val), failure_cases)185 check_raise_error_or_warning(values, check_fn(min_val))186 @staticmethod187 @pytest.mark.parametrize(188 "check_fn", [Check.greater_than_or_equal_to, Check.ge]189 )190 @pytest.mark.parametrize(191 "values, min_val",192 [193 [(2, None), 1],194 [(pd.Timestamp("2015-02-02"), None), pd.Timestamp("2015-02-01")],195 [("b", None), "a"],196 ],197 )198 def test_failing_with_none(check_fn, values, min_val):199 """Validate the check works also on dataframes with None values"""200 check_none_failures(values, check_fn(min_val, ignore_na=False))201class TestLessThan:202 """Tests for Check.less_than"""203 @staticmethod204 @pytest.mark.parametrize("check_fn", [Check.less_than, Check.lt])205 def test_argument_check(check_fn):206 """Test if None is accepted as boundary"""207 with pytest.raises(ValueError):208 check_fn(max_value=None)209 @staticmethod210 @pytest.mark.parametrize("check_fn", [Check.less_than, Check.lt])211 @pytest.mark.parametrize(212 "values, max_value",213 [214 ((1, 2, 3), 4),215 ((-1, 2, 3), 4),216 (217 (218 pd.Timestamp("2015-02-01"),219 pd.Timestamp("2015-02-02"),220 pd.Timestamp("2015-02-03"),221 ),222 pd.Timestamp("2015-02-04"),223 ),224 (("b", "c"), "d"),225 ],226 )227 def test_succeeding(check_fn, values, max_value):228 """Run checks which should succeed"""229 check_values(values, check_fn(max_value), {})230 @staticmethod231 @pytest.mark.parametrize("check_fn", [Check.less_than, Check.lt])232 @pytest.mark.parametrize(233 "values, max_value, failure_cases",234 [235 ((1, 2, 3), 3, {3}),236 ((3, 2, 1), 3, {3}),237 ((1, 2, 3), 2, {3, 2}),238 (239 (240 pd.Timestamp("2015-02-01"),241 pd.Timestamp("2015-02-02"),242 pd.Timestamp("2015-02-03"),243 ),244 pd.Timestamp("2015-02-02"),245 {pd.Timestamp("2015-02-02"), pd.Timestamp("2015-02-03")},246 ),247 (("b", "c"), "c", {"c"}),248 ],249 )250 def test_failing(check_fn, values, max_value, failure_cases):251 """Run checks which should fail"""252 check_values(values, check_fn(max_value), failure_cases)253 check_raise_error_or_warning(values, check_fn(max_value))254 @staticmethod255 @pytest.mark.parametrize("check_fn", [Check.less_than, Check.lt])256 @pytest.mark.parametrize(257 "values, max_value",258 [259 [(2, None), 3],260 [(pd.Timestamp("2015-02-02"), None), pd.Timestamp("2015-02-03")],261 [("b", None), "c"],262 ],263 )264 def test_failing_with_none(check_fn, values, max_value):265 """Validate the check works also on dataframes with None values"""266 check_none_failures(values, check_fn(max_value, ignore_na=False))267class TestLessThanOrEqualTo:268 """Tests for Check.less_than_or_equal_to"""269 @staticmethod270 @pytest.mark.parametrize(271 "check_fn", [Check.less_than_or_equal_to, Check.le]272 )273 def test_argument_check(check_fn):274 """Test if None is accepted as boundary"""275 with pytest.raises(ValueError):276 check_fn(max_value=None)277 @staticmethod278 @pytest.mark.parametrize(279 "check_fn", [Check.less_than_or_equal_to, Check.le]280 )281 @pytest.mark.parametrize(282 "values, max_value",283 [284 ((1, 2, 3), 3),285 ((-1, 2, 3), 3),286 (287 (288 pd.Timestamp("2015-02-01"),289 pd.Timestamp("2015-02-02"),290 pd.Timestamp("2015-02-03"),291 ),292 pd.Timestamp("2015-02-03"),293 ),294 (("b", "a"), "b"),295 ],296 )297 def test_succeeding(check_fn, values, max_value):298 """Run checks which should succeed"""299 check_values(values, check_fn(max_value), {})300 @staticmethod301 @pytest.mark.parametrize(302 "check_fn", [Check.less_than_or_equal_to, Check.le]303 )304 @pytest.mark.parametrize(305 "values, max_value, failure_cases",306 [307 ((1, 2, 3), 2, {3}),308 ((3, 2, 1), 2, {3}),309 ((1, 2, 3), 1, {2, 3}),310 (311 (312 pd.Timestamp("2015-02-01"),313 pd.Timestamp("2015-02-02"),314 pd.Timestamp("2015-02-03"),315 ),316 pd.Timestamp("2015-02-02"),317 {pd.Timestamp("2015-02-03")},318 ),319 (("b", "c"), "b", {"c"}),320 ],321 )322 def test_failing(check_fn, values, max_value, failure_cases):323 """Run checks which should fail"""324 check_values(values, check_fn(max_value), failure_cases)325 check_raise_error_or_warning(values, check_fn(max_value))326 @staticmethod327 @pytest.mark.parametrize(328 "check_fn", [Check.less_than_or_equal_to, Check.le]329 )330 @pytest.mark.parametrize(331 "values, max_value",332 [333 [(2, None), 2],334 [(pd.Timestamp("2015-02-02"), None), pd.Timestamp("2015-02-02")],335 [("b", None), "b"],336 ],337 )338 def test_failing_with_none(check_fn, values, max_value):339 """Validate the check works also on dataframes with None values"""340 check_none_failures(values, check_fn(max_value, ignore_na=False))341class TestInRange:342 """Tests for Check.in_range"""343 @staticmethod344 @pytest.mark.parametrize(345 "args",346 [(None, 1), (1, None), (2, 1), (1, 1, False), (1, 1, True, False)],347 )348 def test_argument_check(args):349 """Test invalid arguments"""350 with pytest.raises(ValueError):351 Check.in_range(*args)352 @staticmethod353 @pytest.mark.parametrize(354 "values, check_args",355 [356 ((1, 2, 3), (0, 4)),357 ((1, 2, 3), (0, 4, False, False)),358 ((1, 2, 3), (1, 3)),359 ((-1, 2, 3), (-1, 3)),360 (361 (362 pd.Timestamp("2015-02-01"),363 pd.Timestamp("2015-02-02"),364 pd.Timestamp("2015-02-03"),365 ),366 (pd.Timestamp("2015-02-01"), pd.Timestamp("2015-02-03")),367 ),368 (("b", "c"), ("b", "c")),369 (("b", "c"), ("a", "d", False, False)),370 ],371 )372 def test_succeeding(values, check_args):373 """Run checks which should succeed"""374 check_values(values, Check.in_range(*check_args), {})375 @staticmethod376 @pytest.mark.parametrize(377 "values, check_args, failure_cases",378 [379 ((1, 2, 3), (0, 2), {3}),380 ((1, 2, 3), (2, 3), {1}),381 ((1, 2, 3), (1, 3, True, False), {3}),382 ((1, 2, 3), (1, 3, False, True), {1}),383 ((-1, 2, 3), (-1, 3, False), {-1}),384 (385 (386 pd.Timestamp("2015-02-01"),387 pd.Timestamp("2015-02-02"),388 pd.Timestamp("2015-02-03"),389 ),390 (pd.Timestamp("2015-02-01"), pd.Timestamp("2015-02-02")),391 {pd.Timestamp("2015-02-03")},392 ),393 (("a", "c"), ("b", "c"), {"a"}),394 (("b", "c"), ("b", "c", False, True), {"b"}),395 (("b", "c"), ("b", "c", True, False), {"c"}),396 ],397 )398 def test_failing(values, check_args, failure_cases):399 """Run checks which should fail"""400 check_values(values, Check.in_range(*check_args), failure_cases)401 check_raise_error_or_warning(values, Check.in_range(*check_args))402 @staticmethod403 @pytest.mark.parametrize(404 "values, check_args",405 [406 [(2, None), (0, 4)],407 [408 (pd.Timestamp("2015-02-02"), None),409 (pd.Timestamp("2015-02-01"), pd.Timestamp("2015-02-03")),410 ],411 [("b", None), ("a", "c")],412 ],413 )414 def test_failing_with_none(values, check_args):415 """Validate the check works also on dataframes with None values"""416 check_none_failures(417 values, Check.in_range(*check_args, ignore_na=False)418 )419class TestEqualTo:420 """Tests for Check.equal_to"""421 @staticmethod422 @pytest.mark.parametrize("check_fn", [Check.equal_to, Check.eq])423 @pytest.mark.parametrize(424 "series_values, value",425 [426 ((1, 1), 1),427 ((-1, -1, -1), -1),428 (429 (pd.Timestamp("2015-02-01"), pd.Timestamp("2015-02-01")),430 pd.Timestamp("2015-02-01"),431 ),432 (("foo", "foo"), "foo"),433 ],434 )435 def test_succeeding(check_fn, series_values, value):436 """Run checks which should succeed"""437 check_values(series_values, check_fn(value), {})438 @staticmethod439 @pytest.mark.parametrize("check_fn", [Check.equal_to, Check.eq])440 @pytest.mark.parametrize(441 "values, value, failure_cases",442 [443 ((1, 2), 1, {2}),444 ((-1, -2, 3), -1, {-2, 3}),445 (446 (pd.Timestamp("2015-02-01"), pd.Timestamp("2015-02-02")),447 pd.Timestamp("2015-02-01"),448 {pd.Timestamp("2015-02-02")},449 ),450 (("foo", "bar"), "foo", {"bar"}),451 ],452 )453 def test_failing(check_fn, values, value, failure_cases):454 """Run checks which should fail"""455 check_values(values, check_fn(value), failure_cases)456 check_raise_error_or_warning(values, check_fn(value))457 @staticmethod458 @pytest.mark.parametrize("check_fn", [Check.equal_to, Check.eq])459 @pytest.mark.parametrize(460 "series_values, value",461 [462 [(1, None), 1],463 [(-1, None, -1), -1],464 [(pd.Timestamp("2015-02-01"), None), pd.Timestamp("2015-02-01")],465 [("foo", None), "foo"],466 ],467 )468 def test_failing_with_none(check_fn, series_values, value):469 """Validate the check works also on dataframes with None values"""470 check_none_failures(series_values, check_fn(value, ignore_na=False))471class TestNotEqualTo:472 """Tests for Check.not_equal_to"""473 @staticmethod474 @pytest.mark.parametrize("check_fn", [Check.not_equal_to, Check.ne])475 @pytest.mark.parametrize(476 "series_values, value",477 [478 ((1, 1), 2),479 ((-1, -1, -1), -2),480 (481 (pd.Timestamp("2015-02-01"), pd.Timestamp("2015-02-01")),482 pd.Timestamp("2015-02-02"),483 ),484 (("foo", "foo"), "bar"),485 ],486 )487 def test_succeeding(check_fn, series_values, value):488 """Run checks which should succeed"""489 check_values(series_values, check_fn(value), {})490 @staticmethod491 @pytest.mark.parametrize("check_fn", [Check.not_equal_to, Check.ne])492 @pytest.mark.parametrize(493 "values, value, failure_cases",494 [495 ((1, 2), 1, {1}),496 ((-1, -2, 3), -1, {-1}),497 (498 (pd.Timestamp("2015-02-01"), pd.Timestamp("2015-02-02")),499 pd.Timestamp("2015-02-01"),500 {pd.Timestamp("2015-02-01")},501 ),502 (("foo", "bar"), "foo", {"foo"}),503 ],504 )505 def test_failing(check_fn, values, value, failure_cases):506 """Run checks which should fail"""507 check_values(values, check_fn(value), failure_cases)508 check_raise_error_or_warning(values, check_fn(value))509class TestIsin:510 """Tests for Check.isin"""511 @staticmethod512 @pytest.mark.parametrize(513 "args",514 [515 (1,), # Not Iterable516 (None,), # None should also not be accepted517 ],518 )519 def test_argument_check(args):520 """Test invalid arguments"""521 with pytest.raises(ValueError):522 Check.isin(*args)523 @staticmethod524 @pytest.mark.parametrize(525 "series_values, allowed",526 [527 ((1, 1), (1, 2, 3)),528 ((-1, -1, -1), {-2, -1}),529 ((-1, -1, -1), pd.Series((-2, -1))),530 (531 (pd.Timestamp("2015-02-01"), pd.Timestamp("2015-02-01")),532 [pd.Timestamp("2015-02-01")],533 ),534 (("foo", "foo"), {"foo", "bar"}),535 (("f", "o"), "foobar"),536 ],537 )538 def test_succeeding(series_values, allowed):539 """Run checks which should succeed"""540 check_values(series_values, Check.isin(allowed), {})541 @staticmethod542 @pytest.mark.parametrize(543 "values, allowed, failure_cases",544 [545 ((1, 2), [2], {1}),546 ((-1, -2, 3), (-2, -3), {-1, 3}),547 ((-1, -2, 3), pd.Series((-2, 3)), {-1}),548 (549 (pd.Timestamp("2015-02-01"), pd.Timestamp("2015-02-02")),550 {pd.Timestamp("2015-02-01")},551 {pd.Timestamp("2015-02-02")},552 ),553 (("foo", "bar"), {"foo"}, {"bar"}),554 (("foo", "f"), "foobar", {"foo"}),555 ],556 )557 def test_failing(values, allowed, failure_cases):558 """Run checks which should fail"""559 check_values(values, Check.isin(allowed), failure_cases)560 check_raise_error_or_warning(values, Check.isin(allowed))561 @staticmethod562 @pytest.mark.parametrize(563 "values, allowed",564 [565 [(2, None), {2}],566 [(pd.Timestamp("2015-02-02"), None), {pd.Timestamp("2015-02-02")}],567 [("b", None), {"b"}],568 [("f", None), "foo"],569 ],570 )571 def test_failing_with_none(values, allowed):572 """Validate the check works also on dataframes with None values"""573 check_none_failures(values, Check.isin(allowed, ignore_na=False))574 @staticmethod575 def test_ignore_mutable_arg():576 """577 Check if a mutable argument passed by reference can be changed from578 outside579 """580 series_values = (1, 1)581 df = pd.DataFrame({"allowed": (1, 2)})582 check = Check.isin(df["allowed"])583 # Up to here the test should succeed584 check_values(series_values, check, {})585 # When the Series with the allowed values is changed it should still586 # succeed587 df["allowed"] = 2588 check_values(series_values, check, {})589class TestNotin:590 """Tests for Check.notin"""591 @staticmethod592 @pytest.mark.parametrize(593 "args",594 [595 (1,), # Not Iterable596 (None,), # None should also not be accepted597 ],598 )599 def test_argument_check(args):600 """Test invalid arguments"""601 with pytest.raises(ValueError):602 Check.notin(*args)603 @staticmethod604 @pytest.mark.parametrize(605 "series_values, forbidden",606 [607 ((1, 1), (2, 3)),608 ((-1, -1, -1), {-2, 1}),609 ((-1, -1, -1), pd.Series((-2, 1))),610 (611 (pd.Timestamp("2015-02-01"), pd.Timestamp("2015-02-01")),612 [pd.Timestamp("2015-02-02")],613 ),614 (("foo", "foo"), {"foobar", "bar"}),615 (("f", "o"), "bar"),616 ],617 )618 def test_succeeding(series_values, forbidden):619 """Run checks which should succeed"""620 check_values(series_values, Check.notin(forbidden), {})621 @staticmethod622 @pytest.mark.parametrize(623 "series_values, forbidden, failure_cases",624 [625 ((1, 2), [2], {2}),626 ((-1, -2, 3), (-2, -3), {-2}),627 ((-1, -2, 3), pd.Series((-2, 3)), {-2, 3}),628 (629 (pd.Timestamp("2015-02-01"), pd.Timestamp("2015-02-02")),630 {pd.Timestamp("2015-02-01")},631 {pd.Timestamp("2015-02-01")},632 ),633 (("foo", "bar"), {"foo"}, {"foo"}),634 (("foo", "f"), "foobar", {"f"}),635 ],636 )637 def test_failing(series_values, forbidden, failure_cases):638 """Run checks which should fail"""639 check_values(series_values, Check.notin(forbidden), failure_cases)640 check_raise_error_or_warning(series_values, Check.notin(forbidden))641 @staticmethod642 def test_ignore_mutable_arg():643 """644 Check if a mutable argument passed by reference can be changed from645 outside646 """647 series_values = (1, 1)648 df = pd.DataFrame({"forbidden": (0, 2)})649 check = Check.notin(df["forbidden"])650 # Up to here the test should succeed651 check_values(series_values, check, {})652 # When the Series with the allowed values is changed it should still653 # succeed654 df["forbidden"] = 1655 check_values(series_values, check, {})656class TestStrMatches:657 """Tests for Check.str_matches"""658 @staticmethod659 @pytest.mark.parametrize("pattern", [(1,), (None,)])660 def test_argument_check(pattern):661 """Test invalid arguments"""662 with pytest.raises(ValueError):663 Check.str_matches(pattern)664 @staticmethod665 @pytest.mark.parametrize(666 "series_values, pattern",667 [668 (("foo", "fooo"), r"fo"), # Plain string as pattern669 (("foo", "bar"), r"[a-z]+"), # Character sets670 (("24.55", "24"), r"(\d+)\.?(\d+)?"), # Groups and quantifiers671 (("abcdef", "abcccdef"), r"abc+(?=def)"), # Lookahead672 ],673 )674 def test_succeeding(series_values, pattern):675 """Run checks which should succeed"""676 check_values(series_values, Check.str_matches(pattern), {})677 @staticmethod678 @pytest.mark.parametrize(679 "series_values, pattern, failure_cases",680 [681 (("foo", "_foo"), r"fo", ("_foo",)),682 (("foo", "bar", "lamp"), r"[a-k]+", ("lamp",)),683 (("24.55", "24.5.6"), r"(\d+)\.?(\d+)?$", ("24.5.6",)),684 ],685 )686 def test_failing(series_values, pattern, failure_cases):687 """Run checks which should fail"""688 check_values(series_values, Check.str_matches(pattern), failure_cases)689 check_raise_error_or_warning(series_values, Check.str_matches(pattern))690 @staticmethod691 @pytest.mark.parametrize(692 "series_values, pattern",693 [694 (("foo", None, "fooo"), r"fo"),695 (("foo", "bar", None), r"[a-z]+"),696 ],697 )698 def test_failing_with_none(series_values, pattern):699 """Validate the check works also on dataframes with None values"""700 check_none_failures(701 series_values, Check.str_matches(pattern, ignore_na=False)702 )703class TestStrContains:704 """Tests for Check.str_contains"""705 @staticmethod706 @pytest.mark.parametrize("pattern", [(1,), (None,)])707 def test_argument_check(pattern):708 """Test invalid arguments"""709 with pytest.raises(ValueError):710 Check.str_contains(pattern)711 @staticmethod712 @pytest.mark.parametrize(713 "series_values, pattern",714 [715 (("foo", "fooo", "barfoo"), r"fo"), # Plain string as pattern716 (("foo", "bar", "5abc"), r"[a-z]+"), # Character sets717 (("24.55", "24", "-24.55-"), r"\d+\.?\d+?"), # Quantifiers718 (("abcdef", "abcccdef", "-abcdef-"), r"abc+(?=def)"), # Lookahead719 ],720 )721 def test_succeeding(series_values, pattern):722 """Run checks which should succeed"""723 check_values(series_values, Check.str_contains(pattern), {})724 @staticmethod725 @pytest.mark.parametrize(726 "series_values, pattern, failure_cases",727 [728 (("foo", "_foo", "f-o"), r"fo", ("f-o",)),729 (("foo", "xyz", "lamp"), r"[a-k]+", ("xyz",)),730 (("24.55", "24.5.6"), r"^\d+\.?\d+?$", ("24.5.6",)),731 ],732 )733 def test_failing(series_values, pattern, failure_cases):734 """Run checks which should fail"""735 check_values(series_values, Check.str_contains(pattern), failure_cases)736 check_raise_error_or_warning(737 series_values, Check.str_contains(pattern)738 )739 @staticmethod740 @pytest.mark.parametrize(741 "series_values, pattern",742 [((None, "fooo"), r"fo"), (("foo", None, "5abc"), r"[a-z]+")],743 )744 def test_failing_with_none(series_values, pattern):745 """Validate the check works also on dataframes with None values"""746 check_none_failures(747 series_values, Check.str_contains(pattern, ignore_na=False)748 )749class TestStrStartsWith:750 """Tests for Check.str_startswith"""751 @staticmethod752 @pytest.mark.parametrize(753 "series_values, pattern",754 [755 (("abc", "abcdef"), "ab"),756 # Ensure regex patterns are ignored757 ((r"$a\dbc", r"$a\dbcdef"), r"$a\d"),758 ],759 )760 def test_succeeding(series_values, pattern):761 """Run checks which should succeed"""762 check_values(series_values, Check.str_startswith(pattern), {})763 @staticmethod764 @pytest.mark.parametrize(765 "series_values, pattern, failure_cases",766 [767 (("abc", "abcdef", " abc"), "ab", {" abc"}),768 ((r"abc def", r"def abc"), "def", {"abc def"}),769 ],770 )771 def test_failing(series_values, pattern, failure_cases):772 """Run checks which should fail"""773 check_values(774 series_values, Check.str_startswith(pattern), failure_cases775 )776 check_raise_error_or_warning(777 series_values, Check.str_startswith(pattern)778 )779 @staticmethod780 @pytest.mark.parametrize(781 "series_values, pattern",782 [783 ((None, "abc", "abcdef"), "ab"),784 ],785 )786 def test_failing_with_none(series_values, pattern):787 """Run checks which should succeed"""788 check_none_failures(789 series_values, Check.str_startswith(pattern, ignore_na=False)790 )791class TestStrEndsWith:792 """Tests for Check.str_endswith"""793 @staticmethod794 @pytest.mark.parametrize(795 "series_values, pattern",796 [797 (("abc", "defabc"), "bc"),798 # Ensure regex patterns are ignored799 ((r"bc^a\d", r"abc^a\d"), r"^a\d"),800 ],801 )802 def test_succeeding(series_values, pattern):803 """Run checks which should succeed"""804 check_values(series_values, Check.str_endswith(pattern), {})805 @staticmethod806 @pytest.mark.parametrize(807 "series_values, pattern, failure_cases",808 [809 (("abc", "abcdef", " abc"), "bc", {"abcdef"}),810 (("abc", "abc "), "bc", {"abc "}),811 ((r"abc def", r"def abc"), "def", {"def abc"}),812 ],813 )814 def test_failing(series_values, pattern, failure_cases):815 """Run checks which should fail"""816 check_values(series_values, Check.str_endswith(pattern), failure_cases)817 check_raise_error_or_warning(818 series_values, Check.str_endswith(pattern)819 )820 @staticmethod821 @pytest.mark.parametrize(822 "series_values, pattern",823 [824 ((None, "abc", "defabc"), "bc"),825 ],826 )827 def test_failing_with_none(series_values, pattern):828 """Run checks which should succeed"""829 check_none_failures(830 series_values, Check.str_endswith(pattern, ignore_na=False)831 )832class TestStrLength:833 """Tests for Check.str_length"""834 @staticmethod835 def test_argument_check():836 """Test if at least one argument is enforced"""837 with pytest.raises(ValueError):838 Check.str_length()839 @staticmethod840 @pytest.mark.parametrize(841 "series_values, min_len, max_len",842 [843 (("abc", "defabc"), 1, 6),844 (("abc", "defabc"), None, 6),845 (("abc", "defabc"), 1, None),846 ],847 )848 def test_succeeding(series_values, min_len, max_len):849 """Run checks which should succeed"""850 check_values(series_values, Check.str_length(min_len, max_len), {})851 @staticmethod852 @pytest.mark.parametrize(853 "series_values, min_len, max_len, failure_cases",854 [855 (("abc", "defabc"), 1, 5, {"defabc"}),856 (("abc", "defabc"), None, 5, {"defabc"}),857 (("abc", "defabc"), 4, None, {"abc"}),858 ],859 )860 def test_failing(series_values, min_len, max_len, failure_cases):861 """Run checks which should fail"""862 check_values(863 series_values, Check.str_length(min_len, max_len), failure_cases864 )865 check_raise_error_or_warning(866 series_values, Check.str_length(min_len, max_len)867 )868 @staticmethod869 @pytest.mark.parametrize(870 "series_values, min_len, max_len",871 [872 ((None, "defabc"), 1, 6),873 ((None, "defabc"), None, 6),874 ((None, "defabc"), 1, None),875 ],876 )877 def test_failing_with_none(series_values, min_len, max_len):878 """Run checks which should succeed"""879 check_none_failures(880 series_values, Check.str_length(min_len, max_len, ignore_na=False)...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!