-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathtest_validate.py
7606 lines (6406 loc) · 245 KB
/
test_validate.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import pathlib
import pprint
import sys
import re
from unittest.mock import patch
import pytest
import pandas as pd
import polars as pl
import ibis
from datetime import datetime
import great_tables as GT
import narwhals as nw
from pointblank.validate import (
Validate,
load_dataset,
preview,
missing_vals_tbl,
get_column_count,
get_row_count,
PointblankConfig,
_ValidationInfo,
_process_title_text,
_get_default_title_text,
_fmt_lg,
_create_table_time_html,
_create_table_type_html,
)
from pointblank.thresholds import Thresholds
from pointblank.schema import Schema, _get_schema_validation_info
from pointblank.column import (
col,
starts_with,
ends_with,
contains,
matches,
everything,
first_n,
last_n,
)
TBL_LIST = [
"tbl_pd",
"tbl_pl",
"tbl_parquet",
"tbl_duckdb",
"tbl_sqlite",
]
TBL_MISSING_LIST = [
"tbl_missing_pd",
"tbl_missing_pl",
"tbl_missing_parquet",
"tbl_missing_duckdb",
"tbl_missing_sqlite",
]
TBL_DATES_TIMES_TEXT_LIST = [
"tbl_dates_times_text_pd",
"tbl_dates_times_text_pl",
"tbl_dates_times_text_parquet",
"tbl_dates_times_text_duckdb",
"tbl_dates_times_text_sqlite",
]
@pytest.fixture
def tbl_pd():
return pd.DataFrame({"x": [1, 2, 3, 4], "y": [4, 5, 6, 7], "z": [8, 8, 8, 8]})
@pytest.fixture
def tbl_missing_pd():
return pd.DataFrame({"x": [1, 2, pd.NA, 4], "y": [4, pd.NA, 6, 7], "z": [8, pd.NA, 8, 8]})
@pytest.fixture
def tbl_dates_times_text_pd():
return pd.DataFrame(
{
"date": ["2021-01-01", "2021-02-01", pd.NA],
"dttm": ["2021-01-01 00:00:00", pd.NA, "2021-02-01 00:00:00"],
"text": [pd.NA, "5-egh-163", "8-kdg-938"],
}
)
@pytest.fixture
def tbl_pl():
return pl.DataFrame({"x": [1, 2, 3, 4], "y": [4, 5, 6, 7], "z": [8, 8, 8, 8]})
@pytest.fixture
def tbl_missing_pl():
return pl.DataFrame({"x": [1, 2, None, 4], "y": [4, None, 6, 7], "z": [8, None, 8, 8]})
@pytest.fixture
def tbl_dates_times_text_pl():
return pl.DataFrame(
{
"date": ["2021-01-01", "2021-02-01", None],
"dttm": ["2021-01-01 00:00:00", None, "2021-02-01 00:00:00"],
"text": [None, "5-egh-163", "8-kdg-938"],
}
)
@pytest.fixture
def tbl_parquet():
file_path = pathlib.Path.cwd() / "tests" / "tbl_files" / "tbl_xyz.parquet"
return ibis.read_parquet(file_path)
@pytest.fixture
def tbl_missing_parquet():
file_path = pathlib.Path.cwd() / "tests" / "tbl_files" / "tbl_xyz_missing.parquet"
return ibis.read_parquet(file_path)
@pytest.fixture
def tbl_dates_times_text_parquet():
file_path = pathlib.Path.cwd() / "tests" / "tbl_files" / "tbl_dates_times_text.parquet"
return ibis.read_parquet(file_path)
@pytest.fixture
def tbl_duckdb():
file_path = pathlib.Path.cwd() / "tests" / "tbl_files" / "tbl_xyz.ddb"
return ibis.connect(f"duckdb://{file_path}").table("tbl_xyz")
@pytest.fixture
def tbl_missing_duckdb():
file_path = pathlib.Path.cwd() / "tests" / "tbl_files" / "tbl_xyz_missing.ddb"
return ibis.connect(f"duckdb://{file_path}").table("tbl_xyz_missing")
@pytest.fixture
def tbl_dates_times_text_duckdb():
file_path = pathlib.Path.cwd() / "tests" / "tbl_files" / "tbl_dates_times_text.ddb"
return ibis.connect(f"duckdb://{file_path}").table("tbl_dates_times_text")
@pytest.fixture
def tbl_sqlite():
file_path = pathlib.Path.cwd() / "tests" / "tbl_files" / "tbl_xyz.sqlite"
return ibis.sqlite.connect(file_path).table("tbl_xyz")
@pytest.fixture
def tbl_missing_sqlite():
file_path = pathlib.Path.cwd() / "tests" / "tbl_files" / "tbl_xyz_missing.sqlite"
return ibis.sqlite.connect(file_path).table("tbl_xyz_missing")
@pytest.fixture
def tbl_dates_times_text_sqlite():
file_path = pathlib.Path.cwd() / "tests" / "tbl_files" / "tbl_dates_times_text.sqlite"
return ibis.sqlite.connect(file_path).table("tbl_dates_times_text")
@pytest.fixture
def tbl_pl_variable_names():
return pl.DataFrame(
{
"word": ["apple", "banana"],
"low_numbers": [1, 2],
"high_numbers": [13500, 95000],
"low_floats": [41.6, 41.2],
"high_floats": [41.6, 41.2],
"superhigh_floats": [23453.23, 32453532.33],
"date": ["2021-01-01", "2021-01-02"],
"datetime": ["2021-01-01 00:00:00", "2021-01-02 00:00:00"],
"bools": [True, False],
}
)
@pytest.fixture
def tbl_pd_variable_names():
return pd.DataFrame(
{
"word": ["apple", "banana"],
"low_numbers": [1, 2],
"high_numbers": [13500, 95000],
"low_floats": [41.6, 41.2],
"high_floats": [41.6, 41.2],
"superhigh_floats": [23453.23, 32453532.33],
"date": ["2021-01-01", "2021-01-02"],
"datetime": ["2021-01-01 00:00:00", "2021-01-02 00:00:00"],
"bools": [True, False],
}
)
@pytest.fixture
def tbl_memtable_variable_names():
return ibis.memtable(
pd.DataFrame(
{
"word": ["apple", "banana"],
"low_numbers": [1, 2],
"high_numbers": [13500, 95000],
"low_floats": [41.6, 41.2],
"high_floats": [41.6, 41.2],
"superhigh_floats": [23453.23, 32453532.33],
"date": ["2021-01-01", "2021-01-02"],
"datetime": ["2021-01-01 00:00:00", "2021-01-02 00:00:00"],
"bools": [True, False],
}
)
)
@pytest.fixture
def tbl_schema_tests():
return pl.DataFrame(
{
"a": ["apple", "banana", "cherry", "date"],
"b": [1, 6, 3, 5],
"c": [1.1, 2.2, 3.3, 4.4],
}
)
def test_validation_info():
v = _ValidationInfo(
i=1,
i_o=1,
step_id="col_vals_gt",
sha1="a",
assertion_type="col_vals_gt",
column="x",
values=0,
inclusive=True,
na_pass=False,
thresholds=Thresholds(),
label=None,
brief=None,
active=True,
eval_error=False,
all_passed=True,
n=4,
n_passed=4,
n_failed=0,
f_passed=1.0,
f_failed=0.0,
warn=None,
stop=None,
notify=None,
time_processed="2021-08-01T00:00:00",
proc_duration_s=0.0,
)
assert v.i == 1
assert v.i_o == 1
assert v.step_id == "col_vals_gt"
assert v.sha1 == "a"
assert v.assertion_type == "col_vals_gt"
assert v.column == "x"
assert v.values == 0
assert v.inclusive is True
assert v.na_pass is False
assert v.thresholds == Thresholds()
assert v.label is None
assert v.brief is None
assert v.active is True
assert v.eval_error is False
assert v.all_passed is True
assert v.n == 4
assert v.n_passed == 4
assert v.n_failed == 0
assert v.f_passed == 1.0
assert v.f_failed == 0.0
assert v.warn is None
assert v.stop is None
assert v.notify is None
assert isinstance(v.time_processed, str)
assert isinstance(v.proc_duration_s, float)
@pytest.mark.parametrize("tbl_fixture", TBL_LIST)
def test_col_vals_all_passing(request, tbl_fixture):
tbl = request.getfixturevalue(tbl_fixture)
v = Validate(tbl).col_vals_gt(columns="x", value=0).interrogate()
if tbl_fixture not in ["tbl_parquet", "tbl_duckdb", "tbl_sqlite"]:
assert v.data.shape == (4, 3)
assert str(v.data["x"].dtype).lower() == "int64"
assert str(v.data["y"].dtype).lower() == "int64"
assert str(v.data["z"].dtype).lower() == "int64"
# There is a single validation check entry in the `validation_info` attribute
assert len(v.validation_info) == 1
# The single step had no failing test units so the `all_passed` attribute is `True`
assert v.all_passed()
# Test other validation types for all passing behavior in single steps
assert Validate(tbl).col_vals_lt(columns="x", value=5).interrogate().all_passed()
assert Validate(tbl).col_vals_eq(columns="z", value=8).interrogate().all_passed()
assert Validate(tbl).col_vals_ge(columns="x", value=1).interrogate().all_passed()
assert Validate(tbl).col_vals_le(columns="x", value=4).interrogate().all_passed()
assert Validate(tbl).col_vals_between(columns="x", left=0, right=5).interrogate().all_passed()
assert Validate(tbl).col_vals_outside(columns="x", left=-5, right=0).interrogate().all_passed()
assert (
Validate(tbl).col_vals_in_set(columns="x", set=[1, 2, 3, 4, 5]).interrogate().all_passed()
)
assert Validate(tbl).col_vals_not_in_set(columns="x", set=[5, 6, 7]).interrogate().all_passed()
@pytest.mark.parametrize("tbl_fixture", TBL_LIST)
def test_validation_plan_and_interrogation(request, tbl_fixture):
tbl = request.getfixturevalue(tbl_fixture)
# Create a validation plan
v = Validate(tbl).col_vals_gt(columns="x", value=0)
# A single validation step was added to the plan so `validation_info` has a single entry
assert len(v.validation_info) == 1
# Extract the `validation_info` object to check its attributes
val_info = v.validation_info[0]
assert [
attr
for attr in val_info.__dict__.keys()
if not attr.startswith("__") and not attr.endswith("__")
] == [
"i",
"i_o",
"step_id",
"sha1",
"assertion_type",
"column",
"values",
"inclusive",
"na_pass",
"pre",
"thresholds",
"label",
"brief",
"active",
"eval_error",
"all_passed",
"n",
"n_passed",
"n_failed",
"f_passed",
"f_failed",
"warn",
"stop",
"notify",
"tbl_checked",
"extract",
"val_info",
"time_processed",
"proc_duration_s",
]
# Check the attributes of the `validation_info` object
assert val_info.i is None
assert val_info.i_o == 1
assert val_info.assertion_type == "col_vals_gt"
assert val_info.column == "x"
assert val_info.values == 0
assert val_info.na_pass is False
assert val_info.thresholds == Thresholds()
assert val_info.label is None
assert val_info.brief is None
assert val_info.active is True
assert val_info.eval_error is None
assert val_info.all_passed is None
assert val_info.n is None
assert val_info.n_passed is None
assert val_info.n_failed is None
assert val_info.f_passed is None
assert val_info.f_failed is None
assert val_info.warn is None
assert val_info.stop is None
assert val_info.notify is None
assert val_info.tbl_checked is None
assert val_info.extract is None
assert val_info.val_info is None
assert val_info.time_processed is None
assert val_info.proc_duration_s is None
# Interrogate the validation plan
v_int = v.interrogate()
# The length of the validation info list is still 1
assert len(v_int.validation_info) == 1
# Extract the validation info object to check its attributes
val_info_int = v.validation_info[0]
# The attribute names of `validation_info` object are the same as before
assert [
attr
for attr in val_info_int.__dict__.keys()
if not attr.startswith("__") and not attr.endswith("__")
] == [
"i",
"i_o",
"step_id",
"sha1",
"assertion_type",
"column",
"values",
"inclusive",
"na_pass",
"pre",
"thresholds",
"label",
"brief",
"active",
"eval_error",
"all_passed",
"n",
"n_passed",
"n_failed",
"f_passed",
"f_failed",
"warn",
"stop",
"notify",
"tbl_checked",
"extract",
"val_info",
"time_processed",
"proc_duration_s",
]
# Check the attributes of the `validation_info` object
assert val_info.i == 1
assert val_info.assertion_type == "col_vals_gt"
assert val_info.column == "x"
assert val_info.values == 0
assert val_info.na_pass is False
assert val_info.thresholds == Thresholds()
assert val_info.label is None
assert val_info.brief is None
assert val_info.active is True
assert val_info.eval_error is None
assert val_info.all_passed is True
assert val_info.n == 4
assert val_info.n_passed == 4
assert val_info.n_failed == 0
assert val_info.f_passed == 1.0
assert val_info.f_failed == 0.0
assert val_info.warn is None
assert val_info.stop is None
assert val_info.notify is None
assert val_info.tbl_checked is not None
assert val_info.val_info is None
assert isinstance(val_info.time_processed, str)
assert val_info.proc_duration_s > 0.0
@pytest.mark.parametrize("tbl_fixture", TBL_LIST)
def test_validation_attr_getters(request, tbl_fixture):
tbl = request.getfixturevalue(tbl_fixture)
v = Validate(tbl).col_vals_gt(columns="x", value=0).interrogate()
# Get the total number of test units as a dictionary
n_dict = v.n()
assert len(n_dict) == 1
assert n_dict.keys() == {1}
assert n_dict[1] == 4
# Get the number of passing test units
n_passed_dict = v.n_passed()
assert len(n_passed_dict) == 1
assert n_passed_dict.keys() == {1}
assert n_passed_dict[1] == 4
# Get the number of failing test units
n_failed_dict = v.n_failed()
assert len(n_failed_dict) == 1
assert n_failed_dict.keys() == {1}
assert n_failed_dict[1] == 0
# Get the fraction of passing test units
f_passed_dict = v.f_passed()
assert len(f_passed_dict) == 1
assert f_passed_dict.keys() == {1}
assert f_passed_dict[1] == 1.0
# Get the fraction of failing test units
f_failed_dict = v.f_failed()
assert len(f_failed_dict) == 1
assert f_failed_dict.keys() == {1}
assert f_failed_dict[1] == 0.0
# Get the warn status
warn_dict = v.warn()
assert len(warn_dict) == 1
assert warn_dict.keys() == {1}
assert warn_dict[1] is None
# Get the stop status
stop_dict = v.stop()
assert len(stop_dict) == 1
assert stop_dict.keys() == {1}
assert stop_dict[1] is None
# Get the notify status
notify_dict = v.notify()
assert len(notify_dict) == 1
assert notify_dict.keys() == {1}
assert notify_dict[1] is None
@pytest.mark.parametrize("tbl_fixture", TBL_LIST)
def test_validation_attr_getters_no_dict(request, tbl_fixture):
tbl = request.getfixturevalue(tbl_fixture)
v = Validate(tbl).col_vals_gt(columns="x", value=0).interrogate()
# Get the total number of test units as a dictionary
n_val = v.n(i=1, scalar=True)
assert n_val == 4
# Get the number of passing test units
n_passed_val = v.n_passed(i=1, scalar=True)
assert n_passed_val == 4
# Get the number of failing test units
n_failed_val = v.n_failed(i=1, scalar=True)
assert n_failed_val == 0
# Get the fraction of passing test units
f_passed_val = v.f_passed(i=1, scalar=True)
assert f_passed_val == 1.0
# Get the fraction of failing test units
f_failed_val = v.f_failed(i=1, scalar=True)
assert f_failed_val == 0.0
# Get the warn status
warn_val = v.warn(i=1, scalar=True)
assert warn_val is None
# Get the stop status
stop_val = v.stop(i=1, scalar=True)
assert stop_val is None
# Get the notify status
notify_val = v.notify(i=1, scalar=True)
assert notify_val is None
@pytest.mark.parametrize("tbl_fixture", TBL_LIST)
def test_get_json_report(request, tbl_fixture):
tbl = request.getfixturevalue(tbl_fixture)
v = Validate(tbl).col_vals_gt(columns="x", value=0).interrogate()
assert v.get_json_report() != v.get_json_report(
exclude_fields=["time_processed", "proc_duration_s"]
)
# A ValueError is raised when `use_fields=` includes invalid fields
with pytest.raises(ValueError):
v.get_json_report(use_fields=["invalid_field"])
# A ValueError is raised when `exclude_fields=` includes invalid fields
with pytest.raises(ValueError):
v.get_json_report(exclude_fields=["invalid_field"])
# A ValueError is raised `use_fields=` and `exclude_fields=` are both provided
with pytest.raises(ValueError):
v.get_json_report(use_fields=["i"], exclude_fields=["i_o"])
@pytest.mark.parametrize("tbl_fixture", TBL_LIST)
def test_validation_report_interrogate_snap(request, tbl_fixture, snapshot):
tbl = request.getfixturevalue(tbl_fixture)
report = (
Validate(tbl)
.col_vals_gt(columns="x", value=0)
.interrogate()
.get_json_report(exclude_fields=["time_processed", "proc_duration_s"])
)
# Use the snapshot fixture to create and save the snapshot
snapshot.assert_match(report, "validation_report.json")
@pytest.mark.parametrize("tbl_fixture", TBL_LIST)
def test_validation_report_no_interrogate_snap(request, tbl_fixture, snapshot):
tbl = request.getfixturevalue(tbl_fixture)
report = (
Validate(tbl)
.col_vals_gt(columns="x", value=0)
.get_json_report(exclude_fields=["time_processed", "proc_duration_s"])
)
# Use the snapshot fixture to create and save the snapshot
snapshot.assert_match(report, "validation_report.json")
@pytest.mark.parametrize("tbl_fixture", TBL_LIST)
def test_validation_report_use_fields_snap(request, tbl_fixture, snapshot):
tbl = request.getfixturevalue(tbl_fixture)
report = (
Validate(tbl)
.col_vals_gt(columns="x", value=0)
.get_json_report(
use_fields=[
"i",
"assertion_type",
"all_passed",
"n",
"f_passed",
"f_failed",
]
)
)
# Use the snapshot fixture to create and save the snapshot
snapshot.assert_match(report, "validation_report.json")
@pytest.mark.parametrize("tbl_fixture", TBL_LIST)
def test_validation_report_json_no_steps(request, tbl_fixture):
tbl = request.getfixturevalue(tbl_fixture)
assert Validate(tbl).get_json_report() == "[]"
assert Validate(tbl).interrogate().get_json_report() == "[]"
@pytest.mark.parametrize("tbl_fixture", TBL_LIST)
def test_validation_check_column_input(request, tbl_fixture):
tbl = request.getfixturevalue(tbl_fixture)
# Raise a ValueError when `columns=` is not a string
with pytest.raises(ValueError):
Validate(tbl).col_vals_gt(columns=9, value=0)
with pytest.raises(ValueError):
Validate(tbl).col_vals_lt(columns=9, value=0)
with pytest.raises(ValueError):
Validate(tbl).col_vals_eq(columns=9, value=0)
with pytest.raises(ValueError):
Validate(tbl).col_vals_ne(columns=9, value=0)
with pytest.raises(ValueError):
Validate(tbl).col_vals_ge(columns=9, value=0)
with pytest.raises(ValueError):
Validate(tbl).col_vals_le(columns=9, value=0)
with pytest.raises(ValueError):
Validate(tbl).col_vals_between(columns=9, left=0, right=5)
with pytest.raises(ValueError):
Validate(tbl).col_vals_outside(columns=9, left=-5, right=0)
with pytest.raises(ValueError):
Validate(tbl).col_vals_in_set(columns=9, set=[1, 2, 3, 4, 5])
with pytest.raises(ValueError):
Validate(tbl).col_vals_not_in_set(columns=9, set=[5, 6, 7])
with pytest.raises(ValueError):
Validate(tbl).col_vals_null(columns=9)
with pytest.raises(ValueError):
Validate(tbl).col_vals_not_null(columns=9)
with pytest.raises(ValueError):
Validate(tbl).col_exists(columns=9)
@pytest.mark.parametrize("tbl_fixture", TBL_LIST)
def test_validation_check_column_input_with_col(request, tbl_fixture):
tbl = request.getfixturevalue(tbl_fixture)
# Check that using `col(column_name)` in `columns=` is allowed and doesn't raise an error
Validate(tbl).col_vals_gt(columns=col("x"), value=0).interrogate()
Validate(tbl).col_vals_lt(columns=col("x"), value=0).interrogate()
Validate(tbl).col_vals_eq(columns=col("x"), value=0).interrogate()
Validate(tbl).col_vals_ne(columns=col("x"), value=0).interrogate()
Validate(tbl).col_vals_ge(columns=col("x"), value=0).interrogate()
Validate(tbl).col_vals_le(columns=col("x"), value=0).interrogate()
Validate(tbl).col_vals_between(columns=col("x"), left=0, right=5).interrogate()
Validate(tbl).col_vals_outside(columns=col("x"), left=-5, right=0).interrogate()
Validate(tbl).col_vals_in_set(columns=col("x"), set=[1, 2, 3, 4, 5]).interrogate()
Validate(tbl).col_vals_not_in_set(columns=col("x"), set=[5, 6, 7]).interrogate()
Validate(tbl).col_vals_null(columns=col("x")).interrogate()
Validate(tbl).col_vals_not_null(columns=col("x")).interrogate()
Validate(tbl).col_exists(columns=col("x")).interrogate()
@pytest.mark.parametrize("tbl_fixture", TBL_LIST)
def test_validation_check_na_pass_input(request, tbl_fixture):
tbl = request.getfixturevalue(tbl_fixture)
# Raise a ValueError when `na_pass=` is not a boolean
with pytest.raises(ValueError):
Validate(tbl).col_vals_gt(columns="x", value=0, na_pass=9)
with pytest.raises(ValueError):
Validate(tbl).col_vals_lt(columns="x", value=0, na_pass=9)
with pytest.raises(ValueError):
Validate(tbl).col_vals_eq(columns="x", value=0, na_pass=9)
with pytest.raises(ValueError):
Validate(tbl).col_vals_ne(columns="x", value=0, na_pass=9)
with pytest.raises(ValueError):
Validate(tbl).col_vals_ge(columns="x", value=0, na_pass=9)
with pytest.raises(ValueError):
Validate(tbl).col_vals_le(columns="x", value=0, na_pass=9)
with pytest.raises(ValueError):
Validate(tbl).col_vals_between(columns="x", left=0, right=5, na_pass=9)
with pytest.raises(ValueError):
Validate(tbl).col_vals_outside(columns="x", left=-5, right=0, na_pass=9)
@pytest.mark.parametrize("tbl_fixture", TBL_LIST)
def test_validation_check_thresholds_input(request, tbl_fixture):
tbl = request.getfixturevalue(tbl_fixture)
# Check that allowed forms for `thresholds=` don't raise an error
Validate(tbl).col_vals_gt(columns="x", value=0, thresholds=1)
Validate(tbl).col_vals_gt(columns="x", value=0, thresholds=0.1)
Validate(tbl).col_vals_gt(columns="x", value=0, thresholds=(0.1))
Validate(tbl).col_vals_gt(columns="x", value=0, thresholds=(0.1, 0.2))
Validate(tbl).col_vals_gt(columns="x", value=0, thresholds=(0.1, 0.2, 0.3))
Validate(tbl).col_vals_gt(columns="x", value=0, thresholds=(0.1, 2, 0.3))
Validate(tbl).col_vals_gt(columns="x", value=0, thresholds=(1))
Validate(tbl).col_vals_gt(columns="x", value=0, thresholds=(1, 2))
Validate(tbl).col_vals_gt(columns="x", value=0, thresholds=(1, 3, 4))
Validate(tbl).col_vals_gt(columns="x", value=0, thresholds=(1, 0.3, 4))
Validate(tbl).col_vals_gt(columns="x", value=0, thresholds={"warn_at": 0.1})
Validate(tbl).col_vals_gt(columns="x", value=0, thresholds={"stop_at": 0.1})
Validate(tbl).col_vals_gt(columns="x", value=0, thresholds={"notify_at": 0.1})
Validate(tbl).col_vals_gt(columns="x", value=0, thresholds={"warn_at": 0.05, "notify_at": 0.1})
Validate(tbl).col_vals_gt(columns="x", value=0, thresholds=Thresholds())
Validate(tbl).col_vals_gt(
columns="x", value=0, thresholds=Thresholds(warn_at=0.1, stop_at=0.2, notify_at=0.3)
)
Validate(tbl).col_vals_gt(
columns="x", value=0, thresholds=Thresholds(warn_at=1, stop_at=2, notify_at=3)
)
# Raise a ValueError when `thresholds=` is not one of the allowed types
with pytest.raises(ValueError):
Validate(tbl).col_vals_gt(columns="x", value=0, thresholds="invalid")
with pytest.raises(ValueError):
Validate(tbl).col_vals_gt(columns="x", value=0, thresholds=[1, 2, 3])
with pytest.raises(ValueError):
Validate(tbl).col_vals_gt(columns="x", value=0, thresholds=-2)
with pytest.raises(ValueError):
Validate(tbl).col_vals_gt(columns="x", value=0, thresholds=(1, 2, 3, 4))
with pytest.raises(ValueError):
Validate(tbl).col_vals_gt(columns="x", value=0, thresholds=())
with pytest.raises(ValueError):
Validate(tbl).col_vals_gt(columns="x", value=0, thresholds=(1, -2))
with pytest.raises(ValueError):
Validate(tbl).col_vals_gt(columns="x", value=0, thresholds=(1, [2], 3))
with pytest.raises(ValueError):
Validate(tbl).col_vals_gt(
columns="x", value=0, thresholds={"warning": 0.05, "notify_at": 0.1}
)
with pytest.raises(ValueError):
Validate(tbl).col_vals_gt(
columns="x", value=0, thresholds={"warn_at": 0.05, "notify_at": -0.1}
)
with pytest.raises(ValueError):
Validate(tbl).col_vals_gt(
columns="x", value=0, thresholds={"warn_at": "invalid", "stop_at": 3}
)
@pytest.mark.parametrize("tbl_fixture", TBL_LIST)
def test_validation_check_active_input(request, tbl_fixture):
tbl = request.getfixturevalue(tbl_fixture)
# Raise a ValueError when `active=` is not a boolean
with pytest.raises(ValueError):
Validate(tbl).col_vals_gt(columns="x", value=0, active=9)
with pytest.raises(ValueError):
Validate(tbl).col_vals_lt(columns="x", value=0, active=9)
with pytest.raises(ValueError):
Validate(tbl).col_vals_eq(columns="x", value=0, active=9)
with pytest.raises(ValueError):
Validate(tbl).col_vals_ne(columns="x", value=0, active=9)
with pytest.raises(ValueError):
Validate(tbl).col_vals_ge(columns="x", value=0, active=9)
with pytest.raises(ValueError):
Validate(tbl).col_vals_le(columns="x", value=0, active=9)
with pytest.raises(ValueError):
Validate(tbl).col_vals_between(columns="x", left=0, right=5, active=9)
with pytest.raises(ValueError):
Validate(tbl).col_vals_outside(columns="x", left=-5, right=0, active=9)
with pytest.raises(ValueError):
Validate(tbl).col_vals_in_set(columns="x", set=[1, 2, 3, 4, 5], active=9)
with pytest.raises(ValueError):
Validate(tbl).col_vals_not_in_set(columns="x", set=[5, 6, 7], active=9)
with pytest.raises(ValueError):
Validate(tbl).col_vals_null(columns="x", active=9)
with pytest.raises(ValueError):
Validate(tbl).col_vals_not_null(columns="x", active=9)
with pytest.raises(ValueError):
Validate(tbl).col_exists(columns="x", active=9)
@pytest.mark.parametrize("tbl_fixture", TBL_LIST)
def test_validation_check_thresholds_inherit(request, tbl_fixture):
tbl = request.getfixturevalue(tbl_fixture)
# Check that the `thresholds=` argument is inherited from Validate, in those steps where
# it is not explicitly provided (is `None`)
v = (
Validate(tbl, thresholds=Thresholds(warn_at=1, stop_at=2, notify_at=3))
.col_vals_gt(columns="x", value=0)
.col_vals_gt(columns="x", value=0, thresholds=0.5)
.col_vals_lt(columns="x", value=2)
.col_vals_lt(columns="x", value=2, thresholds=0.5)
.col_vals_eq(columns="z", value=4)
.col_vals_eq(columns="z", value=4, thresholds=0.5)
.col_vals_ne(columns="z", value=6)
.col_vals_ne(columns="z", value=6, thresholds=0.5)
.col_vals_ge(columns="z", value=8)
.col_vals_ge(columns="z", value=8, thresholds=0.5)
.col_vals_le(columns="z", value=10)
.col_vals_le(columns="z", value=10, thresholds=0.5)
.col_vals_between(columns="x", left=0, right=5)
.col_vals_between(columns="x", left=0, right=5, thresholds=0.5)
.col_vals_outside(columns="x", left=-5, right=0)
.col_vals_outside(columns="x", left=-5, right=0, thresholds=0.5)
.col_vals_in_set(columns="x", set=[1, 2, 3, 4, 5])
.col_vals_in_set(columns="x", set=[1, 2, 3, 4, 5], thresholds=0.5)
.col_vals_not_in_set(columns="x", set=[5, 6, 7])
.col_vals_not_in_set(columns="x", set=[5, 6, 7], thresholds=0.5)
.col_vals_null(columns="x")
.col_vals_null(columns="x", thresholds=0.5)
.col_vals_not_null(columns="x")
.col_vals_not_null(columns="x", thresholds=0.5)
.col_exists(columns="x")
.col_exists(columns="x", thresholds=0.5)
.interrogate()
)
# col_vals_gt - inherited
assert v.validation_info[0].thresholds.warn_at == 1
assert v.validation_info[0].thresholds.stop_at == 2
assert v.validation_info[0].thresholds.notify_at == 3
# col_vals_gt - overridden
assert v.validation_info[1].thresholds.warn_at == 0.5
assert v.validation_info[1].thresholds.stop_at is None
assert v.validation_info[1].thresholds.notify_at is None
# col_vals_lt - inherited
assert v.validation_info[2].thresholds.warn_at == 1
assert v.validation_info[2].thresholds.stop_at == 2
assert v.validation_info[2].thresholds.notify_at == 3
# col_vals_lt - overridden
assert v.validation_info[3].thresholds.warn_at == 0.5
assert v.validation_info[3].thresholds.stop_at is None
assert v.validation_info[3].thresholds.notify_at is None
# col_vals_eq - inherited
assert v.validation_info[4].thresholds.warn_at == 1
assert v.validation_info[4].thresholds.stop_at == 2
assert v.validation_info[4].thresholds.notify_at == 3
# col_vals_eq - overridden
assert v.validation_info[5].thresholds.warn_at == 0.5
assert v.validation_info[5].thresholds.stop_at is None
assert v.validation_info[5].thresholds.notify_at is None
# col_vals_ne - inherited
assert v.validation_info[6].thresholds.warn_at == 1
assert v.validation_info[6].thresholds.stop_at == 2
assert v.validation_info[6].thresholds.notify_at == 3
# col_vals_ne - overridden
assert v.validation_info[7].thresholds.warn_at == 0.5
assert v.validation_info[7].thresholds.stop_at is None
assert v.validation_info[7].thresholds.notify_at is None
# col_vals_ge - inherited
assert v.validation_info[8].thresholds.warn_at == 1
assert v.validation_info[8].thresholds.stop_at == 2
assert v.validation_info[8].thresholds.notify_at == 3
# col_vals_ge - overridden
assert v.validation_info[9].thresholds.warn_at == 0.5
assert v.validation_info[9].thresholds.stop_at is None
assert v.validation_info[9].thresholds.notify_at is None
# col_vals_le - inherited
assert v.validation_info[10].thresholds.warn_at == 1
assert v.validation_info[10].thresholds.stop_at == 2
assert v.validation_info[10].thresholds.notify_at == 3
# col_vals_le - overridden
assert v.validation_info[11].thresholds.warn_at == 0.5
assert v.validation_info[11].thresholds.stop_at is None
assert v.validation_info[11].thresholds.notify_at is None
# col_vals_between - inherited
assert v.validation_info[12].thresholds.warn_at == 1
assert v.validation_info[12].thresholds.stop_at == 2
assert v.validation_info[12].thresholds.notify_at == 3
# col_vals_between - overridden
assert v.validation_info[13].thresholds.warn_at == 0.5
assert v.validation_info[13].thresholds.stop_at is None
assert v.validation_info[13].thresholds.notify_at is None
# col_vals_outside - inherited
assert v.validation_info[14].thresholds.warn_at == 1
assert v.validation_info[14].thresholds.stop_at == 2
assert v.validation_info[14].thresholds.notify_at == 3
# col_vals_outside - overridden
assert v.validation_info[15].thresholds.warn_at == 0.5
assert v.validation_info[15].thresholds.stop_at is None
assert v.validation_info[15].thresholds.notify_at is None
# col_vals_in_set - inherited
assert v.validation_info[16].thresholds.warn_at == 1
assert v.validation_info[16].thresholds.stop_at == 2
assert v.validation_info[16].thresholds.notify_at == 3
# col_vals_in_set - overridden
assert v.validation_info[17].thresholds.warn_at == 0.5
assert v.validation_info[17].thresholds.stop_at is None
assert v.validation_info[17].thresholds.notify_at is None
# col_vals_not_in_set - inherited
assert v.validation_info[18].thresholds.warn_at == 1
assert v.validation_info[18].thresholds.stop_at == 2
assert v.validation_info[18].thresholds.notify_at == 3
# col_vals_not_in_set - overridden
assert v.validation_info[19].thresholds.warn_at == 0.5
assert v.validation_info[19].thresholds.stop_at is None
assert v.validation_info[19].thresholds.notify_at is None
# col_vals_null - inherited
assert v.validation_info[20].thresholds.warn_at == 1
assert v.validation_info[20].thresholds.stop_at == 2
assert v.validation_info[20].thresholds.notify_at == 3
# col_vals_null - overridden
assert v.validation_info[21].thresholds.warn_at == 0.5
assert v.validation_info[21].thresholds.stop_at is None
assert v.validation_info[21].thresholds.notify_at is None
# col_vals_not_null - inherited
assert v.validation_info[22].thresholds.warn_at == 1
assert v.validation_info[22].thresholds.stop_at == 2
assert v.validation_info[22].thresholds.notify_at == 3
# col_vals_not_null - overridden
assert v.validation_info[23].thresholds.warn_at == 0.5
assert v.validation_info[23].thresholds.stop_at is None
assert v.validation_info[23].thresholds.notify_at is None
# col_exists - inherited
assert v.validation_info[24].thresholds.warn_at == 1
assert v.validation_info[24].thresholds.stop_at == 2
assert v.validation_info[24].thresholds.notify_at == 3
# col_exists - overridden
assert v.validation_info[25].thresholds.warn_at == 0.5
assert v.validation_info[25].thresholds.stop_at is None
assert v.validation_info[25].thresholds.notify_at is None
def test_validation_with_preprocessing_pd(tbl_pd):
v = (
Validate(tbl_pd)
.col_vals_eq(columns="z", value=8)
.col_vals_eq(columns="z", value=16, pre=lambda df: df.assign(z=df["z"] * 2))