Skip to content

Commit

Permalink
testcase updated
Browse files Browse the repository at this point in the history
  • Loading branch information
sudan45 committed Apr 29, 2024
1 parent 5522557 commit df88420
Show file tree
Hide file tree
Showing 9 changed files with 256 additions and 36 deletions.
11 changes: 10 additions & 1 deletion apps/entry/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@

from gallery.factories import FileFactory

from .models import Entry, Attribute
from .models import (
Entry,
Attribute,
EntryComment,
)


class EntryFactory(DjangoModelFactory):
Expand Down Expand Up @@ -39,3 +43,8 @@ class EntryAttributeFactory(DjangoModelFactory):

class Meta:
model = Attribute


class EntryCommentFactory(DjangoModelFactory):
class Meta:
model = EntryComment
6 changes: 3 additions & 3 deletions apps/notification/dataloaders.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down
1 change: 0 additions & 1 deletion apps/notification/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion apps/notification/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
15 changes: 12 additions & 3 deletions apps/notification/tests/test_apis.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import pytest
from datetime import timedelta
from django.contrib.contenttypes.models import ContentType

from deep.tests import TestCase
from django.utils import timezone
Expand Down Expand Up @@ -508,9 +509,17 @@ def test_assignment_is_done(self):
project = self.create(Project)
user1 = self.create(User)
user2 = self.create(User)
assignment = self.create(Assignment, created_for=user1, project=project, created_by=user2)
self.create(Assignment, created_for=user1, project=project, created_by=user2)
self.create(Assignment, created_for=user1, project=project, created_by=user2)
lead = self.create(Lead, project=project)
kwargs = {
'object_id': lead.id,
'content_type': ContentType.objects.get_for_model(Lead),
'project': project,
'created_for': user1,
'created_by': user2,
}
assignment = self.create(Assignment, **kwargs)
self.create(Assignment, **kwargs)
self.create(Assignment, **kwargs)

url = '/api/v1/assignments/'
self.authenticate(user1)
Expand Down
89 changes: 86 additions & 3 deletions apps/notification/tests/test_mutation.py
Original file line number Diff line number Diff line change
@@ -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):
Expand Down Expand Up @@ -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)
Expand All @@ -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)
151 changes: 130 additions & 21 deletions apps/notification/tests/test_schemas.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
import datetime
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):
Expand Down Expand Up @@ -192,35 +197,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=}')
1 change: 1 addition & 0 deletions deep/dataloaders.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Loading

0 comments on commit df88420

Please sign in to comment.