|
| 1 | +import graphene |
| 2 | + |
| 3 | +from graphene.relay import Node |
| 4 | + |
| 5 | +from .models import Article, Editor |
| 6 | +from .nodes import ArticleNode, EditorNode |
| 7 | +from .types import ArticleInput, EditorInput |
| 8 | + |
| 9 | + |
| 10 | +def test_should_create(fixtures): |
| 11 | + class CreateArticle(graphene.Mutation): |
| 12 | + class Arguments: |
| 13 | + article = ArticleInput(required=True) |
| 14 | + |
| 15 | + article = graphene.Field(ArticleNode) |
| 16 | + |
| 17 | + def mutate(self, info, article): |
| 18 | + article = Article(**article) |
| 19 | + article.save() |
| 20 | + |
| 21 | + return CreateArticle(article=article) |
| 22 | + |
| 23 | + class Query(graphene.ObjectType): |
| 24 | + |
| 25 | + node = Node.Field() |
| 26 | + |
| 27 | + class Mutation(graphene.ObjectType): |
| 28 | + |
| 29 | + create_article = CreateArticle.Field() |
| 30 | + |
| 31 | + query = """ |
| 32 | + mutation ArticleCreator { |
| 33 | + createArticle( |
| 34 | + article: {headline: "My Article"} |
| 35 | + ) { |
| 36 | + article { |
| 37 | + headline |
| 38 | + } |
| 39 | + } |
| 40 | + } |
| 41 | + """ |
| 42 | + expected = {"createArticle": {"article": {"headline": "My Article"}}} |
| 43 | + schema = graphene.Schema(query=Query, mutation=Mutation) |
| 44 | + result = schema.execute(query) |
| 45 | + assert not result.errors |
| 46 | + assert result.data == expected |
| 47 | + |
| 48 | + |
| 49 | +def test_should_update(fixtures): |
| 50 | + class UpdateEditor(graphene.Mutation): |
| 51 | + class Arguments: |
| 52 | + id = graphene.ID(required=True) |
| 53 | + editor = EditorInput(required=True) |
| 54 | + |
| 55 | + editor = graphene.Field(EditorNode) |
| 56 | + |
| 57 | + def mutate(self, info, id, editor): |
| 58 | + editor_to_update = Editor.objects.get(id=id) |
| 59 | + for key, value in editor.items(): |
| 60 | + setattr(editor_to_update, key, value) |
| 61 | + editor_to_update.save() |
| 62 | + return UpdateEditor(editor=editor_to_update) |
| 63 | + |
| 64 | + class Query(graphene.ObjectType): |
| 65 | + |
| 66 | + node = Node.Field() |
| 67 | + |
| 68 | + class Mutation(graphene.ObjectType): |
| 69 | + |
| 70 | + update_editor = UpdateEditor.Field() |
| 71 | + |
| 72 | + query = """ |
| 73 | + mutation EditorUpdater { |
| 74 | + updateEditor( |
| 75 | + id: "1" |
| 76 | + editor: { |
| 77 | + lastName: "Lane" |
| 78 | + } |
| 79 | + ) { |
| 80 | + editor { |
| 81 | + firstName |
| 82 | + lastName |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | + """ |
| 87 | + expected = {"updateEditor": {"editor": {"firstName": "Penny", "lastName": "Lane"}}} |
| 88 | + schema = graphene.Schema(query=Query, mutation=Mutation) |
| 89 | + result = schema.execute(query) |
| 90 | + # print(result.data) |
| 91 | + assert not result.errors |
| 92 | + assert result.data == expected |
0 commit comments