Skip to content

Commit 614a3c4

Browse files
authored
Merge pull request #3 from abawchen/test-mutation
test: Add test_query.test_should_mutate_well
2 parents 0840c70 + a2af979 commit 614a3c4

File tree

3 files changed

+82
-17
lines changed

3 files changed

+82
-17
lines changed

graphene_mongo/tests/test_query.py

Lines changed: 64 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from graphene.relay import Node
66

77
from .models import Article, Editor, EmbeddedArticle, Reporter
8+
from .utils import with_local_registry
89
from ..fields import MongoengineConnectionField
910
from ..types import MongoengineObjectType
1011

@@ -27,6 +28,7 @@ def setup_fixtures():
2728
setup_fixtures()
2829

2930

31+
@with_local_registry
3032
def test_should_query_editor_well():
3133
class EditorType(MongoengineObjectType):
3234
class Meta:
@@ -73,6 +75,7 @@ def resolve_editors(self, *args, **kwargs):
7375
assert all(item in result.data['editors'] for item in expected['editors'])
7476

7577

78+
@with_local_registry
7679
def test_should_query_reporter_well():
7780
class ArticleType(MongoengineObjectType):
7881
class Meta:
@@ -119,13 +122,17 @@ def resolve_reporter(self, *args, **kwargs):
119122
assert not result.errors
120123
assert dict(result.data['reporter']) == expected['reporter']
121124

125+
126+
@with_local_registry
122127
def test_should_node():
123128
class ArticleNode(MongoengineObjectType):
129+
124130
class Meta:
125131
model = Article
126132
interfaces = (Node,)
127133

128134
class ReporterNode(MongoengineObjectType):
135+
129136
class Meta:
130137
model = Reporter
131138
interfaces = (Node,)
@@ -180,6 +187,8 @@ def resolve_reporter(self, *args, **kwargs):
180187
assert not result.errors
181188
assert dict(result.data['reporter']) == expected['reporter']
182189

190+
191+
@with_local_registry
183192
def test_should_connection_field():
184193
class EditorNode(MongoengineObjectType):
185194

@@ -226,9 +235,62 @@ class Query(graphene.ObjectType):
226235
assert not result.errors
227236
assert dict(result.data['allEditors']) == expected['allEditors']
228237

229-
# TODO:
238+
239+
@with_local_registry
230240
def test_should_mutate_well():
231-
pass
241+
class ArticleNode(MongoengineObjectType):
242+
243+
class Meta:
244+
model = Article
245+
interfaces = (Node,)
246+
247+
248+
class CreateArticle(graphene.Mutation):
249+
250+
class Arguments:
251+
headline = graphene.String()
252+
253+
article = graphene.Field(ArticleNode)
254+
255+
def mutate(self, info, headline):
256+
article = Article(
257+
headline=headline
258+
)
259+
article.save()
260+
261+
return CreateArticle(article=article)
262+
263+
264+
class Query(graphene.ObjectType):
265+
node = Node.Field()
266+
267+
268+
class Mutation(graphene.ObjectType):
269+
270+
create_article = CreateArticle.Field()
271+
272+
query = '''
273+
mutation ArticleCreator {
274+
createArticle(
275+
headline: "My Article"
276+
) {
277+
article {
278+
headline
279+
}
280+
}
281+
}
282+
'''
283+
expected = {
284+
'createArticle': {
285+
'article': {
286+
'headline': 'My Article'
287+
}
288+
}
289+
}
290+
schema = graphene.Schema(query=Query, mutation=Mutation)
291+
result = schema.execute(query)
292+
assert not result.errors
293+
assert result.data == expected
232294

233295
# TODO:
234296
def test_should_filter():

graphene_mongo/tests/test_types.py

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from ..types import MongoengineObjectType
88
from .models import Article
99
from .models import Reporter
10+
from .utils import with_local_registry
1011

1112
registry.reset_global_registry()
1213

@@ -80,21 +81,6 @@ class Meta:
8081
assert 'valid Mongoengine Model' in str(excinfo.value)
8182

8283

83-
def with_local_registry(func):
84-
def inner(*args, **kwargs):
85-
old = registry.get_global_registry()
86-
registry.reset_global_registry()
87-
try:
88-
retval = func(*args, **kwargs)
89-
except Exception as e:
90-
registry.registry = old
91-
raise e
92-
else:
93-
registry.registry = old
94-
return retval
95-
return inner
96-
97-
9884
@with_local_registry
9985
def test_mongoengine_objecttype_only_fields():
10086
class A(MongoengineObjectType):

graphene_mongo/tests/utils.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from .. import registry
2+
3+
4+
def with_local_registry(func):
5+
def inner(*args, **kwargs):
6+
old = registry.get_global_registry()
7+
registry.reset_global_registry()
8+
try:
9+
retval = func(*args, **kwargs)
10+
except Exception as e:
11+
registry.registry = old
12+
raise e
13+
else:
14+
registry.registry = old
15+
return retval
16+
return inner
17+

0 commit comments

Comments
 (0)