How to use test_succeeding method in pandera

Best Python code snippet using pandera_python

test_checks_builtin.py

Source:test_checks_builtin.py Github

copy

Full Screen

...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(...

Full Screen

Full Screen

test_success_messages.py

Source:test_success_messages.py Github

copy

Full Screen

1# Copyright (C) 2018 Leiden University Medical Center2#3# This program is free software: you can redistribute it and/or modify4# it under the terms of the GNU Affero General Public License as5# published by the Free Software Foundation, either version 3 of the6# License, or (at your option) any later version.7#8# This program is distributed in the hope that it will be useful,9# but WITHOUT ANY WARRANTY; without even the implied warranty of10# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the11# GNU Affero General Public License for more details.12#13# You should have received a copy of the GNU Affero General Public License14# along with this program. If not, see <https://www.gnu.org/licenses/15"""Tests the success messages"""16import shutil17import subprocess # nosec18import textwrap19from pathlib import Path20import pytest21MOO_FILE = textwrap.dedent("""\22- name: moo file23 # Gruesome command injection here. This is for testing purposes only.24 # Do not try this at home.25 command: "bash -c 'echo moo > moo.txt'"26 files:27 - path: "moo.txt"28 contains:29 - "moo"30 must_not_contain:31 - "Cock a doodle doo" # Unless our cow has severe identity disorders32 md5sum: e583af1f8b00b53cda87ae9ead88022433""")34SIMPLE_ECHO = textwrap.dedent("""\35- name: simple echo36 command: "echo moo"37 files:38 - path: "moo.txt"39 should_exist: false40 stdout:41 contains:42 - "moo"43 must_not_contain:44 - "Cock a doodle doo"45""")46FAILING_GREP = textwrap.dedent("""\47- name: failing grep48 command: "grep"49 stdout:50 must_not_contain:51 - "grep" # stdout should be empty52 stderr:53 contains:54 - "'grep --help'"55 exit_code: 256""")57ZIPPED_FILE = textwrap.dedent("""\58- name: zipped file59 command: bash -c 'echo moo | gzip -c > moo.gz'60 files:61 - path: moo.gz62 contains:63 - moo64""")65REGEX_FILE = textwrap.dedent("""\66- name: regex67 command: bash -c 'echo Hello, world'68 stdout:69 contains_regex:70 - "ello"71 - '^H.*d$'72 must_not_contain_regex:73 - "Hello.*world!"74""")75SUCCEEDING_TESTS_YAML = (MOO_FILE + SIMPLE_ECHO + FAILING_GREP + ZIPPED_FILE +76 REGEX_FILE)77SUCCESS_MESSAGES = [78 ["test_succeeding.yml::moo file::exit code should be 0 PASSED"],79 ["test_succeeding.yml::moo file::moo.txt::content::contains 'moo' PASSED"],80 ["test_succeeding.yml::moo file::moo.txt::content::does not contain 'Cock a doodle doo' PASSED"], # noqa: E50181 ["test_succeeding.yml::moo file::moo.txt::md5sum PASSED"],82 ["test_succeeding.yml::moo file::moo.txt::should exist PASSED"],83 ["test_succeeding.yml::simple echo::exit code should be 0 PASSED"],84 ["test_succeeding.yml::simple echo::moo.txt::should not exist PASSED"],85 ["test_succeeding.yml::simple echo::stdout::contains 'moo' PASSED"],86 ["test_succeeding.yml::simple echo::stdout::does not contain 'Cock a doodle doo' PASSED"], # noqa: E50187 ["test_succeeding.yml::simple echo::exit code should be 0 PASSED"],88 ["test_succeeding.yml::failing grep::exit code should be 2 PASSED"],89 ["test_succeeding.yml::failing grep::stdout::does not contain 'grep' PASSED"], # noqa: E50190 ["test_succeeding.yml::failing grep::stderr::contains ''grep --help''"],91 ["test_succeeding.yml::zipped file::moo.gz::content::contains 'moo' PASSED"], # noqa: E50192 ["moo file:\n\tcommand: bash -c 'echo moo > moo.txt'\n\tdirectory"],93 ["'moo file' done."],94 ["test_succeeding.yml::regex::exit code should be 0 PASSED"],95 ["test_succeeding.yml::regex::stdout::contains 'ello' PASSED"],96 ["test_succeeding.yml::regex::stdout::contains '^H.*d$' PASSED"],97 ["test_succeeding.yml::regex::stdout::does not contain 'Hello.*world!' PASSED"] # noqa: E50198]99@pytest.fixture(scope="session")100def succeeding_tests_output(tmp_path_factory: pytest.TempPathFactory):101 """This fixture was written because the pytester function has a default102 scope of 'function'. This is very inefficient when testing multiple103 success messages in the output as the whole test yaml with all commands104 has to be run again.105 This fixture runs the succeeding tests once with pytest -v"""106 tempdir = tmp_path_factory.mktemp("succeeding_tests")107 test_file = Path(tempdir, "test_succeeding.yml")108 with test_file.open("w") as file_handler:109 file_handler.write(SUCCEEDING_TESTS_YAML)110 process_out = subprocess.run(args=["pytest", "-v"], # nosec111 stdout=subprocess.PIPE,112 stderr=subprocess.PIPE,113 cwd=tempdir)114 yield process_out.stdout.decode()115 shutil.rmtree(tempdir)116@pytest.mark.parametrize(["message"], SUCCESS_MESSAGES)117def test_message_in_result(message: str, succeeding_tests_output):118 assert message in succeeding_tests_output119def test_message_success_no_errors_or_fails(succeeding_tests_output):120 assert "ERROR" not in succeeding_tests_output121 assert "FAIL" not in succeeding_tests_output122def test_message_directory_kept_no_errors_or_fails(pytester):123 pytester.makefile(".yml", test=SUCCEEDING_TESTS_YAML)124 result = pytester.runpytest("-v", "--keep-workflow-wd")125 assert "ERROR" not in result.stdout.str()...

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run pandera automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful