From cf2b8d3babf29552e5eeb1ab535506408e18a3e5 Mon Sep 17 00:00:00 2001 From: Hassan Abouelela Date: Wed, 2 Jun 2021 04:03:17 +0300 Subject: 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 --- backend/models/question.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) (limited to 'backend/models/question.py') 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 -- cgit v1.2.3 From ee15ceccc8b2b690d0c383ac596ca95c36180a9a Mon Sep 17 00:00:00 2001 From: Hassan Abouelela Date: Wed, 2 Jun 2021 13:35:21 +0300 Subject: Adds Missing Docstrings Signed-off-by: Hassan Abouelela --- backend/models/question.py | 3 +++ 1 file changed, 3 insertions(+) (limited to 'backend/models/question.py') diff --git a/backend/models/question.py b/backend/models/question.py index 9829843..8972bc1 100644 --- a/backend/models/question.py +++ b/backend/models/question.py @@ -8,11 +8,13 @@ _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 not len(value.keys()): raise ValueError("Must have at least one test in a test suite.") @@ -20,6 +22,7 @@ class Unittests(BaseModel): class CodeQuestion(BaseModel): + """Schema model for questions of type `code`.""" language: str unittests: t.Optional[Unittests] -- cgit v1.2.3 From 9991a8e8031ab6c1d33577bc759a6c7b03183ea3 Mon Sep 17 00:00:00 2001 From: Hassan Abouelela Date: Thu, 3 Jun 2021 03:02:01 +0300 Subject: Clarify Validator Semantics Co-authored-by: Chris Signed-off-by: Hassan Abouelela --- backend/models/question.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'backend/models/question.py') diff --git a/backend/models/question.py b/backend/models/question.py index 8972bc1..5a1334a 100644 --- a/backend/models/question.py +++ b/backend/models/question.py @@ -15,7 +15,7 @@ class Unittests(BaseModel): @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 not len(value.keys()): + if isinstance(value, dict) and len(value.keys()) == 0: raise ValueError("Must have at least one test in a test suite.") return value -- cgit v1.2.3