Skip to content

Commit

Permalink
[#213] Create test case to add new answer when update
Browse files Browse the repository at this point in the history
  • Loading branch information
wayangalihpratama committed Oct 10, 2022
1 parent c8b2760 commit 716c671
Show file tree
Hide file tree
Showing 8 changed files with 141 additions and 55 deletions.
6 changes: 1 addition & 5 deletions backend/db/crud_answer.py
Expand Up @@ -37,7 +37,7 @@ def add_answer(
session.add(answer)
session.commit()
session.flush()
session.refresh(answer)
# session.refresh(answer)
return answer


Expand All @@ -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,
Expand Down
5 changes: 3 additions & 2 deletions backend/tests/test_03_form.py
Expand Up @@ -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,
Expand All @@ -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"] == [{
Expand Down
45 changes: 44 additions & 1 deletion backend/tests/test_04_submission.py
Expand Up @@ -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": [{
Expand Down Expand Up @@ -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,
Expand Down
6 changes: 3 additions & 3 deletions backend/tests/test_05_data.py
Expand Up @@ -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",
Expand Down
12 changes: 10 additions & 2 deletions backend/tests/test_06_maps.py
Expand Up @@ -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,
Expand All @@ -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,
}]
51 changes: 44 additions & 7 deletions backend/tests/test_07_history.py
Expand Up @@ -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=[{
Expand Down Expand Up @@ -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():
Expand All @@ -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",
Expand Down
4 changes: 2 additions & 2 deletions backend/tests/test_13_worker_service.py
Expand Up @@ -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,
Expand Down Expand Up @@ -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
67 changes: 34 additions & 33 deletions backend/tests/test_14_advanced_query.py
Expand Up @@ -22,29 +22,31 @@ 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"},
{"id": 3, "question": "6", "answer": "i"},
{"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"},
Expand All @@ -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,
Expand All @@ -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
}]

0 comments on commit 716c671

Please sign in to comment.