Skip to content

Commit 18da7dc

Browse files
committed
Updated current tests to all pass
1 parent 3d5afc2 commit 18da7dc

File tree

8 files changed

+121
-132
lines changed

8 files changed

+121
-132
lines changed

__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
from .handler import handler
1+
from .handler import handler # noqa

handler.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def handle_command(event: JsonType, command: str) -> HandlerResponse:
2828
response = Response(
2929
error=ErrorResponse(message=f"Unknown command '{command}'.")
3030
)
31-
validator = ResBodyValidators.GENERIC
31+
validator = ResBodyValidators.EVALUATION
3232

3333
validate.body(response, validator)
3434

tests/__init__.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
1-
from . import requests
2-
from . import responses
1+
from .commands import TestCommandsModule # noqa
2+
from .docs import TestDocsModule # noqa
3+
from .parse import TestParseModule # noqa
4+
from .requests import TestRequestValidation # noqa
5+
from .responses import TestResponseValidation # noqa

tests/handling.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ def test_handle_bodyless_event(self):
1313
error = response.get("error")
1414

1515
self.assertEqual(
16-
error.get("message"),
17-
"No grading data supplied in request body.",
16+
error["message"], # type: ignore
17+
"No data supplied in request body.",
1818
)
1919

2020
def test_non_json_body(self):
@@ -26,7 +26,7 @@ def test_non_json_body(self):
2626
error = response.get("error")
2727

2828
self.assertEqual(
29-
error.get("message"), "Request body is not valid JSON."
29+
error["message"], "Request body is not valid JSON." # type: ignore
3030
)
3131

3232
def test_eval(self):
@@ -101,7 +101,7 @@ def test_healthcheck(self):
101101

102102
result = response.get("result")
103103

104-
self.assertTrue(result.get("tests_passed"))
104+
self.assertTrue(result["tests_passed"]) # type: ignore
105105

106106
def test_invalid_command(self):
107107
event = {
@@ -114,7 +114,8 @@ def test_invalid_command(self):
114114
error = response.get("error")
115115

116116
self.assertEqual(
117-
error.get("message"), "Unknown command 'not a command'."
117+
error["message"], # type: ignore
118+
"Unknown command 'not a command'.",
118119
)
119120

120121

tests/requests.py

Lines changed: 37 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -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

139125
if __name__ == "__main__":

0 commit comments

Comments
 (0)