diff --git a/swh/graphql/tests/conftest.py b/swh/graphql/tests/conftest.py index a1e21d2..da619ef 100644 --- a/swh/graphql/tests/conftest.py +++ b/swh/graphql/tests/conftest.py @@ -1,47 +1,48 @@ # Copyright (C) 2022 The Software Heritage developers # See the AUTHORS file at the top-level directory of this distribution # License: GNU General Public License version 3, or any later version # See top-level LICENSE file for more information from ariadne import graphql_sync from flask import Flask, jsonify, request import pytest from swh.graphql import server as app_server from swh.graphql.app import schema from swh.storage import get_storage as get_swhstorage from .data import populate_dummy_data @pytest.fixture def storage(): storage = get_swhstorage(cls="memory") # set the global var to use the in-memory storage app_server.storage = storage # populate the in-memory storage populate_dummy_data(storage) + return storage @pytest.fixture def test_app(storage): app = Flask(__name__) @app.route("/", methods=["POST"]) def graphql_server(): # GraphQL queries are always sent as POST data = request.get_json() success, result = graphql_sync( schema, data, context_value=request, debug=app.debug ) status_code = 200 if success else 400 return jsonify(result), status_code yield app @pytest.fixture def client(test_app): with test_app.test_client() as client: yield client diff --git a/swh/graphql/tests/functional/test_visit_node.py b/swh/graphql/tests/functional/test_visit_node.py new file mode 100644 index 0000000..062ae71 --- /dev/null +++ b/swh/graphql/tests/functional/test_visit_node.py @@ -0,0 +1,69 @@ +# Copyright (C) 2022 The Software Heritage developers +# See the AUTHORS file at the top-level directory of this distribution +# License: GNU General Public License version 3, or any later version +# See top-level LICENSE file for more information + +import pytest + +from ..data import get_origins +from .utils import get_query_response + + +@pytest.mark.parametrize("origin", get_origins()) +def test_get_visit(client, storage, origin): + query_str = """ + { + visit(originUrl: "%s", visitId: %s) { + visitId + date + type + latestStatus { + status + date + type + snapshot { + swhid + } + } + status { + nodes { + status + } + } + } + } + """ + + visits_and_statuses = storage.origin_visit_get_with_statuses(origin.url).results + for vws in visits_and_statuses: + visit = vws.visit + statuses = vws.statuses + data, _ = get_query_response(client, query_str % (origin.url, visit.visit)) + assert data["visit"] == { + "visitId": visit.visit, + "type": visit.type, + "date": visit.date.isoformat(), + "latestStatus": { + "date": statuses[-1].date.isoformat(), + "type": statuses[-1].type, + "status": statuses[-1].status, + "snapshot": ({"swhid": f"swh:1:snp:{statuses[-1].snapshot.hex()}"}) + if statuses[-1].snapshot + else None, + }, + "status": {"nodes": [{"status": status.status} for status in statuses]}, + } + + +def test_invalid_get_visit(client): + query_str = """ + { + visit(originUrl: "http://example.com/forge1", visitId: 3) { + type + } + } + """ + data, errors = get_query_response(client, query_str) + assert data["visit"] is None + assert len(errors) == 1 + assert errors[0]["message"] == "Requested object is not available"