Tests for NEW enum resolver - only support Python-style enums

This commit is contained in:
Aviv Eyal 2018-12-16 18:23:17 -08:00
parent 863b07b183
commit 1db22d5840
4 changed files with 68 additions and 22 deletions

View File

@ -3,14 +3,14 @@ droid_data = {}
def setup(): def setup():
from .schema import Human, Droid from .schema import Human, Droid, Episode
global human_data, droid_data global human_data, droid_data
luke = Human( luke = Human(
id="1000", id="1000",
name="Luke Skywalker", name="Luke Skywalker",
friends=["1002", "1003", "2000", "2001"], friends=["1002", "1003", "2000", "2001"],
appears_in=[4, 5, 6], appears_in=[Episode.NEWHOPE, Episode.EMPIRE, Episode.JEDI],
home_planet="Tatooine", home_planet="Tatooine",
) )
@ -18,7 +18,7 @@ def setup():
id="1001", id="1001",
name="Darth Vader", name="Darth Vader",
friends=["1004"], friends=["1004"],
appears_in=[4, 5, 6], appears_in=[Episode.NEWHOPE, Episode.EMPIRE, Episode.JEDI],
home_planet="Tatooine", home_planet="Tatooine",
) )
@ -26,7 +26,7 @@ def setup():
id="1002", id="1002",
name="Han Solo", name="Han Solo",
friends=["1000", "1003", "2001"], friends=["1000", "1003", "2001"],
appears_in=[4, 5, 6], appears_in=[Episode.NEWHOPE, Episode.EMPIRE, Episode.JEDI],
home_planet=None, home_planet=None,
) )
@ -34,7 +34,7 @@ def setup():
id="1003", id="1003",
name="Leia Organa", name="Leia Organa",
friends=["1000", "1002", "2000", "2001"], friends=["1000", "1002", "2000", "2001"],
appears_in=[4, 5, 6], appears_in=[Episode.NEWHOPE, Episode.EMPIRE, Episode.JEDI],
home_planet="Alderaan", home_planet="Alderaan",
) )
@ -42,7 +42,7 @@ def setup():
id="1004", id="1004",
name="Wilhuff Tarkin", name="Wilhuff Tarkin",
friends=["1001"], friends=["1001"],
appears_in=[4], appears_in=[Episode.NEWHOPE],
home_planet=None, home_planet=None,
) )
@ -58,7 +58,7 @@ def setup():
id="2000", id="2000",
name="C-3PO", name="C-3PO",
friends=["1000", "1002", "1003", "2001"], friends=["1000", "1002", "1003", "2001"],
appears_in=[4, 5, 6], appears_in=[Episode.NEWHOPE, Episode.EMPIRE, Episode.JEDI],
primary_function="Protocol", primary_function="Protocol",
) )
@ -66,7 +66,7 @@ def setup():
id="2001", id="2001",
name="R2-D2", name="R2-D2",
friends=["1000", "1002", "1003"], friends=["1000", "1002", "1003"],
appears_in=[4, 5, 6], appears_in=[Episode.NEWHOPE, Episode.EMPIRE, Episode.JEDI],
primary_function="Astromech", primary_function="Astromech",
) )
@ -82,7 +82,9 @@ def get_friends(character):
def get_hero(episode): def get_hero(episode):
if episode == 5: from .schema import Episode
if episode == Episode.EMPIRE:
return human_data["1000"] return human_data["1000"]
return droid_data["2001"] return droid_data["2001"]

View File

@ -183,17 +183,6 @@ def test_enum_value_as_unmounted_argument():
assert unmounted_field.type == RGB 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(): def test_enum_can_be_initialzied():
class RGB(Enum): class RGB(Enum):
RED = 1 RED = 1

View File

@ -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

View File

@ -49,11 +49,11 @@ def test_enum():
assert values == [ assert values == [
GraphQLEnumValue( GraphQLEnumValue(
name="foo", name="foo",
value=1, value=MyEnum.foo,
description="Description foo=1", description="Description foo=1",
deprecation_reason="Is deprecated", deprecation_reason="Is deprecated",
), ),
GraphQLEnumValue(name="bar", value=2, description="Description bar=2"), GraphQLEnumValue(name="bar", value=MyEnum.bar, description="Description bar=2"),
] ]