From 716c67135b2ce6c7ce44f5666c1407498ebd038e Mon Sep 17 00:00:00 2001 From: wayangalihpratama Date: Mon, 10 Oct 2022 11:55:57 +0800 Subject: [PATCH] [#213] Create test case to add new answer when update --- backend/db/crud_answer.py | 6 +-- backend/tests/test_03_form.py | 5 +- backend/tests/test_04_submission.py | 45 ++++++++++++++++- backend/tests/test_05_data.py | 6 +-- backend/tests/test_06_maps.py | 12 ++++- backend/tests/test_07_history.py | 51 ++++++++++++++++--- backend/tests/test_13_worker_service.py | 4 +- backend/tests/test_14_advanced_query.py | 67 +++++++++++++------------ 8 files changed, 141 insertions(+), 55 deletions(-) diff --git a/backend/db/crud_answer.py b/backend/db/crud_answer.py index a237e4c2..bf7a7efa 100644 --- a/backend/db/crud_answer.py +++ b/backend/db/crud_answer.py @@ -37,7 +37,7 @@ def add_answer( session.add(answer) session.commit() session.flush() - session.refresh(answer) + # session.refresh(answer) return answer @@ -56,10 +56,6 @@ def update_answer( return answer -def get_answer(session: Session) -> List[AnswerDict]: - return session.query(Answer).all() - - def get_answer_by_question( session: Session, question: int, diff --git a/backend/tests/test_03_form.py b/backend/tests/test_03_form.py index d3f4008d..eb06dcfa 100644 --- a/backend/tests/test_03_form.py +++ b/backend/tests/test_03_form.py @@ -205,7 +205,8 @@ async def test_datapoint_name_question(self, app: FastAPI, "form": 1, "question_group": "Test Question Group", "meta": True, - "type": "text" + "type": "text", + "required": False, }, json={"dependency": [{ "id": 1, @@ -221,7 +222,7 @@ async def test_datapoint_name_question(self, app: FastAPI, assert res["order"] == 4 assert res["name"] == "Test Datapoint Text Question" assert res["meta"] is True - assert res["required"] is True + assert res["required"] is False assert res["rule"] is None assert res["type"] == "text" assert res["dependency"] == [{ diff --git a/backend/tests/test_04_submission.py b/backend/tests/test_04_submission.py index 72c6a9df..31805cdd 100644 --- a/backend/tests/test_04_submission.py +++ b/backend/tests/test_04_submission.py @@ -131,7 +131,7 @@ async def test_get_form(self, app: FastAPI, session: Session, "order": 4, "question_group": 1, "type": "text", - "required": True, + "required": False, "rule": None, "option": [], "dependency": [{ @@ -264,6 +264,49 @@ async def test_submit_data( "value": "Garut" }] } + # second submission + 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 + } + }], + headers={"Authorization": f"Bearer {account.token}"}) + assert res.status_code == 200 + res = res.json() + assert res == { + "id": 2, + "name": "Garut", + "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 2" + }, { + "question": 2, + "value": 10 + }, { + "question": 3, + "value": "-7.836114|110.331143" + }] + } @pytest.mark.asyncio async def test_get_all_option(self, app: FastAPI, session: Session, diff --git a/backend/tests/test_05_data.py b/backend/tests/test_05_data.py index f8dc0b38..c800e9dd 100644 --- a/backend/tests/test_05_data.py +++ b/backend/tests/test_05_data.py @@ -22,10 +22,10 @@ async def test_get_data(self, app: FastAPI, session: Session, assert res.status_code == 200 res = res.json() assert res["current"] == 1 - assert res["total"] == 1 + assert res["total"] == 2 assert res["total_page"] == 1 - assert len(res["data"]) == 1 - first_data = res["data"][0] + assert len(res["data"]) == 2 + first_data = res["data"][len(res["data"]) - 1] assert first_data == { "id": 1, "name": "Garut - Garut", diff --git a/backend/tests/test_06_maps.py b/backend/tests/test_06_maps.py index 9d8695ad..0ac35850 100644 --- a/backend/tests/test_06_maps.py +++ b/backend/tests/test_06_maps.py @@ -12,8 +12,8 @@ class TestMapsRoutes(): @pytest.mark.asyncio - async def test_get_data(self, app: FastAPI, session: Session, - client: AsyncClient) -> None: + async def test_get_map_data( + self, app: FastAPI, session: Session, client: AsyncClient) -> None: res = await client.get(app.url_path_for("maps:get", form_id=1), params={ "marker": 1, @@ -29,4 +29,12 @@ async def test_get_data(self, app: FastAPI, session: Session, "marker": "Option 1", "marker_hover": None, "shape": 10.0, + }, { + "id": 2, + "geo": [-7.836114, 110.331143], + "name": "Garut", + "loc": "Garut", + "marker": "Option 2", + "marker_hover": None, + "shape": 10.0, }] diff --git a/backend/tests/test_07_history.py b/backend/tests/test_07_history.py index d84c5c80..10fbcf44 100644 --- a/backend/tests/test_07_history.py +++ b/backend/tests/test_07_history.py @@ -14,8 +14,8 @@ class TestDataUpdateRoutes(): @pytest.mark.asyncio - async def test_update_data(self, app: FastAPI, session: Session, - client: AsyncClient) -> None: + async def test_update_data( + self, app: FastAPI, session: Session, client: AsyncClient) -> None: res = await client.put( app.url_path_for("data:update", id=1), json=[{ @@ -55,6 +55,43 @@ async def test_update_data(self, app: FastAPI, session: Session, "value": "Bandung" }] } + # update data 2 + res = await client.put( + app.url_path_for("data:update", id=2), + json=[{ + "question": 4, + "value": "Bali" + }], + headers={"Authorization": f"Bearer {account.token}"}) + assert res.status_code == 200 + res = res.json() + assert res == { + "id": 2, + "name": "Garut", + "administration": 10, + "created_by": "Akvo Support", + "created": today, + "form": 1, + "geo": { + "lat": -7.836114, + "long": 110.331143 + }, + "updated_by": "Akvo Support", + "updated": today, + "answer": [{ + "question": 1, + "value": "Option 2" + }, { + "question": 2, + "value": 10 + }, { + "question": 3, + "value": "-7.836114|110.331143" + }, { + "question": 4, + "value": "Bali" + }] + } class TestHistoryRoutes(): @@ -77,18 +114,18 @@ async def test_get_history(self, app: FastAPI, session: Session, }] @pytest.mark.asyncio - async def test_get_data(self, app: FastAPI, session: Session, - client: AsyncClient) -> None: + async def test_get_data_with_history_status( + self, app: FastAPI, session: Session, client: AsyncClient) -> None: res = await client.get( app.url_path_for("data:get", form_id=1), headers={"Authorization": f"Bearer {account.token}"}) assert res.status_code == 200 res = res.json() assert res["current"] == 1 - assert res["total"] == 1 + assert res["total"] == 2 assert res["total_page"] == 1 - assert len(res["data"]) == 1 - first_data = res["data"][0] + assert len(res["data"]) == 2 + first_data = res["data"][len(res["data"]) - 1] assert first_data == { "id": 1, "name": "Garut - Garut", diff --git a/backend/tests/test_13_worker_service.py b/backend/tests/test_13_worker_service.py index ea9da1e4..4e11479d 100644 --- a/backend/tests/test_13_worker_service.py +++ b/backend/tests/test_13_worker_service.py @@ -25,7 +25,7 @@ async def test_get_data_before_recorded(self, app: FastAPI, headers={"Authorization": f"Bearer {account.token}"}) assert res.status_code == 200 res = res.json() - assert res["total"] == 1 + assert res["total"] == 2 @pytest.mark.asyncio async def test_execute_first_queue(self, worker: FastAPI, session: Session, @@ -64,4 +64,4 @@ async def test_get_data_after_recorded(self, app: FastAPI, headers={"Authorization": f"Bearer {account.token}"}) assert res.status_code == 200 res = res.json() - assert res["total"] == 3 + assert res["total"] == 4 diff --git a/backend/tests/test_14_advanced_query.py b/backend/tests/test_14_advanced_query.py index eeb908a8..7014409d 100644 --- a/backend/tests/test_14_advanced_query.py +++ b/backend/tests/test_14_advanced_query.py @@ -22,18 +22,19 @@ async def test_get_data_with_query_option(self, app: FastAPI, data_view = [d.raw for d in data_view] assert data_view == [ [ - {"id": 2, "question": "1", "answer": "option 1"}, - {"id": 2, "question": "6", "answer": "o"}, - {"id": 2, "question": "6", "answer": "p"}, - {"id": 2, "question": "6", "answer": "t"}, - {"id": 2, "question": "6", "answer": "i"}, - {"id": 2, "question": "6", "answer": "o"}, - {"id": 2, "question": "6", "answer": "n"}, - {"id": 2, "question": "6", "answer": " "}, - {"id": 2, "question": "6", "answer": "a"}, + {"id": 4, "question": "1", "answer": "option 2"}, + {"id": 4, "question": "6", "answer": "o"}, + {"id": 4, "question": "6", "answer": "p"}, + {"id": 4, "question": "6", "answer": "t"}, + {"id": 4, "question": "6", "answer": "i"}, + {"id": 4, "question": "6", "answer": "o"}, + {"id": 4, "question": "6", "answer": "n"}, + {"id": 4, "question": "6", "answer": " "}, + {"id": 4, "question": "6", "answer": "b"}, ], + [{"id": 2, "question": "1", "answer": "option 2"}], [ - {"id": 3, "question": "1", "answer": "option 2"}, + {"id": 3, "question": "1", "answer": "option 1"}, {"id": 3, "question": "6", "answer": "o"}, {"id": 3, "question": "6", "answer": "p"}, {"id": 3, "question": "6", "answer": "t"}, @@ -41,10 +42,11 @@ async def test_get_data_with_query_option(self, app: FastAPI, {"id": 3, "question": "6", "answer": "o"}, {"id": 3, "question": "6", "answer": "n"}, {"id": 3, "question": "6", "answer": " "}, - {"id": 3, "question": "6", "answer": "b"}, + {"id": 3, "question": "6", "answer": "a"}, ], [{"id": 1, "question": "1", "answer": "option 2"}], ] + res = await client.get( app.url_path_for("data:get", form_id=1), params={"q": "1|option 1"}, @@ -63,9 +65,9 @@ async def test_get_data_with_query_option(self, app: FastAPI, assert res.status_code == 200 res = res.json() assert res["current"] == 1 - assert res["total"] == 2 + assert res["total"] == 3 assert res["total_page"] == 1 - assert len(res["data"]) == 2 + assert len(res["data"]) == 3 @pytest.mark.asyncio async def test_get_maps_with_query_option(self, app: FastAPI, @@ -83,36 +85,35 @@ async def test_get_maps_with_query_option(self, app: FastAPI, res = res.json() assert res == [{ "id": 1, + "loc": "Garut", "geo": [-7.836114, 110.331143], "name": "Garut - Garut", - "loc": "Garut", "marker": "Option 2", + "shape": 10.0, "marker_hover": [ - { - "id": 2, - "value": 10.0, - }, - { - "id": 1, - "value": "Option 2", - } + {"id": 2, "value": 10.0}, + {"id": 1, "value": "Option 2"} ], + }, { + "id": 2, + "loc": "Garut", + "geo": [-7.836114, 110.331143], + "name": "Garut", + "marker": "Option 2", "shape": 10.0, + "marker_hover": [ + {"id": 1, "value": "Option 2"}, + {"id": 2, "value": 10.0} + ], }, { - "id": 3, + "id": 4, + "loc": "Bantul", "geo": [-6.2, 106.81], "name": "Bantul - Testing Data 2", - "loc": "Bantul", "marker": "Option 2", + "shape": 24.0, "marker_hover": [ - { - "id": 1, - "value": "Option 2", - }, - { - "id": 2, - "value": 24.0, - } + {"id": 1, "value": "Option 2"}, + {"id": 2, "value": 24.0} ], - "shape": 24.0 }]