mirror of
https://github.com/graphql-python/graphene.git
synced 2025-07-22 13:59:51 +03:00
Clean up tests
This commit is contained in:
parent
1ce5a97bd5
commit
4ac2bd25e4
|
@ -350,98 +350,221 @@ def test_enum_resolver_invalid():
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def test_enum_mutation():
|
def test_field_enum_argument():
|
||||||
from enum import Enum as PyEnum
|
class Color(Enum):
|
||||||
|
|
||||||
class Color(PyEnum):
|
|
||||||
RED = 1
|
RED = 1
|
||||||
GREEN = 2
|
GREEN = 2
|
||||||
BLUE = 3
|
BLUE = 3
|
||||||
|
|
||||||
GColor = Enum.from_enum(Color)
|
class Brick(ObjectType):
|
||||||
|
color = Color(required=True)
|
||||||
|
|
||||||
my_fav_color = None
|
color_filter = None
|
||||||
|
|
||||||
class Query(ObjectType):
|
class Query(ObjectType):
|
||||||
fav_color = GColor(required=True)
|
bricks_by_color = Field(Brick, color=Color(required=True))
|
||||||
|
|
||||||
def resolve_fav_color(_, info):
|
def resolve_bricks_by_color(_, info, color):
|
||||||
return my_fav_color
|
nonlocal color_filter
|
||||||
|
color_filter = color
|
||||||
|
return Brick(color=color)
|
||||||
|
|
||||||
class SetFavColor(Mutation):
|
schema = Schema(query=Query)
|
||||||
class Arguments:
|
|
||||||
fav_color = Argument(GColor, required=True)
|
|
||||||
|
|
||||||
Output = Query
|
|
||||||
|
|
||||||
def mutate(self, info, fav_color):
|
|
||||||
nonlocal my_fav_color
|
|
||||||
my_fav_color = fav_color
|
|
||||||
return Query()
|
|
||||||
|
|
||||||
class MyMutations(ObjectType):
|
|
||||||
set_fav_color = SetFavColor.Field()
|
|
||||||
|
|
||||||
schema = Schema(query=Query, mutation=MyMutations)
|
|
||||||
|
|
||||||
results = schema.execute(
|
results = schema.execute(
|
||||||
"""mutation {
|
"""
|
||||||
setFavColor(favColor: RED) {
|
query {
|
||||||
favColor
|
bricksByColor(color: RED) {
|
||||||
|
color
|
||||||
}
|
}
|
||||||
}"""
|
}
|
||||||
|
"""
|
||||||
)
|
)
|
||||||
assert not results.errors
|
assert not results.errors
|
||||||
|
assert results.data == {"bricksByColor": {"color": "RED"}}
|
||||||
assert my_fav_color == Color.RED
|
assert color_filter == Color.RED
|
||||||
|
|
||||||
assert results.data["setFavColor"]["favColor"] == Color.RED.name
|
|
||||||
|
|
||||||
|
|
||||||
def test_enum_mutation_compat():
|
def test_mutation_enum_input():
|
||||||
from enum import Enum as PyEnum
|
class RGB(Enum):
|
||||||
|
"""Available colors"""
|
||||||
|
|
||||||
class Color(PyEnum):
|
|
||||||
RED = 1
|
RED = 1
|
||||||
GREEN = 2
|
GREEN = 2
|
||||||
BLUE = 3
|
BLUE = 3
|
||||||
|
|
||||||
GColor = Enum.from_enum(Color)
|
color_input = None
|
||||||
|
|
||||||
my_fav_color = None
|
class CreatePaint(Mutation):
|
||||||
|
class Arguments:
|
||||||
|
color = RGB(required=True)
|
||||||
|
|
||||||
|
color = RGB(required=True)
|
||||||
|
|
||||||
|
def mutate(_, info, color):
|
||||||
|
nonlocal color_input
|
||||||
|
color_input = color
|
||||||
|
return CreatePaint(color=color)
|
||||||
|
|
||||||
|
class MyMutation(ObjectType):
|
||||||
|
create_paint = CreatePaint.Field()
|
||||||
|
|
||||||
class Query(ObjectType):
|
class Query(ObjectType):
|
||||||
fav_color = GColor(required=True)
|
a = String()
|
||||||
|
|
||||||
def resolve_fav_color(_, info):
|
schema = Schema(query=Query, mutation=MyMutation)
|
||||||
return my_fav_color
|
result = schema.execute(
|
||||||
|
""" mutation MyMutation {
|
||||||
|
createPaint(color: RED) {
|
||||||
|
color
|
||||||
|
}
|
||||||
|
}
|
||||||
|
"""
|
||||||
|
)
|
||||||
|
assert not result.errors
|
||||||
|
assert result.data == {"createPaint": {"color": "RED"}}
|
||||||
|
|
||||||
class SetFavColor(Mutation):
|
assert color_input == RGB.RED
|
||||||
|
|
||||||
|
|
||||||
|
def test_mutation_enum_input_type():
|
||||||
|
class RGB(Enum):
|
||||||
|
"""Available colors"""
|
||||||
|
|
||||||
|
RED = 1
|
||||||
|
GREEN = 2
|
||||||
|
BLUE = 3
|
||||||
|
|
||||||
|
class ColorInput(InputObjectType):
|
||||||
|
color = RGB(required=True)
|
||||||
|
|
||||||
|
color_input_value = None
|
||||||
|
|
||||||
|
class CreatePaint(Mutation):
|
||||||
class Arguments:
|
class Arguments:
|
||||||
fav_color = Argument(GColor, required=True)
|
color_input = ColorInput(required=True)
|
||||||
|
|
||||||
Output = Query
|
color = RGB(required=True)
|
||||||
|
|
||||||
def mutate(self, info, fav_color):
|
def mutate(_, info, color_input):
|
||||||
nonlocal my_fav_color
|
nonlocal color_input_value
|
||||||
my_fav_color = fav_color
|
color_input_value = color_input.color
|
||||||
return Query()
|
return CreatePaint(color=color_input.color)
|
||||||
|
|
||||||
class MyMutations(ObjectType):
|
class MyMutation(ObjectType):
|
||||||
set_fav_color = SetFavColor.Field()
|
create_paint = CreatePaint.Field()
|
||||||
|
|
||||||
schema = Schema(query=Query, mutation=MyMutations)
|
class Query(ObjectType):
|
||||||
|
a = String()
|
||||||
|
|
||||||
results = schema.execute(
|
schema = Schema(query=Query, mutation=MyMutation)
|
||||||
"""mutation {
|
result = schema.execute(
|
||||||
setFavColor(favColor: RED) {
|
""" mutation MyMutation {
|
||||||
favColor
|
createPaint(colorInput: { color: RED }) {
|
||||||
}
|
color
|
||||||
}""",
|
}
|
||||||
|
}
|
||||||
|
""",
|
||||||
|
)
|
||||||
|
assert not result.errors
|
||||||
|
assert result.data == {"createPaint": {"color": "RED"}}
|
||||||
|
|
||||||
|
assert color_input_value == RGB.RED
|
||||||
|
|
||||||
|
|
||||||
|
def test_mutation_enum_input_compatability_middleware():
|
||||||
|
"""Test the `enum_value_convertor_middleware`"""
|
||||||
|
|
||||||
|
class RGB(Enum):
|
||||||
|
"""Available colors"""
|
||||||
|
|
||||||
|
RED = 1
|
||||||
|
GREEN = 2
|
||||||
|
BLUE = 3
|
||||||
|
|
||||||
|
color_input = None
|
||||||
|
|
||||||
|
class CreatePaint(Mutation):
|
||||||
|
class Arguments:
|
||||||
|
color = RGB(required=True)
|
||||||
|
|
||||||
|
color = RGB(required=True)
|
||||||
|
|
||||||
|
def mutate(_, info, color):
|
||||||
|
nonlocal color_input
|
||||||
|
color_input = color
|
||||||
|
return CreatePaint(color=color)
|
||||||
|
|
||||||
|
class MyMutation(ObjectType):
|
||||||
|
create_paint = CreatePaint.Field()
|
||||||
|
|
||||||
|
class Query(ObjectType):
|
||||||
|
a = String()
|
||||||
|
|
||||||
|
schema = Schema(query=Query, mutation=MyMutation)
|
||||||
|
result = schema.execute(
|
||||||
|
""" mutation MyMutation {
|
||||||
|
createPaint(color: RED) {
|
||||||
|
color
|
||||||
|
}
|
||||||
|
}
|
||||||
|
""",
|
||||||
middleware=[enum_value_convertor_middleware],
|
middleware=[enum_value_convertor_middleware],
|
||||||
)
|
)
|
||||||
assert not results.errors
|
assert not result.errors
|
||||||
|
assert result.data == {"createPaint": {"color": "RED"}}
|
||||||
|
|
||||||
assert my_fav_color == Color.RED.value
|
assert color_input == 1
|
||||||
|
assert type(color_input) == int
|
||||||
|
|
||||||
assert results.data["setFavColor"]["favColor"] == Color.RED.name
|
|
||||||
|
def test_mutation_enum_input_compatability_middleware_input_type():
|
||||||
|
"""Test the `enum_value_convertor_middleware`"""
|
||||||
|
|
||||||
|
class RGB(Enum):
|
||||||
|
"""Available colors"""
|
||||||
|
|
||||||
|
RED = 1
|
||||||
|
GREEN = 2
|
||||||
|
BLUE = 3
|
||||||
|
|
||||||
|
class SecondColorInput(InputObjectType):
|
||||||
|
color = RGB(required=True)
|
||||||
|
|
||||||
|
class ColorInput(InputObjectType):
|
||||||
|
color_input = SecondColorInput(required=True)
|
||||||
|
|
||||||
|
color_input_value = None
|
||||||
|
|
||||||
|
class CreatePaint(Mutation):
|
||||||
|
class Arguments:
|
||||||
|
color_input = ColorInput(required=True)
|
||||||
|
|
||||||
|
color = RGB(required=True)
|
||||||
|
|
||||||
|
def mutate(_, info, color_input):
|
||||||
|
nonlocal color_input_value
|
||||||
|
color_input_value = color_input.color_input.color
|
||||||
|
return CreatePaint(color=color_input_value)
|
||||||
|
|
||||||
|
class MyMutation(ObjectType):
|
||||||
|
create_paint = CreatePaint.Field()
|
||||||
|
|
||||||
|
class Query(ObjectType):
|
||||||
|
a = String()
|
||||||
|
|
||||||
|
schema = Schema(query=Query, mutation=MyMutation)
|
||||||
|
result = schema.execute(
|
||||||
|
""" mutation MyMutation {
|
||||||
|
createPaint(colorInput: { colorInput: { color: RED } }) {
|
||||||
|
color
|
||||||
|
}
|
||||||
|
}
|
||||||
|
""",
|
||||||
|
middleware=[enum_value_convertor_middleware],
|
||||||
|
)
|
||||||
|
assert not result.errors
|
||||||
|
assert result.data == {"createPaint": {"color": "RED"}}
|
||||||
|
|
||||||
|
assert color_input_value == 1
|
||||||
|
assert type(color_input_value) == int
|
||||||
|
|
|
@ -8,9 +8,6 @@ from ..scalars import String
|
||||||
from ..schema import Schema
|
from ..schema import Schema
|
||||||
from ..structures import NonNull
|
from ..structures import NonNull
|
||||||
from ..interface import Interface
|
from ..interface import Interface
|
||||||
from ..enum import Enum
|
|
||||||
from ..inputobjecttype import InputObjectType
|
|
||||||
from graphene.compat.middleware import enum_value_convertor_middleware
|
|
||||||
|
|
||||||
|
|
||||||
class MyType(Interface):
|
class MyType(Interface):
|
||||||
|
@ -221,188 +218,3 @@ def test_mutation_as_subclass():
|
||||||
)
|
)
|
||||||
assert not result.errors
|
assert not result.errors
|
||||||
assert result.data == {"createUserWithPlanet": {"name": "Peter", "planet": "earth"}}
|
assert result.data == {"createUserWithPlanet": {"name": "Peter", "planet": "earth"}}
|
||||||
|
|
||||||
|
|
||||||
def test_mutation_enum_input():
|
|
||||||
class RGB(Enum):
|
|
||||||
"""Available colors"""
|
|
||||||
|
|
||||||
RED = 1
|
|
||||||
GREEN = 2
|
|
||||||
BLUE = 3
|
|
||||||
|
|
||||||
color_input = None
|
|
||||||
|
|
||||||
class CreatePaint(Mutation):
|
|
||||||
class Arguments:
|
|
||||||
color = RGB(required=True)
|
|
||||||
|
|
||||||
color = RGB(required=True)
|
|
||||||
|
|
||||||
def mutate(_, info, color):
|
|
||||||
nonlocal color_input
|
|
||||||
color_input = color
|
|
||||||
return CreatePaint(color=color)
|
|
||||||
|
|
||||||
class MyMutation(ObjectType):
|
|
||||||
create_paint = CreatePaint.Field()
|
|
||||||
|
|
||||||
class Query(ObjectType):
|
|
||||||
a = String()
|
|
||||||
|
|
||||||
schema = Schema(query=Query, mutation=MyMutation)
|
|
||||||
result = schema.execute(
|
|
||||||
""" mutation MyMutation {
|
|
||||||
createPaint(color: RED) {
|
|
||||||
color
|
|
||||||
}
|
|
||||||
}
|
|
||||||
"""
|
|
||||||
)
|
|
||||||
assert not result.errors
|
|
||||||
assert result.data == {"createPaint": {"color": "RED"}}
|
|
||||||
|
|
||||||
assert color_input == RGB.RED
|
|
||||||
|
|
||||||
|
|
||||||
def test_mutation_enum_input_type():
|
|
||||||
class RGB(Enum):
|
|
||||||
"""Available colors"""
|
|
||||||
|
|
||||||
RED = 1
|
|
||||||
GREEN = 2
|
|
||||||
BLUE = 3
|
|
||||||
|
|
||||||
class ColorInput(InputObjectType):
|
|
||||||
color = RGB(required=True)
|
|
||||||
|
|
||||||
color_input_value = None
|
|
||||||
|
|
||||||
class CreatePaint(Mutation):
|
|
||||||
class Arguments:
|
|
||||||
color_input = ColorInput(required=True)
|
|
||||||
|
|
||||||
color = RGB(required=True)
|
|
||||||
|
|
||||||
def mutate(_, info, color_input):
|
|
||||||
nonlocal color_input_value
|
|
||||||
color_input_value = color_input.color
|
|
||||||
return CreatePaint(color=color_input.color)
|
|
||||||
|
|
||||||
class MyMutation(ObjectType):
|
|
||||||
create_paint = CreatePaint.Field()
|
|
||||||
|
|
||||||
class Query(ObjectType):
|
|
||||||
a = String()
|
|
||||||
|
|
||||||
schema = Schema(query=Query, mutation=MyMutation)
|
|
||||||
result = schema.execute(
|
|
||||||
""" mutation MyMutation {
|
|
||||||
createPaint(colorInput: { color: RED }) {
|
|
||||||
color
|
|
||||||
}
|
|
||||||
}
|
|
||||||
""",
|
|
||||||
)
|
|
||||||
assert not result.errors
|
|
||||||
assert result.data == {"createPaint": {"color": "RED"}}
|
|
||||||
|
|
||||||
assert color_input_value == RGB.RED
|
|
||||||
|
|
||||||
|
|
||||||
def test_mutation_enum_input_compatability_middleware():
|
|
||||||
"""Test the `enum_value_convertor_middleware`"""
|
|
||||||
|
|
||||||
class RGB(Enum):
|
|
||||||
"""Available colors"""
|
|
||||||
|
|
||||||
RED = 1
|
|
||||||
GREEN = 2
|
|
||||||
BLUE = 3
|
|
||||||
|
|
||||||
color_input = None
|
|
||||||
|
|
||||||
class CreatePaint(Mutation):
|
|
||||||
class Arguments:
|
|
||||||
color = RGB(required=True)
|
|
||||||
|
|
||||||
color = RGB(required=True)
|
|
||||||
|
|
||||||
def mutate(_, info, color):
|
|
||||||
nonlocal color_input
|
|
||||||
color_input = color
|
|
||||||
return CreatePaint(color=color)
|
|
||||||
|
|
||||||
class MyMutation(ObjectType):
|
|
||||||
create_paint = CreatePaint.Field()
|
|
||||||
|
|
||||||
class Query(ObjectType):
|
|
||||||
a = String()
|
|
||||||
|
|
||||||
schema = Schema(query=Query, mutation=MyMutation)
|
|
||||||
result = schema.execute(
|
|
||||||
""" mutation MyMutation {
|
|
||||||
createPaint(color: RED) {
|
|
||||||
color
|
|
||||||
}
|
|
||||||
}
|
|
||||||
""",
|
|
||||||
middleware=[enum_value_convertor_middleware],
|
|
||||||
)
|
|
||||||
assert not result.errors
|
|
||||||
assert result.data == {"createPaint": {"color": "RED"}}
|
|
||||||
|
|
||||||
assert color_input == 1
|
|
||||||
assert type(color_input) == int
|
|
||||||
|
|
||||||
|
|
||||||
def test_mutation_enum_input_compatability_middleware_input_type():
|
|
||||||
"""Test the `enum_value_convertor_middleware`"""
|
|
||||||
|
|
||||||
class RGB(Enum):
|
|
||||||
"""Available colors"""
|
|
||||||
|
|
||||||
RED = 1
|
|
||||||
GREEN = 2
|
|
||||||
BLUE = 3
|
|
||||||
|
|
||||||
class SecondColorInput(InputObjectType):
|
|
||||||
color = RGB(required=True)
|
|
||||||
|
|
||||||
class ColorInput(InputObjectType):
|
|
||||||
color_input = SecondColorInput(required=True)
|
|
||||||
|
|
||||||
color_input_value = None
|
|
||||||
|
|
||||||
class CreatePaint(Mutation):
|
|
||||||
class Arguments:
|
|
||||||
color_input = ColorInput(required=True)
|
|
||||||
|
|
||||||
color = RGB(required=True)
|
|
||||||
|
|
||||||
def mutate(_, info, color_input):
|
|
||||||
nonlocal color_input_value
|
|
||||||
color_input_value = color_input.color_input.color
|
|
||||||
return CreatePaint(color=color_input_value)
|
|
||||||
|
|
||||||
class MyMutation(ObjectType):
|
|
||||||
create_paint = CreatePaint.Field()
|
|
||||||
|
|
||||||
class Query(ObjectType):
|
|
||||||
a = String()
|
|
||||||
|
|
||||||
schema = Schema(query=Query, mutation=MyMutation)
|
|
||||||
result = schema.execute(
|
|
||||||
""" mutation MyMutation {
|
|
||||||
createPaint(colorInput: { colorInput: { color: RED } }) {
|
|
||||||
color
|
|
||||||
}
|
|
||||||
}
|
|
||||||
""",
|
|
||||||
middleware=[enum_value_convertor_middleware],
|
|
||||||
)
|
|
||||||
assert not result.errors
|
|
||||||
assert result.data == {"createPaint": {"color": "RED"}}
|
|
||||||
|
|
||||||
assert color_input_value == 1
|
|
||||||
assert type(color_input_value) == int
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user