How to use validate_sample method in pandera

Best Python code snippet using pandera_python

test_constraints.py

Source:test_constraints.py Github

copy

Full Screen

...132 with raises(ValueError):133 Constraints(cons_list, space)134 # Check that validate_sample validates samples correctly135 cons = Constraints([Sum((0, 1), 6)], space)136 assert not cons.validate_sample([0.0, 7, "a"])137 assert not cons.validate_sample([7.0, 0, "a"])138 assert not cons.validate_sample([3.00001, 3, "a"])139 assert cons.validate_sample([2.99999, 3, "a"])140 # Check that validate_sample validates samples correctly141 # for less_than = False142 cons = Constraints([Sum((0, 1), 6, less_than=False)], space)143 assert cons.validate_sample([0.0, 7, "a"])144 assert cons.validate_sample([7.0, 0, "a"])145 assert cons.validate_sample([3.00001, 3, "a"])146 assert not cons.validate_sample([2.99999, 3, "a"])147 # Check that only valid samples are drawn148 samples = cons.rvs(n_samples=1000)149 for sample in samples:150 assert cons.validate_sample(sample)151@pytest.mark.fast_test152def test_Conditional():153 # Test conditional constraints154 space = Space([list("ab"), list("ab"), list("ab"), [1, 4], [1, 4], [1, 4]])155 condition = Single(0, "a", "categorical")156 if_true = Inclusive(3, [1, 2], "integer")157 if_false = Exclusive(3, [1, 2], "integer")158 # Test init of constriants159 Conditional(condition, if_true, if_false)160 Conditional(condition, if_true=if_true, if_false=if_false)161 cons_list = [Conditional(condition, if_true=if_true, if_false=if_false)]162 cons = Constraints(cons_list, space)163 # Check that only valid samples are being drawn164 samples = cons.rvs(n_samples=100)165 for sample in samples:166 if sample[0] == "a":167 assert sample[3] < 3168 else:169 assert sample[3] > 2170 # Test that validate_sample works171 assert not cons.validate_sample(["a", "a", "a", 3, 3, 3])172 assert cons.validate_sample(["a", "a", "a", 2, 3, 3])173 assert not cons.validate_sample(["b", "a", "a", 2, 3, 3])174 assert cons.validate_sample(["b", "a", "a", 3, 3, 3])175 # Test list of constraints176 cons = Constraints(177 [178 Conditional(179 Single(0, "a", "categorical"),180 if_true=[181 Single(1, "a", "categorical"),182 Single(2, "a", "categorical"),183 ],184 if_false=[185 Single(1, "b", "categorical"),186 Single(2, "b", "categorical"),187 ],188 )189 ],190 space,191 )192 assert not cons.validate_sample(["a", "a", "b", 3, 3, 3])193 assert cons.validate_sample(["a", "a", "a", 3, 3, 3])194 assert not cons.validate_sample(["b", "a", "b", 3, 3, 3])195 assert cons.validate_sample(["b", "b", "b", 3, 3, 3])196 # Test nested contraints197 cons = Constraints(198 [199 Conditional(200 Inclusive(3, [1, 2], "integer"),201 Conditional(202 Single(0, "a", "categorical"),203 if_true=[204 Single(1, "a", "categorical"),205 Single(2, "a", "categorical"),206 ],207 if_false=[208 Single(1, "b", "categorical"),209 Single(2, "b", "categorical"),210 ],211 ),212 Conditional(213 Single(0, "a", "categorical"),214 if_true=[215 Single(1, "b", "categorical"),216 Single(2, "b", "categorical"),217 ],218 if_false=[219 Single(1, "a", "categorical"),220 Single(2, "a", "categorical"),221 ],222 ),223 )224 ],225 space,226 )227 assert not cons.validate_sample(["a", "a", "b", 2, 3, 3])228 assert cons.validate_sample(["a", "a", "a", 2, 3, 3])229 assert not cons.validate_sample(["b", "a", "b", 2, 3, 3])230 assert cons.validate_sample(["b", "b", "b", 2, 3, 3])231 assert cons.validate_sample(["a", "b", "b", 3, 3, 3])232 assert not cons.validate_sample(["a", "a", "a", 3, 3, 3])233 assert cons.validate_sample(["b", "a", "a", 3, 3, 3])234 assert not cons.validate_sample(["b", "b", "b", 3, 3, 3])235 samples = cons.rvs(n_samples=100)236 for sample in samples:237 assert cons.validate_sample(sample)238@pytest.mark.fast_test239def test_check_constraints():240 space = Space([(0.0, 5.0), (1.0, 5.0)])241 cons_list = Single(0, 1, "integer")242 # Constraints_list must be a list243 with raises(TypeError):244 check_constraints(space, cons_list)245 # Constraints dimension must be less than number of dimensions in space246 cons_list = [Single(2, 1, "integer")]247 with raises(IndexError):248 check_constraints(space, cons_list)249 # Constraints dimension_types must be the same as corresponding dimension250 # in space251 space = Space([(0.0, 5.0), (0, 5), ["a", "b", "c"]])252 cons_list = [Single(2, 1, "integer")]253 with raises(TypeError):254 check_constraints(space, cons_list)255 cons_list = [Single(1, 1.0, "real")]256 with raises(TypeError):257 check_constraints(space, cons_list)258 cons_list = [Single(0, "a", "categorical")]259 with raises(TypeError):260 check_constraints(space, cons_list)261 # Only one Single constraint per dimension262 cons_list = [Single(0, 1.0, "real"), Single(0, 2.0, "real")]263 with raises(IndexError):264 check_constraints(space, cons_list)265@pytest.mark.fast_test266def test_check_bounds():267 # Check that no error is raised when using valid bounds268 space = Space([(0.0, 5.0), (0, 5), ["a", "b", "c", 1, 1.0]])269 check_bounds(space.dimensions[0], (1.0, 2.0))270 check_bounds(space.dimensions[1], (1, 3))271 check_bounds(space.dimensions[2], ("a", "b"))272 check_bounds(space.dimensions[2], ("a", 1.0))273 # Check that error is raised when using invalid bounds274 with raises(ValueError):275 check_bounds(space.dimensions[0], (-1.0, 2.0))276 with raises(ValueError):277 check_bounds(space.dimensions[0], (2.0, 10.0))278 with raises(ValueError):279 check_bounds(space.dimensions[1], (-1, 2))280 with raises(ValueError):281 check_bounds(space.dimensions[1], (2, 10))282 with raises(ValueError):283 check_bounds(space.dimensions[2], ("k", -1.0))284 with raises(ValueError):285 check_bounds(space.dimensions[2], ("a", "b", "c", 1.2))286@pytest.mark.fast_test287def test_check_value():288 # Test that no error is raised when using a valid value289 space = Space([(0.0, 5.0), (0, 5), ["a", "b", "c", 1, 1.0]])290 check_value(space.dimensions[0], 1.0)291 check_value(space.dimensions[1], 1)292 check_value(space.dimensions[2], "b")293 check_value(space.dimensions[2], 1)294 check_value(space.dimensions[2], 1.0)295 # Test that error is raised when using an invalid value296 with raises(ValueError):297 check_value(space.dimensions[0], 10.0)298 with raises(ValueError):299 check_value(space.dimensions[0], -1.0)300 with raises(ValueError):301 check_value(space.dimensions[1], 10)302 with raises(ValueError):303 check_value(space.dimensions[1], -1)304 with raises(ValueError):305 check_value(space.dimensions[2], "wow")306 with raises(ValueError):307 check_value(space.dimensions[2], 1.2)308@pytest.mark.fast_test309def test_single_validate_constraint():310 # Test categorical311 cons = Single(0, 1.0, "categorical")312 assert cons._validate_constraint(1.0)313 assert not cons._validate_constraint(1.1)314 cons = Single(0, "a", "categorical")315 assert cons._validate_constraint("a")316 assert not cons._validate_constraint("b")317 # Test real318 cons = Single(0, 1.0, "real")319 assert cons._validate_constraint(1.0)320 assert not cons._validate_constraint(1.1)321 # Test integer322 cons = Single(0, 1, "integer")323 assert cons._validate_constraint(1)324 assert not cons._validate_constraint(2)325@pytest.mark.fast_test326def test_Constraints_init():327 space = Space(328 [329 Real(1, 10),330 Real(1, 10),331 Real(1, 10),332 Integer(0, 10),333 Integer(0, 10),334 Integer(0, 10),335 Categorical(list("abcdefg")),336 Categorical(list("abcdefg")),337 Categorical(list("abcdefg")),338 ]339 )340 cons_list = [341 Single(0, 5.0, "real"),342 Inclusive(1, (3.0, 5.0), "real"),343 Exclusive(2, (3.0, 5.0), "real"),344 Single(3, 5, "integer"),345 Inclusive(4, (3, 5), "integer"),346 Exclusive(5, (3, 5), "integer"),347 Single(6, "b", "categorical"),348 Inclusive(7, ("c", "d", "e"), "categorical"),349 Exclusive(8, ("c", "d", "e"), "categorical"),350 # Note that two ocnstraints are being added to dimension 4 and 5351 Inclusive(4, (7, 9), "integer"),352 Exclusive(5, (7, 9), "integer"),353 ]354 cons = Constraints(cons_list, space)355 # Test that space and constriants_list are being saved in object356 assert_equal(cons.space, space)357 assert_equal(cons.constraints_list, cons_list)358 # Test that a correct list of single constraints have been made359 assert_equal(len(cons.single), space.n_dims)360 assert_equal(cons.single[1], None)361 assert_equal(cons.single[-1], None)362 assert not cons.single[0] == None363 assert not cons.single[6] == None364 # Test that a correct list of inclusive constraints have been made365 assert_equal(len(cons.inclusive), space.n_dims)366 assert_equal(cons.inclusive[0], [])367 assert_equal(cons.inclusive[2], [])368 assert not cons.inclusive[1] == []369 assert not cons.inclusive[7] == []370 assert_equal(len(cons.inclusive[4]), 2)371 # Test that a correct list of exclusive constraints have been made372 assert_equal(len(cons.exclusive), space.n_dims)373 assert_equal(cons.exclusive[3], [])374 assert_equal(cons.exclusive[7], [])375 assert not cons.exclusive[2] == []376 assert not cons.exclusive[5] == []377 assert_equal(len(cons.exclusive[5]), 2)378@pytest.mark.fast_test379def test_Constraints_validate_sample():380 space = Space(381 [382 Real(1, 10),383 Real(1, 10),384 Real(1, 10),385 Integer(0, 10),386 Integer(0, 10),387 Integer(0, 10),388 Categorical(list("abcdefg")),389 Categorical(list("abcdefg")),390 Categorical(list("abcdefg")),391 ]392 )393 # Test validation of single constraints394 cons_list = [Single(0, 5.0, "real")]395 cons = Constraints(cons_list, space)396 sample = [0] * space.n_dims397 sample[0] = 5.0398 assert cons.validate_sample(sample)399 sample[0] = 5.00001400 assert not cons.validate_sample(sample)401 sample[0] = 4.99999402 assert not cons.validate_sample(sample)403 cons_list = [Single(3, 5, "integer")]404 cons = Constraints(cons_list, space)405 sample = [0] * space.n_dims406 sample[3] = 5407 assert cons.validate_sample(sample)408 sample[3] = 6409 assert not cons.validate_sample(sample)410 sample[3] = -5411 assert not cons.validate_sample(sample)412 sample[3] = 5.000001413 assert not cons.validate_sample(sample)414 cons_list = [Single(6, "a", "categorical")]415 cons = Constraints(cons_list, space)416 sample = [0] * space.n_dims417 sample[6] = "a"418 assert cons.validate_sample(sample)419 sample[6] = "b"420 assert not cons.validate_sample(sample)421 sample[6] = -5422 assert not cons.validate_sample(sample)423 sample[6] = 5.000001424 assert not cons.validate_sample(sample)425 # Test validation of inclusive constraints426 cons_list = [Inclusive(0, (5.0, 7.0), "real")]427 cons = Constraints(cons_list, space)428 sample = [0] * space.n_dims429 sample[0] = 5.0430 assert cons.validate_sample(sample)431 sample[0] = 7.0432 assert cons.validate_sample(sample)433 sample[0] = 7.00001434 assert not cons.validate_sample(sample)435 sample[0] = 4.99999436 assert not cons.validate_sample(sample)437 sample[0] = -10438 assert not cons.validate_sample(sample)439 cons_list = [Inclusive(3, (5, 7), "integer")]440 cons = Constraints(cons_list, space)441 sample = [0] * space.n_dims442 sample[3] = 5443 assert cons.validate_sample(sample)444 sample[3] = 6445 assert cons.validate_sample(sample)446 sample[3] = 7447 assert cons.validate_sample(sample)448 sample[3] = 8449 assert not cons.validate_sample(sample)450 sample[3] = 4451 assert not cons.validate_sample(sample)452 sample[3] = -4453 assert not cons.validate_sample(sample)454 cons_list = [Inclusive(6, ("c", "d", "e"), "categorical")]455 cons = Constraints(cons_list, space)456 sample = [0] * space.n_dims457 sample[6] = "c"458 assert cons.validate_sample(sample)459 sample[6] = "e"460 assert cons.validate_sample(sample)461 sample[6] = "f"462 assert not cons.validate_sample(sample)463 sample[6] = -5464 assert not cons.validate_sample(sample)465 sample[6] = 3.3466 assert not cons.validate_sample(sample)467 sample[6] = "a"468 assert not cons.validate_sample(sample)469 # Test validation of exclusive constraints470 cons_list = [Exclusive(0, (5.0, 7.0), "real")]471 cons = Constraints(cons_list, space)472 sample = [0] * space.n_dims473 sample[0] = 5.0474 assert not cons.validate_sample(sample)475 sample[0] = 7.0476 assert not cons.validate_sample(sample)477 sample[0] = 7.00001478 assert cons.validate_sample(sample)479 sample[0] = 4.99999480 assert cons.validate_sample(sample)481 sample[0] = -10482 assert cons.validate_sample(sample)483 cons_list = [Exclusive(3, (5, 7), "integer")]484 cons = Constraints(cons_list, space)485 sample = [0] * space.n_dims486 sample[3] = 5487 assert not cons.validate_sample(sample)488 sample[3] = 6489 assert not cons.validate_sample(sample)490 sample[3] = 7491 assert not cons.validate_sample(sample)492 sample[3] = 8493 assert cons.validate_sample(sample)494 sample[3] = 4495 assert cons.validate_sample(sample)496 sample[3] = -4497 assert cons.validate_sample(sample)498 cons_list = [Exclusive(3, (5, 5), "integer")]499 cons = Constraints(cons_list, space)500 sample = [0] * space.n_dims501 sample[3] = 5502 assert not cons.validate_sample(sample)503 cons_list = [Exclusive(6, ("c", "d", "e"), "categorical")]504 cons = Constraints(cons_list, space)505 sample = [0] * space.n_dims506 sample[6] = "c"507 assert not cons.validate_sample(sample)508 sample[6] = "e"509 assert not cons.validate_sample(sample)510 sample[6] = "f"511 assert cons.validate_sample(sample)512 sample[6] = -5513 assert cons.validate_sample(sample)514 sample[6] = 3.3515 assert cons.validate_sample(sample)516 sample[6] = "a"517 assert cons.validate_sample(sample)518 # Test more than one constraint per dimension519 cons_list = [520 Inclusive(0, (1.0, 2.0), "real"),521 Inclusive(0, (3.0, 4.0), "real"),522 Inclusive(0, (5.0, 6.0), "real"),523 ]524 cons = Constraints(cons_list, space)525 sample = [0] * space.n_dims526 sample[0] = 1.3527 assert cons.validate_sample(sample)528 sample[0] = 6.0529 assert cons.validate_sample(sample)530 sample[0] = 5.0531 assert cons.validate_sample(sample)532 sample[0] = 3.0533 assert cons.validate_sample(sample)534 sample[0] = 4.0535 assert cons.validate_sample(sample)536 sample[0] = 5.5537 assert cons.validate_sample(sample)538 sample[0] = 2.1539 assert not cons.validate_sample(sample)540 sample[0] = 4.9541 assert not cons.validate_sample(sample)542 sample[0] = 7.0543 assert not cons.validate_sample(sample)544 cons_list = [545 Exclusive(0, (1.0, 2.0), "real"),546 Exclusive(0, (3.0, 4.0), "real"),547 Exclusive(0, (5.0, 6.0), "real"),548 ]549 cons = Constraints(cons_list, space)550 sample = [0] * space.n_dims551 sample[0] = 1.3552 assert not cons.validate_sample(sample)553 sample[0] = 6.0554 assert not cons.validate_sample(sample)555 sample[0] = 5.0556 assert not cons.validate_sample(sample)557 sample[0] = 3.0558 assert not cons.validate_sample(sample)559 sample[0] = 4.0560 assert not cons.validate_sample(sample)561 sample[0] = 5.5562 assert not cons.validate_sample(sample)563 sample[0] = 2.1564 assert cons.validate_sample(sample)565 sample[0] = 4.9566 assert cons.validate_sample(sample)567 sample[0] = 7.0568 assert cons.validate_sample(sample)569@pytest.mark.slow_test570def test_constraints_rvs():571 space = Space(572 [573 Real(1, 10),574 Real(1, 10),575 Real(1, 10),576 Integer(0, 10),577 Integer(0, 10),578 Integer(0, 10),579 Categorical(list("abcdefg")),580 Categorical(list("abcdefg")),581 Categorical(list("abcdefg")),582 ]...

Full Screen

Full Screen

test_validation.py

Source:test_validation.py Github

copy

Full Screen

...22 :return:23 '''24 sample = get_sample(sample_mimetype=None, data_mimetype=None)25 result_id = 126 result = tks_provider.validate_sample(sample, validation_id=result_id)27 check_validation_result(result)28 assert result.contribution is None29 assert result.message_code_id == 'PROVIDER_MISSING_MIMETYPE'30 assert result.error_message == 'Missing mimetype.'31 assert result.status == 232def test_different_mimetypes(tks_provider):33 '''34 Test different mimetypes35 :param tks_provider:36 :return:37 '''38 sample = get_sample(sample_mimetype='image/png')39 result_id = 140 result = tks_provider.validate_sample(sample, validation_id=result_id)41 check_validation_result(result)42 assert result.contribution is None43 assert result.message_code_id == 'PROVIDER_INVALID_MIMETYPE'44 assert result.error_message == 'Mimetype in sample data differs from sample mimetype'45 assert result.status == 246def test_unsuported_mimetypes(tks_provider):47 '''48 Test unsuported mimetypes49 :param tks_provider:50 :return:51 '''52 sample = get_sample(sample_mimetype='image/other', data_mimetype='image/other')53 result_id = 154 result = tks_provider.validate_sample(sample, validation_id=result_id)55 check_validation_result(result)56 assert result.contribution is None57 assert result.message_code_id == 'PROVIDER_INVALID_MIMETYPE'58 assert result.error_message == \59 f"Invalid mimetype. Accepted types are: [{', '.join(tks_provider.accepted_mimetypes)}]"60 assert result.status == 261def test_mimetype_both(tks_provider):62 '''63 Test mimetype both64 :param tks_provider:65 :return:66 '''67 sample = get_sample()68 result_id = 169 result = tks_provider.validate_sample(sample, validation_id=result_id)70 check_validation_result(result)71 assert result.contribution > 072 assert result.message_code_id is None73 assert result.error_message is None74 assert result.status == 175 assert result.info is None76def test_mimetype_sample(tks_provider):77 '''78 Test mimetype sample79 :param tks_provider:80 :return:81 '''82 sample = get_sample(sample_mimetype=None)83 result_id = 184 result = tks_provider.validate_sample(sample, validation_id=result_id)85 check_validation_result(result)86 assert result.contribution > 087 assert result.message_code_id is None88 assert result.error_message is None89 assert result.status == 190 assert result.info is None91def test_invalid_sample_datab64(tks_provider):92 '''93 Test invalid sample datab6494 :param tks_provider:95 :return:96 '''97 sample = get_sample(ks_data='this is not a b64')98 result_id = 199 result = tks_provider.validate_sample(sample, validation_id=result_id)100 check_validation_result(result)101 assert result.contribution is None102 assert result.message_code_id == 'PROVIDER_INVALID_SAMPLE_DATA'103 assert result.error_message == 'Invalid image format in sample data.'104 assert result.status == 2105def test_invalid_sample_data(tks_provider):106 '''107 Test invalid sample data108 :param tks_provider:109 :return:110 '''111 sample = get_sample(ks_data=base64.b64encode(b'this is not a b64').decode())112 result_id = 1113 result = tks_provider.validate_sample(sample, validation_id=result_id)114 check_validation_result(result)115 assert result.contribution is None116 assert result.message_code_id == 'PROVIDER_INVALID_SAMPLE_DATA'117 assert result.error_message == 'Invalid image format in sample data.'...

Full Screen

Full Screen

test_remove_invalid_lines.py

Source:test_remove_invalid_lines.py Github

copy

Full Screen

...16 "testing testing one two three or maybe 10 4",17 "testing testing one two three",18 "testing testing, one two three!",19)20def validate_sample(ext, expected_transcripts, out_segments):21 base_output = f"{test_dir}/good"22 convert(f"{sample_dir}/invalid.stm", base_output + ext)23 validated_transcript = time_aligned_text(base_output + ext)24 assert len(validated_transcript.segments) == out_segments25 for seg, expected_text in zip(validated_transcript.segments, expected_transcripts):26 assert seg.text == expected_text27def test_stm_to_stm():28 " Test stm to stm validation "29 validate_sample(".stm", EXPECTED_UNFORMATTED_TRANSCRIPTS, 3)30def test_stm_to_json():31 " Test stm to gk json validation "32 validate_sample(".json", EXPECTED_FORMATTED_TRANSCRIPTS, 3)33def test_stm_to_vtt():34 " Test stm to vtt validation "35 validate_sample(".vtt", EXPECTED_FORMATTED_TRANSCRIPTS, 3)36def test_stm_to_html():37 " Test stm to html validation "38 validate_sample(".html", EXPECTED_FORMATTED_TRANSCRIPTS, 3)39def test_stm_to_srt():40 " Test stm to gk json validation "41 validate_sample(".srt", EXPECTED_FORMATTED_TRANSCRIPTS, 3)42def test_stm_to_txt():43 " Test stm to gk json validation "44 validate_sample(".txt", EXPECTED_FORMATTED_TRANSCRIPTS, 3)45if __name__ == "__main__":46 import sys47 import pytest...

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