Skip to content

Commit

Permalink
Update testcase of mutation
Browse files Browse the repository at this point in the history
  • Loading branch information
sudan45 authored and Rup-Narayan-Rajbanshi committed Apr 4, 2024
1 parent 415b460 commit 86d53c0
Show file tree
Hide file tree
Showing 7 changed files with 190 additions and 45 deletions.
6 changes: 6 additions & 0 deletions apps/project/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
Project,
ProjectJoinRequest,
ProjectOrganization,
ProjectPinned,
)


Expand All @@ -31,3 +32,8 @@ class Meta:
class ProjectOrganizationFactory(DjangoModelFactory):
class Meta:
model = ProjectOrganization


class ProjectPinnedFactory(DjangoModelFactory):
class Meta:
model = ProjectPinned
14 changes: 14 additions & 0 deletions apps/project/migrations/0007_merge_20240218_0618.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Generated by Django 3.2.17 on 2024-02-18 06:18

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('project', '0005_merge_20231227_0610'),
('project', '0006_projectpinned_modified_at'),
]

operations = [
]
6 changes: 3 additions & 3 deletions apps/project/mutation.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@
)

UserPinnedProjectReOrderInputType = generate_input_type_for_serializer(
'BulkUpdateProjectPinned',
'UserPinnedProjectReOrderInputType',
serializer_class=BulkProjectPinnedSerializer,
)

Expand Down Expand Up @@ -333,7 +333,7 @@ class Arguments:

class CreateUserPinnedProject(PsGrapheneMutation):
class Arguments:
data = ProjectPinnedInputType(required=False)
data = ProjectPinnedInputType(required=True)
model = ProjectPinned
result = graphene.Field(UserPinnedProjectType)
serializer_class = UserPinnedProjectSerializer
Expand Down Expand Up @@ -412,7 +412,7 @@ def perform_mutate(cls, root, info, **kwargs):
serializer = cls.serializer_class(data=data, instance=instance, context={'request': info.context.request})
errors_data.append(mutation_is_not_valid(serializer)) # errors_data also add empty list
serializers_data.append(serializer)
errors_data = [items for items in errors_data if items] # list compreshive removing empty list
errors_data = [items for items in errors_data if items] # list comprehension removing empty list
if errors_data:
return cls(errors=errors_data, ok=False)
for serializer in serializers_data:
Expand Down
47 changes: 34 additions & 13 deletions apps/project/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,8 @@ class Meta:
status_display = EnumDescription(source='get_status_display', required=True)
organizations = graphene.List(graphene.NonNull(ProjectOrganizationType))
has_analysis_framework = graphene.Boolean(required=True)
has_assessment_template = graphene.Boolean(required=True)
is_project_pinned = graphene.Boolean(required=True)

# NOTE: This is a custom feature
# see: https://github.com/eamigo86/graphene-django-extras/compare/graphene-v2...the-deep:graphene-v2
Expand Down Expand Up @@ -298,6 +300,12 @@ def resolve_regions(root, info, **kwargs):
return info.context.dl.project.geo_region.load(root.pk)
return info.context.dl.project.public_geo_region.load(root.pk)

def resolve_is_project_pinned(root, info, **kwargs):
return ProjectPinned.objects.filter(
project=root,
user=info.context.request.user
).exists()


class RecentActivityType(graphene.ObjectType):
id = graphene.ID(required=True)
Expand Down Expand Up @@ -384,17 +392,6 @@ def resolve_public_url(root, info, **_):
return root.get_public_url(info.context.request)


class UserPinnedProjectType(ClientIdMixin, DjangoObjectType):
class Meta:
model = ProjectPinned
only_fields = (
"project",
"user",
"order",
"client_id",
)


class ProjectDetailType(
# -- Start --Project scopped entities
LeadQuery,
Expand Down Expand Up @@ -458,6 +455,10 @@ class Meta:
# Other scoped queries
unified_connector = graphene.Field(UnifiedConnectorQueryType)
assisted_tagging = graphene.Field(AssistedTaggingQueryType)
is_project_pinned = graphene.Boolean(
required=True,
description='Check if user have pinned the project'
)

@staticmethod
def resolve_user_members(root, info, **kwargs):
Expand Down Expand Up @@ -507,6 +508,26 @@ def resolve_assisted_tagging(root, info, **kwargs):
if root.get_current_user_role(info.context.request.user) is not None:
return {}

@staticmethod
def resolve_is_project_pinned(root, info, **kwargs):
return ProjectPinned.objects.filter(
project=root,
user=info.context.request.user
).exists()


class UserPinnedProjectType(ClientIdMixin, DjangoObjectType):
class Meta:
model = ProjectPinned
only_fields = (
'id',
"project",
"user",
"order",
"client_id",
)
project = graphene.Field(graphene.NonNull(ProjectDetailType))


class ProjectByRegion(graphene.ObjectType):
id = graphene.ID(required=True, description='Region\'s ID')
Expand Down Expand Up @@ -588,7 +609,7 @@ class Query:
page_size_query_param='pageSize'
)
)
pinned_project = DjangoListField(UserPinnedProjectType, required=True)
user_pinned_projects = DjangoListField(UserPinnedProjectType, required=True)

# NOTE: This is a custom feature, see https://github.com/the-deep/graphene-django-extras
# see: https://github.com/eamigo86/graphene-django-extras/compare/graphene-v2...the-deep:graphene-v2
Expand Down Expand Up @@ -627,5 +648,5 @@ def resolve_public_projects_by_region(*args, **kwargs):
return Query.resolve_projects_by_region(*args, **kwargs)

@staticmethod
def resolve_pinned_project(root, info, **kwargs):
def resolve_user_pinned_project(root, info, **kwargs):
return ProjectPinned.objects.filter(user=info.context.user)
4 changes: 2 additions & 2 deletions apps/project/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
URLCachedFileField,
IntegerIDField,
TempClientIdMixin,
ProjectPropertySerializerMixin,
ProjectPropertySerializerMixin
)
from geo.models import Region
from geo.serializers import SimpleRegionSerializer
Expand Down Expand Up @@ -930,7 +930,7 @@ def update(self):


class BulkProjectPinnedSerializer(TempClientIdMixin, UserResourceSerializer):
id = serializers.IntegerField(required=True)
id = IntegerIDField(required=True)

class Meta:
model = ProjectPinned
Expand Down
102 changes: 101 additions & 1 deletion apps/project/tests/test_mutations.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,12 @@
from entry.factories import EntryFactory, EntryAttributeFactory
from analysis_framework.factories import AnalysisFrameworkFactory, WidgetFactory
from user_group.factories import UserGroupFactory
from project.factories import ProjectFactory, ProjectJoinRequestFactory, ProjectOrganizationFactory
from project.factories import (
ProjectFactory,
ProjectJoinRequestFactory,
ProjectOrganizationFactory,
ProjectPinnedFactory,
)
from organization.factories import OrganizationFactory
from geo.factories import RegionFactory

Expand Down Expand Up @@ -1381,3 +1386,98 @@ def _get_project_ids():
project6.id,
]
)

def test_create_user_pinned_project(self):
query = '''
mutation MyMutation($project: ID!) {
createUserPinnedProject(data: {project: $project}) {
ok
errors
result {
clientId
order
user{
id
}
project{
id
}
}
}
}
'''
project1 = ProjectFactory.create(
title='Test Project 1',
)
project2 = ProjectFactory.create(
title='Test Project 2',
)
member_user = UserFactory.create()
owner_user = UserFactory.create()
project1.add_member(member_user, role=self.project_role_member)
project2.add_member(owner_user, role=self.project_role_owner)
minput = dict(
project=project1.id
)

def _query_check(**kwargs):
return self.query_check(
query,
variables=minput,
**kwargs,
)
self.force_login(member_user)
response = _query_check()['data']['createUserPinnedProject']['result']
self.assertEqual(response['clientId'], str(project1.id))
self.assertEqual(response['order'], 1)
self.assertEqual(response['user']['id'], str(member_user.id))
self.assertEqual(response['project']['id'], str(project1.id))
# pin project which is already pinned by user
response = _query_check(assert_for_error=True)['errors']
self.assertIn("Project already pinned!!", response[0]['message'])
# pin another project
minput['project'] = project2.id
response = _query_check()['data']['createUserPinnedProject']['result']
self.assertEqual(response['clientId'], str(project2.id))
self.assertEqual(response['order'], 2)
self.assertEqual(response['project']['id'], str(project2.id))

def test_bulk_reorder_pinned_project(self):
project1 = ProjectFactory.create(title='Test project 3')
project2 = ProjectFactory.create(title='Test project 4')
member_user = UserFactory.create()
project1.add_member(member_user, role=self.project_role_member)
project2.add_member(member_user, role=self.project_role_member)
pinned_project1 = ProjectPinnedFactory.create(project=project1, user=member_user, order=10)
# pinned_project2 = ProjectPinnedFactory.create(project=project2, user=member_user, order=12)
minput = dict(
order=14,
id=pinned_project1.id
)
query = '''
mutation MyMutation($bulkReorder: UserPinnedProjectReOrderInputType!) {
reorderPinnedProjects(items: $bulkReorder) {
errors
ok
result {
clientId
order
project {
title
id
}
user {
id
}
}
}
}
'''

def _query_check(**kwargs):
return self.query_check(
query,
variable=minput,
**kwargs
)
self.force_login(member_user)
Loading

0 comments on commit 86d53c0

Please sign in to comment.