diff --git a/swh/deposit/tests/cli/test_client.py b/swh/deposit/tests/cli/test_client.py --- a/swh/deposit/tests/cli/test_client.py +++ b/swh/deposit/tests/cli/test_client.py @@ -3,6 +3,7 @@ # License: GNU General Public License version 3, or any later version # See top-level LICENSE file for more information +import ast import contextlib import json import logging @@ -11,6 +12,7 @@ from click.testing import CliRunner import pytest +import yaml from swh.deposit.cli import deposit as cli from swh.deposit.cli.client import InputError, _client, _collection, _url, generate_slug @@ -418,3 +420,59 @@ # FIXME: should be "deposited" but current limitation in the # requests_mock_datadir_visits use, cannot find a way to make it work right now assert actual_deposit["deposit_status"] == "partial" + + +@pytest.mark.parametrize( + "output_format,callable_fn", + [ + ("json", json.loads), + ("yaml", yaml.safe_load), + ( + "logging", + ast.literal_eval, + ), # not enough though, the caplog fixture is needed + ], +) +def test_cli_deposit_status_json( + output_format, callable_fn, datadir, slug, requests_mock_datadir, caplog +): + """Check deposit status as with differing output + + """ + api_url_basename = "deposit.test.status" + deposit_id = 1033 + deposit_status_xml_path = os.path.join( + datadir, f"https_{api_url_basename}", f"1_test_{deposit_id}_status" + ) + with open(deposit_status_xml_path, "r") as f: + deposit_status_xml = f.read() + expected_deposit_dict = dict(parse_xml(deposit_status_xml)) + + runner = CliRunner() + result = runner.invoke( + cli, + [ + "status", + "--url", + f"https://{api_url_basename}/1", + "--username", + TEST_USER["username"], + "--password", + TEST_USER["password"], + "--deposit-id", + deposit_id, + "--format", + output_format, + ], + ) + assert result.exit_code == 0, f"unexpected output: {result.output}" + + if output_format == "logging": + assert len(caplog.record_tuples) == 1 + # format: (, , ) + _, _, result_output = caplog.record_tuples[0] + else: + result_output = result.output + + actual_deposit = callable_fn(result_output) + assert actual_deposit == expected_deposit_dict diff --git a/swh/deposit/tests/data/https_deposit.test.status/1_servicedocument b/swh/deposit/tests/data/https_deposit.test.status/1_servicedocument new file mode 100644 --- /dev/null +++ b/swh/deposit/tests/data/https_deposit.test.status/1_servicedocument @@ -0,0 +1,26 @@ + + + + 2.0 + 209715200 + + + The Software Heritage (SWH) Archive + + test Software Collection + application/zip + application/x-tar + Collection Policy + Software Heritage Archive + Collect, Preserve, Share + false + false + http://purl.org/net/sword/package/SimpleZip + https://deposit.test.status/1/test/ + test + + + diff --git a/swh/deposit/tests/data/https_deposit.test.status/1_test_1033_status b/swh/deposit/tests/data/https_deposit.test.status/1_test_1033_status new file mode 100644 --- /dev/null +++ b/swh/deposit/tests/data/https_deposit.test.status/1_test_1033_status @@ -0,0 +1,10 @@ + + 1033 + done + The deposit has been successfully loaded into the Software Heritage archive + swh:1:dir:ef04a768181417fbc5eef4243e2507915f24deea + swh:1:dir:ef04a768181417fbc5eef4243e2507915f24deea;origin=https://www.softwareheritage.org/check-deposit-2020-10-08T13:52:34.509655;visit=swh:1:snp:c477c6ef51833127b13a86ece7d75e5b3cc4e93d;anchor=swh:1:rev:f26f3960c175f15f6e24200171d446b86f6f7230;path=/ + check-deposit-2020-10-08T13:52:34.509655 +