Skip to content

Commit

Permalink
Add test cases for Analysis Mutation
Browse files Browse the repository at this point in the history
  • Loading branch information
sauravsapkota committed Aug 16, 2024
1 parent 15a40ca commit 05d71a4
Show file tree
Hide file tree
Showing 4 changed files with 403 additions and 42 deletions.
29 changes: 9 additions & 20 deletions apps/analysis/mutation.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
generate_input_type_for_serializer,
PsGrapheneMutation,
PsDeleteMutation,
PsBulkGrapheneMutation,
)
from deep.permissions import ProjectPermissions as PP

Expand Down Expand Up @@ -278,15 +277,21 @@ class Arguments:
result = graphene.Field(AnalysisReportUploadType)


class CreateAnalysis(RequiredPermissionMixin, PsGrapheneMutation):
class AnalysisMutationMixin(RequiredPermissionMixin):
@classmethod
def filter_queryset(cls, qs, info):
return qs.filter(project=info.context.active_project)


class CreateAnalysis(AnalysisMutationMixin, PsGrapheneMutation):
class Arguments:
data = AnalysisInputType(required=True)
model = Analysis
serializer_class = AnalysisGqlSerializer
result = graphene.Field(AnalysisType)


class UpdateAnalysis(RequiredPermissionMixin, PsGrapheneMutation):
class UpdateAnalysis(AnalysisMutationMixin, PsGrapheneMutation):
class Arguments:
data = AnalysisInputType(required=True)
id = graphene.ID(required=True)
Expand All @@ -295,28 +300,13 @@ class Arguments:
result = graphene.Field(AnalysisType)


class DeleteAnalysis(RequiredPermissionMixin, PsDeleteMutation):
class DeleteAnalysis(AnalysisMutationMixin, PsDeleteMutation):
class Arguments:
id = graphene.ID(required=True)
model = Analysis
result = graphene.Field(AnalysisType)


class BulkAnalysisInputType(AnalysisInputType):
id = graphene.ID()


class BulkAnalysis(RequiredPermissionMixin, PsBulkGrapheneMutation):
class Arguments:
items = graphene.List(graphene.NonNull(BulkAnalysisInputType))
delete_ids = graphene.List(graphene.NonNull(graphene.ID))

result = graphene.List(AnalysisType)
deleted_result = graphene.List(graphene.NonNull(AnalysisType))
model = Analysis
serializer_class = AnalysisGqlSerializer


class Mutation():
# Analysis Pillar
analysis_pillar_update = UpdateAnalysisPillar.Field()
Expand All @@ -341,4 +331,3 @@ class Mutation():
analysis_create = CreateAnalysis.Field()
analysis_update = UpdateAnalysis.Field()
analysis_delete = DeleteAnalysis.Field()
analysis_bulk = BulkAnalysis.Field()
37 changes: 34 additions & 3 deletions apps/analysis/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -331,11 +331,13 @@ def validate(self, data):


class AnalysisPillarGqlSerializer(TempClientIdMixin, UserResourceSerializer):
id = IntegerIDField(required=False)
statements = AnalyticalStatementGqlSerializer(many=True, source='analyticalstatement_set', required=False)

class Meta:
model = AnalysisPillar
fields = (
'id',
'title',
'main_statement',
'information_gap',
Expand Down Expand Up @@ -410,19 +412,16 @@ def validate(self, data):


class AnalysisGqlSerializer(UserResourceSerializer, ProjectPropertySerializerMixin):
id = IntegerIDField(required=False)
analysis_pillar = AnalysisPillarGqlSerializer(many=True, source='analysispillar_set', required=False)
start_date = serializers.DateField(required=False, allow_null=True)

class Meta:
model = Analysis
fields = (
'id',
'title',
'team_lead',
'start_date',
'end_date',
'cloned_from',
'analysis_pillar',
)

Expand All @@ -441,6 +440,38 @@ def validate(self, data):
)
return data

def update(self, instance, validated_data):
with transaction.atomic():
if 'analysispillar_set' in validated_data:
pillars = validated_data.pop('analysispillar_set')
errors = {}
for pillar in pillars:
data = {
"title": pillar.get('title'),
"assignee": pillar.get('assignee').id,
"analysis": instance.id,
}
pillar_id = pillar.get('id', None)
if pillar_id:
data["id"] = pillar_id
analysis_pillar = get_object_or_404(AnalysisPillar, pk=pillar_id)
analysis_pillar_serializer = AnalysisPillarGqlSerializer(
analysis_pillar,
data=data,
context=self.context
)
else:
analysis_pillar_serializer = AnalysisPillarGqlSerializer(data=data, context=self.context)
if analysis_pillar_serializer.is_valid():
analysis_pillar_serializer.save()
else:
errors[pillar.get('id', 'new')] = analysis_pillar_serializer.errors

if errors:
raise serializers.ValidationError(errors)

return super().update(instance, validated_data)


AnalysisCloneGqlSerializer = AnalysisCloneInputSerializer

Expand Down
Loading

0 comments on commit 05d71a4

Please sign in to comment.