1+ import base64
12import datetime
23
34import pytest
78
89from django .db .models import Q
910
11+ from graphql_relay import to_global_id
1012import graphene
1113from graphene .relay import Node
1214
@@ -951,8 +953,7 @@ class Query(graphene.ObjectType):
951953
952954def test_proxy_model_support ():
953955 """
954- This test asserts that we can query for all Reporters,
955- even if some are of a proxy model type at runtime.
956+ This test asserts that we can query for all Reporters and proxied Reporters.
956957 """
957958
958959 class ReporterType (DjangoObjectType ):
@@ -961,11 +962,17 @@ class Meta:
961962 interfaces = (Node ,)
962963 use_connection = True
963964
964- reporter_1 = Reporter .objects .create (
965+ class CNNReporterType (DjangoObjectType ):
966+ class Meta :
967+ model = CNNReporter
968+ interfaces = (Node ,)
969+ use_connection = True
970+
971+ reporter = Reporter .objects .create (
965972 first_name = "John" ,
last_name = "Doe" ,
email = "[email protected] " ,
a_choice = 1 966973 )
967974
968- reporter_2 = CNNReporter .objects .create (
975+ cnn_reporter = CNNReporter .objects .create (
969976 first_name = "Some" ,
970977 last_name = "Guy" ,
971978@@ -975,6 +982,7 @@ class Meta:
975982
976983 class Query (graphene .ObjectType ):
977984 all_reporters = DjangoConnectionField (ReporterType )
985+ cnn_reporters = DjangoConnectionField (CNNReporterType )
978986
979987 schema = graphene .Schema (query = Query )
980988 query = """
@@ -986,63 +994,7 @@ class Query(graphene.ObjectType):
986994 }
987995 }
988996 }
989- }
990- """
991-
992- expected = {
993- "allReporters" : {
994- "edges" : [
995- {"node" : {"id" : "UmVwb3J0ZXJUeXBlOjE=" }},
996- {"node" : {"id" : "UmVwb3J0ZXJUeXBlOjI=" }},
997- ]
998- }
999- }
1000-
1001- result = schema .execute (query )
1002- assert not result .errors
1003- assert result .data == expected
1004-
1005-
1006- def test_proxy_model_fails ():
1007- """
1008- This test asserts that if you try to query for a proxy model,
1009- that query will fail with:
1010- GraphQLError('Expected value of type "CNNReporterType" but got:
1011- CNNReporter.',)
1012-
1013- This is because a proxy model has the identical model definition
1014- to its superclass, and defines its behavior at runtime, rather than
1015- at the database level. Currently, filtering objects of the proxy models'
1016- type isn't supported. It would require a field on the model that would
1017- represent the type, and it doesn't seem like there is a clear way to
1018- enforce this pattern across all projects
1019- """
1020-
1021- class CNNReporterType (DjangoObjectType ):
1022- class Meta :
1023- model = CNNReporter
1024- interfaces = (Node ,)
1025- use_connection = True
1026-
1027- reporter_1 = Reporter .objects .create (
1028- first_name = "John" ,
last_name = "Doe" ,
email = "[email protected] " ,
a_choice = 1 1029- )
1030-
1031- reporter_2 = CNNReporter .objects .create (
1032- first_name = "Some" ,
1033- last_name = "Guy" ,
1034- 1035- a_choice = 1 ,
1036- reporter_type = 2 , # set this guy to be CNN
1037- )
1038-
1039- class Query (graphene .ObjectType ):
1040- all_reporters = DjangoConnectionField (CNNReporterType )
1041-
1042- schema = graphene .Schema (query = Query )
1043- query = """
1044- query ProxyModelQuery {
1045- allReporters {
997+ cnnReporters {
1046998 edges {
1047999 node {
10481000 id
@@ -1055,15 +1007,21 @@ class Query(graphene.ObjectType):
10551007 expected = {
10561008 "allReporters" : {
10571009 "edges" : [
1058- {"node" : {"id" : "UmVwb3J0ZXJUeXBlOjE=" }},
1059- {"node" : {"id" : "UmVwb3J0ZXJUeXBlOjI=" }},
1010+ {"node" : {"id" : to_global_id ("ReporterType" , reporter .id )}},
1011+ {"node" : {"id" : to_global_id ("ReporterType" , cnn_reporter .id )}},
1012+ ]
1013+ },
1014+ "cnnReporters" : {
1015+ "edges" : [
1016+ {"node" : {"id" : to_global_id ("CNNReporterType" , cnn_reporter .id )}}
10601017 ]
10611018 }
10621019 }
10631020
10641021 result = schema .execute (query )
1065- assert result .errors
1066-
1022+ assert not result .errors
1023+ assert result .data == expected
1024+
10671025
10681026def test_should_resolve_get_queryset_connectionfields ():
10691027 reporter_1 = Reporter .objects .create (
0 commit comments