Changeset View
Changeset View
Standalone View
Standalone View
swh/deposit/tests/cli/test_client.py
# Copyright (C) 2019-2020 The Software Heritage developers | # Copyright (C) 2019-2020 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 ast | |||||
import contextlib | import contextlib | ||||
import json | import json | ||||
import logging | import logging | ||||
import os | import os | ||||
from unittest.mock import MagicMock | from unittest.mock import MagicMock | ||||
from click.testing import CliRunner | from click.testing import CliRunner | ||||
import pytest | import pytest | ||||
import yaml | |||||
from swh.deposit.cli import deposit as cli | from swh.deposit.cli import deposit as cli | ||||
from swh.deposit.cli.client import InputError, _client, _collection, _url, generate_slug | from swh.deposit.cli.client import InputError, _client, _collection, _url, generate_slug | ||||
from swh.deposit.client import MaintenanceError, PublicApiDepositClient | from swh.deposit.client import MaintenanceError, PublicApiDepositClient | ||||
from swh.deposit.parsers import parse_xml | from swh.deposit.parsers import parse_xml | ||||
from ..conftest import TEST_USER | from ..conftest import TEST_USER | ||||
▲ Show 20 Lines • Show All 391 Lines • ▼ Show 20 Lines | def test_cli_multisteps_deposit(sample_archive, datadir, slug, requests_mock_datadir): | ||||
assert result.exit_code == 0, f"unexpected output: {result.output}" | assert result.exit_code == 0, f"unexpected output: {result.output}" | ||||
assert result.output is not None | assert result.output is not None | ||||
actual_deposit = json.loads(result.output) | actual_deposit = json.loads(result.output) | ||||
# deposit update scenario actually returns a deposit status dict | # deposit update scenario actually returns a deposit status dict | ||||
assert actual_deposit["deposit_id"] == "666" | assert actual_deposit["deposit_id"] == "666" | ||||
# FIXME: should be "deposited" but current limitation in the | # FIXME: should be "deposited" but current limitation in the | ||||
# requests_mock_datadir_visits use, cannot find a way to make it work right now | # requests_mock_datadir_visits use, cannot find a way to make it work right now | ||||
assert actual_deposit["deposit_status"] == "partial" | 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 | |||||
vlorentz: I don't understand this sentence | |||||
ardumontAuthorUnsubmitted Done Inline Actionsneither can i ardumont: neither can i | |||||
ardumontAuthorUnsubmitted Done Inline Actionsneither do* i, i've fixed it. ardumont: neither do* i, i've fixed it. | |||||
moraneggUnsubmitted Not Done Inline Actionsmaybe adding in (e.g ) the possible output formats? moranegg: maybe adding in `(e.g )` the possible output formats?
and if it's a success test or a failure… | |||||
ardumontAuthorUnsubmitted Done Inline Actionsok ;) ardumont: ok ;) | |||||
ardumontAuthorUnsubmitted Done Inline Actionsardumont: D4225 | |||||
""" | |||||
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: (<module>, <log-level>, <log-msg>) | |||||
_, _, result_output = caplog.record_tuples[0] | |||||
else: | |||||
result_output = result.output | |||||
actual_deposit = callable_fn(result_output) | |||||
assert actual_deposit == expected_deposit_dict |
I don't understand this sentence