Changeset View
Changeset View
Standalone View
Standalone View
swh/deposit/tests/cli/test_client.py
# Copyright (C) 2019 The Software Heritage developers | # Copyright (C) 2019 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 logging | ||||
import os | |||||
from unittest.mock import MagicMock | from unittest.mock import MagicMock | ||||
from click.testing import CliRunner | |||||
import pytest | |||||
from swh.deposit.client import PublicApiDepositClient | from swh.deposit.client import PublicApiDepositClient | ||||
from swh.deposit.cli.client import _url, _client, _collection, InputError | from swh.deposit.cli.client import ( | ||||
generate_slug, _url, _client, _collection, InputError) | |||||
from swh.deposit.cli import deposit as cli | |||||
from ..conftest import TEST_USER | |||||
EXAMPLE_SERVICE_DOCUMENT = { | |||||
'service': { | |||||
'workspace': { | |||||
'collection': { | |||||
'sword:name': 'softcol', | |||||
} | |||||
} | |||||
} | |||||
} | |||||
def test_url(): | def test_url(): | ||||
assert _url('http://deposit') == 'http://deposit/1' | assert _url('http://deposit') == 'http://deposit/1' | ||||
assert _url('https://other/1') == 'https://other/1' | assert _url('https://other/1') == 'https://other/1' | ||||
def test_client(): | def test_client(): | ||||
Show All 10 Lines | def test_collection_error(): | ||||
with pytest.raises(InputError) as e: | with pytest.raises(InputError) as e: | ||||
_collection(mock_client) | _collection(mock_client) | ||||
assert 'Service document retrieval: something went wrong' == str(e.value) | assert 'Service document retrieval: something went wrong' == str(e.value) | ||||
def test_collection_ok(): | def test_collection_ok(): | ||||
mock_client = MagicMock() | mock_client = MagicMock() | ||||
mock_client.service_document.return_value = { | mock_client.service_document.return_value = EXAMPLE_SERVICE_DOCUMENT | ||||
'service': { | |||||
'workspace': { | |||||
'collection': { | |||||
'sword:name': 'softcol', | |||||
} | |||||
} | |||||
} | |||||
} | |||||
collection_name = _collection(mock_client) | collection_name = _collection(mock_client) | ||||
assert collection_name == 'softcol' | assert collection_name == 'softcol' | ||||
ardumont: Same as slug/external_id shenanigans.
We have 2 terms for the same meaning, partial and in… | |||||
def test_single_minimal_deposit(sample_archive, mocker, caplog): | |||||
""" from: | |||||
https://docs.softwareheritage.org/devel/swh-deposit/getting-started.html#single-deposit | |||||
""" # noqa | |||||
slug = generate_slug() | |||||
mocker.patch('swh.deposit.cli.client.generate_slug', return_value=slug) | |||||
mock_client = MagicMock() | |||||
mocker.patch( | |||||
'swh.deposit.cli.client._client', | |||||
return_value=mock_client) | |||||
mock_client.service_document.return_value = EXAMPLE_SERVICE_DOCUMENT | |||||
mock_client.deposit_create.return_value = '{"foo": "bar"}' | |||||
runner = CliRunner() | |||||
result = runner.invoke(cli, [ | |||||
'upload', | |||||
'--url', 'mock://deposit.swh/1', | |||||
'--username', TEST_USER['username'], | |||||
'--password', TEST_USER['password'], | |||||
'--name', 'test-project', | |||||
'--archive', sample_archive['path'], | |||||
]) | |||||
assert result.exit_code == 0, result.output | |||||
assert result.output == '' | |||||
assert caplog.record_tuples == [ | |||||
('swh.deposit.cli.client', logging.INFO, '{"foo": "bar"}'), | |||||
] | |||||
mock_client.deposit_create.assert_called_once_with( | |||||
archive=sample_archive['path'], | |||||
collection='softcol', in_progress=False, metadata=None, | |||||
slug=slug) | |||||
def test_single_deposit_slug_collection(sample_archive, mocker, caplog): | |||||
""" from: | |||||
https://docs.softwareheritage.org/devel/swh-deposit/getting-started.html#single-deposit | |||||
""" # noqa | |||||
slug = 'my-slug' | |||||
collection = 'my-collection' | |||||
mock_client = MagicMock() | |||||
mocker.patch( | |||||
'swh.deposit.cli.client._client', | |||||
return_value=mock_client) | |||||
mock_client.service_document.return_value = EXAMPLE_SERVICE_DOCUMENT | |||||
mock_client.deposit_create.return_value = '{"foo": "bar"}' | |||||
runner = CliRunner() | |||||
result = runner.invoke(cli, [ | |||||
'upload', | |||||
'--url', 'mock://deposit.swh/1', | |||||
'--username', TEST_USER['username'], | |||||
'--password', TEST_USER['password'], | |||||
'--name', 'test-project', | |||||
'--archive', sample_archive['path'], | |||||
'--slug', slug, | |||||
'--collection', collection, | |||||
]) | |||||
assert result.exit_code == 0, result.output | |||||
assert result.output == '' | |||||
assert caplog.record_tuples == [ | |||||
('swh.deposit.cli.client', logging.INFO, '{"foo": "bar"}'), | |||||
] | |||||
mock_client.deposit_create.assert_called_once_with( | |||||
archive=sample_archive['path'], | |||||
collection=collection, in_progress=False, metadata=None, | |||||
slug=slug) | |||||
def test_multisteps_deposit( | |||||
sample_archive, atom_dataset, mocker, caplog, datadir): | |||||
""" from: | |||||
https://docs.softwareheritage.org/devel/swh-deposit/getting-started.html#multisteps-deposit | |||||
""" # noqa | |||||
slug = generate_slug() | |||||
mocker.patch('swh.deposit.cli.client.generate_slug', return_value=slug) | |||||
mock_client = MagicMock() | |||||
mocker.patch( | |||||
'swh.deposit.cli.client._client', | |||||
return_value=mock_client) | |||||
# https://docs.softwareheritage.org/devel/swh-deposit/getting-started.html#create-an-incomplete-deposit | |||||
mock_client.service_document.return_value = EXAMPLE_SERVICE_DOCUMENT | |||||
mock_client.deposit_create.return_value = '{"deposit_id": "42"}' | |||||
runner = CliRunner() | |||||
result = runner.invoke(cli, [ | |||||
'upload', | |||||
'--url', 'mock://deposit.swh/1', | |||||
'--username', TEST_USER['username'], | |||||
'--password', TEST_USER['password'], | |||||
'--archive', sample_archive['path'], | |||||
'--partial', | |||||
]) | |||||
assert result.exit_code == 0, result.output | |||||
assert result.output == '' | |||||
assert caplog.record_tuples == [ | |||||
('swh.deposit.cli.client', logging.INFO, '{"deposit_id": "42"}'), | |||||
] | |||||
mock_client.deposit_create.assert_called_once_with( | |||||
archive=sample_archive['path'], | |||||
collection='softcol', in_progress=True, metadata=None, | |||||
slug=slug) | |||||
# Clear mocking state | |||||
caplog.clear() | |||||
mock_client.reset_mock() | |||||
# https://docs.softwareheritage.org/devel/swh-deposit/getting-started.html#add-content-or-metadata-to-the-deposit | |||||
metadata_path = os.path.join( | |||||
datadir, 'atom', 'entry-data-deposit-binary.xml') | |||||
result = runner.invoke(cli, [ | |||||
'upload', | |||||
'--url', 'mock://deposit.swh/1', | |||||
'--username', TEST_USER['username'], | |||||
'--password', TEST_USER['password'], | |||||
'--metadata', metadata_path, | |||||
'--partial', | |||||
]) | |||||
assert result.exit_code == 0, result.output | |||||
assert result.output == '' | |||||
assert caplog.record_tuples == [ | |||||
('swh.deposit.cli.client', logging.INFO, '{"deposit_id": "42"}'), | |||||
] | |||||
mock_client.deposit_create.assert_called_once_with( | |||||
archive=None, | |||||
collection='softcol', in_progress=True, metadata=metadata_path, | |||||
slug=slug) | |||||
# Clear mocking state | |||||
caplog.clear() | |||||
mock_client.reset_mock() | |||||
# https://docs.softwareheritage.org/devel/swh-deposit/getting-started.html#finalize-deposit | |||||
result = runner.invoke(cli, [ | |||||
'upload', | |||||
'--url', 'mock://deposit.swh/1', | |||||
'--username', TEST_USER['username'], | |||||
'--password', TEST_USER['password'], | |||||
]) | |||||
assert result.exit_code == 0, result.output | |||||
assert result.output == '' | |||||
assert caplog.record_tuples == [ | |||||
('swh.deposit.cli.client', logging.INFO, '{"deposit_id": "42"}'), | |||||
] | |||||
mock_client.deposit_create.assert_called_once_with( | |||||
archive=None, | |||||
collection='softcol', in_progress=False, metadata=None, | |||||
slug=slug) |
Same as slug/external_id shenanigans.
We have 2 terms for the same meaning, partial and in-progress deposit are the same thing.
Maybe we should think about unifying this once and for all as partial (choosing partial here because 1. it's shorter 2. it's public facing).
Again, not for now ;)