|
| 1 | +import json |
| 2 | + |
| 3 | +import graphene |
| 4 | + |
| 5 | +from graphene.relay import Node |
| 6 | + |
| 7 | +from .models import Article, Editor, EmbeddedArticle, Reporter |
| 8 | +from ..fields import MongoengineConnectionField |
| 9 | +from ..types import MongoengineObjectType |
| 10 | + |
| 11 | + |
| 12 | +def setup_fixtures(): |
| 13 | + editor1 = Editor(first_name='Penny', last_name='Hardaway') |
| 14 | + editor1.save() |
| 15 | + editor2 = Editor(first_name='Grant', last_name='Hill') |
| 16 | + editor2.save() |
| 17 | + |
| 18 | + reporter = Reporter(first_name='Allen', last_name='Iverson', |
| 19 | + email='[email protected]', awards=[ '2010-mvp']) |
| 20 | + article1 = Article(headline='Hello', editor=editor1) |
| 21 | + article1.save() |
| 22 | + article2 = Article(headline='World', editor=editor2) |
| 23 | + article2.save() |
| 24 | + reporter.articles = [article1, article2] |
| 25 | + reporter.save() |
| 26 | + |
| 27 | +setup_fixtures() |
| 28 | + |
| 29 | + |
| 30 | +def test_should_query_editor_well(): |
| 31 | + class EditorType(MongoengineObjectType): |
| 32 | + class Meta: |
| 33 | + model = Editor |
| 34 | + |
| 35 | + class Query(graphene.ObjectType): |
| 36 | + editor = graphene.Field(EditorType) |
| 37 | + editors = graphene.List(EditorType) |
| 38 | + |
| 39 | + def resolve_editor(self, *args, **kwargs): |
| 40 | + return Editor.objects.first() |
| 41 | + |
| 42 | + def resolve_editors(self, *args, **kwargs): |
| 43 | + return list(Editor.objects.all()) |
| 44 | + |
| 45 | + query = ''' |
| 46 | + query EditorQuery { |
| 47 | + editor { |
| 48 | + firstName |
| 49 | + } |
| 50 | + editors { |
| 51 | + firstName, |
| 52 | + lastName |
| 53 | + } |
| 54 | + } |
| 55 | + ''' |
| 56 | + expected = { |
| 57 | + 'editor': { |
| 58 | + 'firstName': 'Penny' |
| 59 | + }, |
| 60 | + 'editors': [{ |
| 61 | + 'firstName': 'Penny', |
| 62 | + 'lastName': 'Hardaway' |
| 63 | + }, { |
| 64 | + 'firstName': 'Grant', |
| 65 | + 'lastName': 'Hill' |
| 66 | + }] |
| 67 | + } |
| 68 | + |
| 69 | + schema = graphene.Schema(query=Query) |
| 70 | + result = schema.execute(query) |
| 71 | + assert not result.errors |
| 72 | + assert dict(result.data['editor']) == expected['editor'] |
| 73 | + assert all(item in result.data['editors'] for item in expected['editors']) |
| 74 | + |
| 75 | + |
| 76 | +def test_should_query_reporter_well(): |
| 77 | + class ArticleType(MongoengineObjectType): |
| 78 | + class Meta: |
| 79 | + model = Article |
| 80 | + |
| 81 | + class ReporterType(MongoengineObjectType): |
| 82 | + class Meta: |
| 83 | + model = Reporter |
| 84 | + |
| 85 | + class Query(graphene.ObjectType): |
| 86 | + reporter = graphene.Field(ReporterType) |
| 87 | + |
| 88 | + def resolve_reporter(self, *args, **kwargs): |
| 89 | + return Reporter.objects.first() |
| 90 | + |
| 91 | + query = ''' |
| 92 | + query ReporterQuery { |
| 93 | + reporter { |
| 94 | + firstName, |
| 95 | + lastName, |
| 96 | + email, |
| 97 | + articles { |
| 98 | + headline |
| 99 | + }, |
| 100 | + awards |
| 101 | + } |
| 102 | + } |
| 103 | + ''' |
| 104 | + expected = { |
| 105 | + 'reporter': { |
| 106 | + 'firstName': 'Allen', |
| 107 | + 'lastName': 'Iverson', |
| 108 | + |
| 109 | + 'articles': [ |
| 110 | + {'headline': 'Hello'}, |
| 111 | + {'headline': 'World'} |
| 112 | + ], |
| 113 | + 'awards': ['2010-mvp'] |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + schema = graphene.Schema(query=Query) |
| 118 | + result = schema.execute(query) |
| 119 | + assert not result.errors |
| 120 | + assert dict(result.data['reporter']) == expected['reporter'] |
| 121 | + |
| 122 | +def test_should_node(): |
| 123 | + class ArticleNode(MongoengineObjectType): |
| 124 | + class Meta: |
| 125 | + model = Article |
| 126 | + interfaces = (Node,) |
| 127 | + |
| 128 | + class ReporterNode(MongoengineObjectType): |
| 129 | + class Meta: |
| 130 | + model = Reporter |
| 131 | + interfaces = (Node,) |
| 132 | + |
| 133 | + class Query(graphene.ObjectType): |
| 134 | + node = Node.Field() |
| 135 | + reporter = graphene.Field(ReporterNode) |
| 136 | + |
| 137 | + def resolve_reporter(self, *args, **kwargs): |
| 138 | + return Reporter.objects.first() |
| 139 | + |
| 140 | + query = ''' |
| 141 | + query ReporterQuery { |
| 142 | + reporter { |
| 143 | + firstName, |
| 144 | + articles { |
| 145 | + edges { |
| 146 | + node { |
| 147 | + headline |
| 148 | + } |
| 149 | + } |
| 150 | + } |
| 151 | + lastName, |
| 152 | + email |
| 153 | + } |
| 154 | + } |
| 155 | + ''' |
| 156 | + expected = { |
| 157 | + 'reporter': { |
| 158 | + 'firstName': 'Allen', |
| 159 | + 'lastName': 'Iverson', |
| 160 | + 'articles': { |
| 161 | + 'edges': [ |
| 162 | + { |
| 163 | + 'node': { |
| 164 | + 'headline': 'Hello' |
| 165 | + } |
| 166 | + }, |
| 167 | + { |
| 168 | + 'node': { |
| 169 | + 'headline': 'World' |
| 170 | + } |
| 171 | + } |
| 172 | + ], |
| 173 | + }, |
| 174 | + |
| 175 | + } |
| 176 | + } |
| 177 | + |
| 178 | + schema = graphene.Schema(query=Query) |
| 179 | + result = schema.execute(query) |
| 180 | + assert not result.errors |
| 181 | + assert dict(result.data['reporter']) == expected['reporter'] |
| 182 | + |
| 183 | +# TODO: |
| 184 | +def test_should_custom_identifier(): |
| 185 | + pass |
| 186 | + |
| 187 | +# TODO: |
| 188 | +def test_should_mutate_well(): |
| 189 | + pass |
| 190 | + |
| 191 | +# TODO: |
| 192 | +def test_should_filter(): |
| 193 | + pass |
| 194 | + |
| 195 | +# TODO: |
| 196 | +def test_should_paging(): |
| 197 | + pass |
| 198 | + |
0 commit comments