Skip to content

Commit d33bce9

Browse files
authored
Merge pull request #34 from graphql-python/test-should-more-mutate
Test should more mutate
2 parents 2488f6f + 6f34417 commit d33bce9

File tree

6 files changed

+134
-78
lines changed

6 files changed

+134
-78
lines changed

graphene_mongo/tests/fixtures.py renamed to graphene_mongo/tests/setup.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1+
import pytest
12
from .models import (
23
Article, Editor, EmbeddedArticle, Player,
34
Reporter, Child, ProfessorMetadata, ProfessorVector,
45
)
56

67

7-
def setup_fixtures():
8+
@pytest.fixture(scope='module')
9+
def fixtures():
810
Editor.drop_collection()
911
editor1 = Editor(
1012
id='1',

graphene_mongo/tests/test_mutation.py

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
import graphene
2+
3+
from graphene.relay import Node
4+
5+
from .setup import fixtures
6+
from .models import (Article, Editor)
7+
from .types import (ArticleNode, EditorNode)
8+
9+
10+
def test_should_create(fixtures):
11+
12+
class CreateArticle(graphene.Mutation):
13+
14+
class Arguments:
15+
16+
headline = graphene.String()
17+
18+
article = graphene.Field(ArticleNode)
19+
20+
def mutate(self, info, headline):
21+
article = Article(
22+
headline=headline
23+
)
24+
article.save()
25+
26+
return CreateArticle(article=article)
27+
28+
class Query(graphene.ObjectType):
29+
30+
node = Node.Field()
31+
32+
class Mutation(graphene.ObjectType):
33+
34+
create_article = CreateArticle.Field()
35+
36+
query = '''
37+
mutation ArticleCreator {
38+
createArticle(
39+
headline: "My Article"
40+
) {
41+
article {
42+
headline
43+
}
44+
}
45+
}
46+
'''
47+
expected = {
48+
'createArticle': {
49+
'article': {
50+
'headline': 'My Article'
51+
}
52+
}
53+
}
54+
schema = graphene.Schema(query=Query, mutation=Mutation)
55+
result = schema.execute(query)
56+
assert not result.errors
57+
assert result.data == expected
58+
59+
60+
def test_should_update(fixtures):
61+
62+
class UpdateEditor(graphene.Mutation):
63+
64+
class Arguments:
65+
id = graphene.ID()
66+
first_name = graphene.String()
67+
68+
editor = graphene.Field(EditorNode)
69+
70+
def mutate(self, info, id, first_name):
71+
editor = Editor.objects.get(id=id)
72+
editor.first_name = first_name
73+
editor.save()
74+
return UpdateEditor(editor=editor)
75+
76+
class Query(graphene.ObjectType):
77+
78+
node = Node.Field()
79+
80+
class Mutation(graphene.ObjectType):
81+
82+
update_editor = UpdateEditor.Field()
83+
84+
query = '''
85+
mutation EditorUpdater {
86+
updateEditor(
87+
id: "1"
88+
firstName: "Tony"
89+
) {
90+
editor {
91+
firstName
92+
}
93+
}
94+
}
95+
'''
96+
expected = {
97+
'updateEditor': {
98+
'editor': {
99+
'firstName': 'Tony'
100+
}
101+
}
102+
}
103+
schema = graphene.Schema(query=Query, mutation=Mutation)
104+
result = schema.execute(query)
105+
# print(result.data)
106+
assert not result.errors
107+
assert result.data == expected

graphene_mongo/tests/test_query.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,16 @@
22

33
import graphene
44

5-
from .fixtures import setup_fixtures
5+
from .setup import fixtures
66
from .models import (
77
Editor, Player, Reporter, ProfessorVector
88
)
99
from .types import (
1010
EditorType, PlayerType, ReporterType, ProfessorVectorType
1111
)
1212

13-
setup_fixtures()
1413

15-
16-
def test_should_query_editor():
14+
def test_should_query_editor(fixtures):
1715

1816
class Query(graphene.ObjectType):
1917

@@ -65,7 +63,7 @@ def resolve_editors(self, *args, **kwargs):
6563
assert all(item in result.data['editors'] for item in expected['editors'])
6664

6765

68-
def test_should_query_reporter():
66+
def test_should_query_reporter(fixtures):
6967

7068
class Query(graphene.ObjectType):
7169
reporter = graphene.Field(ReporterType)
@@ -127,7 +125,7 @@ def resolve_reporter(self, *args, **kwargs):
127125
assert dict(result.data['reporter']) == expected['reporter']
128126

129127

130-
def test_should_custom_kwargs():
128+
def test_should_custom_kwargs(fixtures):
131129

132130
class Query(graphene.ObjectType):
133131

@@ -165,7 +163,7 @@ def resolve_editors(self, *args, **kwargs):
165163
assert all(item in result.data['editors'] for item in expected['editors'])
166164

167165

168-
def test_should_self_reference():
166+
def test_should_self_reference(fixtures):
169167

170168
class Query(graphene.ObjectType):
171169

@@ -229,7 +227,7 @@ def resolve_all_players(self, *args, **kwargs):
229227
assert json.dumps(result.data, sort_keys=True) == json.dumps(expected, sort_keys=True)
230228

231229

232-
def test_should_query_with_embedded_document():
230+
def test_should_query_with_embedded_document(fixtures):
233231

234232
class Query(graphene.ObjectType):
235233
professor_vector = graphene.Field(ProfessorVectorType, id=graphene.String())

graphene_mongo/tests/test_relay_query.py

Lines changed: 13 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
from graphene.relay import Node
66

7-
from .fixtures import setup_fixtures
7+
from .setup import fixtures
88
from .models import Article, Reporter
99
from .types import (ArticleNode,
1010
EditorNode,
@@ -13,14 +13,12 @@
1313
ChildNode,)
1414
from ..fields import MongoengineConnectionField
1515

16-
setup_fixtures()
17-
1816

1917
def get_nodes(data, key):
2018
return map(lambda edge: edge['node'], data[key]['edges'])
2119

2220

23-
def test_should_query_reporter():
21+
def test_should_query_reporter(fixtures):
2422

2523
class Query(graphene.ObjectType):
2624
node = Node.Field()
@@ -115,7 +113,7 @@ def resolve_reporter(self, *args, **kwargs):
115113
assert dict(result.data['reporter']) == expected['reporter']
116114

117115

118-
def test_should_query_all_editors():
116+
def test_should_query_all_editors(fixtures):
119117

120118
class Query(graphene.ObjectType):
121119
node = Node.Field()
@@ -168,7 +166,7 @@ class Query(graphene.ObjectType):
168166
assert dict(result.data['allEditors']) == expected['allEditors']
169167

170168

171-
def test_should_filter_editors_by_id():
169+
def test_should_filter_editors_by_id(fixtures):
172170

173171
class Query(graphene.ObjectType):
174172
node = Node.Field()
@@ -207,55 +205,7 @@ class Query(graphene.ObjectType):
207205
assert dict(result.data['allEditors']) == expected['allEditors']
208206

209207

210-
def test_should_mutate():
211-
212-
class CreateArticle(graphene.Mutation):
213-
214-
class Arguments:
215-
headline = graphene.String()
216-
217-
article = graphene.Field(ArticleNode)
218-
219-
def mutate(self, info, headline):
220-
article = Article(
221-
headline=headline
222-
)
223-
article.save()
224-
225-
return CreateArticle(article=article)
226-
227-
class Query(graphene.ObjectType):
228-
node = Node.Field()
229-
230-
class Mutation(graphene.ObjectType):
231-
232-
create_article = CreateArticle.Field()
233-
234-
query = '''
235-
mutation ArticleCreator {
236-
createArticle(
237-
headline: "My Article"
238-
) {
239-
article {
240-
headline
241-
}
242-
}
243-
}
244-
'''
245-
expected = {
246-
'createArticle': {
247-
'article': {
248-
'headline': 'My Article'
249-
}
250-
}
251-
}
252-
schema = graphene.Schema(query=Query, mutation=Mutation)
253-
result = schema.execute(query)
254-
assert not result.errors
255-
assert result.data == expected
256-
257-
258-
def test_should_filter():
208+
def test_should_filter(fixtures):
259209

260210
class Query(graphene.ObjectType):
261211
node = Node.Field()
@@ -295,7 +245,7 @@ class Query(graphene.ObjectType):
295245
assert result.data == expected
296246

297247

298-
def test_should_filter_by_reference_field():
248+
def test_should_filter_by_reference_field(fixtures):
299249

300250
class Query(graphene.ObjectType):
301251
node = Node.Field()
@@ -335,7 +285,7 @@ class Query(graphene.ObjectType):
335285
assert result.data == expected
336286

337287

338-
def test_should_filter_through_inheritance():
288+
def test_should_filter_through_inheritance(fixtures):
339289

340290
class Query(graphene.ObjectType):
341291
node = Node.Field()
@@ -372,7 +322,7 @@ class Query(graphene.ObjectType):
372322
expected, sort_keys=True)
373323

374324

375-
def test_should_get_node_by_id():
325+
def test_should_get_node_by_id(fixtures):
376326
# Notes: https://goo.gl/hMNRgs
377327
class Query(graphene.ObjectType):
378328
reporter = Node.Field(ReporterNode)
@@ -398,7 +348,7 @@ class Query(graphene.ObjectType):
398348
assert result.data == expected
399349

400350

401-
def test_should_first_n():
351+
def test_should_first_n(fixtures):
402352

403353
class Query(graphene.ObjectType):
404354

@@ -454,7 +404,7 @@ class Query(graphene.ObjectType):
454404
for item in get_nodes(expected, 'editors'))
455405

456406

457-
def test_should_after():
407+
def test_should_after(fixtures):
458408
class Query(graphene.ObjectType):
459409

460410
players = MongoengineConnectionField(PlayerNode)
@@ -497,7 +447,7 @@ class Query(graphene.ObjectType):
497447
expected, sort_keys=True)
498448

499449

500-
def test_should_before():
450+
def test_should_before(fixtures):
501451
class Query(graphene.ObjectType):
502452

503453
players = MongoengineConnectionField(PlayerNode)
@@ -540,7 +490,7 @@ class Query(graphene.ObjectType):
540490
expected, sort_keys=True)
541491

542492

543-
def test_should_last_n():
493+
def test_should_last_n(fixtures):
544494
class Query(graphene.ObjectType):
545495
players = MongoengineConnectionField(PlayerNode)
546496

@@ -582,7 +532,7 @@ class Query(graphene.ObjectType):
582532
expected, sort_keys=True)
583533

584534

585-
def test_should_self_reference():
535+
def test_should_self_reference(fixtures):
586536

587537
class Query(graphene.ObjectType):
588538

@@ -681,8 +631,3 @@ class Query(graphene.ObjectType):
681631
assert not result.errors
682632
assert json.dumps(result.data, sort_keys=True) == json.dumps(
683633
expected, sort_keys=True)
684-
685-
686-
# TODO:
687-
def test_should_paging():
688-
pass

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
coveralls==1.2.0
22
flake8==3.5.0
3+
flake8-per-file-ignores==0.6
34
graphene==2.0.1
45
iso8601==0.1.12
56
mongoengine==0.15.0

setup.cfg

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ description-file = README.md
44
[flake8]
55
exclude = setup.py,docs/*,examples/*
66
max-line-length = 130
7-
7+
per-file-ignores =
8+
graphene_mongo/tests/test_mutation.py: F401, F811
9+
graphene_mongo/tests/test_query.py: F401, F811
10+
graphene_mongo/tests/test_relay_query.py: F401, F811
811
[coverage:run]
912
omit = */tests/*
1013

0 commit comments

Comments
 (0)