@@ -13,10 +13,39 @@ def test_check_init(args, expected_class):
1313 assert isinstance (CheckType .init (* args ), expected_class )
1414
1515
16- def test_check_type_raises_not_implemented_error_for_invalid_check_type ():
17- """Validate that CheckType raises a NotImplementedError when passed a non-existant check_type."""
18- with pytest .raises (NotImplementedError ):
19- CheckType .init ("does_not_exist" )
16+ exception_tests_init = [
17+ (("does_not_exist" ,), NotImplementedError , "" ),
18+ (("tolerance" ,), IndexError , "parameter" ),
19+ (("tolerance" , "non-float" ), ValueError , "non-float" ),
20+ ]
21+
22+
23+ @pytest .mark .parametrize ("check_type_args, exception_type, expected_in_output" , exception_tests_init )
24+ def tests_exceptions_init (check_type_args , exception_type , expected_in_output ):
25+ """Tests exceptions when check object is initialized."""
26+ with pytest .raises (exception_type ) as error :
27+ CheckType .init (* check_type_args )
28+ assert expected_in_output in error .value .__str__ ()
29+
30+
31+ exception_tests_eval = [
32+ ("parameter_match" , ([{}], "not-dict" ), TypeError , "dict()" ),
33+ ("parameter_match" , ({}, {}), TypeError , "'values' must be of type List" ),
34+ ("regex" , ([{}], "not-dict" ), TypeError , "dict()" ),
35+ ("regex" , ([{}], {"mode" : "match" }), KeyError , "check-option" ),
36+ ("regex" , ([{}], {"mode" : "regex" }), KeyError , "check-option" ),
37+ ("regex" , ([{}], {"regex" : "" , "mode" : "unknown" }), ValueError , "check-option" ),
38+ ("regex" , ({}, {"regex" : "" , "mode" : "match" }), TypeError , "'values' must be of type List" ),
39+ ]
40+
41+
42+ @pytest .mark .parametrize ("check_type, args, exception_type, expected_in_output" , exception_tests_eval )
43+ def tests_exceptions_eval (check_type , args , exception_type , expected_in_output ):
44+ """Tests exceptions when calling .evaluate() method."""
45+ with pytest .raises (exception_type ) as error :
46+ check = CheckType .init (check_type )
47+ check .evaluate (* args )
48+ assert expected_in_output in error .value .__str__ ()
2049
2150
2251exact_match_test_values_no_change = (
@@ -187,12 +216,10 @@ def test_check_type_results(check_type_args, folder_name, path, expected_results
187216]
188217
189218
190- @pytest .mark .parametrize ("folder_name, check_args , path, expected_result" , check_tests )
191- def test_checks (folder_name , check_args , path , expected_result ):
219+ @pytest .mark .parametrize ("folder_name, check_type_args , path, expected_result" , check_tests )
220+ def test_checks (folder_name , check_type_args , path , expected_result ):
192221 """Validate multiple checks on the same data to catch corner cases."""
193- pre_data , post_data = load_mocks (folder_name )
194-
195- check = CheckType .init (* check_args )
222+ check = CheckType .init (* check_type_args )
196223 pre_data , post_data = load_mocks (folder_name )
197224 pre_value = check .get_value (pre_data , path )
198225 post_value = check .get_value (post_data , path )
@@ -217,14 +244,14 @@ def test_checks(folder_name, check_args, path, expected_result):
217244)
218245
219246
220- @pytest .mark .parametrize ("filename, check_args , path, expected_result" , [parameter_match_api ])
221- def test_param_match (filename , check_args , path , expected_result ):
247+ @pytest .mark .parametrize ("filename, check_type_args , path, expected_result" , [parameter_match_api ])
248+ def test_param_match (filename , check_type_args , path , expected_result ):
222249 """Validate parameter_match check type."""
223- check = CheckType .init (* check_args )
250+ check = CheckType .init (check_type_args [ 0 ] )
224251 # There is not concept of "pre" and "post" in parameter_match.
225252 data = load_json_file ("parameter_match" , filename )
226253 value = check .get_value (data , path )
227- actual_results = check .evaluate (value , check_args )
254+ actual_results = check .evaluate (value , check_type_args [ 1 ] )
228255 assert actual_results == expected_result , ASSERT_FAIL_MESSAGE .format (
229256 output = actual_results , expected_output = expected_result
230257 )
@@ -262,14 +289,14 @@ def test_param_match(filename, check_args, path, expected_result):
262289]
263290
264291
265- @pytest .mark .parametrize ("filename, check_args , path, expected_result" , regex_match )
266- def test_regex_match (filename , check_args , path , expected_result ):
292+ @pytest .mark .parametrize ("filename, check_type_args , path, expected_result" , regex_match )
293+ def test_regex_match (filename , check_type_args , path , expected_result ):
267294 """Validate regex check type."""
268- check = CheckType .init (* check_args )
295+ check = CheckType .init (check_type_args [ 0 ] )
269296 # There is not concept of "pre" and "post" in parameter_match.
270297 data = load_json_file ("api" , filename )
271298 value = check .get_value (data , path )
272- actual_results = check .evaluate (value , check_args )
299+ actual_results = check .evaluate (value , check_type_args [ 1 ] )
273300 assert actual_results == expected_result , ASSERT_FAIL_MESSAGE .format (
274301 output = actual_results , expected_output = expected_result
275302 )
0 commit comments