@@ -9,108 +9,95 @@ def test_empty_request_body(self):
99 body = {}
1010
1111 with self .assertRaises (ValidationError ) as e :
12- validate .body (body , ReqBodyValidators .GENERIC )
13- self .assertEqual (
14- str (e ),
15- "Schema threw an error when validating the request body." ,
16- )
12+ validate .body (body , ReqBodyValidators .EVALUATION )
13+
14+ self .assertEqual (
15+ str (e .exception .message ),
16+ "Failed to validate body against the evaluation schema." ,
17+ )
1718
1819 def test_missing_response (self ):
1920 body = {"answer" : "example" , "params" : {}}
20- validation_error = validate .body (body , ReqBodyValidators .GENERIC )
2121
22- self .assertNotEqual (validation_error , None )
22+ with self .assertRaises (ValidationError ) as e :
23+ validate .body (body , ReqBodyValidators .EVALUATION )
2324
2425 self .assertEqual (
25- validation_error . get ( " error_thrown" ). get ( " message"),
26+ e . exception . error_thrown [ " message"], # type: ignore
2627 "'response' is a required property" ,
2728 )
2829
2930 def test_null_response_for_eval (self ):
3031 body = {"response" : None , "answer" : "example" , "params" : {}}
31- validation_error = validate .body (body , ReqBodyValidators .GENERIC )
3232
33- self .assertNotEqual (validation_error , None )
33+ with self .assertRaises (ValidationError ) as e :
34+ validate .body (body , ReqBodyValidators .EVALUATION )
3435
3536 self .assertEqual (
36- validation_error . get ( " error_thrown" ). get ( " message"),
37+ e . exception . error_thrown [ " message"], # type: ignore
3738 "None should not be valid under {'type': 'null'}" ,
3839 )
3940
4041 def test_null_response_for_preview (self ):
41- body = {"response" : None , "answer" : "example" , "params" : {}}
42- validation_error = validate .body (body , ReqBodyValidators .GENERIC )
42+ body = {"response" : None , "params" : {}}
4343
44- self .assertNotEqual (validation_error , None )
44+ with self .assertRaises (ValidationError ) as e :
45+ validate .body (body , ReqBodyValidators .PREVIEW )
4546
4647 self .assertEqual (
47- validation_error . get ( " error_thrown" ). get ( " message"),
48+ e . exception . error_thrown [ " message"], # type: ignore
4849 "None should not be valid under {'type': 'null'}" ,
4950 )
5051
5152 def test_missing_answer_in_eval (self ):
5253 body = {"response" : "example" , "params" : {}}
53- validation_error = validate .body (body , ReqBodyValidators .GENERIC )
5454
55- self .assertNotEqual (validation_error , None )
55+ with self .assertRaises (ValidationError ) as e :
56+ validate .body (body , ReqBodyValidators .EVALUATION )
5657
5758 self .assertEqual (
58- validation_error . get ( " error_thrown" ). get ( " message"),
59+ e . exception . error_thrown [ " message"], # type: ignore
5960 "'answer' is a required property" ,
6061 )
6162
6263 def test_missing_answer_in_preview (self ):
6364 body = {"response" : "example" , "params" : {}}
64- validation_error = validate .body (body , ReqBodyValidators .GENERIC )
65-
66- self .assertNotEqual (validation_error , None )
67-
68- self .assertEqual (
69- validation_error .get ("error_thrown" ).get ("message" ),
70- "'answer' is a required property" ,
71- )
65+ validate .body (body , ReqBodyValidators .PREVIEW )
7266
7367 def test_including_answer_in_eval (self ):
74- body = {"response" : "example" , "params" : {}}
75- validation_error = validate .body (body , ReqBodyValidators .GENERIC )
76-
77- self .assertNotEqual (validation_error , None )
78-
79- self .assertEqual (
80- validation_error .get ("error_thrown" ).get ("message" ),
81- "'answer' is a required property" ,
82- )
68+ body = {"response" : "example" , "answer" : "anything" , "params" : {}}
69+ validate .body (body , ReqBodyValidators .EVALUATION )
8370
8471 def test_including_answer_in_preview (self ):
85- body = {"response" : "example" , "params" : {}}
86- validation_error = validate .body (body , ReqBodyValidators .GENERIC )
72+ body = {"response" : "example" , "answer" : "anything" , "params" : {}}
8773
88- self .assertNotEqual (validation_error , None )
74+ with self .assertRaises (ValidationError ) as e :
75+ validate .body (body , ReqBodyValidators .PREVIEW )
8976
9077 self .assertEqual (
91- validation_error . get ( " error_thrown" ). get ( " message"),
92- "'answer' is a required property " ,
78+ e . exception . error_thrown [ " message"], # type: ignore
79+ "Additional properties are not allowed ( 'answer' was unexpected) " ,
9380 )
9481
9582 def test_null_answer (self ):
9683 body = {"response" : "example" , "answer" : None , "params" : {}}
97- validation_error = validate .body (body , ReqBodyValidators .GENERIC )
9884
99- self .assertNotEqual (validation_error , None )
85+ with self .assertRaises (ValidationError ) as e :
86+ validate .body (body , ReqBodyValidators .EVALUATION )
10087
10188 self .assertEqual (
102- validation_error . get ( " error_thrown" ). get ( " message"),
89+ e . exception . error_thrown [ " message"], # type: ignore
10390 "None should not be valid under {'type': 'null'}" ,
10491 )
10592
10693 def test_bad_params (self ):
10794 body = {"response" : "example" , "answer" : "example" , "params" : 2 }
108- validation_error = validate .body (body , ReqBodyValidators .GENERIC )
10995
110- self .assertNotEqual (validation_error , None )
96+ with self .assertRaises (ValidationError ) as e :
97+ validate .body (body , ReqBodyValidators .EVALUATION )
11198
11299 self .assertEqual (
113- validation_error . get ( " error_thrown" ). get ( " message"),
100+ e . exception . error_thrown [ " message"], # type: ignore
114101 "2 is not of type 'object'" ,
115102 )
116103
@@ -122,18 +109,17 @@ def test_extra_fields(self):
122109 "hello" : "world" ,
123110 }
124111
125- validation_error = validate . body ( body , ReqBodyValidators . GENERIC )
126- self . assertNotEqual ( validation_error , None )
112+ with self . assertRaises ( ValidationError ) as e :
113+ validate . body ( body , ReqBodyValidators . EVALUATION )
127114
128115 self .assertEqual (
129- validation_error . get ( " error_thrown" ). get ( " message"),
116+ e . exception . error_thrown [ " message"], # type: ignore
130117 "Additional properties are not allowed ('hello' was unexpected)" ,
131118 )
132119
133120 def test_valid_request_body (self ):
134121 body = {"response" : "" , "answer" : "" }
135- validation_error = validate .body (body , ReqBodyValidators .GENERIC )
136- self .assertEqual (validation_error , None )
122+ validate .body (body , ReqBodyValidators .EVALUATION )
137123
138124
139125if __name__ == "__main__" :
0 commit comments