Changeset View
Changeset View
Standalone View
Standalone View
swh/graphql/tests/conftest.py
# Copyright (C) 2022 The Software Heritage developers | # Copyright (C) 2022 The Software Heritage developers | ||||
# See the AUTHORS file at the top-level directory of this distribution | # See the AUTHORS file at the top-level directory of this distribution | ||||
# License: GNU General Public License version 3, or any later version | # License: GNU General Public License version 3, or any later version | ||||
# See top-level LICENSE file for more information | # See top-level LICENSE file for more information | ||||
from ariadne import graphql_sync | from ariadne import graphql_sync | ||||
from flask import Flask, jsonify, request | from flask import Flask, jsonify, request | ||||
import pytest | import pytest | ||||
from swh.graphql import server as app_server | from swh.graphql import server as app_server | ||||
from swh.graphql.app import schema | from swh.graphql.app import schema, validation_rules | ||||
from swh.storage import get_storage as get_swhstorage | from swh.storage import get_storage as get_swhstorage | ||||
from .data import populate_dummy_data | from .data import populate_dummy_data | ||||
@pytest.fixture | @pytest.fixture | ||||
def storage(): | def storage(): | ||||
storage = get_swhstorage(cls="memory") | storage = get_swhstorage(cls="memory") | ||||
# set the global var to use the in-memory storage | # set the global var to use the in-memory storage | ||||
app_server.storage = storage | app_server.storage = storage | ||||
# populate the in-memory storage | # populate the in-memory storage | ||||
populate_dummy_data(storage) | populate_dummy_data(storage) | ||||
return storage | return storage | ||||
@pytest.fixture | @pytest.fixture | ||||
def test_app(storage): | def test_app(storage): | ||||
app = Flask(__name__) | app = Flask(__name__) | ||||
@app.route("/", methods=["POST"]) | @app.route("/", methods=["POST"]) | ||||
def graphql_server(): | def graphql_server(): | ||||
# GraphQL queries are always sent as POST | # GraphQL queries are always sent as POST | ||||
data = request.get_json() | data = request.get_json() | ||||
success, result = graphql_sync( | success, result = graphql_sync( | ||||
schema, data, context_value=request, debug=app.debug | schema, | ||||
data, | |||||
context_value=request, | |||||
debug=app.debug, | |||||
validation_rules=validation_rules, | |||||
) | ) | ||||
status_code = 200 if success else 400 | status_code = 200 if success else 400 | ||||
return jsonify(result), status_code | return jsonify(result), status_code | ||||
yield app | yield app | ||||
@pytest.fixture | @pytest.fixture | ||||
def client(test_app): | def client(test_app): | ||||
with test_app.test_client() as client: | with test_app.test_client() as client: | ||||
yield client | yield client |