|
| 1 | +from textwrap import dedent |
| 2 | + |
1 | 3 | from ..argument import Argument
|
2 | 4 | from ..enum import Enum, PyEnum
|
3 | 5 | from ..field import Field
|
4 | 6 | from ..inputfield import InputField
|
| 7 | +from ..inputobjecttype import InputObjectType |
| 8 | +from ..mutation import Mutation |
| 9 | +from ..scalars import String |
5 | 10 | from ..schema import ObjectType, Schema
|
6 | 11 |
|
7 | 12 |
|
@@ -224,3 +229,245 @@ class Meta:
|
224 | 229 | "GREEN": RGB1.GREEN,
|
225 | 230 | "BLUE": RGB1.BLUE,
|
226 | 231 | }
|
| 232 | + |
| 233 | + |
| 234 | +def test_enum_types(): |
| 235 | + from enum import Enum as PyEnum |
| 236 | + |
| 237 | + class Color(PyEnum): |
| 238 | + """Primary colors""" |
| 239 | + |
| 240 | + RED = 1 |
| 241 | + YELLOW = 2 |
| 242 | + BLUE = 3 |
| 243 | + |
| 244 | + GColor = Enum.from_enum(Color) |
| 245 | + |
| 246 | + class Query(ObjectType): |
| 247 | + color = GColor(required=True) |
| 248 | + |
| 249 | + def resolve_color(_, info): |
| 250 | + return Color.RED |
| 251 | + |
| 252 | + schema = Schema(query=Query) |
| 253 | + |
| 254 | + assert str(schema) == dedent( |
| 255 | + '''\ |
| 256 | + type Query { |
| 257 | + color: Color! |
| 258 | + } |
| 259 | +
|
| 260 | + """Primary colors""" |
| 261 | + enum Color { |
| 262 | + RED |
| 263 | + YELLOW |
| 264 | + BLUE |
| 265 | + } |
| 266 | + ''' |
| 267 | + ) |
| 268 | + |
| 269 | + |
| 270 | +def test_enum_resolver(): |
| 271 | + from enum import Enum as PyEnum |
| 272 | + |
| 273 | + class Color(PyEnum): |
| 274 | + RED = 1 |
| 275 | + GREEN = 2 |
| 276 | + BLUE = 3 |
| 277 | + |
| 278 | + GColor = Enum.from_enum(Color) |
| 279 | + |
| 280 | + class Query(ObjectType): |
| 281 | + color = GColor(required=True) |
| 282 | + |
| 283 | + def resolve_color(_, info): |
| 284 | + return Color.RED |
| 285 | + |
| 286 | + schema = Schema(query=Query) |
| 287 | + |
| 288 | + results = schema.execute("query { color }") |
| 289 | + assert not results.errors |
| 290 | + |
| 291 | + assert results.data["color"] == Color.RED.name |
| 292 | + |
| 293 | + |
| 294 | +def test_enum_resolver_compat(): |
| 295 | + from enum import Enum as PyEnum |
| 296 | + |
| 297 | + class Color(PyEnum): |
| 298 | + RED = 1 |
| 299 | + GREEN = 2 |
| 300 | + BLUE = 3 |
| 301 | + |
| 302 | + GColor = Enum.from_enum(Color) |
| 303 | + |
| 304 | + class Query(ObjectType): |
| 305 | + color = GColor(required=True) |
| 306 | + color_by_name = GColor(required=True) |
| 307 | + |
| 308 | + def resolve_color(_, info): |
| 309 | + return Color.RED.value |
| 310 | + |
| 311 | + def resolve_color_by_name(_, info): |
| 312 | + return Color.RED.name |
| 313 | + |
| 314 | + schema = Schema(query=Query) |
| 315 | + |
| 316 | + results = schema.execute( |
| 317 | + """query { |
| 318 | + color |
| 319 | + colorByName |
| 320 | + }""" |
| 321 | + ) |
| 322 | + assert not results.errors |
| 323 | + |
| 324 | + assert results.data["color"] == Color.RED.name |
| 325 | + assert results.data["colorByName"] == Color.RED.name |
| 326 | + |
| 327 | + |
| 328 | +def test_enum_resolver_invalid(): |
| 329 | + from enum import Enum as PyEnum |
| 330 | + |
| 331 | + class Color(PyEnum): |
| 332 | + RED = 1 |
| 333 | + GREEN = 2 |
| 334 | + BLUE = 3 |
| 335 | + |
| 336 | + GColor = Enum.from_enum(Color) |
| 337 | + |
| 338 | + class Query(ObjectType): |
| 339 | + color = GColor(required=True) |
| 340 | + |
| 341 | + def resolve_color(_, info): |
| 342 | + return "BLACK" |
| 343 | + |
| 344 | + schema = Schema(query=Query) |
| 345 | + |
| 346 | + results = schema.execute("query { color }") |
| 347 | + assert results.errors |
| 348 | + assert ( |
| 349 | + results.errors[0].message |
| 350 | + == "Expected a value of type 'Color' but received: 'BLACK'" |
| 351 | + ) |
| 352 | + |
| 353 | + |
| 354 | +def test_field_enum_argument(): |
| 355 | + class Color(Enum): |
| 356 | + RED = 1 |
| 357 | + GREEN = 2 |
| 358 | + BLUE = 3 |
| 359 | + |
| 360 | + class Brick(ObjectType): |
| 361 | + color = Color(required=True) |
| 362 | + |
| 363 | + color_filter = None |
| 364 | + |
| 365 | + class Query(ObjectType): |
| 366 | + bricks_by_color = Field(Brick, color=Color(required=True)) |
| 367 | + |
| 368 | + def resolve_bricks_by_color(_, info, color): |
| 369 | + nonlocal color_filter |
| 370 | + color_filter = color |
| 371 | + return Brick(color=color) |
| 372 | + |
| 373 | + schema = Schema(query=Query) |
| 374 | + |
| 375 | + results = schema.execute( |
| 376 | + """ |
| 377 | + query { |
| 378 | + bricksByColor(color: RED) { |
| 379 | + color |
| 380 | + } |
| 381 | + } |
| 382 | + """ |
| 383 | + ) |
| 384 | + assert not results.errors |
| 385 | + assert results.data == {"bricksByColor": {"color": "RED"}} |
| 386 | + assert color_filter == Color.RED |
| 387 | + |
| 388 | + |
| 389 | +def test_mutation_enum_input(): |
| 390 | + class RGB(Enum): |
| 391 | + """Available colors""" |
| 392 | + |
| 393 | + RED = 1 |
| 394 | + GREEN = 2 |
| 395 | + BLUE = 3 |
| 396 | + |
| 397 | + color_input = None |
| 398 | + |
| 399 | + class CreatePaint(Mutation): |
| 400 | + class Arguments: |
| 401 | + color = RGB(required=True) |
| 402 | + |
| 403 | + color = RGB(required=True) |
| 404 | + |
| 405 | + def mutate(_, info, color): |
| 406 | + nonlocal color_input |
| 407 | + color_input = color |
| 408 | + return CreatePaint(color=color) |
| 409 | + |
| 410 | + class MyMutation(ObjectType): |
| 411 | + create_paint = CreatePaint.Field() |
| 412 | + |
| 413 | + class Query(ObjectType): |
| 414 | + a = String() |
| 415 | + |
| 416 | + schema = Schema(query=Query, mutation=MyMutation) |
| 417 | + result = schema.execute( |
| 418 | + """ mutation MyMutation { |
| 419 | + createPaint(color: RED) { |
| 420 | + color |
| 421 | + } |
| 422 | + } |
| 423 | + """ |
| 424 | + ) |
| 425 | + assert not result.errors |
| 426 | + assert result.data == {"createPaint": {"color": "RED"}} |
| 427 | + |
| 428 | + assert color_input == RGB.RED |
| 429 | + |
| 430 | + |
| 431 | +def test_mutation_enum_input_type(): |
| 432 | + class RGB(Enum): |
| 433 | + """Available colors""" |
| 434 | + |
| 435 | + RED = 1 |
| 436 | + GREEN = 2 |
| 437 | + BLUE = 3 |
| 438 | + |
| 439 | + class ColorInput(InputObjectType): |
| 440 | + color = RGB(required=True) |
| 441 | + |
| 442 | + color_input_value = None |
| 443 | + |
| 444 | + class CreatePaint(Mutation): |
| 445 | + class Arguments: |
| 446 | + color_input = ColorInput(required=True) |
| 447 | + |
| 448 | + color = RGB(required=True) |
| 449 | + |
| 450 | + def mutate(_, info, color_input): |
| 451 | + nonlocal color_input_value |
| 452 | + color_input_value = color_input.color |
| 453 | + return CreatePaint(color=color_input.color) |
| 454 | + |
| 455 | + class MyMutation(ObjectType): |
| 456 | + create_paint = CreatePaint.Field() |
| 457 | + |
| 458 | + class Query(ObjectType): |
| 459 | + a = String() |
| 460 | + |
| 461 | + schema = Schema(query=Query, mutation=MyMutation) |
| 462 | + result = schema.execute( |
| 463 | + """ mutation MyMutation { |
| 464 | + createPaint(colorInput: { color: RED }) { |
| 465 | + color |
| 466 | + } |
| 467 | + } |
| 468 | + """, |
| 469 | + ) |
| 470 | + assert not result.errors |
| 471 | + assert result.data == {"createPaint": {"color": "RED"}} |
| 472 | + |
| 473 | + assert color_input_value == RGB.RED |
0 commit comments