From 0698e73c3250b38c5ea8a77f776ff0a5d7f2d8c8 Mon Sep 17 00:00:00 2001 From: Sudan Bhandari Date: Mon, 29 Apr 2024 13:54:39 +0545 Subject: [PATCH] testcase updated --- apps/entry/factories.py | 11 +- apps/notification/dataloaders.py | 6 +- apps/notification/models.py | 1 - apps/notification/schema.py | 5 +- apps/notification/tests/test_mutation.py | 89 ++++++++++++- apps/notification/tests/test_schemas.py | 152 +++++++++++++++++++---- deep/dataloaders.py | 1 + schema.graphql | 13 +- 8 files changed, 245 insertions(+), 33 deletions(-) diff --git a/apps/entry/factories.py b/apps/entry/factories.py index 580fb2bb61..6f49b618dc 100644 --- a/apps/entry/factories.py +++ b/apps/entry/factories.py @@ -4,7 +4,11 @@ from gallery.factories import FileFactory -from .models import Entry, Attribute +from .models import ( + Entry, + Attribute, + EntryComment, +) class EntryFactory(DjangoModelFactory): @@ -39,3 +43,8 @@ class EntryAttributeFactory(DjangoModelFactory): class Meta: model = Attribute + + +class EntryCommentFactory(DjangoModelFactory): + class Meta: + model = EntryComment diff --git a/apps/notification/dataloaders.py b/apps/notification/dataloaders.py index c16b64ad63..7e56e10ada 100644 --- a/apps/notification/dataloaders.py +++ b/apps/notification/dataloaders.py @@ -2,7 +2,6 @@ from promise import Promise -from entry.models import EntryComment from notification.models import Assignment from lead.models import Lead from quality_assurance.models import EntryReviewComment @@ -57,8 +56,9 @@ def batch_load_fn(self, keys): _result = { _id: { 'content_type': content_type, - 'lead': _lead_id_map.get(object_id) if content_type == 'lead' else None, - 'entry_review_comment': _entry_review_comment_id_map.get(object_id) if content_type == 'entryreviewcomment' else None, + 'lead': _lead_id_map.get(object_id) if content_type == Lead._meta.model else None, + 'entry_review_comment': + _entry_review_comment_id_map.get(object_id) if content_type == EntryReviewComment._meta.model else None, } for _id, content_type, object_id in assignment_qs } diff --git a/apps/notification/models.py b/apps/notification/models.py index 3f33daa423..9c9d81aec2 100644 --- a/apps/notification/models.py +++ b/apps/notification/models.py @@ -2,7 +2,6 @@ from django.contrib.contenttypes.models import ContentType from django.db import models from django.utils import timezone -from quality_assurance.models import EntryReviewComment from user.models import User from project.models import Project diff --git a/apps/notification/schema.py b/apps/notification/schema.py index d367cdc3bb..961f9e0fc1 100644 --- a/apps/notification/schema.py +++ b/apps/notification/schema.py @@ -3,7 +3,6 @@ from django.db.models import QuerySet from graphene_django import DjangoObjectType from graphene_django_extras import DjangoObjectField, PageGraphqlPagination -from lead.models import Lead from utils.graphene.enums import EnumDescription from utils.graphene.types import CustomDjangoListObjectType @@ -111,3 +110,7 @@ class Query: @staticmethod def resolve_notifications(root, info, **kwargs) -> QuerySet: return get_user_notification_qs(info) + + @staticmethod + def resolve_assignments(root, info, **kwargs) -> QuerySet: + return Assignment.get_for(info.context.user) diff --git a/apps/notification/tests/test_mutation.py b/apps/notification/tests/test_mutation.py index 2e61a9b42a..8cc5c66579 100644 --- a/apps/notification/tests/test_mutation.py +++ b/apps/notification/tests/test_mutation.py @@ -1,8 +1,12 @@ from utils.graphene.tests import GraphQLTestCase +from django.contrib.contenttypes.models import ContentType from user.factories import UserFactory -from notification.factories import NotificationFactory -from notification.models import Notification +from project.factories import ProjectFactory +from lead.factories import LeadFactory +from notification.factories import NotificationFactory, AssignmentFactory +from notification.models import Assignment, Notification +from lead.models import Lead class NotificationMutation(GraphQLTestCase): @@ -30,7 +34,6 @@ def _query_check(minput, **kwargs): minput=minput, **kwargs ) - minput = dict(id=notification.id, status=self.genum(Notification.Status.SEEN)) # -- Without login _query_check(minput, assert_for_error=True) @@ -48,3 +51,83 @@ def _query_check(minput, **kwargs): self.force_login(another_user) content = _query_check(minput, okay=False)['data']['notificationStatusUpdate']['result'] self.assertEqual(content, None, content) + + +class TestAssignmentMutation(GraphQLTestCase): + def test_assginment_bulk_status_mark_as_done(self): + self.assignment_query = ''' + mutation MyMutation { + assignmentBulkStatusMarkAsDone { + errors + ok + } + } + ''' + project = ProjectFactory.create() + user = UserFactory.create() + lead = LeadFactory.create() + AssignmentFactory.create_batch( + 3, + project=project, + object_id=lead.id, + content_type=ContentType.objects.get_for_model(Lead), + created_for=user, + is_done=False + ) + + def _query_check(**kwargs): + return self.query_check( + self.assignment_query, + **kwargs + ) + + self.force_login(user) + content = _query_check() + assignments_qs = Assignment.get_for(user).filter(is_done=False) + self.assertEqual(content['data']['assignmentBulkStatusMarkAsDone']['errors'], None) + self.assertEqual(len(assignments_qs), 0) + + def test_individual_assignment_update_status(self): + self.indivdual_assignment_query = ''' + mutation Mutation($isDone: Boolean, $id: ID! ){ + assignmentUpdate(id: $id, data: {isDone: $isDone}){ + ok + errors + result{ + id, + isDone + } + } + } + ''' + + user = UserFactory.create() + project = ProjectFactory.create() + lead = LeadFactory.create() + assignment = AssignmentFactory.create( + project=project, + object_id=lead.id, + content_type=ContentType.objects.get_for_model(Lead), + created_for=user, + is_done=False + + ) + + def _query_check(**kwargs): + return self.query_check( + self.indivdual_assignment_query, + variables={"isDone": True, "id": assignment.id}, + **kwargs + ) + + # without login + + _query_check(assert_for_error=True) + + # with normal login + + self.force_login(user) + content = _query_check() + assignment_qs = Assignment.get_for(user).filter(id=assignment.id, is_done=False) + self.assertEqual(content['data']['assignmentUpdate']['errors'], None) + self.assertEqual(len(assignment_qs), 0) diff --git a/apps/notification/tests/test_schemas.py b/apps/notification/tests/test_schemas.py index 29195196a3..fd02fb8189 100644 --- a/apps/notification/tests/test_schemas.py +++ b/apps/notification/tests/test_schemas.py @@ -1,13 +1,19 @@ import datetime +from re import S import pytz +from django.contrib.contenttypes.models import ContentType from utils.graphene.tests import GraphQLTestCase +from lead.models import Lead +from notification.models import Notification +from quality_assurance.models import EntryReviewComment + from user.factories import UserFactory from project.factories import ProjectFactory - -from notification.models import Notification from notification.factories import AssignmentFactory, NotificationFactory from lead.factories import LeadFactory +from entry.factories import EntryCommentFactory, EntryFactory +from analysis_framework.factories import AnalysisFrameworkFactory class TestNotificationQuerySchema(GraphQLTestCase): @@ -192,35 +198,139 @@ def _query_check(filters, **kwargs): class TestAssignmentQuerySchema(GraphQLTestCase): def test_assignments_query(self): query = ''' - query MyQuery { - assignments(isDone: false) { - results { - contentData { - contentType - entry { - id + query MyQuery { + assignments { + results { + contentData { + contentType + entryReviewComment { + entryId + id + leadId + } + lead { + id + title + } } - lead { + createdAt + id + isDone + objectId + project { id title } + createdFor { + id + } + createdBy { + id + } } - createdAt - id - isDone - objectId - project { - id - title - } + totalCount } } +''' + project = ProjectFactory.create() + user = UserFactory.create() + another = UserFactory.create() + lead = LeadFactory.create() + af = AnalysisFrameworkFactory.create() + entry = EntryFactory.create( + analysis_framework=af, + lead=lead + ) + entry_comment = EntryCommentFactory.create( + entry=entry, + created_by=user + ) + AssignmentFactory.create_batch( + 3, + project=project, + object_id=lead.id, + content_type=ContentType.objects.get_for_model(Lead), + created_for=user + ) + + def _query_check(**kwargs): + return self.query_check(query, **kwargs) + + # -- without login + _query_check(assert_for_error=True) + + # -- with login with different user + self.force_login(another) + content = _query_check() + self.assertEqual(content['data']['assignments']['results'], [], content) + + # -- with login normal user + self.force_login(user) + content = _query_check() + self.assertEqual(content['data']['assignments']['totalCount'], 3) + self.assertEqual(content['data']['assignments']['results'][0]['contentData']['contentType'], 'LEAD') + AssignmentFactory.create_batch( + 3, + project=project, + object_id=entry_comment.id, + content_type=ContentType.objects.get_for_model(EntryReviewComment), + created_for=user + ) + content = _query_check() + self.assertEqual(content['data']['assignments']['totalCount'], 6) + + def test_assignments_with_filter_query(self): + query = ''' + query MyQuery ( + $isDone : Boolean, + ){ + assignments( + isDone: $isDone, + ){ + totalCount + results{ + id + } + } } ''' project = ProjectFactory.create() user = UserFactory.create() lead = LeadFactory.create() - assignment = AssignmentFactory.create( - project=project.id, - object_id=lead.id + af = AnalysisFrameworkFactory.create() + entry = EntryFactory.create( + analysis_framework=af, + lead=lead ) + entry_comment = EntryCommentFactory.create( + entry=entry, + created_by=user + ) + AssignmentFactory.create_batch( + 3, + project=project, + object_id=lead.id, + content_type=ContentType.objects.get_for_model(Lead), + created_for=user, + is_done=False + ) + AssignmentFactory.create_batch( + 5, + project=project, + object_id=entry_comment.id, + content_type=ContentType.objects.get_for_model(EntryReviewComment), + created_for=user, + is_done=True + ) + + def _query_check(filters, **kwargs): + return self.query_check(query, variables=filters, **kwargs) + + self.force_login(user) + for filters, count in [ + ({'isDone': True}, 5), + ({'isDone': False}, 3), + ]: + content = _query_check(filters) + self.assertEqual(content['data']['assignments']['totalCount'], count, f'\n{filters=} \n{content=}') + self.assertEqual(len(content['data']['assignments']['results']), count, f'\n{filters=} \n{content=}') diff --git a/deep/dataloaders.py b/deep/dataloaders.py index 61ca9f6890..05109008ce 100644 --- a/deep/dataloaders.py +++ b/deep/dataloaders.py @@ -76,5 +76,6 @@ def deep_gallery(self): def assisted_tagging(self): return AssistedTaggingLoaders(context=self.context) + @cached_property def notification(self): return AssignmentLoaders(context=self.context) diff --git a/schema.graphql b/schema.graphql index 259f6fd92c..273caa1913 100644 --- a/schema.graphql +++ b/schema.graphql @@ -3082,13 +3082,20 @@ type AssessmentType { } type AssignmentContentDataType { - contentType: String + contentType: AssignmentContentTypeEnum lead: AssignmentLeadDetailType - entry: AssignmentEntryDetailType + entryReviewComment: AssignmentEntryReviewCommentDetailType } -type AssignmentEntryDetailType { +enum AssignmentContentTypeEnum { + LEAD + ENTRY_REVIEW_COMMENT +} + +type AssignmentEntryReviewCommentDetailType { id: ID! + entryId: ID! + leadId: ID! } input AssignmentInputType {