Changeset 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 | ||||
ardumont: why don't you make that a fixture?
```
@pytest.fixture
def cli_client(mocker):
mock_client… | |||||
'service': { | |||||
'workspace': { | |||||
'collection': { | |||||
'sword:name': 'softcol', | |||||
} | |||||
} | |||||
} | |||||
} | |||||
collection_name = _collection(mock_client) | collection_name = _collection(mock_client) | ||||
assert collection_name == 'softcol' | assert collection_name == 'softcol' | ||||
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 == [ | |||||
ardumontUnsubmitted Done Inline Actionsthat's interesting! [1] [1] https://docs.pytest.org/en/latest/logging.html#caplog-fixture ardumont: that's interesting! [1]
[1] https://docs.pytest.org/en/latest/logging.html#caplog-fixture | |||||
('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' | |||||
ardumontUnsubmitted Not Done Inline Actionsjsyk, as @moranegg probably already told you this is kind of a pain in deposit. The slug is a word used in the sword v2 protocol (it makes no sense to me). We decided to use this as external_id, so whenever you see that, think external_id as a unique identifier for the client system... I don't like the fact that we use both terms but i was a bit worried to unify it under what term and hide that somewhere at the time. Maybe it'd make more sense to rename it once and for all though (later, not right now ;). ardumont: jsyk, as @moranegg probably already told you this is kind of a pain in deposit.
The slug is a… | |||||
ardumontUnsubmitted Not Done Inline Actionss/what term/one term/ ardumont: s/what term/one term/ | |||||
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, | |||||
]) | |||||
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=metadata_path, | |||||
slug=slug) | |||||
# Clear mocking state | |||||
caplog.clear() | |||||
mock_client.reset_mock() |
why don't you make that a fixture?
That will unclutter a bit the scenario below.
If that makes sense.
Note:
you can use the factory pattern if you need some different output in the deposit.create.return_value mock.