graphene/examples/starwars/tests/test_query.py

252 lines
5.9 KiB
Python
Raw Permalink Normal View History

from graphene.test import Client
2017-07-13 07:45:06 +03:00
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
client = Client(schema)
2017-07-13 07:45:06 +03:00
def test_hero_name_query():
result = client.execute("""
2015-09-24 12:11:50 +03:00
query HeroNameQuery {
hero {
name
}
}
""")
assert result == {"data": {"hero": {"name": "R2-D2"}}}
2015-09-24 12:11:50 +03:00
def test_hero_name_and_friends_query():
result = client.execute("""
2015-09-24 12:11:50 +03:00
query HeroNameAndFriendsQuery {
hero {
id
name
friends {
name
}
}
}
""")
assert result == {
"data": {
"hero": {
"id": "2001",
"name": "R2-D2",
"friends": [
{"name": "Luke Skywalker"},
{"name": "Han Solo"},
{"name": "Leia Organa"},
],
}
}
}
2015-09-24 12:11:50 +03:00
def test_nested_query():
result = client.execute("""
2015-09-24 12:11:50 +03:00
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
}
}
}
}
""")
assert result == {
"data": {
"hero": {
"name": "R2-D2",
"friends": [
{
"name": "Luke Skywalker",
"appearsIn": ["NEWHOPE", "EMPIRE", "JEDI"],
"friends": [
{"name": "Han Solo"},
{"name": "Leia Organa"},
{"name": "C-3PO"},
{"name": "R2-D2"},
],
},
{
"name": "Han Solo",
"appearsIn": ["NEWHOPE", "EMPIRE", "JEDI"],
"friends": [
{"name": "Luke Skywalker"},
{"name": "Leia Organa"},
{"name": "R2-D2"},
],
},
{
"name": "Leia Organa",
"appearsIn": ["NEWHOPE", "EMPIRE", "JEDI"],
"friends": [
{"name": "Luke Skywalker"},
{"name": "Han Solo"},
{"name": "C-3PO"},
{"name": "R2-D2"},
],
},
],
}
}
}
2015-09-24 12:11:50 +03:00
def test_fetch_luke_query():
result = client.execute("""
2015-09-24 12:11:50 +03:00
query FetchLukeQuery {
human(id: "1000") {
name
}
}
""")
assert result == {"data": {"human": {"name": "Luke Skywalker"}}}
2015-09-24 12:11:50 +03:00
def test_fetch_some_id_query():
result = client.execute(
"""
2015-09-24 12:11:50 +03:00
query FetchSomeIDQuery($someId: String!) {
human(id: $someId) {
name
}
}
""",
variables={"someId": "1000"},
)
assert result == {"data": {"human": {"name": "Luke Skywalker"}}}
2015-09-24 12:11:50 +03:00
def test_fetch_some_id_query2():
result = client.execute(
"""
2015-09-24 12:11:50 +03:00
query FetchSomeIDQuery($someId: String!) {
human(id: $someId) {
name
}
}
""",
variables={"someId": "1002"},
)
assert result == {"data": {"human": {"name": "Han Solo"}}}
2015-09-24 12:11:50 +03:00
def test_invalid_id_query():
result = client.execute(
"""
2015-09-24 12:11:50 +03:00
query humanQuery($id: String!) {
human(id: $id) {
name
}
}
""",
variables={"id": "not a valid id"},
)
assert result == {"data": {"human": None}}
2015-09-24 12:11:50 +03:00
def test_fetch_luke_aliased():
result = client.execute("""
2015-09-24 12:11:50 +03:00
query FetchLukeAliased {
luke: human(id: "1000") {
name
}
}
""")
assert result == {"data": {"luke": {"name": "Luke Skywalker"}}}
2015-09-24 12:11:50 +03:00
def test_fetch_luke_and_leia_aliased():
result = client.execute("""
2015-09-24 12:11:50 +03:00
query FetchLukeAndLeiaAliased {
luke: human(id: "1000") {
name
}
leia: human(id: "1003") {
name
}
}
""")
assert result == {
"data": {"luke": {"name": "Luke Skywalker"}, "leia": {"name": "Leia Organa"}}
}
2015-09-24 12:11:50 +03:00
def test_duplicate_fields():
result = client.execute("""
2015-09-24 12:11:50 +03:00
query DuplicateFields {
luke: human(id: "1000") {
name
homePlanet
}
leia: human(id: "1003") {
name
homePlanet
}
}
""")
assert result == {
"data": {
"luke": {"name": "Luke Skywalker", "homePlanet": "Tatooine"},
"leia": {"name": "Leia Organa", "homePlanet": "Alderaan"},
}
}
2015-09-24 12:11:50 +03:00
def test_use_fragment():
result = client.execute("""
2015-09-24 12:11:50 +03:00
query UseFragment {
luke: human(id: "1000") {
...HumanFragment
}
leia: human(id: "1003") {
...HumanFragment
}
}
fragment HumanFragment on Human {
name
homePlanet
}
""")
assert result == {
"data": {
"luke": {"name": "Luke Skywalker", "homePlanet": "Tatooine"},
"leia": {"name": "Leia Organa", "homePlanet": "Alderaan"},
}
}
2015-09-24 12:11:50 +03:00
def test_check_type_of_r2():
result = client.execute("""
2015-09-24 12:11:50 +03:00
query CheckTypeOfR2 {
hero {
__typename
name
}
}
""")
assert result == {"data": {"hero": {"__typename": "Droid", "name": "R2-D2"}}}
2015-09-24 12:11:50 +03:00
def test_check_type_of_luke():
result = client.execute("""
2016-06-05 00:05:31 +03:00
query CheckTypeOfLuke {
hero(episode: EMPIRE) {
__typename
name
}
}
""")
assert result == {
"data": {"hero": {"__typename": "Human", "name": "Luke Skywalker"}}
}