From 35ec78750170aa9bfaa977686a041ce64ea7fcfd Mon Sep 17 00:00:00 2001 From: Syrus Akbary Date: Tue, 29 Sep 2015 23:40:40 -0700 Subject: [PATCH] Improved testing --- .travis.yml | 3 +- setup.py | 1 + tests/__init__.py | 0 tests/contrib_django/test_schema.py | 7 ++ tests/django_settings.py | 13 +++ tests/starwars_django/__init__.py | 0 tests/starwars_django/data.py | 98 ++++++++++++++++ tests/starwars_django/models.py | 17 +++ tests/starwars_django/schema.py | 48 ++++++++ tests/starwars_django/test_connections.py | 43 +++++++ .../test_objectidentification.py | 105 ++++++++++++++++++ tox.ini | 4 + 12 files changed, 338 insertions(+), 1 deletion(-) create mode 100644 tests/__init__.py create mode 100644 tests/django_settings.py create mode 100644 tests/starwars_django/__init__.py create mode 100644 tests/starwars_django/data.py create mode 100644 tests/starwars_django/models.py create mode 100644 tests/starwars_django/schema.py create mode 100644 tests/starwars_django/test_connections.py create mode 100644 tests/starwars_django/test_objectidentification.py diff --git a/.travis.yml b/.travis.yml index d9cf9eb9..83d35e1a 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,7 +3,8 @@ sudo: false python: - 2.7 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 graphql-relay - python setup.py develop diff --git a/setup.py b/setup.py index 739f729f..04d1d37e 100644 --- a/setup.py +++ b/setup.py @@ -56,6 +56,7 @@ setup( extras_require={ 'django': [ 'Django>=1.8.0,<1.9', + 'pytest-django', 'singledispatch>=3.4.0.3', ], }, diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/tests/contrib_django/test_schema.py b/tests/contrib_django/test_schema.py index 34617ddf..669c8a7f 100644 --- a/tests/contrib_django/test_schema.py +++ b/tests/contrib_django/test_schema.py @@ -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) + +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(): class ReporterType(DjangoObjectType): class Meta: diff --git a/tests/django_settings.py b/tests/django_settings.py new file mode 100644 index 00000000..4f1bf8ce --- /dev/null +++ b/tests/django_settings.py @@ -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', + } +} diff --git a/tests/starwars_django/__init__.py b/tests/starwars_django/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/tests/starwars_django/data.py b/tests/starwars_django/data.py new file mode 100644 index 00000000..552c2ebe --- /dev/null +++ b/tests/starwars_django/data.py @@ -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) diff --git a/tests/starwars_django/models.py b/tests/starwars_django/models.py new file mode 100644 index 00000000..6afa152e --- /dev/null +++ b/tests/starwars_django/models.py @@ -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 diff --git a/tests/starwars_django/schema.py b/tests/starwars_django/schema.py new file mode 100644 index 00000000..63f499b7 --- /dev/null +++ b/tests/starwars_django/schema.py @@ -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 diff --git a/tests/starwars_django/test_connections.py b/tests/starwars_django/test_connections.py new file mode 100644 index 00000000..5c033216 --- /dev/null +++ b/tests/starwars_django/test_connections.py @@ -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 diff --git a/tests/starwars_django/test_objectidentification.py b/tests/starwars_django/test_objectidentification.py new file mode 100644 index 00000000..3040bd92 --- /dev/null +++ b/tests/starwars_django/test_objectidentification.py @@ -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 diff --git a/tox.ini b/tox.ini index 735a042d..cd4b45a4 100644 --- a/tox.ini +++ b/tox.ini @@ -5,6 +5,7 @@ envlist = py27 deps= pytest>=2.7.2 django>=1.8.0,<1.9 + pytest-django flake8 six blinker @@ -12,3 +13,6 @@ deps= commands= py.test flake8 + +[pytest] +DJANGO_SETTINGS_MODULE = tests.django_settings