Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_custom_root_value_is_passed_to_resolvers(schema):
app = GraphQL(schema, root_value={"test": "TEST-ROOT"})
_, result = app.execute_query({}, {"query": "{ testRoot }"})
assert result == {"data": {"testRoot": "TEST-ROOT"}}
def test_type_defs_resolvers_host_and_ip_are_passed_to_graphql_middleware(
middleware_make_simple_server_mock, schema
):
start_simple_server(schema, host="0.0.0.0", port=4444)
call = middleware_make_simple_server_mock.call_args[0]
assert call[0] == "0.0.0.0"
assert call[1] == 4444
assert isinstance(call[2], GraphQL)
def test_middlewares_and_extensions_are_combined_in_correct_order(schema):
def test_middleware(next_fn, *args, **kwargs):
value = next_fn(*args, **kwargs)
return f"*{value}*"
app = GraphQL(schema, extensions=[CustomExtension], middleware=[test_middleware])
client = TestClient(app)
response = client.post("/", json={"query": '{ hello(name: "BOB") }'})
assert response.json() == {"data": {"hello": "=*Hello, BOB!*="}}
def test_error_formatter_is_called_with_debug_disabled_flag(schema):
error_formatter = Mock(return_value=True)
app = GraphQL(schema, debug=False, error_formatter=error_formatter)
execute_failing_query(app)
error_formatter.assert_called_once_with(ANY, False)
def test_custom_context_value_is_passed_to_resolvers(schema):
app = GraphQL(schema, context_value={"test": "TEST-CONTEXT"})
_, result = app.execute_query({}, {"query": "{ testContext }"})
assert result == {"data": {"testContext": "TEST-CONTEXT"}}
def test_custom_context_value_function_result_is_passed_to_resolvers(schema):
get_context_value = Mock(return_value={"test": "TEST-CONTEXT"})
app = GraphQL(schema, context_value=get_context_value)
_, result = app.execute_query({}, {"query": "{ testContext }"})
assert result == {"data": {"testContext": "TEST-CONTEXT"}}
def test_start_simple_server_from_inheriting_type_respects_inheritance(
type_defs, resolvers, make_server
):
class CustomGraphQL(GraphQL):
pass
schema = make_executable_schema(type_defs, resolvers)
start_simple_server(schema, server_class=CustomGraphQL)
make_server.assert_called_once()
called_with_args = make_server.call_args[0]
assert isinstance(called_with_args[2], CustomGraphQL)
def test_extension_from_option_are_passed_to_query_executor(schema):
app = GraphQL(schema, extensions=[CustomExtension])
client = TestClient(app)
response = client.post("/", json={"query": '{ hello(name: "BOB") }'})
assert response.json() == {"data": {"hello": "hello, bob!"}}
def test_custom_root_value_function_is_called_with_context_value(schema):
get_root_value = Mock(return_value=True)
app = GraphQL(
schema, context_value={"test": "TEST-CONTEXT"}, root_value=get_root_value
)
app.execute_query({}, {"query": "{ status }"})
get_root_value.assert_called_once_with({"test": "TEST-CONTEXT"}, ANY)
def test_default_logger_is_used_to_log_error_if_custom_is_not_set(schema, mocker):
logging_mock = mocker.patch("ariadne.logger.logging")
app = GraphQL(schema)
execute_failing_query(app)
logging_mock.getLogger.assert_called_once_with("ariadne")