mirror of
https://github.com/graphql-python/graphene-django.git
synced 2024-11-10 19:57:15 +03:00
commit
fd3faf04e5
|
@ -116,7 +116,7 @@ class DjangoConnectionField(ConnectionField):
|
|||
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)
|
||||
|
|
|
@ -52,6 +52,19 @@ def test_has_input_fields():
|
|||
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):
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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):
|
||||
|
|
|
@ -606,6 +606,53 @@ def test_should_error_if_first_is_greater_than_max():
|
|||
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 @@ def test_should_query_promise_connectionfields():
|
|||
|
||||
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 test_should_query_promise_connectionfields():
|
|||
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
|
||||
|
|
Loading…
Reference in New Issue
Block a user