def test_execute_result_error(): expected_retries = 3 client = Client( retries=expected_retries, transport=RequestsHTTPTransport( url="https://countries.trevorblades.com/", use_json=True, headers={"Content-type": "application/json"}, ), ) failing_query = gql(""" query getContinents { continents { code name id } } """) with pytest.raises(Exception) as exc_info: client.execute(failing_query) client.close() assert 'Cannot query field "id" on type "Continent".' in str( exc_info.value)
def volumelist_request(self, token): query = gql(VOLUMELIST_QUERY.replace("X_TOKEN", token)) client = Client(transport=self.transport, fetch_schema_from_transport=True) result = client.execute(query) client.close() if self.app is not None: self.app.logger.info('volumelist_result : {}'.format(result)) return result
def test_http_transport_raise_for_status_error(http_transport_query): client = Client(transport=RequestsHTTPTransport( url="https://countries.trevorblades.com/", headers={"Content-type": "application/json"}, )) with pytest.raises(Exception) as exc_info: client.execute(http_transport_query) client.close() assert "400 Client Error: Bad Request for url" in str(exc_info.value)
def login_request(self, username, password): query = gql( LOGIN_QUERY.replace("X_USERNAME", username).replace("X_PASSWORD", password)) client = Client(transport=self.transport, fetch_schema_from_transport=True) result = client.execute(query) client.close() if self.app is not None: self.app.logger.info('login_result : {}'.format(result)) return result
def test_http_transport_verify_error(http_transport_query): client = Client(transport=RequestsHTTPTransport( url="https://countries.trevorblades.com/", use_json=True, headers={"Content-type": "application/json"}, verify=False, )) with pytest.warns(Warning) as record: client.execute(http_transport_query) client.close( ) # We could have written `with client:` on top of the `with pytest...` instead assert len(record) == 1 assert "Unverified HTTPS request is being made to host" in str( record[0].message)
def test_retries(execute_mock): expected_retries = 3 execute_mock.side_effect = Exception("fail") client = Client( retries=expected_retries, transport=RequestsHTTPTransport( url="http://swapi.graphene-python.org/graphql"), ) query = gql(""" { myFavoriteFilm: film(id:"RmlsbToz") { id title episodeId } } """) with pytest.raises(Exception): client.execute(query) client.close() assert execute_mock.call_count == expected_retries
def test_gql(): sample_path = os.path.join( os.path.dirname(os.path.abspath(__file__)), "fixtures", "graphql", "sample.graphql", ) with open(sample_path) as source: document = parse(source.read()) schema = build_ast_schema(document) client = Client(schema=schema) query = gql(""" query getUser { user(id: "1000") { id username } } """) result = client.execute(query) client.close() assert result["user"] is None