|
25 | 25 | )
|
26 | 26 | from ..registry import Registry
|
27 | 27 | from ..types import DjangoObjectType
|
28 |
| -from .models import Article, Film, FilmDetails, Reporter |
| 28 | +from .models import Article, Film, FilmDetails, Reporter, TypedIntChoice, TypedStrChoice |
29 | 29 |
|
30 | 30 | # from graphene.core.types.custom_scalars import DateTime, Time, JSONString
|
31 | 31 |
|
@@ -443,35 +443,102 @@ def test_choice_enum_blank_value():
|
443 | 443 | class ReporterType(DjangoObjectType):
|
444 | 444 | class Meta:
|
445 | 445 | model = Reporter
|
446 |
| - fields = ( |
447 |
| - "first_name", |
448 |
| - "a_choice", |
449 |
| - ) |
| 446 | + fields = ("callable_choice",) |
450 | 447 |
|
451 | 448 | class Query(graphene.ObjectType):
|
452 | 449 | reporter = graphene.Field(ReporterType)
|
453 | 450 |
|
454 | 451 | def resolve_reporter(root, info):
|
455 |
| - return Reporter.objects.first() |
| 452 | + # return a model instance with blank choice field value |
| 453 | + return Reporter(callable_choice="") |
456 | 454 |
|
457 | 455 | schema = graphene.Schema(query=Query)
|
458 | 456 |
|
459 |
| - # Create model with empty choice option |
460 |
| - Reporter.objects.create( |
461 |
| - first_name="Bridget", last_name="Jones", email="[email protected]" |
462 |
| - ) |
463 |
| - |
464 | 457 | result = schema.execute(
|
465 | 458 | """
|
466 | 459 | query {
|
467 | 460 | reporter {
|
468 |
| - firstName |
469 |
| - aChoice |
| 461 | + callableChoice |
470 | 462 | }
|
471 | 463 | }
|
472 | 464 | """
|
473 | 465 | )
|
474 | 466 | assert not result.errors
|
475 | 467 | assert result.data == {
|
476 |
| - "reporter": {"firstName": "Bridget", "aChoice": None}, |
| 468 | + "reporter": {"callableChoice": None}, |
| 469 | + } |
| 470 | + |
| 471 | + |
| 472 | +def test_typed_choice_value(): |
| 473 | + """Test that typed choices fields are resolved correctly to the enum values""" |
| 474 | + |
| 475 | + class ReporterType(DjangoObjectType): |
| 476 | + class Meta: |
| 477 | + model = Reporter |
| 478 | + fields = ("typed_choice", "class_choice", "callable_choice") |
| 479 | + |
| 480 | + class Query(graphene.ObjectType): |
| 481 | + reporter = graphene.Field(ReporterType) |
| 482 | + |
| 483 | + def resolve_reporter(root, info): |
| 484 | + # assign choice values to the fields instead of their str or int values |
| 485 | + return Reporter( |
| 486 | + typed_choice=TypedIntChoice.CHOICE_THIS, |
| 487 | + class_choice=TypedIntChoice.CHOICE_THAT, |
| 488 | + callable_choice=TypedStrChoice.CHOICE_THIS, |
| 489 | + ) |
| 490 | + |
| 491 | + class CreateReporter(graphene.Mutation): |
| 492 | + reporter = graphene.Field(ReporterType) |
| 493 | + |
| 494 | + def mutate(root, info, **kwargs): |
| 495 | + return CreateReporter( |
| 496 | + reporter=Reporter( |
| 497 | + typed_choice=TypedIntChoice.CHOICE_THIS, |
| 498 | + class_choice=TypedIntChoice.CHOICE_THAT, |
| 499 | + callable_choice=TypedStrChoice.CHOICE_THIS, |
| 500 | + ), |
| 501 | + ) |
| 502 | + |
| 503 | + class Mutation(graphene.ObjectType): |
| 504 | + create_reporter = CreateReporter.Field() |
| 505 | + |
| 506 | + schema = graphene.Schema(query=Query, mutation=Mutation) |
| 507 | + |
| 508 | + reporter_fragment = """ |
| 509 | + fragment reporter on ReporterType { |
| 510 | + typedChoice |
| 511 | + classChoice |
| 512 | + callableChoice |
| 513 | + } |
| 514 | + """ |
| 515 | + |
| 516 | + expected_reporter = { |
| 517 | + "typedChoice": "A_1", |
| 518 | + "classChoice": "A_2", |
| 519 | + "callableChoice": "THIS", |
477 | 520 | }
|
| 521 | + |
| 522 | + result = schema.execute( |
| 523 | + reporter_fragment |
| 524 | + + """ |
| 525 | + query { |
| 526 | + reporter { ...reporter } |
| 527 | + } |
| 528 | + """ |
| 529 | + ) |
| 530 | + assert not result.errors |
| 531 | + assert result.data["reporter"] == expected_reporter |
| 532 | + |
| 533 | + result = schema.execute( |
| 534 | + reporter_fragment |
| 535 | + + """ |
| 536 | + mutation { |
| 537 | + createReporter { |
| 538 | + reporter { ...reporter } |
| 539 | + } |
| 540 | + } |
| 541 | + """ |
| 542 | + ) |
| 543 | + assert not result.errors |
| 544 | + assert result.data["createReporter"]["reporter"] == expected_reporter |
0 commit comments