diff options
author | 2021-06-02 04:03:17 +0300 | |
---|---|---|
committer | 2021-06-02 04:05:13 +0300 | |
commit | cf2b8d3babf29552e5eeb1ab535506408e18a3e5 (patch) | |
tree | 7607dd65784559d5af2faa490e7c8e32e0c982c7 /backend/models/question.py | |
parent | Set Form Viewing URL (diff) |
Adds Code Question Model
Adds an explicit model for questions of type `code`, to allow more
complex parsing. Updates schema docs.
Signed-off-by: Hassan Abouelela <[email protected]>
Diffstat (limited to 'backend/models/question.py')
-rw-r--r-- | backend/models/question.py | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/backend/models/question.py b/backend/models/question.py index 7daeb5a..9829843 100644 --- a/backend/models/question.py +++ b/backend/models/question.py @@ -4,6 +4,25 @@ 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): + allow_failure: bool = False + tests: _TESTS_TYPE + + @validator("tests") + def validate_tests(cls, value: _TESTS_TYPE) -> _TESTS_TYPE: + if isinstance(value, dict) and not len(value.keys()): + raise ValueError("Must have at least one test in a test suite.") + + return value + + +class CodeQuestion(BaseModel): + language: str + unittests: t.Optional[Unittests] + class Question(BaseModel): """Schema model for form question.""" @@ -49,4 +68,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 |