Skip to content

Commit d8bb8db

Browse files
Merge pull request #97 from lambda-feedback/boolean-as-number
Write a Boolean answer as 1 or 0, compared as a string
2 parents 621b019 + f94af45 commit d8bb8db

3 files changed

Lines changed: 24 additions & 2 deletions

File tree

‎in2lambda/api/response_area.py‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,10 @@ def _validate(self) -> None:
177177
names = " or ".join(t.__name__ for t in self.payload_types)
178178
raise ValueError(f"test payload {test.payload!r} is not {names}")
179179

180+
def _serial(self, value: Any) -> Any:
181+
"""An answer or test payload as Lambda Feedback stores it."""
182+
return value
183+
180184

181185
@dataclass(kw_only=True)
182186
class _Maths(ResponseArea):
@@ -254,6 +258,14 @@ class Boolean(ResponseArea):
254258
payload_types: ClassVar[tuple[type, ...]] = (bool,)
255259
answer: bool = False
256260

261+
# Lambda Feedback stores the answer, and sends the response, as 1 or 0, and
262+
# isExactEqual compares them as strings.
263+
def _serial(self, value: Any) -> Any:
264+
return int(value) if isinstance(value, bool) else value
265+
266+
def _grade_params(self) -> Optional[dict[str, Any]]:
267+
return {"type": "str", **(self.grade_params or {})}
268+
257269

258270
@dataclass(kw_only=True)
259271
class MultipleChoice(ResponseArea):

‎in2lambda/json_convert/json_convert.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ def _response_area_to_json(area: ResponseArea, order: int) -> dict[str, Any]:
8282
"tests": [
8383
{
8484
"id": test.id,
85-
"payload": test.payload,
85+
"payload": area._serial(test.payload),
8686
"expectedResponse": {"isCorrect": test.is_correct},
8787
}
8888
for test in _tests(area)
@@ -91,7 +91,7 @@ def _response_area_to_json(area: ResponseArea, order: int) -> dict[str, Any]:
9191
"response": {
9292
"responseInput": {
9393
"responseType": area.response_type,
94-
"answer": area.answer,
94+
"answer": area._serial(area.answer),
9595
"config": area._config(),
9696
}
9797
},

‎tests/test_boolean.py‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
"""A Boolean area is written as Lambda Feedback's own teacher test sends it."""
2+
3+
from in2lambda.api.response_area import Boolean
4+
from in2lambda.json_convert.json_convert import _response_area_to_json
5+
6+
7+
def test_a_boolean_answer_is_written_as_a_number_compared_as_a_string() -> None:
8+
area = _response_area_to_json(Boolean(answer=True), 0)
9+
assert area["response"]["responseInput"]["answer"] == 1
10+
assert area["gradeParams"] == {"type": "str"}

0 commit comments

Comments
 (0)