Changeset View
Changeset View
Standalone View
Standalone View
swh/graphql/tests/functional/test_revision.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 | ||||
import pytest | import pytest | ||||
from swh.model.swhids import CoreSWHID | from swh.model.swhids import CoreSWHID | ||||
from . import utils | from . import utils | ||||
from ..data import get_revisions, get_revisions_with_parents | from ..data import get_revisions, get_revisions_with_parents | ||||
@pytest.mark.parametrize("revision", get_revisions()) | @pytest.mark.parametrize("revision", get_revisions()) | ||||
def test_get_revision(client, revision): | def test_get_revision(client, revision): | ||||
query_str = """ | query_str = """ | ||||
{ | query getRevision($swhid: SWHID!) { | ||||
revision(swhid: "%s") { | revision(swhid: $swhid) { | ||||
swhid | swhid | ||||
message { | message { | ||||
text | text | ||||
} | } | ||||
author { | author { | ||||
fullname { | fullname { | ||||
text | text | ||||
} | } | ||||
Show All 18 Lines | query getRevision($swhid: SWHID!) { | ||||
date | date | ||||
type | type | ||||
directory { | directory { | ||||
swhid | swhid | ||||
} | } | ||||
} | } | ||||
} | } | ||||
""" | """ | ||||
data, _ = utils.get_query_response(client, query_str % revision.swhid()) | data, _ = utils.get_query_response(client, query_str, swhid=str(revision.swhid())) | ||||
assert data["revision"] == { | assert data["revision"] == { | ||||
"swhid": str(revision.swhid()), | "swhid": str(revision.swhid()), | ||||
"message": {"text": revision.message.decode()}, | "message": {"text": revision.message.decode()}, | ||||
"author": { | "author": { | ||||
"fullname": {"text": revision.author.fullname.decode()}, | "fullname": {"text": revision.author.fullname.decode()}, | ||||
"name": {"text": revision.author.name.decode()}, | "name": {"text": revision.author.name.decode()}, | ||||
"email": {"text": revision.author.email.decode()}, | "email": {"text": revision.author.email.decode()}, | ||||
}, | }, | ||||
"committer": { | "committer": { | ||||
"fullname": {"text": revision.committer.fullname.decode()}, | "fullname": {"text": revision.committer.fullname.decode()}, | ||||
"name": {"text": revision.committer.name.decode()}, | "name": {"text": revision.committer.name.decode()}, | ||||
"email": {"text": revision.committer.email.decode()}, | "email": {"text": revision.committer.email.decode()}, | ||||
}, | }, | ||||
"date": revision.date.to_datetime().isoformat(), | "date": revision.date.to_datetime().isoformat(), | ||||
"type": revision.type.value, | "type": revision.type.value, | ||||
"directory": { | "directory": { | ||||
"swhid": str(CoreSWHID(object_id=revision.directory, object_type="dir")) | "swhid": str(CoreSWHID(object_id=revision.directory, object_type="dir")) | ||||
}, | }, | ||||
} | } | ||||
def test_get_revision_with_invalid_swhid(client): | def test_get_revision_with_invalid_swhid(client): | ||||
query_str = """ | query_str = """ | ||||
{ | query getRevision($swhid: SWHID!) { | ||||
revision(swhid: "swh:1:cnt:invalid") { | revision(swhid: $swhid) { | ||||
swhid | swhid | ||||
} | } | ||||
} | } | ||||
""" | """ | ||||
errors = utils.get_error_response(client, query_str) | errors = utils.get_error_response(client, query_str, swhid="swh:1:cnt:invalid") | ||||
# API will throw an error in case of an invalid SWHID | # API will throw an error in case of an invalid SWHID | ||||
assert len(errors) == 1 | assert len(errors) == 1 | ||||
assert "Input error: Invalid SWHID" in errors[0]["message"] | assert "Input error: Invalid SWHID" in errors[0]["message"] | ||||
def test_get_revision_as_target(client): | def test_get_revision_as_target(client): | ||||
# SWHID of a snapshot with revision as target | # SWHID of a snapshot with revision as target | ||||
snapshot_swhid = "swh:1:snp:9e78d7105c5e0f886487511e2a92377b4ee4c32a" | snapshot_swhid = "swh:1:snp:9e78d7105c5e0f886487511e2a92377b4ee4c32a" | ||||
query_str = """ | query_str = """ | ||||
{ | query getSnapshot($swhid: SWHID!) { | ||||
snapshot(swhid: "%s") { | snapshot(swhid: $swhid) { | ||||
branches(first: 1, types: [revision]) { | branches(first: 1, types: [revision]) { | ||||
nodes { | nodes { | ||||
targetType | targetType | ||||
target { | target { | ||||
...on Revision { | ...on Revision { | ||||
swhid | swhid | ||||
} | } | ||||
} | } | ||||
} | } | ||||
} | } | ||||
} | } | ||||
} | } | ||||
""" | """ | ||||
data, _ = utils.get_query_response(client, query_str % snapshot_swhid) | data, _ = utils.get_query_response(client, query_str, swhid=snapshot_swhid) | ||||
revision_obj = data["snapshot"]["branches"]["nodes"][0]["target"] | revision_obj = data["snapshot"]["branches"]["nodes"][0]["target"] | ||||
assert revision_obj == { | assert revision_obj == { | ||||
"swhid": "swh:1:rev:66c7c1cd9673275037140f2abff7b7b11fc9439c" | "swhid": "swh:1:rev:66c7c1cd9673275037140f2abff7b7b11fc9439c" | ||||
} | } | ||||
def test_get_revision_log(client): | def test_get_revision_log(client): | ||||
revision_swhid = get_revisions_with_parents()[0].swhid() | revision_swhid = get_revisions_with_parents()[0].swhid() | ||||
query_str = """ | query_str = """ | ||||
{ | query getRevision($swhid: SWHID!) { | ||||
revision(swhid: "%s") { | revision(swhid: $swhid) { | ||||
swhid | swhid | ||||
revisionLog(first: 3) { | revisionLog(first: 3) { | ||||
nodes { | nodes { | ||||
swhid | swhid | ||||
} | } | ||||
} | } | ||||
} | } | ||||
} | } | ||||
""" | """ | ||||
data, _ = utils.get_query_response(client, query_str % revision_swhid) | data, _ = utils.get_query_response(client, query_str, swhid=str(revision_swhid)) | ||||
assert data["revision"]["revisionLog"] == { | assert data["revision"]["revisionLog"] == { | ||||
"nodes": [ | "nodes": [ | ||||
{"swhid": str(revision_swhid)}, | {"swhid": str(revision_swhid)}, | ||||
{"swhid": str(get_revisions()[0].swhid())}, | {"swhid": str(get_revisions()[0].swhid())}, | ||||
{"swhid": str(get_revisions()[1].swhid())}, | {"swhid": str(get_revisions()[1].swhid())}, | ||||
] | ] | ||||
} | } | ||||
def test_get_revision_parents(client): | def test_get_revision_parents(client): | ||||
revision_swhid = get_revisions_with_parents()[0].swhid() | revision_swhid = get_revisions_with_parents()[0].swhid() | ||||
query_str = """ | query_str = """ | ||||
{ | query getRevision($swhid: SWHID!) { | ||||
revision(swhid: "%s") { | revision(swhid: $swhid) { | ||||
swhid | swhid | ||||
parents { | parents { | ||||
nodes { | nodes { | ||||
swhid | swhid | ||||
} | } | ||||
} | } | ||||
} | } | ||||
} | } | ||||
""" | """ | ||||
data, _ = utils.get_query_response(client, query_str % revision_swhid) | data, _ = utils.get_query_response(client, query_str, swhid=str(revision_swhid)) | ||||
assert data["revision"]["parents"] == { | assert data["revision"]["parents"] == { | ||||
"nodes": [ | "nodes": [ | ||||
{"swhid": str(get_revisions()[0].swhid())}, | {"swhid": str(get_revisions()[0].swhid())}, | ||||
{"swhid": str(get_revisions()[1].swhid())}, | {"swhid": str(get_revisions()[1].swhid())}, | ||||
] | ] | ||||
} | } | ||||
def test_get_revision_with_unknown_swhid(client): | def test_get_revision_with_unknown_swhid(client): | ||||
unknown_sha1 = "1" * 40 | unknown_sha1 = "1" * 40 | ||||
query_str = """ | query_str = """ | ||||
{ | query getRevision($swhid: SWHID!) { | ||||
revision(swhid: "swh:1:rev:%s") { | revision(swhid: $swhid) { | ||||
swhid | swhid | ||||
} | } | ||||
} | } | ||||
""" | """ | ||||
utils.assert_missing_object(client, query_str % unknown_sha1, "revision") | utils.assert_missing_object( | ||||
client, | |||||
query_str, | |||||
obj_type="revision", | |||||
swhid=f"swh:1:rev:{unknown_sha1}", | |||||
) |