Skip to content

Commit fd3faf0

Browse files
authored
Merge pull request #341 from urbandove/tests
Add tests
2 parents 53546a2 + 51f794e commit fd3faf0

File tree

5 files changed

+182
-3
lines changed

5 files changed

+182
-3
lines changed

graphene_django/fields.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ def connection_resolver(cls, resolver, connection, default_manager, max_limit,
116116
if last:
117117
assert last <= max_limit, (
118118
'Requesting {} records on the `{}` connection exceeds the `last` limit of {} records.'
119-
).format(first, info.field_name, max_limit)
119+
).format(last, info.field_name, max_limit)
120120
args['last'] = min(last, max_limit)
121121

122122
iterable = resolver(root, info, **args)

graphene_django/rest_framework/tests/test_mutation.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,19 @@ class Meta:
5252
assert 'model' in MyMutation.Input._meta.fields
5353

5454

55+
def test_exclude_fields():
56+
class MyMutation(SerializerMutation):
57+
class Meta:
58+
serializer_class = MyModelSerializer
59+
exclude_fields = ['created']
60+
61+
assert 'cool_name' in MyMutation._meta.fields
62+
assert 'created' not in MyMutation._meta.fields
63+
assert 'errors' in MyMutation._meta.fields
64+
assert 'cool_name' in MyMutation.Input._meta.fields
65+
assert 'created' not in MyMutation.Input._meta.fields
66+
67+
5568
def test_nested_model():
5669

5770
class MyFakeModelGrapheneType(DjangoObjectType):

graphene_django/tests/models.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,18 @@ class Film(models.Model):
2222
reporters = models.ManyToManyField('Reporter',
2323
related_name='films')
2424

25+
class DoeReporterManager(models.Manager):
26+
def get_queryset(self):
27+
return super(DoeReporterManager, self).get_queryset().filter(last_name="Doe")
2528

2629
class Reporter(models.Model):
2730
first_name = models.CharField(max_length=30)
2831
last_name = models.CharField(max_length=30)
2932
email = models.EmailField()
3033
pets = models.ManyToManyField('self')
3134
a_choice = models.CharField(max_length=30, choices=CHOICES)
35+
objects = models.Manager()
36+
doe_objects = DoeReporterManager()
3237

3338
def __str__(self): # __unicode__ on Python 2
3439
return "%s %s" % (self.first_name, self.last_name)

graphene_django/tests/test_forms.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from django.core.exceptions import ValidationError
22
from py.test import raises
33

4-
from ..forms import GlobalIDFormField
4+
from ..forms import GlobalIDFormField,GlobalIDMultipleChoiceField
55

66

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

2020

21+
def test_global_id_multiple_valid():
22+
field = GlobalIDMultipleChoiceField()
23+
field.clean(['TXlUeXBlOmFiYw==', 'TXlUeXBlOmFiYw=='])
24+
25+
26+
def test_global_id_multiple_invalid():
27+
field = GlobalIDMultipleChoiceField()
28+
with raises(ValidationError):
29+
field.clean(['badvalue', 'another bad avue'])
30+
31+
2132
def test_global_id_none():
2233
field = GlobalIDFormField()
2334
with raises(ValidationError):

graphene_django/tests/test_query.py

Lines changed: 151 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -606,6 +606,53 @@ class Query(graphene.ObjectType):
606606
graphene_settings.RELAY_CONNECTION_ENFORCE_FIRST_OR_LAST = False
607607

608608

609+
def test_should_error_if_last_is_greater_than_max():
610+
graphene_settings.RELAY_CONNECTION_MAX_LIMIT = 100
611+
612+
class ReporterType(DjangoObjectType):
613+
614+
class Meta:
615+
model = Reporter
616+
interfaces = (Node, )
617+
618+
class Query(graphene.ObjectType):
619+
all_reporters = DjangoConnectionField(ReporterType)
620+
621+
r = Reporter.objects.create(
622+
first_name='John',
623+
last_name='Doe',
624+
email='johndoe@example.com',
625+
a_choice=1
626+
)
627+
628+
schema = graphene.Schema(query=Query)
629+
query = '''
630+
query NodeFilteringQuery {
631+
allReporters(last: 101) {
632+
edges {
633+
node {
634+
id
635+
}
636+
}
637+
}
638+
}
639+
'''
640+
641+
expected = {
642+
'allReporters': None
643+
}
644+
645+
result = schema.execute(query)
646+
assert len(result.errors) == 1
647+
assert str(result.errors[0]) == (
648+
'Requesting 101 records on the `allReporters` connection '
649+
'exceeds the `last` limit of 100 records.'
650+
)
651+
assert result.data == expected
652+
653+
graphene_settings.RELAY_CONNECTION_ENFORCE_FIRST_OR_LAST = False
654+
655+
609656
def test_should_query_promise_connectionfields():
610657
from promise import Promise
611658

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

621668
def resolve_all_reporters(self, info, **args):
622669
return Promise.resolve([Reporter(id=1)])
623-
670+
624671
schema = graphene.Schema(query=Query)
625672
query = '''
626673
query ReporterPromiseConnectionQuery {
@@ -648,6 +695,109 @@ def resolve_all_reporters(self, info, **args):
648695
assert not result.errors
649696
assert result.data == expected
650697

698+
def test_should_query_connectionfields_with_last():
699+
700+
r = Reporter.objects.create(
701+
first_name='John',
702+
last_name='Doe',
703+
email='johndoe@example.com',
704+
a_choice=1
705+
)
706+
707+
class ReporterType(DjangoObjectType):
708+
709+
class Meta:
710+
model = Reporter
711+
interfaces = (Node, )
712+
713+
class Query(graphene.ObjectType):
714+
all_reporters = DjangoConnectionField(ReporterType)
715+
716+
def resolve_all_reporters(self, info, **args):
717+
return Reporter.objects.all()
718+
719+
schema = graphene.Schema(query=Query)
720+
query = '''
721+
query ReporterLastQuery {
722+
allReporters(last: 1) {
723+
edges {
724+
node {
725+
id
726+
}
727+
}
728+
}
729+
}
730+
'''
731+
732+
expected = {
733+
'allReporters': {
734+
'edges': [{
735+
'node': {
736+
'id': 'UmVwb3J0ZXJUeXBlOjE='
737+
}
738+
}]
739+
}
740+
}
741+
742+
result = schema.execute(query)
743+
assert not result.errors
744+
assert result.data == expected
745+
746+
def test_should_query_connectionfields_with_manager():
747+
748+
r = Reporter.objects.create(
749+
first_name='John',
750+
last_name='Doe',
751+
email='johndoe@example.com',
752+
a_choice=1
753+
)
754+
755+
r = Reporter.objects.create(
756+
first_name='John',
757+
last_name='NotDoe',
758+
email='johndoe@example.com',
759+
a_choice=1
760+
)
761+
762+
class ReporterType(DjangoObjectType):
763+
764+
class Meta:
765+
model = Reporter
766+
interfaces = (Node, )
767+
768+
class Query(graphene.ObjectType):
769+
all_reporters = DjangoConnectionField(ReporterType, on='doe_objects')
770+
771+
def resolve_all_reporters(self, info, **args):
772+
return Reporter.objects.all()
773+
774+
schema = graphene.Schema(query=Query)
775+
query = '''
776+
query ReporterLastQuery {
777+
allReporters(first: 2) {
778+
edges {
779+
node {
780+
id
781+
}
782+
}
783+
}
784+
}
785+
'''
786+
787+
expected = {
788+
'allReporters': {
789+
'edges': [{
790+
'node': {
791+
'id': 'UmVwb3J0ZXJUeXBlOjE='
792+
}
793+
}]
794+
}
795+
}
796+
797+
result = schema.execute(query)
798+
assert not result.errors
799+
assert result.data == expected
800+
651801

652802
def test_should_query_dataloader_fields():
653803
from promise import Promise

0 commit comments

Comments
 (0)