diff --git a/apps/export/tests/test_mutations.py b/apps/export/tests/test_mutations.py index 1346709..754484f 100644 --- a/apps/export/tests/test_mutations.py +++ b/apps/export/tests/test_mutations.py @@ -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): @@ -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): diff --git a/apps/export/tests/test_queries.py b/apps/export/tests/test_queries.py index 34e77f3..0134dd8 100644 --- a/apps/export/tests/test_queries.py +++ b/apps/export/tests/test_queries.py @@ -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): @@ -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) diff --git a/apps/export/tests/test_tasks.py b/apps/export/tests/test_tasks.py index 2a64960..7ca5af7 100644 --- a/apps/export/tests/test_tasks.py +++ b/apps/export/tests/test_tasks.py @@ -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, @@ -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, diff --git a/apps/qbank/types.py b/apps/qbank/types.py index 1c0efce..cdc6332 100644 --- a/apps/qbank/types.py +++ b/apps/qbank/types.py @@ -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') return [q async for q in queryset] @strawberry_django.field diff --git a/apps/questionnaire/tests/test_mutations.py b/apps/questionnaire/tests/test_mutations.py index 5eef378..f1725e6 100644 --- a/apps/questionnaire/tests/test_mutations.py +++ b/apps/questionnaire/tests/test_mutations.py @@ -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, @@ -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) @@ -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 = { @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) @@ -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, @@ -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) @@ -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) @@ -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, @@ -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 @@ -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') @@ -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') diff --git a/apps/questionnaire/tests/test_queries.py b/apps/questionnaire/tests/test_queries.py index 88d146e..8bf22fd 100644 --- a/apps/questionnaire/tests/test_queries.py +++ b/apps/questionnaire/tests/test_queries.py @@ -3,6 +3,7 @@ from apps.user.factories import UserFactory from apps.project.factories import ProjectFactory from apps.questionnaire.models import Question +from apps.qbank.factories import QuestionBankFactory from apps.questionnaire.factories import ( QuestionnaireFactory, QuestionFactory, @@ -69,12 +70,13 @@ def test_questionnaires(self): # Create some users user, user2 = UserFactory.create_batch(2) user_resource_params = {'created_by': user, 'modified_by': user} + qbank = QuestionBankFactory.create(**user_resource_params) project1, project2 = ProjectFactory.create_batch(2, **user_resource_params) project1.add_member(user) project2.add_member(user2) - p1_questionnaires = QuestionnaireFactory.create_batch(3, **user_resource_params, project=project1) - p2_questionnaires = QuestionnaireFactory.create_batch(2, **user_resource_params, project=project2) + p1_questionnaires = QuestionnaireFactory.create_batch(3, **user_resource_params, project=project1, qbank=qbank) + p2_questionnaires = QuestionnaireFactory.create_batch(2, **user_resource_params, project=project2, qbank=qbank) # Without authentication ----- content = self.query_check( @@ -128,10 +130,11 @@ def test_questionnaire(self): # Create some users user = UserFactory.create() user_resource_params = {'created_by': user, 'modified_by': user} + qbank = QuestionBankFactory.create(**user_resource_params) project, project2 = ProjectFactory.create_batch(2, **user_resource_params) - questionnaires = QuestionnaireFactory.create_batch(3, project=project, **user_resource_params) - q2 = QuestionnaireFactory.create(project=project2, **user_resource_params) + questionnaires = QuestionnaireFactory.create_batch(3, project=project, **user_resource_params, qbank=qbank) + q2 = QuestionnaireFactory.create(project=project2, **user_resource_params, qbank=qbank) variables = {'projectId': self.gID(project.id)} # Without authentication ----- @@ -267,10 +270,11 @@ def test_leaf_groups(self): # Create some users 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) - q1, q2, q3 = QuestionnaireFactory.create_batch(3, project=project, **user_resource_params) + q1, q2, q3 = QuestionnaireFactory.create_batch(3, project=project, **user_resource_params, qbank=qbank) q1_groups = QuestionLeafGroupFactory.static_generator(2, **user_resource_params, questionnaire=q1) q2_groups = QuestionLeafGroupFactory.static_generator(3, **user_resource_params, questionnaire=q2) @@ -333,10 +337,11 @@ def test_leaf_group(self): # Create some users user = UserFactory.create() user_resource_params = {'created_by': user, 'modified_by': user} + qbank = QuestionBankFactory.create(**user_resource_params) project, project2 = ProjectFactory.create_batch(2, **user_resource_params) - q1 = QuestionnaireFactory.create(project=project, **user_resource_params) - q2 = QuestionnaireFactory.create(project=project2, **user_resource_params) + q1 = QuestionnaireFactory.create(project=project, **user_resource_params, qbank=qbank) + q2 = QuestionnaireFactory.create(project=project2, **user_resource_params, qbank=qbank) q1_group, *_ = QuestionLeafGroupFactory.static_generator(4, **user_resource_params, questionnaire=q1) [q2_group] = QuestionLeafGroupFactory.static_generator(1, **user_resource_params, questionnaire=q2) @@ -464,10 +469,11 @@ def test_choice_collections(self): # Create some users 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) - q1, q2, q3 = QuestionnaireFactory.create_batch(3, project=project, **user_resource_params) + q1, q2, q3 = QuestionnaireFactory.create_batch(3, project=project, **user_resource_params, qbank=qbank) q1_choice_collections = ChoiceCollectionFactory.create_batch( 2, @@ -533,10 +539,11 @@ def test_choice_collection(self): # Create some users user = UserFactory.create() user_resource_params = {'created_by': user, 'modified_by': user} + qbank = QuestionBankFactory.create(**user_resource_params) project, project2 = ProjectFactory.create_batch(2, **user_resource_params) - q1 = QuestionnaireFactory.create(project=project, **user_resource_params) - q2 = QuestionnaireFactory.create(project=project2, **user_resource_params) + q1 = QuestionnaireFactory.create(project=project, **user_resource_params, qbank=qbank) + q2 = QuestionnaireFactory.create(project=project2, **user_resource_params, qbank=qbank) q1_question_choice_collection, *_ = ChoiceCollectionFactory.create_batch(4, **user_resource_params, questionnaire=q1) q2_question_choice_collection = ChoiceCollectionFactory.create(**user_resource_params, questionnaire=q2) @@ -648,16 +655,7 @@ class Query: modifiedBy { id } - choiceCollection { - id - name - label - choices { - id - name - label - } - } + choiceCollectionId type name label @@ -672,10 +670,11 @@ def test_questions(self): # Create some users 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) - q1, q2, q3 = QuestionnaireFactory.create_batch(3, project=project, **user_resource_params) + q1, q2, q3 = QuestionnaireFactory.create_batch(3, project=project, **user_resource_params, qbank=qbank) [group1] = QuestionLeafGroupFactory.static_generator(1, **user_resource_params, questionnaire=q1) [group2] = QuestionLeafGroupFactory.static_generator(1, **user_resource_params, questionnaire=q2) @@ -744,11 +743,12 @@ def test_question(self): # Create some users user = UserFactory.create() user_resource_params = {'created_by': user, 'modified_by': user} + qbank = QuestionBankFactory.create(**user_resource_params) project, project2 = ProjectFactory.create_batch(2, **user_resource_params) question_params = {**user_resource_params, 'type': Question.Type.DATE} - q1 = QuestionnaireFactory.create(project=project, **user_resource_params) - q2 = QuestionnaireFactory.create(project=project2, **user_resource_params) + q1 = QuestionnaireFactory.create(project=project, **user_resource_params, qbank=qbank) + q2 = QuestionnaireFactory.create(project=project2, **user_resource_params, qbank=qbank) [group1] = QuestionLeafGroupFactory.static_generator(1, **user_resource_params, questionnaire=q1) [group2] = QuestionLeafGroupFactory.static_generator(1, **user_resource_params, questionnaire=q2) @@ -764,20 +764,6 @@ def test_question(self): ) question2 = QuestionFactory.create(**question_params, questionnaire=q2, leaf_group=group2) - choice_collection_response = { - 'id': self.gID(q1_choice_collection.pk), - 'name': self.gID(q1_choice_collection.name), - 'label': self.gID(q1_choice_collection.label), - 'choices': [ - { - 'id': self.gID(choice.pk), - 'name': self.gID(choice.name), - 'label': self.gID(choice.label), - } - for choice in q1_choice_collection.choice_set.all() - ], - } - variables = { 'projectId': self.gID(project.id), 'questionID': self.gID(question.id), @@ -814,7 +800,7 @@ def test_question(self): 'type': self.genum(question.type), 'label': question.label, 'hint': question.hint, - 'choiceCollection': choice_collection_response, + 'choiceCollectionId': self.gID(q1_choice_collection.pk), }, content # Another project question diff --git a/apps/questionnaire/types.py b/apps/questionnaire/types.py index eca54a2..358766a 100644 --- a/apps/questionnaire/types.py +++ b/apps/questionnaire/types.py @@ -110,7 +110,7 @@ def total_required_duration(self, info: Info) -> float: @strawberry_django.field async def leaf_groups(self, info: Info) -> list[QuestionLeafGroupType]: - queryset = QuestionLeafGroupType.get_queryset(None, None, info).filter(questionnaire=self.pk) + queryset = QuestionLeafGroupType.get_queryset(None, None, info).filter(questionnaire=self.pk).order_by('order') return [q async for q in queryset] @strawberry_django.field