diff --git a/graphene_django/fields.py b/graphene_django/fields.py index aa7f1245e..e755b93ec 100644 --- a/graphene_django/fields.py +++ b/graphene_django/fields.py @@ -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) diff --git a/graphene_django/rest_framework/tests/test_mutation.py b/graphene_django/rest_framework/tests/test_mutation.py index c34a971b4..491192ac8 100644 --- a/graphene_django/rest_framework/tests/test_mutation.py +++ b/graphene_django/rest_framework/tests/test_mutation.py @@ -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): diff --git a/graphene_django/tests/models.py b/graphene_django/tests/models.py index 0c62f28ba..406d18448 100644 --- a/graphene_django/tests/models.py +++ b/graphene_django/tests/models.py @@ -22,6 +22,9 @@ 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) @@ -29,6 +32,8 @@ class Reporter(models.Model): 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) diff --git a/graphene_django/tests/test_forms.py b/graphene_django/tests/test_forms.py index ada9e8a91..b15e86641 100644 --- a/graphene_django/tests/test_forms.py +++ b/graphene_django/tests/test_forms.py @@ -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' @@ -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): diff --git a/graphene_django/tests/test_query.py b/graphene_django/tests/test_query.py index 0dece3f61..c4c26f53a 100644 --- a/graphene_django/tests/test_query.py +++ b/graphene_django/tests/test_query.py @@ -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 @@ -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 { @@ -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