Skip to content

Commit

Permalink
refactor: add InvalidJSONResponseError
Browse files Browse the repository at this point in the history
  • Loading branch information
lpm0073 committed Nov 29, 2023
1 parent cbaedad commit 3825aef
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 6 deletions.
1 change: 1 addition & 0 deletions grader/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ class AGRubric:
INCORRECT_RESPONSE_VALUE_PENALTY_PCT = float(os.getenv("AG_INCORRECT_RESPONSE_VALUE_PENALTY_PCT", "0.15"))
RESPONSE_FAILED_PENALTY_PCT = float(os.getenv("AG_RESPONSE_FAILED_PENALTY_PCT", "0.20"))
INVALID_RESPONSE_STRUCTURE_PENALTY_PCT = float(os.getenv("AG_INVALID_RESPONSE_STRUCTURE_PENALTY_PCT", "0.30"))
AG_INVALID_JSON_RESPONSE_PENALTY_PCT = float(os.getenv("AG_INVALID_JSON_RESPONSE_PENALTY_PCT", "0.50"))
9 changes: 9 additions & 0 deletions grader/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ def penalty_pct(self):
return self._penalty_pct


class InvalidJSONResponseError(AGException):
"""A custom exception for the AutomatedGrader class."""

def __init__(self, message):
self.message = message
super().__init__(self.message, penalty_pct=AGRubric.AG_INVALID_JSON_RESPONSE_PENALTY_PCT)


class InvalidResponseStructureError(AGException):
"""A custom exception for the AutomatedGrader class."""

Expand Down Expand Up @@ -58,6 +66,7 @@ def __init__(self, message):

VALID_MESSAGE_TYPES = [
"Success",
InvalidJSONResponseError.__name__,
IncorrectResponseTypeError.__name__,
IncorrectResponseValueError.__name__,
InvalidResponseStructureError.__name__,
Expand Down
5 changes: 3 additions & 2 deletions grader/grader.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
AGException,
IncorrectResponseTypeError,
IncorrectResponseValueError,
InvalidJSONResponseError,
InvalidResponseStructureError,
ResponseFailedError,
)
Expand Down Expand Up @@ -72,8 +73,8 @@ def grade(self):
assignment_json = json.loads(self.assignment)
except json.JSONDecodeError as e:
try:
raise InvalidResponseStructureError("The assignment is not valid JSON") from e
except InvalidResponseStructureError as reraised_e:
raise InvalidJSONResponseError("The assignment is not valid JSON") from e
except InvalidJSONResponseError as reraised_e:
return self.grade_response(reraised_e)

# 2.) attempt to validate the assignment using Pydantic
Expand Down
17 changes: 13 additions & 4 deletions grader/tests/test_responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,9 @@ def test_bad_data(self):
grade = automated_grader.grade()
self.grade_structure(grade)

assert grade["grade"] == 70, "The grade is not 70"
assert grade["message_type"] == "InvalidResponseStructureError"
assert grade["grade"] == 50, "The grade is not 50"
assert grade["message_type"] == "InvalidJSONResponseError"
assert grade["message"] == "The assignment is not valid JSON"

def test_incorrect_response_type(self):
"""Test an assignment with an incorrect response type."""
Expand All @@ -69,8 +70,9 @@ def test_incorrect_response_type(self):
grade = automated_grader.grade()
self.grade_structure(grade)

assert grade["grade"] == 70, "The grade is not 70"
assert grade["message_type"] == "InvalidResponseStructureError"
assert grade["grade"] == 50, "The grade is not 50"
assert grade["message_type"] == "InvalidJSONResponseError"
assert grade["message"] == "The assignment is not valid JSON"

def test_incorrect_response_statuscode(self):
"""Test an assignment with an incorrect response status code."""
Expand All @@ -81,6 +83,7 @@ def test_incorrect_response_statuscode(self):

assert grade["message_type"] == "ResponseFailedError"
assert grade["grade"] == 80, "The grade is not 80"
assert grade["message"] == "status_code must be 200. received: 403"

def test_incorrect_messages(self):
"""Test an assignment with an incorrect message."""
Expand All @@ -91,6 +94,7 @@ def test_incorrect_messages(self):

assert grade["message_type"] == "InvalidResponseStructureError"
assert grade["grade"] == 70, "The grade is not 70"
assert grade["message"] == "The assignment failed pydantic validation."

def test_incorrect_data_type(self):
"""Test an assignment with an incorrect data type."""
Expand All @@ -101,6 +105,7 @@ def test_incorrect_data_type(self):

assert grade["message_type"] == "InvalidResponseStructureError"
assert grade["grade"] == 70, "The grade is not 70"
assert grade["message"] == "The assignment failed pydantic validation."

def test_bad_message_01(self):
"""Test an assignment with an incorrect message."""
Expand All @@ -111,6 +116,7 @@ def test_bad_message_01(self):

assert grade["message_type"] == "InvalidResponseStructureError"
assert grade["grade"] == 70, "The grade is not 70"
assert grade["message"] == "The assignment failed pydantic validation."

def test_bad_message_02(self):
"""Test an assignment with an incorrect message."""
Expand All @@ -121,6 +127,7 @@ def test_bad_message_02(self):

assert grade["message_type"] == "InvalidResponseStructureError"
assert grade["grade"] == 70, "The grade is not 70"
assert grade["message"] == "messages must contain at least 2 objects"

def test_bad_message_03(self):
"""Test an assignment with an incorrect message."""
Expand All @@ -131,6 +138,7 @@ def test_bad_message_03(self):

assert grade["message_type"] == "InvalidResponseStructureError"
assert grade["grade"] == 70, "The grade is not 70"
assert grade["message"] == "The assignment failed pydantic validation."

def test_bad_message_04(self):
"""Test an assignment with an incorrect message."""
Expand All @@ -141,3 +149,4 @@ def test_bad_message_04(self):

assert grade["message_type"] == "InvalidResponseStructureError"
assert grade["grade"] == 70, "The grade is not 70"
assert grade["message"] == "The assignment failed pydantic validation."

0 comments on commit 3825aef

Please sign in to comment.