File: test_enum_colors.py

package info (click to toggle)
python-gql 4.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,900 kB
  • sloc: python: 21,677; makefile: 54
file content (341 lines) | stat: -rw-r--r-- 7,303 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
from enum import Enum
from typing import Optional

import pytest
from graphql import (
    GraphQLArgument,
    GraphQLEnumType,
    GraphQLField,
    GraphQLList,
    GraphQLNamedType,
    GraphQLNonNull,
    GraphQLObjectType,
    GraphQLSchema,
)

from gql import Client, gql
from gql.utilities import update_schema_enum


class Color(Enum):
    RED = 0
    GREEN = 1
    BLUE = 2
    YELLOW = 3
    CYAN = 4
    MAGENTA = 5


RED = Color.RED
GREEN = Color.GREEN
BLUE = Color.BLUE
YELLOW = Color.YELLOW
CYAN = Color.CYAN
MAGENTA = Color.MAGENTA

ALL_COLORS = [c for c in Color]

ColorType = GraphQLEnumType("Color", {c.name: c for c in Color})


def resolve_opposite(_root, _info, color):
    opposite_colors = {
        RED: CYAN,
        GREEN: MAGENTA,
        BLUE: YELLOW,
        YELLOW: BLUE,
        CYAN: RED,
        MAGENTA: GREEN,
    }

    return opposite_colors[color]


def resolve_all(_root, _info):
    return ALL_COLORS


list_of_list_of_list = [[[RED, GREEN], [GREEN, BLUE]], [[YELLOW, CYAN], [MAGENTA, RED]]]


def resolve_list_of_list_of_list(_root, _info):
    return list_of_list_of_list


def resolve_list_of_list(_root, _info):
    return list_of_list_of_list[0]


def resolve_list(_root, _info):
    return list_of_list_of_list[0][0]


queryType = GraphQLObjectType(
    name="RootQueryType",
    fields={
        "all": GraphQLField(
            GraphQLList(ColorType),
            resolve=resolve_all,
        ),
        "opposite": GraphQLField(
            ColorType,
            args={"color": GraphQLArgument(ColorType)},
            resolve=resolve_opposite,
        ),
        "list_of_list_of_list": GraphQLField(
            GraphQLNonNull(
                GraphQLList(
                    GraphQLNonNull(GraphQLList(GraphQLNonNull(GraphQLList(ColorType))))
                )
            ),
            resolve=resolve_list_of_list_of_list,
        ),
        "list_of_list": GraphQLField(
            GraphQLNonNull(GraphQLList(GraphQLNonNull(GraphQLList(ColorType)))),
            resolve=resolve_list_of_list,
        ),
        "list": GraphQLField(
            GraphQLNonNull(GraphQLList(ColorType)),
            resolve=resolve_list,
        ),
    },
)

schema = GraphQLSchema(query=queryType)


def test_parse_value_enum():

    result = ColorType.parse_value("RED")

    print(result)

    assert isinstance(result, Color)
    assert result is RED


def test_serialize_enum():

    result = ColorType.serialize(RED)

    print(result)

    assert result == "RED"


def test_get_all_colors():

    query = gql("{all}")

    client = Client(schema=schema, parse_results=True)

    result = client.execute(query)

    print(result)

    all_colors = result["all"]

    assert all_colors == ALL_COLORS


def test_opposite_color_literal():

    client = Client(schema=schema, parse_results=True)

    query = gql("{opposite(color: RED)}")

    result = client.execute(query)

    print(result)

    opposite_color = result["opposite"]

    assert isinstance(opposite_color, Color)
    assert opposite_color == CYAN


def test_opposite_color_variable_serialized_manually():

    client = Client(schema=schema, parse_results=True)

    query = gql(
        """
        query GetOppositeColor($color: Color) {
            opposite(color:$color)
        }"""
    )

    query.variable_values = {
        "color": "RED",
    }

    result = client.execute(query)

    print(result)

    opposite_color = result["opposite"]

    assert isinstance(opposite_color, Color)
    assert opposite_color == CYAN


def test_opposite_color_variable_serialized_by_gql():

    client = Client(schema=schema, parse_results=True)

    query = gql(
        """
        query GetOppositeColor($color: Color) {
            opposite(color:$color)
        }"""
    )

    query.variable_values = {
        "color": RED,
    }

    result = client.execute(query, serialize_variables=True)

    print(result)

    opposite_color = result["opposite"]

    assert isinstance(opposite_color, Color)
    assert opposite_color == CYAN


def test_list():

    query = gql("{list}")

    client = Client(schema=schema, parse_results=True)

    result = client.execute(query)

    print(result)

    big_list = result["list"]

    assert big_list == list_of_list_of_list[0][0]


def test_list_of_list():

    query = gql("{list_of_list}")

    client = Client(schema=schema, parse_results=True)

    result = client.execute(query)

    print(result)

    big_list = result["list_of_list"]

    assert big_list == list_of_list_of_list[0]


def test_list_of_list_of_list():

    query = gql("{list_of_list_of_list}")

    client = Client(schema=schema, parse_results=True)

    result = client.execute(query)

    print(result)

    big_list = result["list_of_list_of_list"]

    assert big_list == list_of_list_of_list


def test_update_schema_enum():

    color_type: Optional[GraphQLNamedType]

    color_type = schema.get_type("Color")
    assert isinstance(color_type, GraphQLEnumType)
    assert color_type is not None
    assert color_type.parse_value("RED") == Color.RED

    # Using values

    update_schema_enum(schema, "Color", Color, use_enum_values=True)

    color_type = schema.get_type("Color")
    assert isinstance(color_type, GraphQLEnumType)
    assert color_type is not None
    assert color_type.parse_value("RED") == 0
    assert color_type.serialize(1) == "GREEN"

    update_schema_enum(schema, "Color", Color)

    color_type = schema.get_type("Color")
    assert isinstance(color_type, GraphQLEnumType)
    assert color_type is not None
    assert color_type.parse_value("RED") == Color.RED
    assert color_type.serialize(Color.RED) == "RED"


def test_update_schema_enum_errors():

    with pytest.raises(KeyError) as exc_info:
        update_schema_enum(schema, "Corlo", Color)

    assert "Enum Corlo not found in schema!" in str(exc_info)

    with pytest.raises(TypeError) as exc_info2:
        update_schema_enum(schema, "Color", 6)  # type: ignore

    assert "Invalid type for enum values: " in str(exc_info2)

    with pytest.raises(TypeError) as exc_info3:
        update_schema_enum(schema, "RootQueryType", Color)

    assert 'The type "RootQueryType" is not a GraphQLEnumType, it is a' in str(
        exc_info3
    )

    with pytest.raises(KeyError) as exc_info4:
        update_schema_enum(schema, "Color", {"RED": Color.RED})

    assert 'Enum key "GREEN" not found in provided values!' in str(exc_info4)


def test_parse_results_with_operation_type():

    client = Client(schema=schema, parse_results=True)

    query = gql(
        """
        query GetAll {
            all
        }
        query GetOppositeColor($color: Color) {
            opposite(color:$color)
        }
        query GetOppositeColor2($color: Color) {
            other_opposite:opposite(color:$color)
        }
        query GetOppositeColor3 {
            opposite(color: YELLOW)
        }
        query GetListOfListOfList {
            list_of_list_of_list
        }
        """
    )

    query.variable_values = {
        "color": "RED",
    }
    query.operation_name = "GetOppositeColor"

    result = client.execute(query)

    print(result)

    opposite_color = result["opposite"]

    assert isinstance(opposite_color, Color)
    assert opposite_color == CYAN