|
2 | 2 |
|
3 | 3 | from graphene.relay import Node
|
4 | 4 |
|
5 |
| -from .models import (Article,) |
6 |
| -from .types import (ArticleNode,) |
| 5 | +from .fixtures import setup_fixtures |
| 6 | +from .models import (Article, Editor) |
| 7 | +from .types import (ArticleNode, EditorNode) |
7 | 8 |
|
8 |
| -def test_should_mutate_create(): |
| 9 | + |
| 10 | +setup_fixtures() |
| 11 | + |
| 12 | + |
| 13 | +def test_should_create(): |
9 | 14 |
|
10 | 15 | class CreateArticle(graphene.Mutation):
|
11 | 16 |
|
@@ -54,3 +59,53 @@ class Mutation(graphene.ObjectType):
|
54 | 59 | assert not result.errors
|
55 | 60 | assert result.data == expected
|
56 | 61 |
|
| 62 | + |
| 63 | +def test_should_update(): |
| 64 | + |
| 65 | + class UpdateEditor(graphene.Mutation): |
| 66 | + |
| 67 | + class Arguments: |
| 68 | + id = graphene.ID() |
| 69 | + first_name = graphene.String() |
| 70 | + |
| 71 | + editor = graphene.Field(EditorNode) |
| 72 | + |
| 73 | + def mutate(self, info, id, first_name): |
| 74 | + print(id, first_name) |
| 75 | + editor = Editor.objects.get(id=id) |
| 76 | + editor.first_name = first_name |
| 77 | + editor.save() |
| 78 | + return UpdateEditor(editor=editor) |
| 79 | + |
| 80 | + class Query(graphene.ObjectType): |
| 81 | + |
| 82 | + node = Node.Field() |
| 83 | + |
| 84 | + class Mutation(graphene.ObjectType): |
| 85 | + |
| 86 | + update_editor = UpdateEditor.Field() |
| 87 | + |
| 88 | + query = ''' |
| 89 | + mutation EditorUpdater { |
| 90 | + updateEditor( |
| 91 | + id: "1" |
| 92 | + firstName: "Tony" |
| 93 | + ) { |
| 94 | + editor { |
| 95 | + firstName |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | + ''' |
| 100 | + expected = { |
| 101 | + 'updateEditor': { |
| 102 | + 'editor': { |
| 103 | + 'firstName': 'Tony' |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | + schema = graphene.Schema(query=Query, mutation=Mutation) |
| 108 | + result = schema.execute(query) |
| 109 | + # print(result.data) |
| 110 | + assert not result.errors |
| 111 | + assert result.data == expected |
0 commit comments