Update unit test and add mutation

This commit is contained in:
Ramon Wenger 2022-06-14 11:27:32 +02:00
parent 14b2905470
commit e5641b7f93
5 changed files with 58 additions and 5 deletions

View File

@ -3,3 +3,7 @@ import graphene
class FailureNode(graphene.Interface): class FailureNode(graphene.Interface):
reason = graphene.String() reason = graphene.String()
class SuccessNode(graphene.Interface):
message = graphene.String()

View File

@ -1,7 +1,7 @@
from books.schema.mutations.chapter import UpdateChapterVisibility from books.schema.mutations.chapter import UpdateChapterVisibility
from books.schema.mutations.contentblock import MutateContentBlock, AddContentBlock, DeleteContentBlock from books.schema.mutations.contentblock import MutateContentBlock, AddContentBlock, DeleteContentBlock
from books.schema.mutations.module import UpdateSolutionVisibility, UpdateLastModule, SyncModuleVisibility from books.schema.mutations.module import UpdateSolutionVisibility, UpdateLastModule, SyncModuleVisibility
from books.schema.mutations.snapshot import CreateSnapshot, ApplySnapshot, ShareSnapshot, UpdateSnapshot from books.schema.mutations.snapshot import CreateSnapshot, ApplySnapshot, ShareSnapshot, UpdateSnapshot, DeleteSnapshot
from books.schema.mutations.topic import UpdateLastTopic from books.schema.mutations.topic import UpdateLastTopic
@ -18,3 +18,4 @@ class BookMutations(object):
apply_snapshot = ApplySnapshot.Field() apply_snapshot = ApplySnapshot.Field()
share_snapshot = ShareSnapshot.Field() share_snapshot = ShareSnapshot.Field()
update_snapshot = UpdateSnapshot.Field() update_snapshot = UpdateSnapshot.Field()
delete_snapshot = DeleteSnapshot.Field()

View File

@ -2,7 +2,7 @@ import graphene
from django.db.models import Q from django.db.models import Q
from graphene import relay from graphene import relay
from api.types import FailureNode from api.types import FailureNode, SuccessNode
from api.utils import get_object from api.utils import get_object
from books.models import Module, ContentBlock, Chapter from books.models import Module, ContentBlock, Chapter
from books.models.snapshot import Snapshot from books.models.snapshot import Snapshot
@ -15,7 +15,13 @@ class NotOwner(graphene.ObjectType):
interfaces = (FailureNode,) interfaces = (FailureNode,)
class Success(graphene.ObjectType):
class Meta:
interfaces = (SuccessNode,)
NotOwnerFailure = NotOwner(reason="Not the owner") NotOwnerFailure = NotOwner(reason="Not the owner")
DeleteSnapshotSuccess = Success(message='Snapshot deleted successfully')
class UpdateSnapshotResult(graphene.Union): class UpdateSnapshotResult(graphene.Union):
@ -23,6 +29,11 @@ class UpdateSnapshotResult(graphene.Union):
types = (SnapshotNode, NotOwner,) types = (SnapshotNode, NotOwner,)
class DeleteSnapshotResult(graphene.Union):
class Meta:
types = (Success, NotOwner,)
class CreateSnapshot(relay.ClientIDMutation): class CreateSnapshot(relay.ClientIDMutation):
class Input: class Input:
module = graphene.String(required=True) module = graphene.String(required=True)
@ -66,6 +77,25 @@ class UpdateSnapshot(relay.ClientIDMutation):
return cls(snapshot=snapshot) return cls(snapshot=snapshot)
class DeleteSnapshot(relay.ClientIDMutation):
class Input:
id = graphene.ID(required=True)
result = graphene.Field(DeleteSnapshotResult)
@classmethod
def mutate_and_get_payload(cls, root, info, **args):
id = args.get('id')
user = info.context.user
snapshot = get_object(Snapshot, id)
if snapshot.creator != user:
return cls(result=NotOwnerFailure)
snapshot.delete()
return cls(result=DeleteSnapshotResult)
class ApplySnapshot(relay.ClientIDMutation): class ApplySnapshot(relay.ClientIDMutation):
class Input: class Input:
snapshot = graphene.ID(required=True) snapshot = graphene.ID(required=True)

View File

@ -152,3 +152,16 @@ mutation UpdateSnapshot($input: UpdateSnapshotInput!) {
} }
} }
""" """
DELETE_SNAPSHOT_MUTATION = """
mutation DeleteSnapshot($id: ID!) {
deleteSnapshot(id: $id) {
result {
success
...on NotOwner {
reason
}
}
}
}
"""

View File

@ -7,7 +7,7 @@ from api.utils import get_object
from books.factories import ModuleFactory, ChapterFactory, ContentBlockFactory from books.factories import ModuleFactory, ChapterFactory, ContentBlockFactory
from books.models import Snapshot, ChapterSnapshot from books.models import Snapshot, ChapterSnapshot
from books.tests.queries import MODULE_QUERY, SNAPSHOT_MODULE_QUERY, CREATE_SNAPSHOT_MUTATION, APPLY_SNAPSHOT_MUTATION, \ from books.tests.queries import MODULE_QUERY, SNAPSHOT_MODULE_QUERY, CREATE_SNAPSHOT_MUTATION, APPLY_SNAPSHOT_MUTATION, \
MODULE_SNAPSHOTS_QUERY, SHARE_SNAPSHOT_MUTATION, UPDATE_SNAPSHOT_MUTATION MODULE_SNAPSHOTS_QUERY, SHARE_SNAPSHOT_MUTATION, UPDATE_SNAPSHOT_MUTATION, DELETE_SNAPSHOT_MUTATION
from core.tests.base_test import SkillboxTestCase from core.tests.base_test import SkillboxTestCase
from objectives.factories import ObjectiveGroupFactory, ObjectiveFactory from objectives.factories import ObjectiveGroupFactory, ObjectiveFactory
from users.factories import SchoolClassFactory from users.factories import SchoolClassFactory
@ -425,7 +425,6 @@ class SnapshotTestCase(SkillboxTestCase):
self.assertIsNone(result.errors) self.assertIsNone(result.errors)
self.assertEqual(len(result.data.get('module').get('snapshots')), 1) self.assertEqual(len(result.data.get('module').get('snapshots')), 1)
def _setup_title_change(self): def _setup_title_change(self):
self.assertIsNone(self.snapshot.title) self.assertIsNone(self.snapshot.title)
new_title = 'New Snapshot Title' new_title = 'New Snapshot Title'
@ -450,7 +449,13 @@ class SnapshotTestCase(SkillboxTestCase):
self.assertEqual(result.data.get('updateSnapshot').get('snapshot').get('reason'), 'Not the owner') self.assertEqual(result.data.get('updateSnapshot').get('snapshot').get('reason'), 'Not the owner')
def test_delete_snapshot(self): def test_delete_snapshot(self):
self.assertTrue(False) result = self.get_client().execute(DELETE_SNAPSHOT_MUTATION, variables={
'input': {
'id': self.snapshot
}
})
self.assertIsNone(result.errors)
self.assertEqual(result.data.get('deleteSnapshot').get('result').get('__typename'), 'SuccessNode')
def test_delete_snapshot_not_owner_fails(self): def test_delete_snapshot_not_owner_fails(self):
self.assertTrue(False) self.assertTrue(False)