forked from graphql-python/gql
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The tests were copied over from a different repo and so were inconsistent with the Star Wars schema we are using here. This PR should fix the tests (although we are still using a remote schema rather than a local one, so the tests run slowly becuase they need to make a http request each time).
- Loading branch information
Awais Hussain
committed
May 1, 2018
1 parent
1bbfbd7
commit 9d80656
Showing
2 changed files
with
72 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,94 +1,118 @@ | ||
"""Tests for the GraphQL Response Parser. | ||
These tests are worthless until I have a schema I can work with. | ||
At the moment we use the Star Wars schema which is fetched each time from the | ||
server endpoint. In future it would be better to store this schema in a file | ||
locally. | ||
""" | ||
import copy | ||
from gql.response_parser import ResponseParser | ||
|
||
import pytest | ||
import requests | ||
from gql import Client | ||
from gql.transport.requests import RequestsHTTPTransport | ||
|
||
class Capitalize(): | ||
@classmethod | ||
def parse_value(self, value: str): | ||
return value.upper(); | ||
|
||
def test_scalar_type_name_for_scalar_field_returns_name(gql_schema): | ||
parser = ResponseParser(gql_schema) | ||
schema_obj = gql_schema.get_type_map().get('Wallet') | ||
@pytest.fixture | ||
def schema(): | ||
request = requests.get('http://swapi.graphene-python.org/graphql', | ||
headers={ | ||
'Host': 'swapi.graphene-python.org', | ||
'Accept': 'text/html', | ||
}) | ||
request.raise_for_status() | ||
csrf = request.cookies['csrftoken'] | ||
|
||
assert parser._get_scalar_type_name(schema_obj.fields['balance']) == 'Money' | ||
client = Client( | ||
transport=RequestsHTTPTransport(url='http://swapi.graphene-python.org/graphql', | ||
cookies={"csrftoken": csrf}, | ||
headers={'x-csrftoken': csrf}), | ||
fetch_schema_from_transport=True | ||
) | ||
|
||
return client.schema | ||
|
||
def test_scalar_type_name_for_non_scalar_field_returns_none(gql_schema): | ||
parser = ResponseParser(gql_schema) | ||
schema_obj = gql_schema.get_type_map().get('Wallet') | ||
def test_scalar_type_name_for_scalar_field_returns_name(schema): | ||
parser = ResponseParser(schema) | ||
schema_obj = schema.get_query_type().fields['film'] | ||
|
||
assert parser._get_scalar_type_name(schema_obj.type.fields['releaseDate']) == 'DateTime' | ||
|
||
assert parser._get_scalar_type_name(schema_obj.fields['user']) is None | ||
|
||
def test_scalar_type_name_for_non_scalar_field_returns_none(schema): | ||
parser = ResponseParser(schema) | ||
schema_obj = schema.get_query_type().fields['film'] | ||
|
||
assert parser._get_scalar_type_name(schema_obj.type.fields['species']) is None | ||
|
||
def test_lookup_scalar_type(gql_schema): | ||
parser = ResponseParser(gql_schema) | ||
|
||
assert parser._lookup_scalar_type(["wallet"]) is None | ||
assert parser._lookup_scalar_type(["searchWallets"]) is None | ||
assert parser._lookup_scalar_type(["wallet", "balance"]) == 'Money' | ||
assert parser._lookup_scalar_type(["searchWallets", "balance"]) == 'Money' | ||
assert parser._lookup_scalar_type(["wallet", "name"]) == 'String' | ||
assert parser._lookup_scalar_type(["wallet", "invalid"]) is None | ||
assert parser._lookup_scalar_type(["film"]) is None | ||
assert parser._lookup_scalar_type(["film", "releaseDate"]) == 'DateTime' | ||
assert parser._lookup_scalar_type(["film", "species"]) is None | ||
|
||
def test_lookup_scalar_type_in_mutation(gql_schema): | ||
parser = ResponseParser(gql_schema) | ||
def test_lookup_scalar_type_in_mutation(schema): | ||
parser = ResponseParser(schema) | ||
|
||
assert parser._lookup_scalar_type(["manualWithdraw", "agentTransaction"]) is None | ||
assert parser._lookup_scalar_type(["manualWithdraw", "agentTransaction", "amount"]) == 'Money' | ||
assert parser._lookup_scalar_type(["createHero"]) is None | ||
assert parser._lookup_scalar_type(["createHero", "hero"]) is None | ||
assert parser._lookup_scalar_type(["createHero", "ok"]) == 'Boolean' | ||
|
||
def test_parse_response(gql_schema): | ||
def test_parse_response(schema): | ||
custom_scalars = { | ||
'Money': Capitalize | ||
'DateTime': Capitalize | ||
} | ||
parser = ResponseParser(gql_schema, custom_scalars) | ||
parser = ResponseParser(schema, custom_scalars) | ||
|
||
response = { | ||
'wallet': { | ||
'film': { | ||
'id': 'some_id', | ||
'name': 'U1_test', | ||
'releaseDate': 'some_datetime', | ||
} | ||
} | ||
|
||
expected = { | ||
'wallet': { | ||
'film': { | ||
'id': 'some_id', | ||
'name': 'U1_test', | ||
'releaseDate': 'SOME_DATETIME', | ||
} | ||
} | ||
|
||
assert parser.parse(response) == expected | ||
assert response['wallet']['balance'] == 'CFA 3850' | ||
assert response['film']['releaseDate'] == 'some_datetime' # ensure original response is not changed | ||
|
||
def test_parse_response_containing_list(gql_schema): | ||
def test_parse_response_containing_list(schema): | ||
custom_scalars = { | ||
'Money': M | ||
'DateTime': Capitalize | ||
} | ||
parser = ResponseParser(gql_schema, custom_scalars) | ||
parser = ResponseParser(schema, custom_scalars) | ||
|
||
response = { | ||
"searchWallets": [ | ||
{ | ||
"id": "W_wz518BXTDJuQ", | ||
"name": "U2_test", | ||
"balance": "CFA 4148" | ||
}, | ||
{ | ||
"id": "W_uOe9fHPoKO21", | ||
"name": "Agent_test", | ||
"balance": "CFA 2641" | ||
} | ||
] | ||
"allFilms": { | ||
"edges": [{ | ||
"node": { | ||
'id': 'some_id', | ||
'releaseDate': 'some_datetime', | ||
} | ||
},{ | ||
"node": { | ||
'id': 'some_id', | ||
'releaseDate': 'some_other_datetime', | ||
} | ||
}] | ||
} | ||
} | ||
|
||
expected = copy.deepcopy(response) | ||
expected['searchWallets'][0]['balance'] = M("CFA", "4148") | ||
expected['searchWallets'][1]['balance'] = M("CFA", "2641") | ||
expected['allFilms']['edges'][0]['node']['releaseDate'] = "SOME_DATETIME" | ||
expected['allFilms']['edges'][1]['node']['releaseDate'] = "SOME_OTHER_DATETIME" | ||
|
||
result = parser.parse(response) | ||
|
||
assert result == expected | ||
assert response['searchWallets'][0]['balance'] == "CFA 4148" | ||
assert response['searchWallets'][1]['balance'] == "CFA 2641" | ||
expected['allFilms']['edges'][0]['node']['releaseDate'] = "some_datetime" | ||
expected['allFilms']['edges'][1]['node']['releaseDate'] = "some_other_datetime" |