Changeset View
Changeset View
Standalone View
Standalone View
swh/loader/tests/test_cli.py
- This file was added.
# Copyright (C) 2019 The Software Heritage developers | |||||
# See the AUTHORS file at the top-level directory of this distribution | |||||
# License: GNU General Public License version 3, or any later version | |||||
# See top-level LICENSE file for more information | |||||
import pytest | |||||
from swh.loader.cli import run, get_loader, SUPPORTED_LOADERS | |||||
from swh.loader.package.loader import PackageLoader | |||||
from click.testing import CliRunner | |||||
from unittest.mock import patch | |||||
def test_get_loader_wrong_input(swh_config): | |||||
"""Unsupported loader should raise | |||||
""" | |||||
loader_type = 'unknown' | |||||
assert loader_type not in SUPPORTED_LOADERS | |||||
with pytest.raises(ValueError, match='Invalid loader'): | |||||
get_loader(loader_type, url='db-url') | |||||
def test_get_loader(swh_config): | |||||
"""Instantiating a supported loader should be ok | |||||
""" | |||||
loader_input = { | |||||
'archive': { | |||||
'url': 'some-url', | |||||
'artifacts': [], | |||||
}, | |||||
'debian': { | |||||
'url': 'some-url', | |||||
'date': 'something', | |||||
'packages': [], | |||||
}, | |||||
'deposit': { | |||||
'url': 'some-url', | |||||
'deposit_id': 1, | |||||
}, | |||||
'npm': { | |||||
'url': 'https://www.npmjs.com/package/onepackage', | |||||
}, | |||||
'pypi': { | |||||
'url': 'some-url', | |||||
}, | |||||
} | |||||
for loader_type, kwargs in loader_input.items(): | |||||
loader = get_loader(loader_type, **kwargs) | |||||
assert isinstance(loader, PackageLoader) | |||||
help_msg = """Usage: run [OPTIONS] [OPTIONS]... | |||||
Loader cli tools | |||||
Load origin url with loader <name> | |||||
Options: | |||||
-t, --type [archive|debian|deposit|npm|pypi] | |||||
Loader to run | |||||
-u, --url TEXT Origin url to load | |||||
-h, --help Show this message and exit. | |||||
""" | |||||
def test_run_help(swh_config): | |||||
"""Help message should be ok | |||||
""" | |||||
runner = CliRunner() | |||||
result = runner.invoke(run, ['-h']) | |||||
assert result.exit_code == 0 | |||||
assert result.output.startswith(help_msg) | |||||
@patch('swh.loader.package.pypi.loader.PyPILoader') | |||||
anlambert: Still this one to replace with mocker fixture | |||||
def test_run_pypi(mock_loader, swh_config): | |||||
"""Trigger a load should be ok | |||||
Not Done Inline ActionsTriggering anlambert: Triggering | |||||
""" | |||||
# mock_loader.load.return_value = {'result': 'eventful'} | |||||
Not Done Inline Actionsthis should be removed I think anlambert: this should be removed I think | |||||
runner = CliRunner() | |||||
result = runner.invoke(run, [ | |||||
'--type', 'pypi', | |||||
'--url', 'https://some-url' | |||||
]) | |||||
assert result.exit_code == 0 | |||||
mock_loader.assert_called_once_with(url='https://some-url') # constructor | |||||
# mock_loader.load.assert_called_once_with() # load | |||||
Not Done Inline Actionssame here anlambert: same here |
Still this one to replace with mocker fixture