Skip to content

Add tests #341

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Feb 2, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion graphene_django/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ def connection_resolver(cls, resolver, connection, default_manager, max_limit,
if last:
assert last <= max_limit, (
'Requesting {} records on the `{}` connection exceeds the `last` limit of {} records.'
).format(first, info.field_name, max_limit)
).format(last, info.field_name, max_limit)
args['last'] = min(last, max_limit)

iterable = resolver(root, info, **args)
Expand Down
13 changes: 13 additions & 0 deletions graphene_django/rest_framework/tests/test_mutation.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,19 @@ class Meta:
assert 'model' in MyMutation.Input._meta.fields


def test_exclude_fields():
class MyMutation(SerializerMutation):
class Meta:
serializer_class = MyModelSerializer
exclude_fields = ['created']

assert 'cool_name' in MyMutation._meta.fields
assert 'created' not in MyMutation._meta.fields
assert 'errors' in MyMutation._meta.fields
assert 'cool_name' in MyMutation.Input._meta.fields
assert 'created' not in MyMutation.Input._meta.fields


def test_nested_model():

class MyFakeModelGrapheneType(DjangoObjectType):
Expand Down
5 changes: 5 additions & 0 deletions graphene_django/tests/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,18 @@ class Film(models.Model):
reporters = models.ManyToManyField('Reporter',
related_name='films')

class DoeReporterManager(models.Manager):
def get_queryset(self):
return super(DoeReporterManager, self).get_queryset().filter(last_name="Doe")

class Reporter(models.Model):
first_name = models.CharField(max_length=30)
last_name = models.CharField(max_length=30)
email = models.EmailField()
pets = models.ManyToManyField('self')
a_choice = models.CharField(max_length=30, choices=CHOICES)
objects = models.Manager()
doe_objects = DoeReporterManager()

def __str__(self): # __unicode__ on Python 2
return "%s %s" % (self.first_name, self.last_name)
Expand Down
13 changes: 12 additions & 1 deletion graphene_django/tests/test_forms.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from django.core.exceptions import ValidationError
from py.test import raises

from ..forms import GlobalIDFormField
from ..forms import GlobalIDFormField,GlobalIDMultipleChoiceField


# 'TXlUeXBlOmFiYw==' -> 'MyType', 'abc'
Expand All @@ -18,6 +18,17 @@ def test_global_id_invalid():
field.clean('badvalue')


def test_global_id_multiple_valid():
field = GlobalIDMultipleChoiceField()
field.clean(['TXlUeXBlOmFiYw==', 'TXlUeXBlOmFiYw=='])


def test_global_id_multiple_invalid():
field = GlobalIDMultipleChoiceField()
with raises(ValidationError):
field.clean(['badvalue', 'another bad avue'])


def test_global_id_none():
field = GlobalIDFormField()
with raises(ValidationError):
Expand Down
152 changes: 151 additions & 1 deletion graphene_django/tests/test_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -606,6 +606,53 @@ class Query(graphene.ObjectType):
graphene_settings.RELAY_CONNECTION_ENFORCE_FIRST_OR_LAST = False


def test_should_error_if_last_is_greater_than_max():
graphene_settings.RELAY_CONNECTION_MAX_LIMIT = 100

class ReporterType(DjangoObjectType):

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

class Query(graphene.ObjectType):
all_reporters = DjangoConnectionField(ReporterType)

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(last: 101) {
edges {
node {
id
}
}
}
}
'''

expected = {
'allReporters': None
}

result = schema.execute(query)
assert len(result.errors) == 1
assert str(result.errors[0]) == (
'Requesting 101 records on the `allReporters` connection '
'exceeds the `last` limit of 100 records.'
)
assert result.data == expected

graphene_settings.RELAY_CONNECTION_ENFORCE_FIRST_OR_LAST = False


def test_should_query_promise_connectionfields():
from promise import Promise

Expand All @@ -620,7 +667,7 @@ class Query(graphene.ObjectType):

def resolve_all_reporters(self, info, **args):
return Promise.resolve([Reporter(id=1)])

schema = graphene.Schema(query=Query)
query = '''
query ReporterPromiseConnectionQuery {
Expand Down Expand Up @@ -648,6 +695,109 @@ def resolve_all_reporters(self, info, **args):
assert not result.errors
assert result.data == expected

def test_should_query_connectionfields_with_last():

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

class ReporterType(DjangoObjectType):

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

class Query(graphene.ObjectType):
all_reporters = DjangoConnectionField(ReporterType)

def resolve_all_reporters(self, info, **args):
return Reporter.objects.all()

schema = graphene.Schema(query=Query)
query = '''
query ReporterLastQuery {
allReporters(last: 1) {
edges {
node {
id
}
}
}
}
'''

expected = {
'allReporters': {
'edges': [{
'node': {
'id': 'UmVwb3J0ZXJUeXBlOjE='
}
}]
}
}

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

def test_should_query_connectionfields_with_manager():

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

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

class ReporterType(DjangoObjectType):

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

class Query(graphene.ObjectType):
all_reporters = DjangoConnectionField(ReporterType, on='doe_objects')

def resolve_all_reporters(self, info, **args):
return Reporter.objects.all()

schema = graphene.Schema(query=Query)
query = '''
query ReporterLastQuery {
allReporters(first: 2) {
edges {
node {
id
}
}
}
}
'''

expected = {
'allReporters': {
'edges': [{
'node': {
'id': 'UmVwb3J0ZXJUeXBlOjE='
}
}]
}
}

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


def test_should_query_dataloader_fields():
from promise import Promise
Expand Down