graphene/examples/starwars/tests/test_query.py

349 lines
7.5 KiB
Python
Raw Normal View History

from ..data import setup
2016-06-04 21:21:33 +03:00
from ..schema import schema
2015-09-24 12:11:50 +03:00
2015-10-27 09:54:51 +03:00
setup()
2015-09-24 12:11:50 +03:00
2015-09-24 12:11:50 +03:00
def test_hero_name_query():
query = '''
query HeroNameQuery {
hero {
name
}
}
'''
expected = {
'hero': {
'name': 'R2-D2'
}
}
2016-06-04 21:21:33 +03:00
result = schema.execute(query)
2015-09-24 12:11:50 +03:00
assert not result.errors
assert result.data == expected
def test_hero_name_and_friends_query():
query = '''
query HeroNameAndFriendsQuery {
hero {
id
name
friends {
name
}
}
}
'''
expected = {
'hero': {
'id': '2001',
'name': 'R2-D2',
'friends': [
{'name': 'Luke Skywalker'},
{'name': 'Han Solo'},
{'name': 'Leia Organa'},
]
}
}
2016-06-04 21:21:33 +03:00
result = schema.execute(query)
2015-09-24 12:11:50 +03:00
assert not result.errors
assert result.data == expected
def test_nested_query():
query = '''
query NestedQuery {
hero {
name
friends {
name
2016-06-05 00:05:31 +03:00
appearsIn
2015-09-24 12:11:50 +03:00
friends {
name
}
}
}
}
'''
expected = {
'hero': {
'name': 'R2-D2',
'friends': [
{
'name': 'Luke Skywalker',
2016-06-05 00:05:31 +03:00
'appearsIn': ['NEWHOPE', 'EMPIRE', 'JEDI'],
2015-09-24 12:11:50 +03:00
'friends': [
{
'name': 'Han Solo',
},
{
'name': 'Leia Organa',
},
{
'name': 'C-3PO',
},
{
'name': 'R2-D2',
},
]
},
{
'name': 'Han Solo',
2016-06-05 00:05:31 +03:00
'appearsIn': ['NEWHOPE', 'EMPIRE', 'JEDI'],
2015-09-24 12:11:50 +03:00
'friends': [
{
'name': 'Luke Skywalker',
},
{
'name': 'Leia Organa',
},
{
'name': 'R2-D2',
},
]
},
{
'name': 'Leia Organa',
2016-06-05 00:05:31 +03:00
'appearsIn': ['NEWHOPE', 'EMPIRE', 'JEDI'],
2015-09-24 12:11:50 +03:00
'friends': [
{
'name': 'Luke Skywalker',
},
{
'name': 'Han Solo',
},
{
'name': 'C-3PO',
},
{
'name': 'R2-D2',
},
]
},
]
}
}
2016-06-04 21:21:33 +03:00
result = schema.execute(query)
2015-09-24 12:11:50 +03:00
assert not result.errors
assert result.data == expected
def test_fetch_luke_query():
query = '''
query FetchLukeQuery {
human(id: "1000") {
name
}
}
'''
expected = {
'human': {
'name': 'Luke Skywalker',
}
}
2016-06-04 21:21:33 +03:00
result = schema.execute(query)
2015-09-24 12:11:50 +03:00
assert not result.errors
assert result.data == expected
def test_fetch_some_id_query():
query = '''
query FetchSomeIDQuery($someId: String!) {
human(id: $someId) {
name
}
}
'''
params = {
'someId': '1000',
}
expected = {
'human': {
'name': 'Luke Skywalker',
}
}
result = schema.execute(query, None, variable_values=params)
2015-09-24 12:11:50 +03:00
assert not result.errors
assert result.data == expected
def test_fetch_some_id_query2():
query = '''
query FetchSomeIDQuery($someId: String!) {
human(id: $someId) {
name
}
}
'''
params = {
'someId': '1002',
}
expected = {
'human': {
'name': 'Han Solo',
}
}
result = schema.execute(query, None, variable_values=params)
2015-09-24 12:11:50 +03:00
assert not result.errors
assert result.data == expected
def test_invalid_id_query():
query = '''
query humanQuery($id: String!) {
human(id: $id) {
name
}
}
'''
params = {
'id': 'not a valid id',
}
expected = {
'human': None
}
result = schema.execute(query, None, variable_values=params)
2015-09-24 12:11:50 +03:00
assert not result.errors
assert result.data == expected
def test_fetch_luke_aliased():
query = '''
query FetchLukeAliased {
luke: human(id: "1000") {
name
}
}
'''
expected = {
'luke': {
'name': 'Luke Skywalker',
}
}
2016-06-04 21:21:33 +03:00
result = schema.execute(query)
2015-09-24 12:11:50 +03:00
assert not result.errors
assert result.data == expected
def test_fetch_luke_and_leia_aliased():
query = '''
query FetchLukeAndLeiaAliased {
luke: human(id: "1000") {
name
}
leia: human(id: "1003") {
name
}
}
'''
expected = {
'luke': {
'name': 'Luke Skywalker',
},
'leia': {
'name': 'Leia Organa',
}
}
2016-06-04 21:21:33 +03:00
result = schema.execute(query)
2015-09-24 12:11:50 +03:00
assert not result.errors
assert result.data == expected
def test_duplicate_fields():
query = '''
query DuplicateFields {
luke: human(id: "1000") {
name
homePlanet
}
leia: human(id: "1003") {
name
homePlanet
}
}
'''
expected = {
'luke': {
'name': 'Luke Skywalker',
'homePlanet': 'Tatooine',
2015-10-03 08:17:51 +03:00
},
2015-09-24 12:11:50 +03:00
'leia': {
'name': 'Leia Organa',
'homePlanet': 'Alderaan',
2015-10-03 08:17:51 +03:00
}
2015-09-24 12:11:50 +03:00
}
2016-06-04 21:21:33 +03:00
result = schema.execute(query)
2015-09-24 12:11:50 +03:00
assert not result.errors
assert result.data == expected
def test_use_fragment():
query = '''
query UseFragment {
luke: human(id: "1000") {
...HumanFragment
}
leia: human(id: "1003") {
...HumanFragment
}
}
fragment HumanFragment on Human {
name
homePlanet
}
'''
expected = {
'luke': {
'name': 'Luke Skywalker',
'homePlanet': 'Tatooine',
2015-10-03 08:17:51 +03:00
},
2015-09-24 12:11:50 +03:00
'leia': {
'name': 'Leia Organa',
'homePlanet': 'Alderaan',
2015-10-03 08:17:51 +03:00
}
2015-09-24 12:11:50 +03:00
}
2016-06-04 21:21:33 +03:00
result = schema.execute(query)
2015-09-24 12:11:50 +03:00
assert not result.errors
assert result.data == expected
def test_check_type_of_r2():
query = '''
query CheckTypeOfR2 {
hero {
__typename
name
}
}
'''
expected = {
'hero': {
'__typename': 'Droid',
'name': 'R2-D2',
}
}
2016-06-04 21:21:33 +03:00
result = schema.execute(query)
2015-09-24 12:11:50 +03:00
assert not result.errors
assert result.data == expected
2016-06-05 00:05:31 +03:00
def test_check_type_of_luke():
query = '''
query CheckTypeOfLuke {
hero(episode: EMPIRE) {
__typename
name
}
}
'''
expected = {
'hero': {
'__typename': 'Human',
'name': 'Luke Skywalker',
}
}
result = schema.execute(query)
assert not result.errors
assert result.data == expected