mirror of
https://github.com/graphql-python/graphene.git
synced 2025-02-02 12:44:15 +03:00
Improved testing
This commit is contained in:
parent
a7774f0be4
commit
35ec787501
|
@ -3,7 +3,8 @@ sudo: false
|
||||||
python:
|
python:
|
||||||
- 2.7
|
- 2.7
|
||||||
install:
|
install:
|
||||||
- pip install pytest pytest-cov coveralls flake8 six blinker singledispatch django
|
- pip install pytest pytest-cov coveralls flake8 six blinker
|
||||||
|
- pip install -e .[django]
|
||||||
- pip install git+https://github.com/dittos/graphqllib.git # Last version of graphqllib
|
- pip install git+https://github.com/dittos/graphqllib.git # Last version of graphqllib
|
||||||
- pip install graphql-relay
|
- pip install graphql-relay
|
||||||
- python setup.py develop
|
- python setup.py develop
|
||||||
|
|
1
setup.py
1
setup.py
|
@ -56,6 +56,7 @@ setup(
|
||||||
extras_require={
|
extras_require={
|
||||||
'django': [
|
'django': [
|
||||||
'Django>=1.8.0,<1.9',
|
'Django>=1.8.0,<1.9',
|
||||||
|
'pytest-django',
|
||||||
'singledispatch>=3.4.0.3',
|
'singledispatch>=3.4.0.3',
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
|
|
0
tests/__init__.py
Normal file
0
tests/__init__.py
Normal file
|
@ -44,6 +44,13 @@ def test_should_raise_if_model_is_invalid():
|
||||||
assert 'articles (Article) model not mapped in current schema' in str(excinfo.value)
|
assert 'articles (Article) model not mapped in current schema' in str(excinfo.value)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def test_should_map_fields_correctly():
|
||||||
|
class ReporterType2(DjangoObjectType):
|
||||||
|
class Meta:
|
||||||
|
model = Reporter
|
||||||
|
assert ReporterType2._meta.fields_map.keys() == ['articles', 'first_name', 'last_name', 'id', 'email']
|
||||||
|
|
||||||
def test_should_map_fields():
|
def test_should_map_fields():
|
||||||
class ReporterType(DjangoObjectType):
|
class ReporterType(DjangoObjectType):
|
||||||
class Meta:
|
class Meta:
|
||||||
|
|
13
tests/django_settings.py
Normal file
13
tests/django_settings.py
Normal file
|
@ -0,0 +1,13 @@
|
||||||
|
SECRET_KEY = 1
|
||||||
|
|
||||||
|
INSTALLED_APPS = [
|
||||||
|
'graphql.contrib.django',
|
||||||
|
'tests.starwars_django',
|
||||||
|
]
|
||||||
|
|
||||||
|
DATABASES={
|
||||||
|
'default': {
|
||||||
|
'ENGINE': 'django.db.backends.sqlite3',
|
||||||
|
'NAME': 'tests/django.sqlite',
|
||||||
|
}
|
||||||
|
}
|
0
tests/starwars_django/__init__.py
Normal file
0
tests/starwars_django/__init__.py
Normal file
98
tests/starwars_django/data.py
Normal file
98
tests/starwars_django/data.py
Normal file
|
@ -0,0 +1,98 @@
|
||||||
|
from collections import namedtuple
|
||||||
|
|
||||||
|
from .models import Ship, Faction
|
||||||
|
|
||||||
|
def initialize():
|
||||||
|
rebels = Faction(
|
||||||
|
id='1',
|
||||||
|
name='Alliance to Restore the Republic',
|
||||||
|
)
|
||||||
|
rebels.save()
|
||||||
|
|
||||||
|
empire = Faction(
|
||||||
|
id='2',
|
||||||
|
name='Galactic Empire',
|
||||||
|
)
|
||||||
|
empire.save()
|
||||||
|
|
||||||
|
|
||||||
|
xwing = Ship(
|
||||||
|
id='1',
|
||||||
|
name='X-Wing',
|
||||||
|
faction=rebels,
|
||||||
|
)
|
||||||
|
xwing.save()
|
||||||
|
|
||||||
|
ywing = Ship(
|
||||||
|
id='2',
|
||||||
|
name='Y-Wing',
|
||||||
|
faction=rebels,
|
||||||
|
)
|
||||||
|
ywing.save()
|
||||||
|
|
||||||
|
awing = Ship(
|
||||||
|
id='3',
|
||||||
|
name='A-Wing',
|
||||||
|
faction=rebels,
|
||||||
|
)
|
||||||
|
awing.save()
|
||||||
|
|
||||||
|
# Yeah, technically it's Corellian. But it flew in the service of the rebels,
|
||||||
|
# so for the purposes of this demo it's a rebel ship.
|
||||||
|
falcon = Ship(
|
||||||
|
id='4',
|
||||||
|
name='Millenium Falcon',
|
||||||
|
faction=rebels,
|
||||||
|
)
|
||||||
|
falcon.save()
|
||||||
|
|
||||||
|
homeOne = Ship(
|
||||||
|
id='5',
|
||||||
|
name='Home One',
|
||||||
|
faction=rebels,
|
||||||
|
)
|
||||||
|
homeOne.save()
|
||||||
|
|
||||||
|
tieFighter = Ship(
|
||||||
|
id='6',
|
||||||
|
name='TIE Fighter',
|
||||||
|
faction=empire,
|
||||||
|
)
|
||||||
|
tieFighter.save()
|
||||||
|
|
||||||
|
tieInterceptor = Ship(
|
||||||
|
id='7',
|
||||||
|
name='TIE Interceptor',
|
||||||
|
faction=empire,
|
||||||
|
)
|
||||||
|
tieInterceptor.save()
|
||||||
|
|
||||||
|
executor = Ship(
|
||||||
|
id='8',
|
||||||
|
name='Executor',
|
||||||
|
faction=empire,
|
||||||
|
)
|
||||||
|
executor.save()
|
||||||
|
|
||||||
|
|
||||||
|
def createShip(shipName, factionId):
|
||||||
|
nextShip = len(data['Ship'].keys())+1
|
||||||
|
newShip = Ship(
|
||||||
|
id=str(nextShip),
|
||||||
|
name=shipName
|
||||||
|
)
|
||||||
|
newShip.save()
|
||||||
|
return newShip
|
||||||
|
|
||||||
|
|
||||||
|
def getShip(_id):
|
||||||
|
return Ship.objects.get(id=_id)
|
||||||
|
|
||||||
|
def getFaction(_id):
|
||||||
|
return Faction.objects.get(id=_id)
|
||||||
|
|
||||||
|
def getRebels():
|
||||||
|
return getFaction(1)
|
||||||
|
|
||||||
|
def getEmpire():
|
||||||
|
return getFaction(2)
|
17
tests/starwars_django/models.py
Normal file
17
tests/starwars_django/models.py
Normal file
|
@ -0,0 +1,17 @@
|
||||||
|
from __future__ import absolute_import
|
||||||
|
from django.db import models
|
||||||
|
|
||||||
|
|
||||||
|
class Faction(models.Model):
|
||||||
|
name = models.CharField(max_length=50)
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return self.name
|
||||||
|
|
||||||
|
|
||||||
|
class Ship(models.Model):
|
||||||
|
name = models.CharField(max_length=50)
|
||||||
|
faction = models.ForeignKey(Faction, related_name='ships')
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return self.name
|
48
tests/starwars_django/schema.py
Normal file
48
tests/starwars_django/schema.py
Normal file
|
@ -0,0 +1,48 @@
|
||||||
|
import graphene
|
||||||
|
from graphene import resolve_only_args, relay
|
||||||
|
from graphene.contrib.django import (
|
||||||
|
DjangoObjectType,
|
||||||
|
DjangoNode
|
||||||
|
)
|
||||||
|
from .models import Ship as ShipModel, Faction as FactionModel
|
||||||
|
from .data import (
|
||||||
|
getFaction,
|
||||||
|
getShip,
|
||||||
|
getRebels,
|
||||||
|
getEmpire,
|
||||||
|
)
|
||||||
|
|
||||||
|
schema = graphene.Schema(name='Starwars Django Relay Schema')
|
||||||
|
|
||||||
|
class Ship(DjangoNode):
|
||||||
|
class Meta:
|
||||||
|
model = ShipModel
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def get_node(cls, id):
|
||||||
|
return Ship(getShip(id))
|
||||||
|
|
||||||
|
class Faction(DjangoNode):
|
||||||
|
class Meta:
|
||||||
|
model = FactionModel
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def get_node(cls, id):
|
||||||
|
return Faction(getFaction(id))
|
||||||
|
|
||||||
|
|
||||||
|
class Query(graphene.ObjectType):
|
||||||
|
rebels = graphene.Field(Faction)
|
||||||
|
empire = graphene.Field(Faction)
|
||||||
|
node = relay.NodeField()
|
||||||
|
|
||||||
|
@resolve_only_args
|
||||||
|
def resolve_rebels(self):
|
||||||
|
return Faction(getRebels())
|
||||||
|
|
||||||
|
@resolve_only_args
|
||||||
|
def resolve_empire(self):
|
||||||
|
return Faction(getEmpire())
|
||||||
|
|
||||||
|
|
||||||
|
schema.query = Query
|
43
tests/starwars_django/test_connections.py
Normal file
43
tests/starwars_django/test_connections.py
Normal file
|
@ -0,0 +1,43 @@
|
||||||
|
# import pytest
|
||||||
|
# from graphql.core import graphql
|
||||||
|
|
||||||
|
# from .models import *
|
||||||
|
# from .schema import schema
|
||||||
|
# from .data import initialize, getFaction
|
||||||
|
|
||||||
|
# pytestmark = pytest.mark.django_db
|
||||||
|
|
||||||
|
# def test_correct_fetch_first_ship_rebels():
|
||||||
|
# initialize()
|
||||||
|
# print schema.Faction._meta.fields_map
|
||||||
|
# query = '''
|
||||||
|
# query RebelsShipsQuery {
|
||||||
|
# rebels {
|
||||||
|
# name,
|
||||||
|
# ships(first: 1) {
|
||||||
|
# edges {
|
||||||
|
# node {
|
||||||
|
# name
|
||||||
|
# }
|
||||||
|
# }
|
||||||
|
# }
|
||||||
|
# }
|
||||||
|
# }
|
||||||
|
# '''
|
||||||
|
# expected = {
|
||||||
|
# 'rebels': {
|
||||||
|
# 'name': 'Alliance to Restore the Republic',
|
||||||
|
# 'ships': {
|
||||||
|
# 'edges': [
|
||||||
|
# {
|
||||||
|
# 'node': {
|
||||||
|
# 'name': 'X-Wing'
|
||||||
|
# }
|
||||||
|
# }
|
||||||
|
# ]
|
||||||
|
# }
|
||||||
|
# }
|
||||||
|
# }
|
||||||
|
# result = schema.execute(query)
|
||||||
|
# assert not result.errors
|
||||||
|
# assert result.data == expected
|
105
tests/starwars_django/test_objectidentification.py
Normal file
105
tests/starwars_django/test_objectidentification.py
Normal file
|
@ -0,0 +1,105 @@
|
||||||
|
# from pytest import raises
|
||||||
|
# from graphql.core import graphql
|
||||||
|
|
||||||
|
# from .schema import schema
|
||||||
|
|
||||||
|
# def test_correctly_fetches_id_name_rebels():
|
||||||
|
# query = '''
|
||||||
|
# query RebelsQuery {
|
||||||
|
# rebels {
|
||||||
|
# id
|
||||||
|
# name
|
||||||
|
# }
|
||||||
|
# }
|
||||||
|
# '''
|
||||||
|
# expected = {
|
||||||
|
# 'rebels': {
|
||||||
|
# 'id': 'RmFjdGlvbjox',
|
||||||
|
# 'name': 'Alliance to Restore the Republic'
|
||||||
|
# }
|
||||||
|
# }
|
||||||
|
# result = schema.execute(query)
|
||||||
|
# assert not result.errors
|
||||||
|
# assert result.data == expected
|
||||||
|
|
||||||
|
# def test_correctly_refetches_rebels():
|
||||||
|
# query = '''
|
||||||
|
# query RebelsRefetchQuery {
|
||||||
|
# node(id: "RmFjdGlvbjox") {
|
||||||
|
# id
|
||||||
|
# ... on Faction {
|
||||||
|
# name
|
||||||
|
# }
|
||||||
|
# }
|
||||||
|
# }
|
||||||
|
# '''
|
||||||
|
# expected = {
|
||||||
|
# 'node': {
|
||||||
|
# 'id': 'RmFjdGlvbjox',
|
||||||
|
# 'name': 'Alliance to Restore the Republic'
|
||||||
|
# }
|
||||||
|
# }
|
||||||
|
# result = schema.execute(query)
|
||||||
|
# assert not result.errors
|
||||||
|
# assert result.data == expected
|
||||||
|
|
||||||
|
# def test_correctly_fetches_id_name_empire():
|
||||||
|
# query = '''
|
||||||
|
# query EmpireQuery {
|
||||||
|
# empire {
|
||||||
|
# id
|
||||||
|
# name
|
||||||
|
# }
|
||||||
|
# }
|
||||||
|
# '''
|
||||||
|
# expected = {
|
||||||
|
# 'empire': {
|
||||||
|
# 'id': 'RmFjdGlvbjoy',
|
||||||
|
# 'name': 'Galactic Empire'
|
||||||
|
# }
|
||||||
|
# }
|
||||||
|
# result = schema.execute(query)
|
||||||
|
# assert not result.errors
|
||||||
|
# assert result.data == expected
|
||||||
|
|
||||||
|
# def test_correctly_refetches_empire():
|
||||||
|
# query = '''
|
||||||
|
# query EmpireRefetchQuery {
|
||||||
|
# node(id: "RmFjdGlvbjoy") {
|
||||||
|
# id
|
||||||
|
# ... on Faction {
|
||||||
|
# name
|
||||||
|
# }
|
||||||
|
# }
|
||||||
|
# }
|
||||||
|
# '''
|
||||||
|
# expected = {
|
||||||
|
# 'node': {
|
||||||
|
# 'id': 'RmFjdGlvbjoy',
|
||||||
|
# 'name': 'Galactic Empire'
|
||||||
|
# }
|
||||||
|
# }
|
||||||
|
# result = schema.execute(query)
|
||||||
|
# assert not result.errors
|
||||||
|
# assert result.data == expected
|
||||||
|
|
||||||
|
# def test_correctly_refetches_xwing():
|
||||||
|
# query = '''
|
||||||
|
# query XWingRefetchQuery {
|
||||||
|
# node(id: "U2hpcDox") {
|
||||||
|
# id
|
||||||
|
# ... on Ship {
|
||||||
|
# name
|
||||||
|
# }
|
||||||
|
# }
|
||||||
|
# }
|
||||||
|
# '''
|
||||||
|
# expected = {
|
||||||
|
# 'node': {
|
||||||
|
# 'id': 'U2hpcDox',
|
||||||
|
# 'name': 'X-Wing'
|
||||||
|
# }
|
||||||
|
# }
|
||||||
|
# result = schema.execute(query)
|
||||||
|
# assert not result.errors
|
||||||
|
# assert result.data == expected
|
4
tox.ini
4
tox.ini
|
@ -5,6 +5,7 @@ envlist = py27
|
||||||
deps=
|
deps=
|
||||||
pytest>=2.7.2
|
pytest>=2.7.2
|
||||||
django>=1.8.0,<1.9
|
django>=1.8.0,<1.9
|
||||||
|
pytest-django
|
||||||
flake8
|
flake8
|
||||||
six
|
six
|
||||||
blinker
|
blinker
|
||||||
|
@ -12,3 +13,6 @@ deps=
|
||||||
commands=
|
commands=
|
||||||
py.test
|
py.test
|
||||||
flake8
|
flake8
|
||||||
|
|
||||||
|
[pytest]
|
||||||
|
DJANGO_SETTINGS_MODULE = tests.django_settings
|
||||||
|
|
Loading…
Reference in New Issue
Block a user