1
+ import base64
1
2
import datetime
2
3
3
4
import pytest
@@ -895,8 +896,7 @@ class Query(graphene.ObjectType):
895
896
896
897
def test_proxy_model_support ():
897
898
"""
898
- This test asserts that we can query for all Reporters,
899
- even if some are of a proxy model type at runtime.
899
+ This test asserts that we can query for all Reporters and proxied Reporters.
900
900
"""
901
901
902
902
class ReporterType (DjangoObjectType ):
@@ -905,11 +905,17 @@ class Meta:
905
905
interfaces = (Node ,)
906
906
use_connection = True
907
907
908
- reporter_1 = Reporter .objects .create (
908
+ class CNNReporterType (DjangoObjectType ):
909
+ class Meta :
910
+ model = CNNReporter
911
+ interfaces = (Node ,)
912
+ use_connection = True
913
+
914
+ reporter = Reporter .objects .create (
909
915
first_name = "John" ,
last_name = "Doe" ,
email = "[email protected] " ,
a_choice = 1
910
916
)
911
917
912
- reporter_2 = CNNReporter .objects .create (
918
+ cnn_reporter = CNNReporter .objects .create (
913
919
first_name = "Some" ,
914
920
last_name = "Guy" ,
915
921
@@ -919,6 +925,7 @@ class Meta:
919
925
920
926
class Query (graphene .ObjectType ):
921
927
all_reporters = DjangoConnectionField (ReporterType )
928
+ cnn_reporters = DjangoConnectionField (CNNReporterType )
922
929
923
930
schema = graphene .Schema (query = Query )
924
931
query = """
@@ -930,63 +937,7 @@ class Query(graphene.ObjectType):
930
937
}
931
938
}
932
939
}
933
- }
934
- """
935
-
936
- expected = {
937
- "allReporters" : {
938
- "edges" : [
939
- {"node" : {"id" : "UmVwb3J0ZXJUeXBlOjE=" }},
940
- {"node" : {"id" : "UmVwb3J0ZXJUeXBlOjI=" }},
941
- ]
942
- }
943
- }
944
-
945
- result = schema .execute (query )
946
- assert not result .errors
947
- assert result .data == expected
948
-
949
-
950
- def test_proxy_model_fails ():
951
- """
952
- This test asserts that if you try to query for a proxy model,
953
- that query will fail with:
954
- GraphQLError('Expected value of type "CNNReporterType" but got:
955
- CNNReporter.',)
956
-
957
- This is because a proxy model has the identical model definition
958
- to its superclass, and defines its behavior at runtime, rather than
959
- at the database level. Currently, filtering objects of the proxy models'
960
- type isn't supported. It would require a field on the model that would
961
- represent the type, and it doesn't seem like there is a clear way to
962
- enforce this pattern across all projects
963
- """
964
-
965
- class CNNReporterType (DjangoObjectType ):
966
- class Meta :
967
- model = CNNReporter
968
- interfaces = (Node ,)
969
- use_connection = True
970
-
971
- reporter_1 = Reporter .objects .create (
972
- first_name = "John" ,
last_name = "Doe" ,
email = "[email protected] " ,
a_choice = 1
973
- )
974
-
975
- reporter_2 = CNNReporter .objects .create (
976
- first_name = "Some" ,
977
- last_name = "Guy" ,
978
-
979
- a_choice = 1 ,
980
- reporter_type = 2 , # set this guy to be CNN
981
- )
982
-
983
- class Query (graphene .ObjectType ):
984
- all_reporters = DjangoConnectionField (CNNReporterType )
985
-
986
- schema = graphene .Schema (query = Query )
987
- query = """
988
- query ProxyModelQuery {
989
- allReporters {
940
+ cnnReporters {
990
941
edges {
991
942
node {
992
943
id
@@ -999,11 +950,17 @@ class Query(graphene.ObjectType):
999
950
expected = {
1000
951
"allReporters" : {
1001
952
"edges" : [
1002
- {"node" : {"id" : "UmVwb3J0ZXJUeXBlOjE=" }},
1003
- {"node" : {"id" : "UmVwb3J0ZXJUeXBlOjI=" }},
953
+ {"node" : {"id" : base64 .b64encode ("ReporterType:{}" .format (reporter .id ))}},
954
+ {"node" : {"id" : base64 .b64encode ("ReporterType:{}" .format (cnn_reporter .id ))}},
955
+ ]
956
+ },
957
+ "cnnReporters" : {
958
+ "edges" : [
959
+ {"node" : {"id" : base64 .b64encode ("CNNReporterType:{}" .format (cnn_reporter .id ))}}
1004
960
]
1005
961
}
1006
962
}
1007
963
1008
964
result = schema .execute (query )
1009
- assert result .errors
965
+ assert not result .errors
966
+ assert result .data == expected
0 commit comments