Skip to content

Commit

Permalink
Fix test cases for qbank required constraint
Browse files Browse the repository at this point in the history
  • Loading branch information
thenav56 committed Oct 11, 2023
1 parent 5cc8ed6 commit 00ee506
Show file tree
Hide file tree
Showing 9 changed files with 83 additions and 71 deletions.
4 changes: 3 additions & 1 deletion apps/export/tests/test_mutations.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from apps.project.models import ProjectMembership
from apps.export.models import QuestionnaireExport
from apps.questionnaire.factories import QuestionnaireFactory
from apps.qbank.factories import QuestionBankFactory


class TestExportMutation(TestCase):
Expand Down Expand Up @@ -94,11 +95,12 @@ def setUpClass(cls):
cls.user, cls.ro_user, cls.other_user = UserFactory.create_batch(3)

user_resource_params = {'created_by': cls.user, 'modified_by': cls.user}
cls.qbank = QuestionBankFactory.create(**user_resource_params)
cls.project = ProjectFactory.create(**user_resource_params)
cls.project.add_member(cls.user)
cls.project.add_member(cls.ro_user, role=ProjectMembership.Role.VIEWER)

cls.q1, _ = QuestionnaireFactory.create_batch(2, project=cls.project, **user_resource_params)
cls.q1, _ = QuestionnaireFactory.create_batch(2, project=cls.project, qbank=cls.qbank, **user_resource_params)

@patch('apps.export.serializers.export_task.delay')
def test_export(self, export_task_mock):
Expand Down
4 changes: 3 additions & 1 deletion apps/export/tests/test_queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from apps.project.factories import ProjectFactory
from apps.export.factories import QuestionnaireExportFactory
from apps.questionnaire.factories import QuestionnaireFactory
from apps.qbank.factories import QuestionBankFactory


class TestExportQuery(TestCase):
Expand Down Expand Up @@ -78,11 +79,12 @@ def setUpClass(cls):
cls.user1, cls.user2, cls.other_user = UserFactory.create_batch(3)

user_resource_params = {'created_by': cls.user1, 'modified_by': cls.user1}
cls.qbank = QuestionBankFactory.create(**user_resource_params)
cls.project = ProjectFactory.create(**user_resource_params)
cls.project.add_member(cls.user1)
cls.project.add_member(cls.user2)

q1, _ = QuestionnaireFactory.create_batch(2, project=cls.project, **user_resource_params)
q1, _ = QuestionnaireFactory.create_batch(2, project=cls.project, qbank=cls.qbank, **user_resource_params)

cls.exports = QuestionnaireExportFactory.create_batch(3, exported_by=cls.user1, questionnaire=q1)

Expand Down
4 changes: 3 additions & 1 deletion apps/export/tests/test_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from apps.export.factories import QuestionnaireExportFactory
from apps.export.tasks import export_task
from apps.export.models import QuestionnaireExport
from apps.qbank.factories import QuestionBankFactory
from apps.questionnaire.factories import (
QuestionnaireFactory,
QuestionFactory,
Expand All @@ -21,10 +22,11 @@ class TestExportTaskQuery(TestCase):
def test_questionnaire_export(self):
user = UserFactory.create()
user_resource_params = {'created_by': user, 'modified_by': user}
qbank = QuestionBankFactory.create(**user_resource_params)
project = ProjectFactory.create(**user_resource_params)
project.add_member(user)
# TODO: Add more cases
q1, _ = QuestionnaireFactory.create_batch(2, project=project, **user_resource_params)
q1, _ = QuestionnaireFactory.create_batch(2, project=project, **user_resource_params, qbank=qbank)
# For q1 only
choice_collections = ChoiceCollectionFactory.create_batch(
2,
Expand Down
3 changes: 3 additions & 0 deletions apps/qbank/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ def static_generator(cls, count, **kwargs):
if len(collections) < count:
raise Exception('Provided count is higher then available iteration')
leaf_groups = []
order = 1
for group_data in collections[:count]:
leaf_groups.append(
cls(
Expand All @@ -112,9 +113,11 @@ def static_generator(cls, count, **kwargs):
category_2=group_data['category_2'],
category_3=group_data.get('category_3'),
category_4=group_data.get('category_4'),
order=order,
**kwargs,
)
)
order += 1
return leaf_groups


Expand Down
2 changes: 1 addition & 1 deletion apps/qbank/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def get_queryset(_, queryset: models.QuerySet | None, info: Info):

@strawberry_django.field
async def leaf_groups(self, info: Info) -> list[QBLeafGroupType]:
queryset = QBLeafGroupType.get_queryset(None, None, info).filter(qbank=self.pk)
queryset = QBLeafGroupType.get_queryset(None, None, info).filter(qbank=self.pk).order_by('order', 'id')
return [q async for q in queryset]

@strawberry_django.field
Expand Down
71 changes: 44 additions & 27 deletions apps/questionnaire/tests/test_mutations.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
ChoiceCollection,
)
from apps.user.factories import UserFactory
from apps.qbank.models import QuestionBank
from apps.qbank.factories import QuestionBankFactory
from apps.questionnaire.factories import (
QuestionnaireFactory,
QuestionFactory,
Expand Down Expand Up @@ -79,6 +81,8 @@ class Mutation:
def test_create_questionnaire(self):
user = UserFactory.create()
ur_params = dict(created_by=user, modified_by=user)
qbank = QuestionBankFactory.create(**ur_params, status=QuestionBank.Status.SUCCESS)
qbank.activate()
# Create some projects
project = ProjectFactory.create(**ur_params)

Expand Down Expand Up @@ -127,10 +131,11 @@ def test_create_questionnaire(self):
def test_update_questionnaire(self):
user = UserFactory.create()
ur_params = dict(created_by=user, modified_by=user)
qbank = QuestionBankFactory.create(**ur_params)
# Create some projects
project, project2 = ProjectFactory.create_batch(2, **ur_params)
q1 = QuestionnaireFactory.create(**ur_params, project=project)
q2 = QuestionnaireFactory.create(**ur_params, project=project2)
q1 = QuestionnaireFactory.create(**ur_params, project=project, qbank=qbank)
q2 = QuestionnaireFactory.create(**ur_params, project=project2, qbank=qbank)

# Without login
variables = {
Expand Down Expand Up @@ -178,10 +183,11 @@ def test_update_questionnaire(self):
def test_delete_questionnaire(self):
user = UserFactory.create()
ur_params = dict(created_by=user, modified_by=user)
qbank = QuestionBankFactory.create(**ur_params)
# Create some projects
project, project2 = ProjectFactory.create_batch(2, **ur_params)
q1_1, q1_2 = QuestionnaireFactory.create_batch(2, **ur_params, project=project)
q2 = QuestionnaireFactory.create(**ur_params, project=project2)
q1_1, q1_2 = QuestionnaireFactory.create_batch(2, **ur_params, project=project, qbank=qbank)
q2 = QuestionnaireFactory.create(**ur_params, project=project2, qbank=qbank)
# Create some questions, groups and choice collections
[group1_1_1] = QuestionLeafGroupFactory.static_generator(1, **ur_params, questionnaire=q1_1)
[group1_2_1] = QuestionLeafGroupFactory.static_generator(1, **ur_params, questionnaire=q1_2)
Expand Down Expand Up @@ -398,10 +404,11 @@ class Mutation:
def test_create_question(self):
user = UserFactory.create()
ur_params = dict(created_by=user, modified_by=user)
qbank = QuestionBankFactory(**ur_params)
# Create some projects
project, project2 = ProjectFactory.create_batch(2, **ur_params)
q1 = QuestionnaireFactory.create(**ur_params, project=project)
q2 = QuestionnaireFactory.create(**ur_params, project=project2)
q1 = QuestionnaireFactory.create(**ur_params, project=project, qbank=qbank)
q2 = QuestionnaireFactory.create(**ur_params, project=project2, qbank=qbank)

[q1_group] = QuestionLeafGroupFactory.static_generator(1, **ur_params, questionnaire=q1)
[q2_group] = QuestionLeafGroupFactory.static_generator(1, **ur_params, questionnaire=q2)
Expand Down Expand Up @@ -485,10 +492,11 @@ def test_create_question(self):
def test_update_question(self):
user = UserFactory.create()
ur_params = dict(created_by=user, modified_by=user)
qbank = QuestionBankFactory(**ur_params)
# Create some projects
project, project2 = ProjectFactory.create_batch(2, **ur_params)
q1 = QuestionnaireFactory.create(**ur_params, project=project)
q2 = QuestionnaireFactory.create(**ur_params, project=project2)
q1 = QuestionnaireFactory.create(**ur_params, project=project, qbank=qbank)
q2 = QuestionnaireFactory.create(**ur_params, project=project2, qbank=qbank)

[group1] = QuestionLeafGroupFactory.static_generator(1, **ur_params, questionnaire=q1)
[group2] = QuestionLeafGroupFactory.static_generator(1, **ur_params, questionnaire=q2)
Expand Down Expand Up @@ -569,10 +577,11 @@ def test_update_question(self):
def test_delete_question(self):
user = UserFactory.create()
ur_params = dict(created_by=user, modified_by=user)
qbank = QuestionBankFactory(**ur_params)
# Create some projects
project, project2 = ProjectFactory.create_batch(2, **ur_params)
q1 = QuestionnaireFactory.create(**ur_params, project=project)
q2 = QuestionnaireFactory.create(**ur_params, project=project2)
q1 = QuestionnaireFactory.create(**ur_params, project=project, qbank=qbank)
q2 = QuestionnaireFactory.create(**ur_params, project=project2, qbank=qbank)

[group1] = QuestionLeafGroupFactory.static_generator(1, **ur_params, questionnaire=q1)
[group2] = QuestionLeafGroupFactory.static_generator(1, **ur_params, questionnaire=q2)
Expand Down Expand Up @@ -625,11 +634,12 @@ def test_delete_question(self):
def test_question_visibility(self):
user = UserFactory.create()
ur_params = dict(created_by=user, modified_by=user)
qbank = QuestionBankFactory(**ur_params)
# Create some projects
project, project2 = ProjectFactory.create_batch(2, **ur_params)
# Questionnaires
q1_1, q1_2 = QuestionnaireFactory.create_batch(2, **ur_params, project=project)
q2 = QuestionnaireFactory.create(**ur_params, project=project2)
q1_1, q1_2 = QuestionnaireFactory.create_batch(2, **ur_params, project=project, qbank=qbank)
q2 = QuestionnaireFactory.create(**ur_params, project=project2, qbank=qbank)
# Leaf groups
[group1] = QuestionLeafGroupFactory.static_generator(1, **ur_params, questionnaire=q1_1)
[group1_2] = QuestionLeafGroupFactory.static_generator(1, **ur_params, questionnaire=q1_2)
Expand Down Expand Up @@ -708,12 +718,13 @@ def test_question_visibility(self):
def test_question_order_update(self):
user = UserFactory.create()
ur_params = dict(created_by=user, modified_by=user)
qbank = QuestionBankFactory(**ur_params)
# Create some projects
project, project2 = ProjectFactory.create_batch(2, **ur_params)
# Questionnaire
q1_1 = QuestionnaireFactory.create(**ur_params, project=project)
q1_2 = QuestionnaireFactory.create(**ur_params, project=project)
q2_1 = QuestionnaireFactory.create(**ur_params, project=project2)
q1_1 = QuestionnaireFactory.create(**ur_params, project=project, qbank=qbank)
q1_2 = QuestionnaireFactory.create(**ur_params, project=project, qbank=qbank)
q2_1 = QuestionnaireFactory.create(**ur_params, project=project2, qbank=qbank)
# Groups
[
group1_1_1,
Expand Down Expand Up @@ -807,9 +818,10 @@ def setUpClass(cls):
super().setUpClass()
cls.user = UserFactory.create()
cls.ur_params = dict(created_by=cls.user, modified_by=cls.user)
cls.qbank = QuestionBankFactory(**cls.ur_params)
# Create some projects
cls.project = ProjectFactory.create(**cls.ur_params)
cls.q1 = QuestionnaireFactory.create(**cls.ur_params, project=cls.project)
cls.q1 = QuestionnaireFactory.create(**cls.ur_params, project=cls.project, qbank=cls.qbank)
cls.project.add_member(cls.user, role=ProjectMembership.Role.MEMBER)
cls.choice_collection = ChoiceCollectionFactory.create(**cls.ur_params, questionnaire=cls.q1)

Expand Down Expand Up @@ -905,10 +917,11 @@ class Mutation:
def test_question_leaf_group_visibility(self):
user = UserFactory.create()
ur_params = dict(created_by=user, modified_by=user)
qbank = QuestionBankFactory(**ur_params)
# Create some projects
project, project2 = ProjectFactory.create_batch(2, **ur_params)
q1, q1_2 = QuestionnaireFactory.create_batch(2, **ur_params, project=project)
q2 = QuestionnaireFactory.create(**ur_params, project=project2)
q1, q1_2 = QuestionnaireFactory.create_batch(2, **ur_params, project=project, qbank=qbank)
q2 = QuestionnaireFactory.create(**ur_params, project=project2, qbank=qbank)

[group1] = QuestionLeafGroupFactory.static_generator(1, **ur_params, questionnaire=q1)
[group1_2] = QuestionLeafGroupFactory.static_generator(1, **ur_params, questionnaire=q1_2)
Expand Down Expand Up @@ -973,11 +986,12 @@ def test_question_leaf_group_visibility(self):
def test_question_leaf_group_order_update(self):
user = UserFactory.create()
ur_params = dict(created_by=user, modified_by=user)
qbank = QuestionBankFactory(**ur_params)
# Create some projects
project, project2 = ProjectFactory.create_batch(2, **ur_params)
q1_1 = QuestionnaireFactory.create(**ur_params, project=project)
q1_2 = QuestionnaireFactory.create(**ur_params, project=project)
q2_1 = QuestionnaireFactory.create(**ur_params, project=project2)
q1_1 = QuestionnaireFactory.create(**ur_params, project=project, qbank=qbank)
q1_2 = QuestionnaireFactory.create(**ur_params, project=project, qbank=qbank)
q2_1 = QuestionnaireFactory.create(**ur_params, project=project2, qbank=qbank)

[
group1_1_1,
Expand Down Expand Up @@ -1136,10 +1150,11 @@ class Mutation:
def test_create_choice_collection(self):
user = UserFactory.create()
ur_params = dict(created_by=user, modified_by=user)
qbank = QuestionBankFactory(**ur_params)
# Create some projects
project, project2 = ProjectFactory.create_batch(2, **ur_params)
q1 = QuestionnaireFactory.create(**ur_params, project=project)
q2 = QuestionnaireFactory.create(**ur_params, project=project2)
q1 = QuestionnaireFactory.create(**ur_params, project=project, qbank=qbank)
q2 = QuestionnaireFactory.create(**ur_params, project=project2, qbank=qbank)

choice_collection_count = ChoiceCollection.objects.count()
# Without login
Expand Down Expand Up @@ -1233,10 +1248,11 @@ def test_create_choice_collection(self):
def test_update_choice_collection(self):
user = UserFactory.create()
ur_params = dict(created_by=user, modified_by=user)
qbank = QuestionBankFactory(**ur_params)
# Create some projects
project, project2 = ProjectFactory.create_batch(2, **ur_params)
q1 = QuestionnaireFactory.create(**ur_params, project=project)
q2 = QuestionnaireFactory.create(**ur_params, project=project2)
q1 = QuestionnaireFactory.create(**ur_params, project=project, qbank=qbank)
q2 = QuestionnaireFactory.create(**ur_params, project=project2, qbank=qbank)

ChoiceCollectionFactory.create(**ur_params, questionnaire=q1, name='choice_collection_01')
choice_collection_12 = ChoiceCollectionFactory.create(**ur_params, questionnaire=q1, name='choice_collection_02')
Expand Down Expand Up @@ -1313,10 +1329,11 @@ def test_update_choice_collection(self):
def test_delete_choice_collection(self):
user = UserFactory.create()
ur_params = dict(created_by=user, modified_by=user)
qbank = QuestionBankFactory(**ur_params)
# Create some projects
project, project2 = ProjectFactory.create_batch(2, **ur_params)
q1 = QuestionnaireFactory.create(**ur_params, project=project)
q2 = QuestionnaireFactory.create(**ur_params, project=project2)
q1 = QuestionnaireFactory.create(**ur_params, project=project, qbank=qbank)
q2 = QuestionnaireFactory.create(**ur_params, project=project2, qbank=qbank)

question1 = ChoiceCollectionFactory.create(**ur_params, questionnaire=q1, name='choice_collection_0101')
choice_collection_2 = ChoiceCollectionFactory.create(**ur_params, questionnaire=q2, name='choice_collection_0201')
Expand Down
Loading

0 comments on commit 00ee506

Please sign in to comment.