Skip to content

Commit a2b27af

Browse files
committed
Added crud mutations and unittesting
1 parent adf03f4 commit a2b27af

File tree

6 files changed

+244
-19
lines changed

6 files changed

+244
-19
lines changed

examples/django_mongoengine/bike/fixtures.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,17 @@ def fixture_bike_data():
2424
)
2525
bike_two.save()
2626

27+
bike_three = Bike(
28+
id="507f1f77bcf86cd799439011",
29+
name='Moterra Neo',
30+
brand='Cannondale',
31+
year='2019',
32+
size=["M", "L", "XL"],
33+
wheel_size=29,
34+
type='EBike'
35+
)
36+
bike_three.save()
37+
2738

2839
def fixture_shop_data():
2940
Shop.drop_collection()

examples/django_mongoengine/bike/models.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
11
from mongoengine import Document
2-
from mongoengine.fields import FloatField, StringField, ListField, URLField
2+
from mongoengine.fields import (
3+
FloatField, StringField,
4+
ListField, URLField, ObjectIdField
5+
)
36

47

58
class Shop(Document):
69
meta = {'collection': 'shop'}
10+
ID = ObjectIdField()
711
name = StringField()
812
address = StringField()
913
website = URLField()
1014

1115

1216
class Bike(Document):
1317
meta = {'collection': 'bike'}
18+
ID = ObjectIdField()
1419
name = StringField()
1520
brand = StringField()
1621
year = StringField()
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import graphene
2+
from django.core.exceptions import ObjectDoesNotExist
3+
from .models import Bike
4+
from .types import BikeType
5+
6+
7+
class BikeInput(graphene.InputObjectType):
8+
id = graphene.ID()
9+
name = graphene.String()
10+
brand = graphene.String()
11+
year = graphene.String()
12+
size = graphene.List(graphene.String)
13+
wheel_size = graphene.Float()
14+
type = graphene.String()
15+
16+
17+
class CreateBikeMutation(graphene.Mutation):
18+
bike = graphene.Field(BikeType)
19+
20+
class Arguments:
21+
bike_data = BikeInput(required=True)
22+
23+
def mutate(self, info, bike_data=None):
24+
bike = Bike(
25+
name=bike_data.name,
26+
brand=bike_data.brand,
27+
year=bike_data.year,
28+
size=bike_data.size,
29+
wheel_size=bike_data.wheel_size,
30+
type=bike_data.type
31+
)
32+
bike.save()
33+
34+
return CreateBikeMutation(bike=bike)
35+
36+
37+
class UpdateBikeMutation(graphene.Mutation):
38+
bike = graphene.Field(BikeType)
39+
40+
class Arguments:
41+
bike_data = BikeInput(required=True)
42+
43+
@staticmethod
44+
def get_object(id):
45+
return Bike.objects.get(pk=id)
46+
47+
def mutate(self, info, bike_data=None):
48+
bike = UpdateBikeMutation.get_object(bike_data.id)
49+
if bike_data.name:
50+
bike.name = bike_data.name
51+
if bike_data.brand:
52+
bike.brand = bike_data.brand
53+
if bike_data.year:
54+
bike.year = bike_data.year
55+
if bike_data.size:
56+
bike.size = bike_data.size
57+
if bike_data.wheel_size:
58+
bike.wheel_size = bike_data.wheel_size
59+
if bike_data.type:
60+
bike.type = bike_data.type
61+
62+
bike.save()
63+
64+
return UpdateBikeMutation(bike=bike)
65+
66+
67+
class DeleteBikeMutation(graphene.Mutation):
68+
class Arguments:
69+
id = graphene.ID(required=True)
70+
71+
success = graphene.Boolean()
72+
73+
def mutate(self, info, id):
74+
try:
75+
Bike.objects.get(pk=id).delete()
76+
success = True
77+
except ObjectDoesNotExist:
78+
success = False
79+
80+
return DeleteBikeMutation(success=success)

examples/django_mongoengine/bike/schema.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,13 @@
33
from graphene_mongo.fields import MongoengineConnectionField
44
from .models import Shop
55
from .types import BikeType, ShopType
6+
from .mutations import CreateBikeMutation, UpdateBikeMutation, DeleteBikeMutation
7+
8+
9+
class Mutations(graphene.ObjectType):
10+
create_bike = CreateBikeMutation.Field()
11+
update_bike = UpdateBikeMutation.Field()
12+
delete_bike = DeleteBikeMutation.Field()
613

714

815
class Query(graphene.ObjectType):
@@ -14,4 +21,4 @@ def resolve_shop_list(self, info):
1421
return Shop.objects.all()
1522

1623

17-
schema = graphene.Schema(query=Query, types=[BikeType, ShopType])
24+
schema = graphene.Schema(query=Query, mutation=Mutations, types=[BikeType, ShopType])

examples/django_mongoengine/bike/tests.py

Lines changed: 138 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
1+
import pytest
2+
from django.urls import reverse
3+
from django.test import RequestFactory
14
from graphene.test import Client
25
from .schema import schema
36
from .fixtures import fixtures_data
47

58

6-
def test_bikes_last_item_query(fixtures_data):
9+
def test_bikes_first_item_query(fixtures_data):
710
query = '''
811
{
9-
bikes(last: 1){
12+
bikes(first: 1){
1013
edges {
1114
node {
1215
name
@@ -27,14 +30,14 @@ def test_bikes_last_item_query(fixtures_data):
2730
"edges": [
2831
{
2932
"node": {
30-
"name": "CAADX ULTEGRA",
31-
"brand": "Cannondale",
32-
"year": '2019',
33-
"size": ['46', '51', '54', '58'],
34-
"wheelSize": 28,
35-
"type": "Gravel"
33+
"name": "Level R",
34+
"brand": "Mondraker",
35+
"year": '2020',
36+
"size": ['S', 'M', 'L', 'XL'],
37+
"wheelSize": 27.5,
38+
"type": "MTB"
3639
}
37-
},
40+
}
3841
]
3942
}
4043
}
@@ -48,7 +51,7 @@ def test_bikes_last_item_query(fixtures_data):
4851
def test_bikes_filter_by_type_item_query(fixtures_data):
4952
query = '''
5053
{
51-
bikes(first: 2, type: "MTB"){
54+
bikes(first: 2, type: "Gravel"){
5255
edges {
5356
node {
5457
name
@@ -69,12 +72,12 @@ def test_bikes_filter_by_type_item_query(fixtures_data):
6972
"edges": [
7073
{
7174
"node": {
72-
"name": "Level R",
73-
"brand": "Mondraker",
74-
"year": '2020',
75-
"size": ['S', 'M', 'L', 'XL'],
76-
"wheelSize": 27.5,
77-
"type": "MTB"
75+
"name": "CAADX ULTEGRA",
76+
"brand": "Cannondale",
77+
"year": '2019',
78+
"size": ['46', '51', '54', '58'],
79+
"wheelSize": 28,
80+
"type": "Gravel"
7881
}
7982
}
8083
]
@@ -118,3 +121,122 @@ def test_shop_data_query(fixtures_data):
118121
client = Client(schema)
119122
result = client.execute(query)
120123
assert result == expected
124+
125+
126+
@pytest.mark.django_db
127+
def test_create_bike_mutation():
128+
query = '''
129+
mutation {
130+
createBike(bikeData:{
131+
name:"Bullhorn",
132+
brand:"Pegas",
133+
year: "2019",
134+
size: ["56", "58" ],
135+
wheelSize: 28,
136+
type: "Fixie"
137+
}) {
138+
bike {
139+
name
140+
brand
141+
year
142+
size
143+
wheelSize
144+
type
145+
}
146+
}
147+
}
148+
'''
149+
150+
expected = {
151+
"data": {
152+
"createBike": {
153+
"bike":
154+
{
155+
"name": "Bullhorn",
156+
"brand": "Pegas",
157+
"year": "2019",
158+
"size": ["56", "58"],
159+
"wheelSize": 28,
160+
"type": "Fixie",
161+
}
162+
}
163+
}
164+
}
165+
166+
factory = RequestFactory()
167+
request = factory.post(reverse('graphql-query'))
168+
client = Client(schema)
169+
result = client.execute(query, context=request)
170+
assert result == expected
171+
172+
173+
@pytest.mark.django_db
174+
def test_update_bike_mutation():
175+
query = '''
176+
mutation {
177+
updateBike(bikeData:{
178+
id: "507f1f77bcf86cd799439011",
179+
name:"Moterra Neo Updated",
180+
year: "2020",
181+
wheelSize: 27.5,
182+
type: "EBike Updated"
183+
}) {
184+
bike {
185+
name
186+
brand
187+
year
188+
size
189+
wheelSize
190+
type
191+
}
192+
}
193+
}
194+
'''
195+
196+
expected = {
197+
"data": {
198+
"updateBike": {
199+
"bike":
200+
{
201+
"name": "Moterra Neo Updated",
202+
"brand": "Cannondale",
203+
"year": "2020",
204+
"size": ["M", "L", "XL"],
205+
"wheelSize": 27.5,
206+
"type": "EBike Updated"
207+
}
208+
}
209+
}
210+
}
211+
212+
factory = RequestFactory()
213+
request = factory.post(reverse('graphql-query'))
214+
client = Client(schema)
215+
result = client.execute(query, context=request)
216+
print(result)
217+
assert result == expected
218+
219+
220+
@pytest.mark.django_db
221+
def test_delete_bike_mutation():
222+
query = '''
223+
mutation {
224+
deleteBike(id: "507f1f77bcf86cd799439011") {
225+
success
226+
}
227+
}
228+
'''
229+
230+
expected = {
231+
"data": {
232+
"deleteBike": {
233+
"success": True
234+
}
235+
}
236+
}
237+
238+
factory = RequestFactory()
239+
request = factory.post(reverse('graphql-query'))
240+
client = Client(schema)
241+
result = client.execute(query, context=request)
242+
assert result == expected

examples/django_mongoengine/bike/urls.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@
33
from graphene_django.views import GraphQLView
44

55
urlpatterns = [
6-
path('graphql', csrf_exempt(GraphQLView.as_view(graphiql=True))),
6+
path('graphql', csrf_exempt(GraphQLView.as_view(graphiql=True)), name='graphql-query'),
77
]

0 commit comments

Comments
 (0)