aboutsummaryrefslogtreecommitdiffstats
path: root/backend/models/question.py
diff options
context:
space:
mode:
authorGravatar Hassan Abouelela <[email protected]>2021-06-20 21:12:47 +0300
committerGravatar GitHub <[email protected]>2021-06-20 21:12:47 +0300
commitaf65bd5584e4a02057374b344e5d055bc799ba60 (patch)
tree2b3658d1dd491d6cc2c967c59a3fe237cac4281e /backend/models/question.py
parentMerge pull request #90 from python-discord/auth-form (diff)
parentUpdates Unittest Filter To Match New Model (diff)
Merge pull request #92 from python-discord/unittest_failures
Allow Unittest Failures
Diffstat (limited to 'backend/models/question.py')
-rw-r--r--backend/models/question.py26
1 files changed, 26 insertions, 0 deletions
diff --git a/backend/models/question.py b/backend/models/question.py
index 7daeb5a..5a1334a 100644
--- a/backend/models/question.py
+++ b/backend/models/question.py
@@ -4,6 +4,28 @@ from pydantic import BaseModel, Field, root_validator, validator
from backend.constants import QUESTION_TYPES, REQUIRED_QUESTION_TYPE_DATA
+_TESTS_TYPE = t.Union[t.Dict[str, str], int]
+
+
+class Unittests(BaseModel):
+ """Schema model for unittest suites in code questions."""
+ allow_failure: bool = False
+ tests: _TESTS_TYPE
+
+ @validator("tests")
+ def validate_tests(cls, value: _TESTS_TYPE) -> _TESTS_TYPE:
+ """Confirm that at least one test exists in a test suite."""
+ if isinstance(value, dict) and len(value.keys()) == 0:
+ raise ValueError("Must have at least one test in a test suite.")
+
+ return value
+
+
+class CodeQuestion(BaseModel):
+ """Schema model for questions of type `code`."""
+ language: str
+ unittests: t.Optional[Unittests]
+
class Question(BaseModel):
"""Schema model for form question."""
@@ -49,4 +71,8 @@ class Question(BaseModel):
f"got {type(value['data'][key]).__name__} instead."
)
+ # Validate unittest options
+ if value.get("type").lower() == "code":
+ value["data"] = CodeQuestion(**value.get("data")).dict()
+
return value