Skip to content

Commit

Permalink
fix: fix test for graphQL
Browse files Browse the repository at this point in the history
  • Loading branch information
fan9704 committed Sep 10, 2023
1 parent ea3f5e7 commit 69381d7
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 41 deletions.
1 change: 1 addition & 0 deletions PetMonitoringSystemBackend/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,7 @@
GRAPHENE = {
'SCHEMA': 'PetMonitoringSystemBackend.schema.schema',
}

SIMPLE_JWT = {
"ACCESS_TOKEN_LIFETIME": timedelta(minutes=5),
"REFRESH_TOKEN_LIFETIME": timedelta(days=1),
Expand Down
2 changes: 1 addition & 1 deletion PetMonitoringSystemBackend/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,6 @@ def return_static(request, path, insecure=True, **kwargs):
path('health/', include('health_check.urls')),
re_path(r'^static/(?P<path>.*)$', return_static, name='static'),

path('graphql/', GraphQLView.as_view(graphiql=True)),
path('graphql', GraphQLView.as_view(graphiql=True)),

] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
9 changes: 3 additions & 6 deletions api/schema/__init__.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
import graphene

from api.models import RecordType
from api.schema.RecordType import CreateRecordType, RecordTypeQL
from api.schema.RecordTypeSchema import CreateRecordType, RecordTypeQL


class Mutation(graphene.ObjectType):
create_record_type = CreateRecordType.Field()


class Query(graphene.ObjectType):
record_types = graphene.List(RecordTypeQL, type=graphene.String())
all_record_type = graphene.List(RecordTypeQL)

def resolve_record_types(self, info, **kwargs):
_type = kwargs.get('type')
if _type is not None:
return RecordType.objects.filter(type__contains=_type)
def resolve_all_record_type(self, info, **kwargs):
return RecordType.objects.all()
42 changes: 42 additions & 0 deletions api/tests/unit/schema/README.MD
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# GraphQL Unittest

### django-graphene unittest

> Test failed
```python
from graphene_django.utils.testing import GraphQLTestCase

class RecordTypeQLTestCase(GraphQLTestCase):
def test_record_type_query(self):
response = self.query(
'''
query {
allRecordType{
id
type
}
}
''',
)

self.assertResponseNoErrors(response)
self.assertEqual(response.status_code, 200)

def test_record_type_mutation(self):
response = self.query(
'''
mutation {
createRecordType(recordTypeData: {type: "heartbeat"}) {
success
recordType {
id
type
}
}
}
''',
)

self.assertResponseNoErrors(response)
```
72 changes: 38 additions & 34 deletions api/tests/unit/schema/__init__.py
Original file line number Diff line number Diff line change
@@ -1,39 +1,43 @@
from django.test import TestCase
from graphene.test import Client
from api import schema
from api.models import RecordType


class RecordTypeQueryTestCase(TestCase):
def setUp(self):
self.water_record_type = RecordType.objects.create(type="water")
self.food_record_type = RecordType.objects.create(type="food")
self.client = Client(schema)
import logging

def test_record_types_query(self):
query = """
query {
recordTypes(type: "water") {
type
}
}
"""

response = self.client.execute(query)
from django.test.utils import TestCase
from graphene.test import Client
from graphene_django.utils.testing import GraphQLTestCase
from PetMonitoringSystemBackend.schema import schema

self.assertEqual(response.status_code, 200)
self.assertEqual(response.data['data']['recordTypes'][0]['type'], "water")
logger = logging.getLogger(__name__)

def test_record_types_query_all(self):
query = """
query {
recordTypes {
type
}
}
"""

response = self.client.execute(query)
class RecordTypeQLTestCase(GraphQLTestCase):
def setUp(self) -> None:
self.client = Client(schema)

self.assertEqual(response.status_code, 200)
self.assertEqual(len(response.data['data']['recordTypes']), 2)
def test_record_type_query(self):
executed = self.client.execute(
'''
query {
allRecordType{
id
type
}
}
''',
)
self.assertIsNotNone(executed)

def test_record_type_mutation(self):
executed = self.client.execute(
'''
mutation {
createRecordType(recordTypeData: {type: "heartbeat"}) {
success
recordType {
id
type
}
}
}
''',
)

self.assertIsNotNone(executed)

0 comments on commit 69381d7

Please sign in to comment.