Skip to content
This repository was archived by the owner on Jul 6, 2022. It is now read-only.

Commit 68774bd

Browse files
committed
added more tests
1 parent c6ebd1c commit 68774bd

File tree

7 files changed

+157
-8
lines changed

7 files changed

+157
-8
lines changed

squishy/noop.py

+10
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,19 @@
99
log = logging.getLogger(__name__)
1010

1111

12+
noop_dummy = object()
13+
14+
1215
def noop_callback(message):
1316
log.info(pprint.pformat(message))
1417

1518

19+
class NoopCallback(object): # pylint: disable=too-few-public-methods
20+
def __call__(self, message):
21+
log.info(pprint.pformat(message))
22+
23+
noop_callback_obj = NoopCallback()
24+
25+
1626
def noop_session_factory():
1727
return Session()

squishy/workers/__init__.py

+6-5
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22

33

44
WORKER_CLASSES = {
5-
'gevent': 'squishy.workers.gevent.GeventWorker',
6-
'futures_process': 'squishy.workers.futures.ProcessPoolWorker',
7-
'futures_thread': 'squishy.workers.futures.ThreadPoolWorker',
8-
'mp_process': 'squishy.workers.multiprocessing.ProcessPoolWorker',
9-
'mp_thread': 'squishy.workers.multiprocessing.ThreadPoolWorker',
5+
'gevent': 'squishy.workers.gevent:GeventWorker',
6+
'futures_process': 'squishy.workers.futures:ProcessPoolWorker',
7+
'futures_thread': 'squishy.workers.futures:ThreadPoolWorker',
8+
'mp_process': 'squishy.workers.multiprocessing:ProcessPoolWorker',
9+
'mp_thread': 'squishy.workers.multiprocessing:ThreadPoolWorker',
10+
'noop': 'squishy.workers.noop:NoopWorker',
1011
}

squishy/workers/noop.py

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from __future__ import absolute_import
2+
3+
4+
class NoopWorker(object):
5+
__name__ = 'NoopWorker'
6+
7+
def __init__(self, *args, **kwargs):
8+
pass
9+
10+
def process_messages(self, _):
11+
pass
12+
13+
def shutdown(self):
14+
pass

tests/test_cli.py

+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
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)

tests/test_logging.py

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import logging
2+
3+
import squishy.logging as squishy_logging
4+
5+
6+
def test_get_logger():
7+
name = 'testing'
8+
logger = squishy_logging.get_logger(name)
9+
assert logger.name == name
10+
assert isinstance(logger.handlers[0], logging.NullHandler)

tests/test_noop.py

+13-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,13 @@
1-
def test_noop():
2-
return True
1+
from __future__ import absolute_import
2+
3+
from boto3 import Session
4+
5+
from squishy import noop
6+
7+
8+
def test_noop_callback():
9+
assert noop.noop_callback(None) is None
10+
11+
12+
def test_noop_session_factory():
13+
assert isinstance(noop.noop_session_factory(), Session)

tests/test_workers.py

+13-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import pytest
44

5-
from squishy.workers import futures, gevent, multiprocessing
5+
from squishy.workers import base, futures, gevent, multiprocessing
66

77

88
# we declare Callback, Errback and Message mocks here rather than using
@@ -50,3 +50,15 @@ def test_process_messages_with_exception(worker_cls):
5050
to_delete = w.process_messages([msg])
5151
w.shutdown()
5252
assert len(to_delete) == 0
53+
54+
55+
def test_base_process_messages():
56+
w = base.BaseWorker(None)
57+
with pytest.raises(NotImplementedError):
58+
w.process_messages(None)
59+
60+
61+
def test_base_shutdown():
62+
w = base.BaseWorker(None)
63+
with pytest.raises(NotImplementedError):
64+
w.shutdown()

0 commit comments

Comments
 (0)