From 899b69321b0143ba3c246789ccfbdb49d0dd289c Mon Sep 17 00:00:00 2001 From: wayangalihpratama Date: Mon, 10 Oct 2022 16:34:58 +0800 Subject: [PATCH] [#213] Add test case for hint route --- backend/routes/hint.py | 2 +- backend/routes/project.py | 1 + backend/tests/test_18_hint.py | 146 ++++++++++++++++++++++++++++++++++ 3 files changed, 148 insertions(+), 1 deletion(-) create mode 100644 backend/tests/test_18_hint.py diff --git a/backend/routes/hint.py b/backend/routes/hint.py index afd96b07..d7ec44a7 100644 --- a/backend/routes/hint.py +++ b/backend/routes/hint.py @@ -34,7 +34,7 @@ def get(req: Request, question_id: int, Question.id == question_id).first() answers = session.query(Answer).filter( Answer.question == question_id).all() - if question.type == QuestionType.number: + if question.type == QuestionType.number and answers: values = [answer.value for answer in answers] mean = f"Average value is {round(np.mean(values), 2)}" q1 = f"First quantile value is {round(np.quantile(values, .25), 2)}" diff --git a/backend/routes/project.py b/backend/routes/project.py index eeb9385e..2db4ec44 100644 --- a/backend/routes/project.py +++ b/backend/routes/project.py @@ -12,6 +12,7 @@ @project_route.get("/project/{id:path}", response_model=List[ProjectBase], summary="get project by id", + name="project:get_by_id", tags=["Custom"]) def get_by_id(req: Request, id: int, session: Session = Depends(get_session)): project = crud_answer.get_answer_by_question(session=session, question=id) diff --git a/backend/tests/test_18_hint.py b/backend/tests/test_18_hint.py new file mode 100644 index 00000000..8afaf254 --- /dev/null +++ b/backend/tests/test_18_hint.py @@ -0,0 +1,146 @@ +import sys +import pytest +from fastapi import FastAPI +from httpx import AsyncClient +from tests.test_01_auth import Acc +from sqlalchemy.orm import Session +from db.crud_question import get_question_by_id +from models.question import QuestionType +from datetime import datetime + +pytestmark = pytest.mark.asyncio +sys.path.append("..") +account = Acc(True) +today = datetime.today().strftime("%B %d, %Y") + + +class TestHintRoutes(): + @pytest.mark.asyncio + async def test_get_hint( + self, app: FastAPI, session: Session, client: AsyncClient) -> None: + # get hint when no data + question = get_question_by_id(session=session, id=5) + question = question.serialize + assert question["type"] == QuestionType.number + res = await client.get( + app.url_path_for("hint:get", question_id=question["id"])) + assert res.status_code == 200 + res = res.json() + assert res == { + 'mean': None, + 'q1': None, + 'q2': None, + 'q3': None, + 'max': None, + 'min': None + } + # submit data with number + res = await client.post( + app.url_path_for("data:create", form_id=1), + json=[{ + "question": 1, + "value": "Option 1" + }, { + "question": 2, + "value": [2, 10] + }, { + "question": 3, + "value": { + "lat": -7.836114, + "lng": 110.331143 + } + }, { + "question": 4, + "value": "Hint Test" + }, { + "question": 5, + "value": 45 + }, { + "question": 6, + "value": ["Option A", "Option B"] + }, { + "question": 7, + "value": "2021-12-21" + }], + headers={"Authorization": f"Bearer {account.token}"}) + assert res.status_code == 200 + res = res.json() + assert res == { + "id": 5, + "name": "Garut - Hint Test", + "administration": 10, + "created": today, + "created_by": "Akvo Support", + "form": 1, + "geo": { + "lat": -7.836114, + "long": 110.331143 + }, + "updated": None, + "updated_by": None, + "answer": [{ + "question": 1, + "value": "Option 1" + }, { + "question": 2, + "value": 10 + }, { + "question": 3, + "value": "-7.836114|110.331143" + }, { + "question": 4, + "value": "Hint Test" + }, { + "question": 5, + "value": 45 + }, { + "question": 6, + "value": ["Option A", "Option B"] + }, { + "question": 7, + "value": "2021-12-21" + }] + } + # assert second data + res = await client.post( + app.url_path_for("data:create", form_id=1), + json=[{ + "question": 1, + "value": "Option 2" + }, { + "question": 2, + "value": [2, 10] + }, { + "question": 3, + "value": { + "lat": -7.836114, + "lng": 110.331143 + } + }, { + "question": 4, + "value": "Hint Test" + }, { + "question": 5, + "value": 55 + }, { + "question": 6, + "value": ["Option B"] + }, { + "question": 7, + "value": "2022-10-10" + }], + headers={"Authorization": f"Bearer {account.token}"}) + assert res.status_code == 200 + # get hint + res = await client.get( + app.url_path_for("hint:get", question_id=question["id"])) + assert res.status_code == 200 + res = res.json() + assert res == { + 'mean': 'Average value is 50.0', + 'q1': 'First quantile value is 47.5', + 'q2': 'Second quantile value is 50.0', + 'q3': 'Third quantile value is 52.5', + 'max': 'Maximum value is 55.0', + 'min': 'Minimum value is 45.0' + }