diff --git a/swh/graphql/tests/data.py b/swh/graphql/tests/data.py --- a/swh/graphql/tests/data.py +++ b/swh/graphql/tests/data.py @@ -3,15 +3,11 @@ # License: GNU General Public License version 3, or any later version # See top-level LICENSE file for more information +from swh.model.hashutil import hash_to_bytes +from swh.model.model import ObjectType, Release from swh.model.tests import swh_model_data -def populate_dummy_data(storage): - for object_type, objects in swh_model_data.TEST_OBJECTS.items(): - method = getattr(storage, object_type + "_add") - method(objects) - - def populate_search_data(search): search.origin_update({"url": origin.url} for origin in get_origins()) @@ -38,3 +34,57 @@ def get_directories(): return swh_model_data.DIRECTORIES + + +def get_releases_with_target(): + """ + GraphQL will not return a target object unless the target id + is present in the DB. + Return release objects with real targets instead of dummy + targets in swh.model.tests.swh_model_data + """ + with_revision = Release( + id=hash_to_bytes("9129dc4e14acd0e51ca3bcd6b80f4577d281fd25"), + name=b"v0.0.1", + target_type=ObjectType.REVISION, + target=get_revisions()[0].id, + message=b"foo", + synthetic=False, + ) + with_release = Release( + id=hash_to_bytes("6429dc4e14acd0e51ca3bcd6b80f4577d281fd32"), + name=b"v0.0.1", + target_type=ObjectType.RELEASE, + target=get_releases()[0].id, + message=b"foo", + synthetic=False, + ) + with_directory = Release( + id=hash_to_bytes("3129dc4e14acd0e51ca3bcd6b80f4577d281fd42"), + name=b"v0.0.1", + target_type=ObjectType.DIRECTORY, + target=get_directories()[0].id, + message=b"foo", + synthetic=False, + ) + with_content = Release( + id=hash_to_bytes("7589dc4e14acd0e51ca3bcd6b80f4577d281fd34"), + name=b"v0.0.1", + target_type=ObjectType.CONTENT, + target=get_contents()[0].sha1_git, + message=b"foo", + synthetic=False, + ) + return [with_revision, with_release, with_directory, with_content] + + +GRAPHQL_EXTRA_TEST_OBJECTS = {"release": get_releases_with_target()} + + +def populate_dummy_data(storage): + for object_type, objects in swh_model_data.TEST_OBJECTS.items(): + method = getattr(storage, object_type + "_add") + method(objects) + for object_type, objects in GRAPHQL_EXTRA_TEST_OBJECTS.items(): + method = getattr(storage, object_type + "_add") + method(objects) diff --git a/swh/graphql/tests/functional/test_release_node.py b/swh/graphql/tests/functional/test_release_node.py --- a/swh/graphql/tests/functional/test_release_node.py +++ b/swh/graphql/tests/functional/test_release_node.py @@ -7,8 +7,8 @@ import pytest +from . import utils from ..data import get_releases -from .utils import get_query_response @pytest.mark.parametrize("release", get_releases()) @@ -43,7 +43,7 @@ """ % release.swhid() ) - data, _ = get_query_response(client, query_str) + data, _ = utils.get_query_response(client, query_str) assert data["release"] == { "swhid": str(release.swhid()), @@ -62,3 +62,141 @@ "date": release.date.to_datetime().isoformat() if release.date else None, "targetType": release.target_type.value, } + + +def test_get_release_with_invalid_swhid(client): + query_str = """ + { + content(swhid: "swh:1:rel:invalid") { + swhid + } + } + """ + errors = utils.get_error_response(client, query_str) + # API will throw an error in case of an invalid SWHID + assert len(errors) == 1 + + +def test_get_release_target_revision(client): + swhid = "swh:1:rel:9129dc4e14acd0e51ca3bcd6b80f4577d281fd25" + query_str = """ + { + release(swhid: "%s") { + targetType + target { + ...on Revision { + swhid + } + } + } + } + """ + data, _ = utils.get_query_response(client, query_str % swhid) + assert data["release"] == { + "target": {"swhid": "swh:1:rev:66c7c1cd9673275037140f2abff7b7b11fc9439c"}, + "targetType": "revision", + } + + +def test_get_release_target_release(client): + swhid = "swh:1:rel:6429dc4e14acd0e51ca3bcd6b80f4577d281fd32" + query_str = """ + { + release(swhid: "%s") { + targetType + target { + ...on Release { + swhid + } + } + } + } + """ + data, _ = utils.get_query_response(client, query_str % swhid) + assert data["release"] == { + "target": {"swhid": "swh:1:rel:8059dc4e17fcd0e51ca3bcd6b80f4577d281fd08"}, + "targetType": "release", + } + + +def test_get_release_target_directory(client): + swhid = "swh:1:rel:3129dc4e14acd0e51ca3bcd6b80f4577d281fd42" + query_str = """ + { + release(swhid: "%s") { + targetType + target { + ...on Directory { + swhid + } + } + } + } + """ + data, _ = utils.get_query_response(client, query_str % swhid) + assert data["release"] == { + "target": {"swhid": "swh:1:dir:4b825dc642cb6eb9a060e54bf8d69288fbee4904"}, + "targetType": "directory", + } + + +def test_get_release_target_content(client): + swhid = "swh:1:rel:7589dc4e14acd0e51ca3bcd6b80f4577d281fd34" + query_str = """ + { + release(swhid: "%s") { + targetType + target { + ...on Content { + swhid + } + } + } + } + """ + data, _ = utils.get_query_response(client, query_str % swhid) + assert data["release"] == { + "target": {"swhid": "swh:1:cnt:86bc6b377e9d25f9d26777a4a28d08e63e7c5779"}, + "targetType": "content", + } + + +def test_get_release_target_unknown(client): + # Clinet can request all the possible options if the target type + # is unknown. The data under the right type will be returned + + # The target is of type Revision in this case + # ie: both swhid and message will be available in the response + swhid = "swh:1:rel:9129dc4e14acd0e51ca3bcd6b80f4577d281fd25" + query_str = """ + { + release(swhid: "%s") { + targetType + target { + ...on Revision { + swhid + message { + text + } + } + ...on Release { + swhid + } + ...on Directory { + swhid + } + ...on Content { + swhid + } + } + } + } + """ + data, _ = utils.get_query_response(client, query_str % swhid) + assert data["release"] == { + "target": { + "message": {"text": "hello"}, + "swhid": "swh:1:rev:66c7c1cd9673275037140f2abff7b7b11fc9439c", + }, + "targetType": "revision", + }