Improved starwars tests

This commit is contained in:
Syrus Akbary 2016-06-04 11:21:33 -07:00
parent 3acf5fd588
commit 6c42877758
3 changed files with 69 additions and 59 deletions

View File

@ -9,7 +9,7 @@ def setup():
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=[4, 5, 6],
home_planet='Tatooine', home_planet='Tatooine',
) )
@ -17,7 +17,7 @@ def setup():
id='1001', id='1001',
name='Darth Vader', name='Darth Vader',
friends=['1004'], friends=['1004'],
appears_in=[4, 5, 6], # appears_in=[4, 5, 6],
home_planet='Tatooine', home_planet='Tatooine',
) )
@ -25,7 +25,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=[4, 5, 6],
home_planet=None, home_planet=None,
) )
@ -33,7 +33,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=[4, 5, 6],
home_planet='Alderaan', home_planet='Alderaan',
) )
@ -41,7 +41,7 @@ def setup():
id='1004', id='1004',
name='Wilhuff Tarkin', name='Wilhuff Tarkin',
friends=['1001'], friends=['1001'],
appears_in=[4], # appears_in=[4],
home_planet=None, home_planet=None,
) )
@ -53,25 +53,25 @@ def setup():
'1004': tarkin, '1004': tarkin,
} }
threepio = Droid( c3po = Droid(
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=[4, 5, 6],
primary_function='Protocol', primary_function='Protocol',
) )
artoo = Droid( r2d2 = Droid(
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=[4, 5, 6],
primary_function='Astromech', primary_function='Astromech',
) )
droid_data = { droid_data = {
'2000': threepio, '2000': c3po,
'2001': artoo, '2001': r2d2,
} }

View File

@ -4,34 +4,43 @@ from graphene import resolve_only_args
from .data import get_character, get_droid, get_hero, get_human from .data import get_character, get_droid, get_hero, get_human
class Episode(graphene.Enum): # class Episode(graphene.Enum):
NEWHOPE = 4 # NEWHOPE = 4
EMPIRE = 5 # EMPIRE = 5
JEDI = 6 # JEDI = 6
class Character(graphene.Interface): class Character(graphene.Interface):
id = graphene.ID() id = graphene.ID()
name = graphene.String() name = graphene.String()
friends = graphene.List('Character') friends = graphene.List(lambda: Character)
appears_in = graphene.List(Episode) # appears_in = graphene.List(Episode)
@graphene.implements(Character)
class Human(graphene.ObjectType):
home_planet = graphene.String()
def resolve_friends(self, args, *_): def resolve_friends(self, args, *_):
print 'resolve_friends'
# The character friends is a list of strings # The character friends is a list of strings
return [get_character(f) for f in self.friends] return [get_character(f) for f in self.friends]
class Human(Character): @graphene.implements(Character)
home_planet = graphene.String() class Droid(graphene.ObjectType):
class Droid(Character):
primary_function = graphene.String() primary_function = graphene.String()
def resolve_friends(self, args, *_):
print 'resolve_friends'
# print self.name
# The character friends is a list of strings
return [get_character() for f in self.friends]
class Query(graphene.ObjectType): class Query(graphene.ObjectType):
hero = graphene.Field(Character, hero = graphene.Field(Character,
episode=graphene.Argument(Episode) # episode=graphene.Argument(Episode)
) )
human = graphene.Field(Human, human = graphene.Field(Human,
id=graphene.String() id=graphene.String()
@ -42,6 +51,7 @@ class Query(graphene.ObjectType):
@resolve_only_args @resolve_only_args
def resolve_hero(self, episode=None): def resolve_hero(self, episode=None):
print 'get_hero', get_hero(episode)
return get_hero(episode) return get_hero(episode)
@resolve_only_args @resolve_only_args
@ -53,4 +63,4 @@ class Query(graphene.ObjectType):
return get_droid(id) return get_droid(id)
Schema = graphene.Schema(query=Query) schema = graphene.Schema(query=Query)

View File

@ -1,6 +1,6 @@
from ..data import setup from ..data import setup
from ..schema import Schema from ..schema import schema
setup() setup()
@ -18,7 +18,7 @@ def test_hero_name_query():
'name': 'R2-D2' 'name': 'R2-D2'
} }
} }
result = Schema.execute(query) result = schema.execute(query)
assert not result.errors assert not result.errors
assert result.data == expected assert result.data == expected
@ -46,7 +46,7 @@ def test_hero_name_and_friends_query():
] ]
} }
} }
result = Schema.execute(query) result = schema.execute(query)
assert not result.errors assert not result.errors
assert result.data == expected assert result.data == expected
@ -58,7 +58,7 @@ def test_nested_query():
name name
friends { friends {
name name
appearsIn # appearsIn
friends { friends {
name name
} }
@ -72,7 +72,7 @@ def test_nested_query():
'friends': [ 'friends': [
{ {
'name': 'Luke Skywalker', 'name': 'Luke Skywalker',
'appearsIn': ['NEWHOPE', 'EMPIRE', 'JEDI'], # 'appearsIn': ['NEWHOPE', 'EMPIRE', 'JEDI'],
'friends': [ 'friends': [
{ {
'name': 'Han Solo', 'name': 'Han Solo',
@ -90,7 +90,7 @@ def test_nested_query():
}, },
{ {
'name': 'Han Solo', 'name': 'Han Solo',
'appearsIn': ['NEWHOPE', 'EMPIRE', 'JEDI'], # 'appearsIn': ['NEWHOPE', 'EMPIRE', 'JEDI'],
'friends': [ 'friends': [
{ {
'name': 'Luke Skywalker', 'name': 'Luke Skywalker',
@ -105,7 +105,7 @@ def test_nested_query():
}, },
{ {
'name': 'Leia Organa', 'name': 'Leia Organa',
'appearsIn': ['NEWHOPE', 'EMPIRE', 'JEDI'], # 'appearsIn': ['NEWHOPE', 'EMPIRE', 'JEDI'],
'friends': [ 'friends': [
{ {
'name': 'Luke Skywalker', 'name': 'Luke Skywalker',
@ -124,7 +124,7 @@ def test_nested_query():
] ]
} }
} }
result = Schema.execute(query) result = schema.execute(query)
assert not result.errors assert not result.errors
assert result.data == expected assert result.data == expected
@ -142,7 +142,7 @@ def test_fetch_luke_query():
'name': 'Luke Skywalker', 'name': 'Luke Skywalker',
} }
} }
result = Schema.execute(query) result = schema.execute(query)
assert not result.errors assert not result.errors
assert result.data == expected assert result.data == expected
@ -163,7 +163,7 @@ def test_fetch_some_id_query():
'name': 'Luke Skywalker', 'name': 'Luke Skywalker',
} }
} }
result = Schema.execute(query, None, params) result = schema.execute(query, None, params)
assert not result.errors assert not result.errors
assert result.data == expected assert result.data == expected
@ -184,7 +184,7 @@ def test_fetch_some_id_query2():
'name': 'Han Solo', 'name': 'Han Solo',
} }
} }
result = Schema.execute(query, None, params) result = schema.execute(query, None, params)
assert not result.errors assert not result.errors
assert result.data == expected assert result.data == expected
@ -203,7 +203,7 @@ def test_invalid_id_query():
expected = { expected = {
'human': None 'human': None
} }
result = Schema.execute(query, None, params) result = schema.execute(query, None, params)
assert not result.errors assert not result.errors
assert result.data == expected assert result.data == expected
@ -221,7 +221,7 @@ def test_fetch_luke_aliased():
'name': 'Luke Skywalker', 'name': 'Luke Skywalker',
} }
} }
result = Schema.execute(query) result = schema.execute(query)
assert not result.errors assert not result.errors
assert result.data == expected assert result.data == expected
@ -245,7 +245,7 @@ def test_fetch_luke_and_leia_aliased():
'name': 'Leia Organa', 'name': 'Leia Organa',
} }
} }
result = Schema.execute(query) result = schema.execute(query)
assert not result.errors assert not result.errors
assert result.data == expected assert result.data == expected
@ -273,7 +273,7 @@ def test_duplicate_fields():
'homePlanet': 'Alderaan', 'homePlanet': 'Alderaan',
} }
} }
result = Schema.execute(query) result = schema.execute(query)
assert not result.errors assert not result.errors
assert result.data == expected assert result.data == expected
@ -303,7 +303,7 @@ def test_use_fragment():
'homePlanet': 'Alderaan', 'homePlanet': 'Alderaan',
} }
} }
result = Schema.execute(query) result = schema.execute(query)
assert not result.errors assert not result.errors
assert result.data == expected assert result.data == expected
@ -323,26 +323,26 @@ def test_check_type_of_r2():
'name': 'R2-D2', 'name': 'R2-D2',
} }
} }
result = Schema.execute(query) result = schema.execute(query)
assert not result.errors assert not result.errors
assert result.data == expected assert result.data == expected
def test_check_type_of_luke(): # def test_check_type_of_luke():
query = ''' # query = '''
query CheckTypeOfLuke { # query CheckTypeOfLuke {
hero(episode: EMPIRE) { # hero(episode: EMPIRE) {
__typename # __typename
name # name
} # }
} # }
''' # '''
expected = { # expected = {
'hero': { # 'hero': {
'__typename': 'Human', # '__typename': 'Human',
'name': 'Luke Skywalker', # 'name': 'Luke Skywalker',
} # }
} # }
result = Schema.execute(query) # result = schema.execute(query)
assert not result.errors # assert not result.errors
assert result.data == expected # assert result.data == expected