Skip to content

Commit

Permalink
update testcase of mutation
Browse files Browse the repository at this point in the history
  • Loading branch information
sudan45 committed Jan 23, 2024
1 parent 7c5b20a commit 761bc69
Show file tree
Hide file tree
Showing 5 changed files with 149 additions and 43 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
6 changes: 3 additions & 3 deletions apps/project/mutation.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@
)

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

Expand Down Expand Up @@ -332,7 +332,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 @@ -410,7 +410,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
27 changes: 14 additions & 13 deletions apps/project/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -387,17 +387,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 @@ -508,6 +497,18 @@ def resolve_assisted_tagging(root, info, **kwargs):
return {}


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


class ProjectByRegion(graphene.ObjectType):
id = graphene.ID(required=True, description='Region\'s ID')
# NOTE: Annotated by ProjectByRegionGqlFilterSet/PublicProjectByRegionGqlFileterSet
Expand Down Expand Up @@ -587,7 +588,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 @@ -626,5 +627,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)
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)
51 changes: 25 additions & 26 deletions schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -1561,12 +1561,6 @@ type BulkUpdateProjectMembership {
deletedResult: [ProjectMembershipType!]
}

input BulkUpdateProjectPinned {
order: Int!
clientId: String
id: Int!
}

type BulkUpdateProjectRegion {
errors: [[GenericScalar!]]
result: [RegionDetailType!]
Expand Down Expand Up @@ -1831,7 +1825,7 @@ type CreateUserGroup {
type CreateUserPinnedProject {
errors: [GenericScalar!]
ok: Boolean
result: ProjectPinnedDataType
result: UserPinnedProjectType
}

type CreateWrongPredictionReview {
Expand Down Expand Up @@ -1923,6 +1917,12 @@ type DeleteUserGroup {
result: UserGroupType
}

type DeleteUserPinnedProject {
errors: [GenericScalar!]
ok: Boolean
result: UserPinnedProjectType
}

type DeleteWrongPredictionReview {
errors: [GenericScalar!]
ok: Boolean
Expand Down Expand Up @@ -3085,9 +3085,9 @@ type Mutation {
joinProject(data: ProjectJoinRequestInputType!): CreateProjectJoin
projectJoinRequestDelete(projectId: ID!): ProjectJoinRequestDelete
project(id: ID!): ProjectMutationType
createUserPinnedProject(data: ProjectPinnedInputType): CreateUserPinnedProject
reorderPinnedProjects(items: [BulkUpdateProjectPinned!]): ReorderPinnedProjects
deleteUserPinnedProject(id: ID!): deleteUserPinnedProject
createUserPinnedProject(data: ProjectPinnedInputType!): CreateUserPinnedProject
reorderPinnedProjects(items: [UserPinnedProjectReOrderInputType!]): ReorderPinnedProjects
deleteUserPinnedProject(id: ID!): DeleteUserPinnedProject
userGroupCreate(data: UserGroupInputType!): CreateUserGroup
userGroup(id: ID!): UserGroupMutationType
login(data: LoginInputType!): Login
Expand Down Expand Up @@ -3529,13 +3529,6 @@ enum ProjectPermission {
CREATE_ANALYSIS_MODULE
}

type ProjectPinnedDataType {
project: ProjectType!
user: UserType!
order: Int!
clientId: String!
}

input ProjectPinnedInputType {
project: ID!
}
Expand Down Expand Up @@ -3852,7 +3845,7 @@ type Query {
projectsByRegion(projectFilter: RegionProjectFilterData, ordering: String): ProjectByRegionListType
publicProjects(createdAt: DateTime, createdAtGte: DateTime, createdAtLte: DateTime, modifiedAt: DateTime, modifiedAtGte: DateTime, modifiedAtLte: DateTime, createdBy: [ID!], modifiedBy: [ID!], ids: [ID!], excludeIds: [ID!], status: ProjectStatusEnum, organizations: [ID!], analysisFrameworks: [ID!], regions: [ID!], search: String, isCurrentUserMember: Boolean, hasPermissionAccess: ProjectPermission, ordering: [PublicProjectOrderingEnum!], isTest: Boolean, page: Int = 1, pageSize: Int): PublicProjectListType
publicProjectsByRegion(projectFilter: RegionProjectFilterData, page: Int = 1, ordering: String, pageSize: Int): PublicProjectByRegionListType
pinnedProject: [ProjectPinnedDataType!]!
userPinnedProjects: [UserPinnedProjectType!]!
assistedTagging: AssistedTaggingRootQueryType
enums: AppEnumCollection
_debug: DjangoDebug
Expand Down Expand Up @@ -3952,8 +3945,7 @@ input RegisterInputType {
type ReorderPinnedProjects {
errors: [GenericScalar!]
ok: Boolean
result: [ProjectPinnedDataType]
deletedResult: [ProjectPinnedDataType]
result: [UserPinnedProjectType]
}

type ResetPassword {
Expand Down Expand Up @@ -4381,6 +4373,19 @@ type UserMeType {
onlyMemberProjects: [UserMeProjectType]
}

input UserPinnedProjectReOrderInputType {
order: Int!
clientId: String
id: Int!
}

type UserPinnedProjectType {
project: ProjectDetailType
user: UserType!
order: Int!
clientId: String!
}

type UserProfileType {
id: ID!
displayPictureUrl: String
Expand Down Expand Up @@ -4493,9 +4498,3 @@ type WrongPredictionReviewType {
modifiedBy: UserType
prediction: ID!
}

type deleteUserPinnedProject {
errors: [GenericScalar!]
ok: Boolean
result: ProjectPinnedDataType
}

0 comments on commit 761bc69

Please sign in to comment.