diff --git a/examples/starwars/data.py b/examples/starwars/data.py index 6c68b85c..ed65ee85 100644 --- a/examples/starwars/data.py +++ b/examples/starwars/data.py @@ -3,14 +3,14 @@ droid_data = {} def setup(): - from .schema import Human, Droid + from .schema import Human, Droid, Episode global human_data, droid_data luke = Human( id="1000", name="Luke Skywalker", friends=["1002", "1003", "2000", "2001"], - appears_in=[4, 5, 6], + appears_in=[Episode.NEWHOPE, Episode.EMPIRE, Episode.JEDI], home_planet="Tatooine", ) @@ -18,7 +18,7 @@ def setup(): id="1001", name="Darth Vader", friends=["1004"], - appears_in=[4, 5, 6], + appears_in=[Episode.NEWHOPE, Episode.EMPIRE, Episode.JEDI], home_planet="Tatooine", ) @@ -26,7 +26,7 @@ def setup(): id="1002", name="Han Solo", friends=["1000", "1003", "2001"], - appears_in=[4, 5, 6], + appears_in=[Episode.NEWHOPE, Episode.EMPIRE, Episode.JEDI], home_planet=None, ) @@ -34,7 +34,7 @@ def setup(): id="1003", name="Leia Organa", friends=["1000", "1002", "2000", "2001"], - appears_in=[4, 5, 6], + appears_in=[Episode.NEWHOPE, Episode.EMPIRE, Episode.JEDI], home_planet="Alderaan", ) @@ -42,7 +42,7 @@ def setup(): id="1004", name="Wilhuff Tarkin", friends=["1001"], - appears_in=[4], + appears_in=[Episode.NEWHOPE], home_planet=None, ) @@ -58,7 +58,7 @@ def setup(): id="2000", name="C-3PO", friends=["1000", "1002", "1003", "2001"], - appears_in=[4, 5, 6], + appears_in=[Episode.NEWHOPE, Episode.EMPIRE, Episode.JEDI], primary_function="Protocol", ) @@ -66,7 +66,7 @@ def setup(): id="2001", name="R2-D2", friends=["1000", "1002", "1003"], - appears_in=[4, 5, 6], + appears_in=[Episode.NEWHOPE, Episode.EMPIRE, Episode.JEDI], primary_function="Astromech", ) @@ -82,7 +82,9 @@ def get_friends(character): def get_hero(episode): - if episode == 5: + from .schema import Episode + + if episode == Episode.EMPIRE: return human_data["1000"] return droid_data["2001"] diff --git a/graphene/types/tests/test_enum.py b/graphene/types/tests/test_enum.py index 6086f54c..2ef8214f 100644 --- a/graphene/types/tests/test_enum.py +++ b/graphene/types/tests/test_enum.py @@ -183,17 +183,6 @@ def test_enum_value_as_unmounted_argument(): assert unmounted_field.type == RGB -def test_enum_can_be_compared(): - class RGB(Enum): - RED = 1 - GREEN = 2 - BLUE = 3 - - assert RGB.RED == 1 - assert RGB.GREEN == 2 - assert RGB.BLUE == 3 - - def test_enum_can_be_initialzied(): class RGB(Enum): RED = 1 diff --git a/graphene/types/tests/test_resolver_enum_arg.py b/graphene/types/tests/test_resolver_enum_arg.py new file mode 100644 index 00000000..8d871a6e --- /dev/null +++ b/graphene/types/tests/test_resolver_enum_arg.py @@ -0,0 +1,55 @@ +from enum import Enum as PyEnum + +from ..objecttype import ObjectType +from ..scalars import String +from ..schema import Schema + +from ..enum import Enum + + +class PythonEnum(PyEnum): + P1 = "p1" + P2 = "p2" + + +PythonBaseEnum = Enum.from_enum(PythonEnum, legacy_enum_resolver=False) + + +class Query(ObjectType): + python = String(v=PythonBaseEnum(default_value=PythonBaseEnum.P1)) + + def resolve_python(self, _, v): + return "python" + + +def test_fixture_sane(): + """Check that the fixture enums are built correctly""" + assert PythonBaseEnum.P1.value == "p1" + assert PythonBaseEnum.P2.value == "p2" + + assert PythonBaseEnum.P1 != "p1" + assert PythonBaseEnum.P2 != "p2" + + +def _call_and_get_arg(mocker, resolver_name, query): + resolver = mocker.patch.object(Query, resolver_name, return_value="mocked") + schema = Schema(Query) + + r = schema.execute(query) + assert not r.errors + + assert resolver.call_count == 1 + + return resolver.call_args[1]["v"] + + + +def test_resolve_enum_python(mocker): + arg = _call_and_get_arg(mocker, "resolve_python", "{python(v:P2)}") + assert arg == PythonBaseEnum.P2 + assert arg == PythonEnum.P2 + + +def test_resolve_enum_default_value_python(mocker): + param = _call_and_get_arg(mocker, "resolve_python", "{python}") + assert param == PythonBaseEnum.P1 diff --git a/graphene/types/tests/test_typemap.py b/graphene/types/tests/test_typemap.py index f713726f..bcac529b 100644 --- a/graphene/types/tests/test_typemap.py +++ b/graphene/types/tests/test_typemap.py @@ -49,11 +49,11 @@ def test_enum(): assert values == [ GraphQLEnumValue( name="foo", - value=1, + value=MyEnum.foo, description="Description foo=1", deprecation_reason="Is deprecated", ), - GraphQLEnumValue(name="bar", value=2, description="Description bar=2"), + GraphQLEnumValue(name="bar", value=MyEnum.bar, description="Description bar=2"), ]