4
4
import sys
5
5
import typing as T
6
6
import uuid
7
+ from typing import Optional
7
8
8
9
import graphene
9
10
import graphene .types
@@ -70,10 +71,26 @@ def test_builtin_scalars(input, expected):
70
71
assert field .default_value == input [1 ]
71
72
72
73
73
- def test_union ():
74
+ def test_union_optional ():
74
75
field = _convert_field_from_spec ("attr" , (T .Union [int , float , str ], 5.0 ))
75
- assert issubclass (field .type . of_type , graphene .Union )
76
+ assert issubclass (field .type , graphene .Union )
76
77
assert field .default_value == 5.0
78
+ assert field .type .__name__ .startswith ("UnionOf" )
79
+
80
+
81
+ @pytest .mark .parametrize (
82
+ "input" ,
83
+ [
84
+ (T .Union [int , float , str ], ...),
85
+ (T .Union [int , float , str , None ], ...),
86
+ (Optional [T .Union [int , float , str ]], ...),
87
+ (Optional [T .Union [int , float , None ]], ...),
88
+ ],
89
+ )
90
+ def test_union (input ):
91
+ field = _convert_field_from_spec ("attr" , input )
92
+ assert isinstance (field .type , graphene .NonNull )
93
+ assert field .default_value == None
77
94
assert field .type .of_type .__name__ .startswith ("UnionOf" )
78
95
79
96
@@ -95,6 +112,27 @@ def test_literal_singleton():
95
112
assert field .default_value == "literal1"
96
113
assert field .type .of_type == graphene .String
97
114
115
+ def test_union_pipe_optional ():
116
+ field = _convert_field_from_spec ("attr" , (int | float | str , 5.0 ))
117
+ assert issubclass (field .type , graphene .Union )
118
+ assert field .default_value == 5.0
119
+ assert field .type .__name__ .startswith ("UnionOf" )
120
+
121
+ @pytest .mark .parametrize (
122
+ "input" ,
123
+ [
124
+ (int | float | str , ...),
125
+ (int | float | str | None , ...),
126
+ (Optional [int | float | str ], ...),
127
+ (Optional [int | float | None ], ...),
128
+ ],
129
+ )
130
+ def test_union_pipe (input ):
131
+ field = _convert_field_from_spec ("attr" , input )
132
+ assert isinstance (field .type , graphene .NonNull )
133
+ assert field .default_value == None
134
+ assert field .type .of_type .__name__ .startswith ("UnionOf" )
135
+
98
136
99
137
def test_mapping ():
100
138
with pytest .raises (ConversionError ) as exc :
0 commit comments