Skip to content

Commit

Permalink
[#21] Add repeat into Category model
Browse files Browse the repository at this point in the history
  • Loading branch information
wayangalihpratama committed Sep 14, 2023
1 parent 563569b commit 0ab73b4
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 2 deletions.
1 change: 1 addition & 0 deletions dev/backend/tests/test_03_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ async def test_views_get_result_keys(
assert list(res.head(1).to_dict("records")[0].keys()) == [
"id",
"data",
"repeat",
"form",
"name",
"category",
Expand Down
3 changes: 2 additions & 1 deletion src/AkvoResponseGrouper/cli/generate_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ def generate_schema(file_config: str) -> str:
question_config = get_question_config(config=c, cl=question_config)
ql = ",".join(question_config)
mview += (
f"SELECT q.form, a.data, a.repeat, '{config['name']}' as name,"
"SELECT q.form, a.data, COALESCE(a.repeat, 0) as repeat,"
f" '{config['name']}' as name,"
" jsonb_object_agg(a.question,COALESCE(a.options,"
" array[a.value::text])) as opt \n"
)
Expand Down
4 changes: 4 additions & 0 deletions src/AkvoResponseGrouper/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class GroupByDict(TypedDict):
class CategoryDict(TypedDict):
id: int
data: int
repeat: int
form: int
name: str
category: str
Expand All @@ -23,6 +24,7 @@ class CategoryDict(TypedDict):
class CategoryModelDict(TypedDict):
id: int
data: int
repeat: int
form: int
name: str
opt: dict
Expand All @@ -33,6 +35,7 @@ class Category(Base):
id = Column(Integer, primary_key=True)
form = Column(Integer)
data = Column(Integer)
repeat = Column(Integer)
name = Column(Text)
opt = Column(JSON)

Expand All @@ -44,6 +47,7 @@ def serialize(self) -> CategoryModelDict:
return {
"id": self.id,
"data": self.data,
"repeat": self.repeat,
"form": self.form,
"name": self.name,
"opt": self.opt,
Expand Down
5 changes: 4 additions & 1 deletion src/AkvoResponseGrouper/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,16 +132,18 @@ def get_category(data: dict, file_path: str = "./.category.json"):
def transform_categories_to_df(
categories: list, file_path: str = "./.category.json"
):
expected_cols = ["id", "data", "repeat", "form", "name", "opt", "category"]
df = pd.DataFrame(categories)
results = df.to_dict("records")
for d in results:
d.update({"category": get_category(data=d, file_path=file_path)})
res = pd.DataFrame(results)
if list(res) != ["id", "data", "form", "name", "opt", "category"]:
if list(res) != expected_cols:
return pd.DataFrame(
columns=[
"id",
"data",
"repeat",
"form",
"name",
"category",
Expand All @@ -154,6 +156,7 @@ def transform_categories_to_df(
[
"id",
"data",
"repeat",
"form",
"name",
"category",
Expand Down

0 comments on commit 0ab73b4

Please sign in to comment.