1
+ import base64
1
2
import datetime
2
3
3
4
import pytest
7
8
8
9
from django .db .models import Q
9
10
11
+ from graphql_relay import to_global_id
10
12
import graphene
11
13
from graphene .relay import Node
12
14
@@ -951,8 +953,7 @@ class Query(graphene.ObjectType):
951
953
952
954
def test_proxy_model_support ():
953
955
"""
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.
956
957
"""
957
958
958
959
class ReporterType (DjangoObjectType ):
@@ -961,11 +962,17 @@ class Meta:
961
962
interfaces = (Node ,)
962
963
use_connection = True
963
964
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 (
965
972
first_name = "John" , last_name = "Doe" , email = "johndoe@example.com" , a_choice = 1
966
973
)
967
974
968
- reporter_2 = CNNReporter .objects .create (
975
+ cnn_reporter = CNNReporter .objects .create (
969
976
first_name = "Some" ,
970
977
last_name = "Guy" ,
971
978
email = "someguy@cnn.com" ,
@@ -975,6 +982,7 @@ class Meta:
975
982
976
983
class Query (graphene .ObjectType ):
977
984
all_reporters = DjangoConnectionField (ReporterType )
985
+ cnn_reporters = DjangoConnectionField (CNNReporterType )
978
986
979
987
schema = graphene .Schema (query = Query )
980
988
query = """
@@ -986,63 +994,7 @@ class Query(graphene.ObjectType):
986
994
}
987
995
}
988
996
}
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 = "johndoe@example.com" , a_choice = 1
1029
- )
1030
-
1031
- reporter_2 = CNNReporter .objects .create (
1032
- first_name = "Some" ,
1033
- last_name = "Guy" ,
1034
- email = "someguy@cnn.com" ,
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 {
1046
998
edges {
1047
999
node {
1048
1000
id
@@ -1055,15 +1007,21 @@ class Query(graphene.ObjectType):
1055
1007
expected = {
1056
1008
"allReporters" : {
1057
1009
"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 )}}
1060
1017
]
1061
1018
}
1062
1019
}
1063
1020
1064
1021
result = schema .execute (query )
1065
- assert result .errors
1066
-
1022
+ assert not result .errors
1023
+ assert result .data == expected
1024
+
1067
1025
1068
1026
def test_should_resolve_get_queryset_connectionfields ():
1069
1027
reporter_1 = Reporter .objects .create (
0 commit comments