Skip to content

Commit

Permalink
[#157] Create init map & update data/maps endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
wayangalihpratama committed Aug 8, 2023
1 parent f5b0325 commit 075d2c8
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 56 deletions.
33 changes: 29 additions & 4 deletions backend/models/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,10 @@ class DataResponse(BaseModel):

class MapsData(BaseModel):
id: int
school_information: dict
year_conducted: int
geo: List[float]
# TODO:: DELETE commented code
# school_information: dict
# year_conducted: int
# geo: List[float]
answer: Union[AnswerDict, dict]


Expand All @@ -76,6 +77,20 @@ class MapDataResponse(BaseModel):
total_page: int


class InitMapsData(BaseModel):
id: int
school_information: dict
year_conducted: int
geo: List[float]


class InitMapDataResponse(BaseModel):
current: int
data: List[InitMapsData]
total: int
total_page: int


class RegistrationDict(TypedDict):
question: str
answer: Union[str, int, List[str]]
Expand Down Expand Up @@ -207,10 +222,20 @@ def to_maps(self):
return {
"id": self.id,
"identifier": self.identifier,
# TODO:: DELETE commented code
# "school_information": self.school_information,
# "year_conducted": self.year_conducted,
# "geo": self.geo,
"answer": {}
}

@property
def init_maps(self):
return {
"id": self.id,
"school_information": self.school_information,
"year_conducted": self.year_conducted,
"geo": self.geo,
"answer": {}
}

# only used in test case
Expand Down
72 changes: 69 additions & 3 deletions backend/routes/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
)
from models.data import (
MapDataResponse, ChartDataDetail,
DataDetailPopup, DataResponse
DataDetailPopup, DataResponse,
InitMapDataResponse
)
from models.answer import Answer
from models.question import QuestionType
Expand Down Expand Up @@ -109,6 +110,70 @@ def get_paginated_data(
}


@data_route.get(
"/data/maps-init",
response_model=InitMapDataResponse,
name="data:init_maps_data",
summary="get maps data",
tags=["Data"],
)
def get_maps_init(
req: Request,
page: int = 1,
perpage: int = 100,
page_only: Optional[bool] = False,
session: Session = Depends(get_session),
):
# get the data
page_data = get_all_data(
session=session,
current=True,
skip=(perpage * (page - 1)),
perpage=perpage
)
# handle pagination
count = page_data.get("count")
if not count:
return {
"current": page,
"data": [],
"total": count,
"total_page": 0,
}
total_page = ceil(count / perpage) if count > 0 else 0
if total_page < page:
return {
"current": page,
"data": [],
"total": count,
"total_page": total_page,
}
if page_only:
return {
"current": page,
"data": [],
"total": count,
"total_page": total_page,
}
# data
data = page_data.get("data") or []
data = [d.init_maps for d in data]
# transform data
for d in data:
d["school_information"] = extract_school_information(
school_information=d["school_information"], to_object=True)
res = {
"current": page,
"data": data,
"total": count,
"total_page": total_page,
}
return Response(
content=orjson.dumps(res),
media_type="application/json"
)


@data_route.get(
"/data/maps",
response_model=MapDataResponse,
Expand Down Expand Up @@ -214,8 +279,9 @@ def get_maps(
))
# transform data
for d in data:
d["school_information"] = extract_school_information(
school_information=d["school_information"], to_object=True)
# TODO:: DELETE commented code
# d["school_information"] = extract_school_information(
# school_information=d["school_information"], to_object=True)
d["jmp_filter"] = None
data_id = str(d.get('identifier'))
if "jmp" not in str(indicator):
Expand Down
83 changes: 34 additions & 49 deletions backend/tests/test_100_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,30 @@ async def test_get_answers_of_data(
assert "type" in c
assert "value" in c

@pytest.mark.asyncio
async def test_get_init_maps_data(
self, app: FastAPI, session: Session, client: AsyncClient
) -> None:
res = await client.get(
app.url_path_for("data:init_maps_data"),
params={"page_only": True}
)
assert res.status_code == 200
res = res.json()
assert list(res) == [
'current', 'data', 'total', 'total_page'
]
# load all data
res = await client.get(app.url_path_for("data:init_maps_data"))
assert res.status_code == 200
res = res.json()
assert list(res) == [
'current', 'data', 'total', 'total_page'
]
assert list(res["data"][0]) == [
'id', 'school_information', 'year_conducted', 'geo'
]

@pytest.mark.asyncio
async def test_get_maps_data(
self, app: FastAPI, session: Session, client: AsyncClient
Expand All @@ -168,10 +192,7 @@ async def test_get_maps_data(
assert list(res) == [
'current', 'data', 'total', 'total_page'
]
assert list(res["data"][0]) == [
'id', 'school_information',
'year_conducted', 'geo', 'answer'
]
assert list(res["data"][0]) == ['id', 'answer']
# TODO:: Delete
# assert res["data"][0] == {
# 'id': 649130936,
Expand All @@ -195,10 +216,7 @@ async def test_get_maps_data(
assert list(res) == [
'current', 'data', 'total', 'total_page'
]
assert list(res["data"][0]) == [
'id', 'school_information',
'year_conducted', 'geo', 'answer'
]
assert list(res["data"][0]) == ['id', 'answer']
assert list(res["data"][0]["answer"]) == ['question', 'value']
assert res["data"][0]["answer"]["question"] == indicator_id
# TODO:: delete
Expand Down Expand Up @@ -234,10 +252,7 @@ async def test_get_maps_data(
assert list(res) == [
'current', 'data', 'total', 'total_page'
]
assert list(res["data"][0]) == [
'id', 'school_information',
'year_conducted', 'geo', 'answer'
]
assert list(res["data"][0]) == ['id', 'answer']
assert list(res["data"][0]["answer"]) == ['question', 'value']
# TODO:: Delete
# assert res["data"] == [{
Expand Down Expand Up @@ -271,10 +286,7 @@ async def test_get_maps_data(
assert list(res) == [
'current', 'data', 'total', 'total_page'
]
assert list(res["data"][0]) == [
'id', 'school_information',
'year_conducted', 'geo', 'answer'
]
assert list(res["data"][0]) == ['id', 'answer']
assert list(res["data"][0]["answer"]) == ['question', 'value']
assert res["data"][0]["answer"]["question"] == indicator_id
# TODO: Delete
Expand Down Expand Up @@ -311,10 +323,7 @@ async def test_get_maps_data(
assert list(res) == [
'current', 'data', 'total', 'total_page'
]
assert list(res["data"][0]) == [
'id', 'school_information',
'year_conducted', 'geo', 'answer'
]
assert list(res["data"][0]) == ['id', 'answer']
assert list(res["data"][0]["answer"]) == ['question', 'value']
assert res["data"][0]["answer"]["question"] == indicator_id
# TODO: Delete
Expand Down Expand Up @@ -349,12 +358,7 @@ async def test_get_maps_data(
assert list(res) == [
'current', 'data', 'total', 'total_page'
]
assert list(res["data"][0]) == [
'id', 'school_information',
'year_conducted', 'geo', 'answer'
]
for d in res["data"]:
assert d["school_information"]["province"] == "Guadalcanal"
assert list(res["data"][0]) == ['id', 'answer']
# TODO:: delete
# assert res["data"][0] == {
# 'id': 649130936,
Expand Down Expand Up @@ -393,13 +397,7 @@ async def test_get_maps_data(
assert list(res) == [
'current', 'data', 'total', 'total_page'
]
assert list(res["data"][0]) == [
'id', 'school_information',
'year_conducted', 'geo', 'answer'
]
for d in res["data"]:
sctype = d["school_information"]["school_type"]
assert sctype == "Community High School"
assert list(res["data"][0]) == ['id', 'answer']
# TODO:: delete
# assert res["data"][0] == {
# 'id': 649130936,
Expand Down Expand Up @@ -439,14 +437,7 @@ async def test_get_maps_data(
assert list(res) == [
'current', 'data', 'total', 'total_page'
]
assert list(res["data"][0]) == [
'id', 'school_information',
'year_conducted', 'geo', 'answer'
]
for d in res["data"]:
sctype = d["school_information"]["school_type"]
assert sctype == "Community High School"
assert d["school_information"]["province"] == "Guadalcanal"
assert list(res["data"][0]) == ['id', 'answer']
# TODO:: delete
# assert res["data"][0] == {
# 'id': 649130936,
Expand All @@ -473,10 +464,7 @@ async def test_get_maps_data(
assert list(res) == [
'current', 'data', 'total', 'total_page'
]
assert list(res["data"][0]) == [
'id', 'school_information',
'year_conducted', 'geo', 'answer'
]
assert list(res["data"][0]) == ['id', 'answer']
assert list(res["data"][0]["answer"]) == ['question', 'value']
assert indicator_id in res["data"][0]["answer"]["question"]
# TODO: Delete
Expand Down Expand Up @@ -513,10 +501,7 @@ async def test_get_maps_data(
assert list(res) == [
'current', 'data', 'total', 'total_page'
]
assert list(res["data"][0]) == [
'id', 'school_information',
'year_conducted', 'geo', 'answer'
]
assert list(res["data"][0]) == ['id', 'answer']
assert list(res["data"][0]["answer"]) == ['question', 'value']
assert indicator_id in res["data"][0]["answer"]["question"]
# TODO: Delete
Expand Down

0 comments on commit 075d2c8

Please sign in to comment.