Skip to content

Commit

Permalink
Move leaf groups into Quesionnair node
Browse files Browse the repository at this point in the history
- Add Enum Display fields
- Update test cases
  • Loading branch information
thenav56 committed Sep 1, 2023
1 parent 77c8b7a commit 7d13ec1
Show file tree
Hide file tree
Showing 10 changed files with 273 additions and 152 deletions.
9 changes: 0 additions & 9 deletions apps/questionnaire/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,6 @@ class QuestionnaireFilter:
title: strawberry.auto


@strawberry_django.filters.filter(QuestionLeafGroup, lookups=True)
class QuestionLeafGroupFilter:
id: strawberry.auto
questionnaire: strawberry.auto
name: strawberry.auto
is_hidden: strawberry.auto
type: QuestionLeafGroupTypeEnum


@strawberry_django.filters.filter(ChoiceCollection, lookups=True)
class QuestionChoiceCollectionFilter:
id: strawberry.auto
Expand Down
8 changes: 0 additions & 8 deletions apps/questionnaire/orders.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
from .models import (
Questionnaire,
Question,
QuestionLeafGroup,
ChoiceCollection,
)

Expand All @@ -15,13 +14,6 @@ class QuestionnaireOrder:
created_at: strawberry.auto


@strawberry_django.ordering.order(QuestionLeafGroup)
class QuestionLeafGroupOrder:
id: strawberry.auto
order: strawberry.auto
created_at: strawberry.auto


@strawberry_django.ordering.order(ChoiceCollection)
class QuestionChoiceCollectionOrder:
id: strawberry.auto
Expand Down
8 changes: 0 additions & 8 deletions apps/questionnaire/queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,11 @@
from .filters import (
QuestionnaireFilter,
QuestionFilter,
QuestionLeafGroupFilter,
QuestionChoiceCollectionFilter,
)
from .orders import (
QuestionnaireOrder,
QuestionOrder,
QuestionLeafGroupOrder,
QuestionChoiceCollectionOrder,
)
from .types import (
Expand All @@ -33,12 +31,6 @@ class PrivateProjectQuery:
order=QuestionnaireOrder,
)

leafGroups: CountList[QuestionLeafGroupType] = pagination_field(
pagination=True,
filters=QuestionLeafGroupFilter,
order=QuestionLeafGroupOrder,
)

choice_collections: CountList[QuestionChoiceCollectionType] = pagination_field(
pagination=True,
filters=QuestionChoiceCollectionFilter,
Expand Down
130 changes: 75 additions & 55 deletions apps/questionnaire/tests/test_queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,21 +191,19 @@ def test_questionnaire(self):
class TestQuestionGroupQuery(TestCase):
class Query:
QuestionGroupList = '''
query MyQuery($projectId: ID!, $filterData: QuestionLeafGroupFilter) {
query MyQuery($projectId: ID!, $questionnaireId: ID!) {
private {
id
projectScope(pk: $projectId) {
leafGroups(order: {id: ASC}, filters: $filterData) {
count
items {
questionnaire(pk: $questionnaireId) {
leafGroups {
id
questionnaireId
name
order
isHidden
type
category1
category2
category3
category4
relevant
typeDisplay
createdAt
createdBy {
id
Expand All @@ -214,6 +212,15 @@ class Query:
modifiedBy {
id
}
category1
category2
category3
category4
category1Display
category2Display
category3Display
category4Display
relevant
}
}
}
Expand All @@ -229,12 +236,10 @@ class Query:
id
questionnaireId
name
relevant
order
isHidden
type
category1
category2
category3
category4
typeDisplay
createdAt
createdBy {
id
Expand All @@ -243,6 +248,15 @@ class Query:
modifiedBy {
id
}
category1
category2
category3
category4
category1Display
category2Display
category3Display
category4Display
relevant
}
}
}
Expand All @@ -261,8 +275,6 @@ def test_leaf_groups(self):
q1_groups = QuestionLeafGroupFactory.static_generator(2, **user_resource_params, questionnaire=q1)
q2_groups = QuestionLeafGroupFactory.static_generator(3, **user_resource_params, questionnaire=q2)
q3_groups = QuestionLeafGroupFactory.static_generator(5, **user_resource_params, questionnaire=q3)
q3_groups[0].name = 'question-group-unique-0001'
q3_groups[0].save(update_fields=('name',))

variables = {'projectId': self.gID(project.id)}
# Without authentication -----
Expand All @@ -275,46 +287,47 @@ def test_leaf_groups(self):

# With authentication -----
self.force_login(user)
for filter_data, question_leaf_groups in [
({'questionnaire': {'pk': self.gID(q1.id)}}, q1_groups),
({'questionnaire': {'pk': self.gID(q2.id)}}, q2_groups),
({'questionnaire': {'pk': self.gID(q3.id)}}, q3_groups),
({'name': {'exact': 'question-group-unique-0001'}}, [q3_groups[0]]),
for questionnaire_id, question_leaf_groups in [
(q1, q1_groups),
(q2, q2_groups),
(q3, q3_groups),
]:
variables['questionnaireId'] = self.gID(questionnaire_id.id)
content = self.query_check(
self.Query.QuestionGroupList,
variables={
**variables,
'filterData': filter_data,
},
variables=variables,
)
assert_msg = (content, user, filter_data, question_leaf_groups)
assert content['data']['private']['projectScope'] is not None, assert_msg
assert content['data']['private']['projectScope']['leafGroups'] == {
'count': len(question_leaf_groups),
'items': [
{
'id': self.gID(question_leaf_group.pk),
'questionnaireId': self.gID(question_leaf_group.questionnaire_id),
'createdAt': self.gdatetime(question_leaf_group.created_at),
'createdBy': {
'id': self.gID(question_leaf_group.created_by_id),
},
'modifiedAt': self.gdatetime(question_leaf_group.modified_at),
'modifiedBy': {
'id': self.gID(question_leaf_group.modified_by_id),
},
'name': question_leaf_group.name,
'relevant': question_leaf_group.relevant,
'type': self.genum(question_leaf_group.type),
'category1': self.genum(question_leaf_group.category_1),
'category2': self.genum(question_leaf_group.category_2),
'category3': self.genum(question_leaf_group.category_3),
'category4': self.genum(question_leaf_group.category_4),
}
for question_leaf_group in question_leaf_groups
]
}, assert_msg
assert_msg = (content, user, questionnaire_id, question_leaf_groups)
assert content['data']['private']['projectScope']['questionnaire'] is not None, assert_msg
assert content['data']['private']['projectScope']['questionnaire']['leafGroups'] == [
{
'id': self.gID(question_leaf_group.pk),
'questionnaireId': self.gID(question_leaf_group.questionnaire_id),
'name': question_leaf_group.name,
'order': question_leaf_group.order,
'isHidden': question_leaf_group.is_hidden,
'type': self.genum(question_leaf_group.type),
'typeDisplay': question_leaf_group.get_type_display(),
'createdAt': self.gdatetime(question_leaf_group.created_at),
'createdBy': {
'id': self.gID(question_leaf_group.created_by_id),
},
'modifiedAt': self.gdatetime(question_leaf_group.modified_at),
'modifiedBy': {
'id': self.gID(question_leaf_group.modified_by_id),
},
'category1': self.genum(question_leaf_group.category_1),
'category2': self.genum(question_leaf_group.category_2),
'category3': self.genum(question_leaf_group.category_3),
'category4': self.genum(question_leaf_group.category_4),
'category1Display': question_leaf_group.get_category_1_display(),
'category2Display': question_leaf_group.get_category_2_display(),
'category3Display': question_leaf_group.get_category_3_display(),
'category4Display': question_leaf_group.get_category_4_display(),
'relevant': question_leaf_group.relevant,
}
for question_leaf_group in question_leaf_groups
], assert_msg

def test_leaf_group(self):
# Create some users
Expand Down Expand Up @@ -351,6 +364,11 @@ def test_leaf_group(self):
assert content['data']['private']['projectScope']['leafGroup'] == {
'id': self.gID(q1_group.pk),
'questionnaireId': self.gID(q1_group.questionnaire_id),
'name': q1_group.name,
'order': q1_group.order,
'isHidden': q1_group.is_hidden,
'type': self.genum(q1_group.type),
'typeDisplay': q1_group.get_type_display(),
'createdAt': self.gdatetime(q1_group.created_at),
'createdBy': {
'id': self.gID(q1_group.created_by_id),
Expand All @@ -359,13 +377,15 @@ def test_leaf_group(self):
'modifiedBy': {
'id': self.gID(q1_group.modified_by_id),
},
'name': q1_group.name,
'relevant': q1_group.relevant,
'type': self.genum(q1_group.type),
'category1': self.genum(q1_group.category_1),
'category2': self.genum(q1_group.category_2),
'category3': self.genum(q1_group.category_3),
'category4': self.genum(q1_group.category_4),
'category1Display': q1_group.get_category_1_display(),
'category2Display': q1_group.get_category_2_display(),
'category3Display': q1_group.get_category_3_display(),
'category4Display': q1_group.get_category_4_display(),
'relevant': q1_group.relevant,
}, content

# Another project question group
Expand Down
72 changes: 38 additions & 34 deletions apps/questionnaire/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,10 @@
from django.db import models

from utils.common import get_queryset_for_model
from utils.strawberry.enums import enum_display_field, enum_field
from apps.common.types import UserResourceTypeMixin, ClientIdMixin
from apps.project.models import Project

from .enums import (
QuestionTypeEnum,
QuestionLeafGroupTypeEnum,
QuestionLeafGroupCategory1TypeEnum,
QuestionLeafGroupCategory2TypeEnum,
QuestionLeafGroupCategory3TypeEnum,
QuestionLeafGroupCategory4TypeEnum,
)
from .models import (
Questionnaire,
Question,
Expand All @@ -26,40 +19,25 @@
)


@strawberry_django.type(Questionnaire)
class QuestionnaireType(UserResourceTypeMixin):
id: strawberry.ID
title: strawberry.auto

@staticmethod
def get_queryset(_, queryset: models.QuerySet | None, info: Info):
qs = get_queryset_for_model(Questionnaire, queryset)
if (
info.context.active_project and
info.context.has_perm(Project.Permission.VIEW_QUESTIONNAIRE)
):
return qs.filter(project=info.context.active_project.project)
return qs.none()

@strawberry.field
def project_id(self) -> strawberry.ID:
return strawberry.ID(str(self.project_id))


@strawberry_django.type(QuestionLeafGroup)
class QuestionLeafGroupType(UserResourceTypeMixin):
id: strawberry.ID
name: strawberry.auto
type: QuestionLeafGroupTypeEnum
type = enum_field(QuestionLeafGroup.type)
type_display = enum_display_field(QuestionLeafGroup.type)
order: strawberry.auto
is_hidden: strawberry.auto
# Categories
# -- For Matrix1D/Matrix2D
category_1: QuestionLeafGroupCategory1TypeEnum
category_2: QuestionLeafGroupCategory2TypeEnum
category_1 = enum_field(QuestionLeafGroup.category_1)
category_1_display = enum_display_field(QuestionLeafGroup.category_1)
category_2 = enum_field(QuestionLeafGroup.category_2)
category_2_display = enum_display_field(QuestionLeafGroup.category_2)
# -- For Matrix2D
category_3: typing.Optional[QuestionLeafGroupCategory3TypeEnum]
category_4: typing.Optional[QuestionLeafGroupCategory4TypeEnum]
category_3 = enum_field(QuestionLeafGroup.category_3)
category_3_display = enum_display_field(QuestionLeafGroup.category_3)
category_4 = enum_field(QuestionLeafGroup.category_4)
category_4_display = enum_display_field(QuestionLeafGroup.category_4)
# Misc
relevant: strawberry.auto

Expand All @@ -78,6 +56,31 @@ def get_queryset(_, queryset: models.QuerySet | None, info: Info):
return qs.none()


@strawberry_django.type(Questionnaire)
class QuestionnaireType(UserResourceTypeMixin):
id: strawberry.ID
title: strawberry.auto

@staticmethod
def get_queryset(_, queryset: models.QuerySet | None, info: Info):
qs = get_queryset_for_model(Questionnaire, queryset)
if (
info.context.active_project and
info.context.has_perm(Project.Permission.VIEW_QUESTIONNAIRE)
):
return qs.filter(project=info.context.active_project.project)
return qs.none()

@strawberry.field
def project_id(self) -> strawberry.ID:
return strawberry.ID(str(self.project_id))

@strawberry_django.field
async def leaf_groups(self, info: Info) -> list[QuestionLeafGroupType]:
queryset = QuestionLeafGroupType.get_queryset(None, None, info).filter(questionnaire=self.pk)
return [q async for q in queryset]


@strawberry_django.type(Choice)
class QuestionChoiceType(ClientIdMixin):
id: strawberry.ID
Expand Down Expand Up @@ -143,7 +146,8 @@ class QuestionType(UserResourceTypeMixin):
is_or_other: strawberry.auto
or_other_label: strawberry.auto

type: QuestionTypeEnum
type = enum_field(Question.type)
type_display = enum_display_field(Question.type)

@staticmethod
def get_queryset(_, queryset: models.QuerySet | None, info: Info):
Expand Down
Loading

0 comments on commit 7d13ec1

Please sign in to comment.