|
| 1 | +import logging |
| 2 | + |
| 3 | +import click |
| 4 | +from click.testing import CliRunner |
| 5 | +import mock |
| 6 | +import pytest |
| 7 | + |
| 8 | +from squishy import cli |
| 9 | +from squishy.consumer import SqsConsumer |
| 10 | +from squishy.noop import noop_callback |
| 11 | +from squishy.workers.noop import NoopWorker |
| 12 | + |
| 13 | + |
| 14 | +def test_get_log_level(): |
| 15 | + for level in cli.LOG_CHOICES.choices: |
| 16 | + assert (cli.get_log_level(None, None, level) == |
| 17 | + getattr(logging, level.upper())) |
| 18 | + |
| 19 | + |
| 20 | +def test_import_callable_func(): |
| 21 | + func = cli.import_callable(None, None, 'squishy.noop:noop_callback') |
| 22 | + assert callable(func) |
| 23 | + assert func(None) is None |
| 24 | + |
| 25 | + |
| 26 | +def test_import_callable_object(): |
| 27 | + obj = cli.import_callable(None, None, 'squishy.noop:noop_callback_obj') |
| 28 | + assert callable(obj) |
| 29 | + assert obj(None) is None |
| 30 | + |
| 31 | + |
| 32 | +@pytest.mark.parametrize('spec', [ |
| 33 | + 'not_importable', |
| 34 | + 'not.importable', |
| 35 | +]) |
| 36 | +def test_import_callable_invalid_import_spec(spec): |
| 37 | + with pytest.raises(click.BadParameter) as e: |
| 38 | + cli.import_callable(None, None, spec) |
| 39 | + assert 'string to import' in e.value.args[0] |
| 40 | + |
| 41 | + |
| 42 | +def test_import_callable_no_attribute(): |
| 43 | + with pytest.raises(click.BadParameter) as e: |
| 44 | + cli.import_callable(None, None, 'squishy.noop:missing_attribute') |
| 45 | + assert 'does not exist' in e.value.args[0] |
| 46 | + |
| 47 | + |
| 48 | +def test_import_callable_attribute_is_not_callable(): |
| 49 | + with pytest.raises(RuntimeError) as e: |
| 50 | + cli.import_callable(None, None, 'squishy.noop:noop_dummy') |
| 51 | + assert 'is not callable' in e.value.args[0] |
| 52 | + |
| 53 | + |
| 54 | +def test_import_callable_none_value(): |
| 55 | + assert cli.import_callable(None, None, None) is None |
| 56 | + |
| 57 | + |
| 58 | +def test_import_worker(): |
| 59 | + func = cli.import_worker(None, None, 'futures_process') |
| 60 | + assert callable(func) |
| 61 | + |
| 62 | + |
| 63 | +def test_import_gevent_worker(): |
| 64 | + import gevent.monkey |
| 65 | + with mock.patch('gevent.monkey', spec=gevent.monkey) as m: |
| 66 | + func = cli.import_worker(None, None, 'gevent') |
| 67 | + assert callable(func) |
| 68 | + assert len(m.patch_socket.mock_calls) == 1 |
| 69 | + assert len(m.patch_ssl.mock_calls) == 1 |
| 70 | + |
| 71 | + |
| 72 | +@mock.patch('squishy.cli.SqsConsumer', spec=SqsConsumer) |
| 73 | +@mock.patch('squishy.workers.noop.NoopWorker', spec=NoopWorker) |
| 74 | +def test_run_consumer(w_mock, c_mock): |
| 75 | + url = 'https://localhost/test/url' |
| 76 | + callback = 'squishy.noop:noop_callback' |
| 77 | + worker = 'noop' |
| 78 | + |
| 79 | + runner = CliRunner() |
| 80 | + result = runner.invoke(cli.run_consumer, |
| 81 | + [url, callback, '--worker-class', worker]) |
| 82 | + |
| 83 | + assert result.exit_code == 0 |
| 84 | + |
| 85 | + consumer_calls = [ |
| 86 | + mock.call(url, use_short_polling=False, worker=w_mock.return_value, |
| 87 | + polling_timeout=20, polling_count=10), |
| 88 | + mock.call().run(), |
| 89 | + ] |
| 90 | + assert c_mock.mock_calls == consumer_calls |
| 91 | + assert w_mock.mock_calls[0] == mock.call(noop_callback, pool_size=8) |
0 commit comments