1
|
|
|
import asyncio |
2
|
|
|
from unittest import mock |
3
|
|
|
|
4
|
|
|
import pytest |
5
|
|
|
|
6
|
|
|
from loafer.dispatchers import LoaferDispatcher |
|
|
|
|
7
|
|
|
from loafer.exceptions import ProviderError, ConfigurationError |
8
|
|
|
from loafer.managers import LoaferManager |
9
|
|
|
from loafer.routes import Route |
|
|
|
|
10
|
|
|
from loafer.runners import LoaferRunner |
11
|
|
|
|
12
|
|
|
|
13
|
|
|
@pytest.fixture |
14
|
|
|
def dummy_route(dummy_provider): |
15
|
|
|
return Route(dummy_provider, handler=mock.Mock()) |
16
|
|
|
|
17
|
|
|
|
18
|
|
|
def test_dispatcher_invalid_routes(): |
19
|
|
|
manager = LoaferManager(routes=[]) |
20
|
|
|
with pytest.raises(ConfigurationError): |
21
|
|
|
manager.dispatcher |
|
|
|
|
22
|
|
|
|
23
|
|
|
|
24
|
|
|
def test_dispatcher_invalid_route_instance(): |
|
|
|
|
25
|
|
|
manager = LoaferManager(routes=[mock.Mock()]) |
26
|
|
|
with pytest.raises(ConfigurationError): |
27
|
|
|
manager.dispatcher |
|
|
|
|
28
|
|
|
|
29
|
|
|
|
30
|
|
|
def test_dispatcher_without_enabled_routes(dummy_route): |
|
|
|
|
31
|
|
|
dummy_route.enabled = False |
32
|
|
|
manager = LoaferManager(routes=[dummy_route]) |
33
|
|
|
with pytest.raises(ConfigurationError): |
34
|
|
|
manager.dispatcher |
|
|
|
|
35
|
|
|
|
36
|
|
|
|
37
|
|
|
def test_dispatcher(dummy_route): |
38
|
|
|
manager = LoaferManager(routes=[dummy_route]) |
39
|
|
|
assert manager.dispatcher |
40
|
|
|
assert isinstance(manager.dispatcher, LoaferDispatcher) |
41
|
|
|
|
42
|
|
|
|
43
|
|
|
def test_default_runner(): |
44
|
|
|
manager = LoaferManager(routes=[]) |
45
|
|
|
assert manager.runner |
46
|
|
|
assert isinstance(manager.runner, LoaferRunner) |
47
|
|
|
|
48
|
|
|
|
49
|
|
|
def test_custom_runner(): |
50
|
|
|
runner = mock.Mock() |
51
|
|
|
manager = LoaferManager(routes=[], runner=runner) |
52
|
|
|
assert manager.runner |
53
|
|
|
assert isinstance(manager.runner, mock.Mock) |
54
|
|
|
|
55
|
|
|
|
56
|
|
|
def test_on_future_errors(): |
57
|
|
|
manager = LoaferManager(routes=[]) |
58
|
|
|
manager.runner = mock.Mock() |
59
|
|
|
future = asyncio.Future() |
60
|
|
|
future.set_exception(ProviderError) |
61
|
|
|
manager.on_future__errors(future) |
62
|
|
|
|
63
|
|
|
assert manager.runner.stop.called |
64
|
|
|
assert manager.runner.stop.called_once_with() |
65
|
|
|
|
66
|
|
|
|
67
|
|
|
def test_on_loop__stop(): |
68
|
|
|
manager = LoaferManager(routes=[]) |
69
|
|
|
manager.dispatcher = mock.Mock() |
70
|
|
|
manager._future = mock.Mock() |
|
|
|
|
71
|
|
|
manager.on_loop__stop() |
72
|
|
|
|
73
|
|
|
assert manager.dispatcher.stop.called |
74
|
|
|
assert manager._future.cancel.called |
|
|
|
|
75
|
|
|
|