Skip to content

Commit

Permalink
Moved tests to filter field tests
Browse files Browse the repository at this point in the history
  • Loading branch information
syrusakbary committed Apr 15, 2017
1 parent b30b40e commit cc03da0
Show file tree
Hide file tree
Showing 3 changed files with 176 additions and 177 deletions.
12 changes: 6 additions & 6 deletions .travis.yml
Expand Up @@ -46,20 +46,20 @@ after_success:
fi
env:
matrix:
- TEST_TYPE=build DJANGO_VERSION=1.11 DJANGO_FILTER_VERSION=1.0.2
- TEST_TYPE=build DJANGO_VERSION=1.11
matrix:
fast_finish: true
include:
- python: '2.7'
env: TEST_TYPE=build DJANGO_VERSION=1.6 DJANGO_FILTER_VERSION=1.0.0
env: TEST_TYPE=build DJANGO_VERSION=1.6
- python: '2.7'
env: TEST_TYPE=build DJANGO_VERSION=1.7 DJANGO_FILTER_VERSION=1.0.0
env: TEST_TYPE=build DJANGO_VERSION=1.7
- python: '2.7'
env: TEST_TYPE=build DJANGO_VERSION=1.8 DJANGO_FILTER_VERSION=1.0.2
env: TEST_TYPE=build DJANGO_VERSION=1.8
- python: '2.7'
env: TEST_TYPE=build DJANGO_VERSION=1.9 DJANGO_FILTER_VERSION=1.0.2
env: TEST_TYPE=build DJANGO_VERSION=1.9
- python: '2.7'
env: TEST_TYPE=build DJANGO_VERSION=1.10 DJANGO_FILTER_VERSION=1.0.2
env: TEST_TYPE=build DJANGO_VERSION=1.10
- python: '2.7'
env: TEST_TYPE=lint
deploy:
Expand Down
171 changes: 170 additions & 1 deletion graphene_django/filter/tests/test_fields.py
Expand Up @@ -14,11 +14,13 @@

if DJANGO_FILTER_INSTALLED:
import django_filters
from django_filters import FilterSet, NumberFilter

from graphene_django.filter import (GlobalIDFilter, DjangoFilterConnectionField,
GlobalIDMultipleChoiceFilter)
from graphene_django.filter.tests.filters import ArticleFilter, PetFilter, ReporterFilter
else:
pytestmark.append(pytest.mark.skipif(True, reason='django_filters not installed'))
pytestmark.append(pytest.mark.skipif(True, reason='django_filters not installed or not compatible'))

pytestmark.append(pytest.mark.django_db)

Expand Down Expand Up @@ -365,3 +367,170 @@ class Query(ObjectType):
all_reporters = DjangoFilterConnectionField(ReporterFilterNode)

assert ReporterFilterNode._meta.fields['child_reporters'].node_type == ReporterFilterNode


def test_should_query_filter_node_limit():
class ReporterFilter(FilterSet):
limit = NumberFilter(method='filter_limit')

def filter_limit(self, queryset, name, value):
return queryset[:value]

class Meta:
model = Reporter
fields = ['first_name', ]

class ReporterType(DjangoObjectType):

class Meta:
model = Reporter
interfaces = (Node, )

class ArticleType(DjangoObjectType):

class Meta:
model = Article
interfaces = (Node, )
filter_fields = ('lang', )

class Query(ObjectType):
all_reporters = DjangoFilterConnectionField(
ReporterType,
filterset_class=ReporterFilter
)

def resolve_all_reporters(self, args, context, info):
return Reporter.objects.order_by('a_choice')

Reporter.objects.create(
first_name='Bob',
last_name='Doe',
email='bobdoe@example.com',
a_choice=2
)
r = Reporter.objects.create(
first_name='John',
last_name='Doe',
email='johndoe@example.com',
a_choice=1
)

Article.objects.create(
headline='Article Node 1',
pub_date=datetime.now(),
reporter=r,
editor=r,
lang='es'
)
Article.objects.create(
headline='Article Node 2',
pub_date=datetime.now(),
reporter=r,
editor=r,
lang='en'
)

schema = Schema(query=Query)
query = '''
query NodeFilteringQuery {
allReporters(limit: 1) {
edges {
node {
id
firstName
articles(lang: "es") {
edges {
node {
id
lang
}
}
}
}
}
}
}
'''

expected = {
'allReporters': {
'edges': [{
'node': {
'id': 'UmVwb3J0ZXJUeXBlOjI=',
'firstName': 'John',
'articles': {
'edges': [{
'node': {
'id': 'QXJ0aWNsZVR5cGU6MQ==',
'lang': 'ES'
}
}]
}
}
}]
}
}

result = schema.execute(query)
assert not result.errors
assert result.data == expected


def test_should_query_filter_node_double_limit_raises():
class ReporterFilter(FilterSet):
limit = NumberFilter(method='filter_limit')

def filter_limit(self, queryset, name, value):
return queryset[:value]

class Meta:
model = Reporter
fields = ['first_name', ]

class ReporterType(DjangoObjectType):

class Meta:
model = Reporter
interfaces = (Node, )

class Query(ObjectType):
all_reporters = DjangoFilterConnectionField(
ReporterType,
filterset_class=ReporterFilter
)

def resolve_all_reporters(self, args, context, info):
return Reporter.objects.order_by('a_choice')[:2]

Reporter.objects.create(
first_name='Bob',
last_name='Doe',
email='bobdoe@example.com',
a_choice=2
)
r = Reporter.objects.create(
first_name='John',
last_name='Doe',
email='johndoe@example.com',
a_choice=1
)

schema = Schema(query=Query)
query = '''
query NodeFilteringQuery {
allReporters(limit: 1) {
edges {
node {
id
firstName
}
}
}
}
'''

result = schema.execute(query)
assert len(result.errors) == 1
assert str(result.errors[0]) == (
'Received two sliced querysets (high mark) in the connection, please slice only in one.'
)
170 changes: 0 additions & 170 deletions graphene_django/tests/test_query.py
Expand Up @@ -5,15 +5,12 @@
from django.utils.functional import SimpleLazyObject
from py.test import raises

from django_filters import FilterSet, NumberFilter

import graphene
from graphene.relay import Node

from ..utils import DJANGO_FILTER_INSTALLED
from ..compat import MissingType, JSONField
from ..fields import DjangoConnectionField
from ..filter.fields import DjangoFilterConnectionField
from ..types import DjangoObjectType
from .models import Article, Reporter

Expand Down Expand Up @@ -455,170 +452,3 @@ class Query(graphene.ObjectType):
result = schema.execute(query)
assert not result.errors
assert result.data == expected


def test_should_query_filter_node_limit():
class ReporterFilter(FilterSet):
limit = NumberFilter(method='filter_limit')

def filter_limit(self, queryset, name, value):
return queryset[:value]

class Meta:
model = Reporter
fields = ['first_name', ]

class ReporterType(DjangoObjectType):

class Meta:
model = Reporter
interfaces = (Node, )

class ArticleType(DjangoObjectType):

class Meta:
model = Article
interfaces = (Node, )
filter_fields = ('lang', )

class Query(graphene.ObjectType):
all_reporters = DjangoFilterConnectionField(
ReporterType,
filterset_class=ReporterFilter
)

def resolve_all_reporters(self, args, context, info):
return Reporter.objects.order_by('a_choice')

Reporter.objects.create(
first_name='Bob',
last_name='Doe',
email='bobdoe@example.com',
a_choice=2
)
r = Reporter.objects.create(
first_name='John',
last_name='Doe',
email='johndoe@example.com',
a_choice=1
)

Article.objects.create(
headline='Article Node 1',
pub_date=datetime.date.today(),
reporter=r,
editor=r,
lang='es'
)
Article.objects.create(
headline='Article Node 2',
pub_date=datetime.date.today(),
reporter=r,
editor=r,
lang='en'
)

schema = graphene.Schema(query=Query)
query = '''
query NodeFilteringQuery {
allReporters(limit: 1) {
edges {
node {
id
firstName
articles(lang: "es") {
edges {
node {
id
lang
}
}
}
}
}
}
}
'''

expected = {
'allReporters': {
'edges': [{
'node': {
'id': 'UmVwb3J0ZXJUeXBlOjI=',
'firstName': 'John',
'articles': {
'edges': [{
'node': {
'id': 'QXJ0aWNsZVR5cGU6MQ==',
'lang': 'ES'
}
}]
}
}
}]
}
}

result = schema.execute(query)
assert not result.errors
assert result.data == expected


def test_should_query_filter_node_double_limit_raises():
class ReporterFilter(FilterSet):
limit = NumberFilter(method='filter_limit')

def filter_limit(self, queryset, name, value):
return queryset[:value]

class Meta:
model = Reporter
fields = ['first_name', ]

class ReporterType(DjangoObjectType):

class Meta:
model = Reporter
interfaces = (Node, )

class Query(graphene.ObjectType):
all_reporters = DjangoFilterConnectionField(
ReporterType,
filterset_class=ReporterFilter
)

def resolve_all_reporters(self, args, context, info):
return Reporter.objects.order_by('a_choice')[:2]

Reporter.objects.create(
first_name='Bob',
last_name='Doe',
email='bobdoe@example.com',
a_choice=2
)
r = Reporter.objects.create(
first_name='John',
last_name='Doe',
email='johndoe@example.com',
a_choice=1
)

schema = graphene.Schema(query=Query)
query = '''
query NodeFilteringQuery {
allReporters(limit: 1) {
edges {
node {
id
firstName
}
}
}
}
'''

result = schema.execute(query)
assert len(result.errors) == 1
assert str(result.errors[0]) == (
'Received two sliced querysets (high mark) in the connection, please slice only in one.'
)

0 comments on commit cc03da0

Please sign in to comment.