Completed
Push — master ( 52de43...bc9f46 )
by Daniel
01:15
created

test_signal_registration()   A

Complexity

Conditions 3

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 1
Metric Value
cc 3
c 3
b 1
f 1
dl 0
loc 5
rs 9.4285
1
import pytest
2
3
4
def test_signal_registration(basicApp):
5
    plugin = basicApp.plugins.get("BasicPlugin")
6
    signals = plugin.signals.get()
7
    assert len(signals.keys()) == 1
8
    assert "test" in signals.keys()
9
10
11
def test_signal_send(basicApp):
12
    answers = basicApp.signals.send("test", basicApp, text="test")
13
    assert len(answers) == 1
14
    assert isinstance(answers, list)
15
    assert len(answers[0]) == 2
16
    assert isinstance(answers[0], tuple)
17
    assert answers[0][1] == {'text': 'test'}
18
19
20
def test_signal_connect(basicApp):
21
    def _test_command(plugin, **kwargs):
22
        return "12345"
23
24
    plugin = basicApp.plugins.get("BasicPlugin")
25
    plugin.signals.connect("12345 receiver", "test", _test_command, "receiver 12345 for test")
26
    answers = plugin.signals.send("test")
27
    assert len(answers) == 2
28
    for answer in answers:
29
        if answer[0] == _test_command:
30
            assert answer[1] == "12345"
31
32
33
def test_signal_disconnect(basicApp):
34
    def _test_command(plugin, **kwargs):
35
        return "12345"
36
37
    plugin = basicApp.plugins.get("BasicPlugin")
38
    plugin.signals.connect("12345 receiver", "test", _test_command, "receiver 12345 for test")
39
    answers = plugin.signals.send("test")
40
    assert len(answers) == 2
41
    for answer in answers:
42
        if answer[0] == _test_command:
43
            assert answer[1] == "12345"
44
45
    plugin.signals.disconnect("12345 receiver")
46
    answers = plugin.signals.send("test")
47
    assert len(answers) == 1
48
49
50
def test_receiver_get(basicApp):
51
    def _test_command(plugin, **kwargs):
52
        return "12345"
53
54
    plugin = basicApp.plugins.get("BasicPlugin")
55
    receivers = plugin.signals.get_receiver()
56
    assert len(receivers) == 1  # test receiver
57
    plugin.signals.connect("12345 receiver", "test", _test_command, "receiver 12345 for test")
58
    receivers = plugin.signals.get_receiver()
59
    assert len(receivers) == 2
60
    plugin.deactivate()
61
    receivers = plugin.signals.get_receiver()
62
    assert len(receivers) == 0
63
64
65
def test_multi_plugin_deactivations(basicApp, EmptyPlugin):
66
    def test():
67
        pass
68
69
    plugin = basicApp.plugins.get("BasicPlugin")
70
    plugin2 = EmptyPlugin(app=basicApp, name="EmptyPlugin")
71
    plugin2.activate()
72
    assert plugin2.active is True
73
74
    plugin2.signals.register("test123", "test123 description")
75
    plugin2_signals = plugin2.signals.get()
76
    assert len(plugin2_signals) == 1
77
78
    plugin_signals = plugin.signals.get()
79
    # BasicPlugin has already registered a signal during init
80
    assert len(plugin_signals) == 1
81
82
    plugin2.deactivate()
83
    plugin2_signals = plugin2.signals.get()
84
    assert len(plugin2_signals) == 0
85
    assert plugin2.active is False
86
87
    assert len(plugin_signals) == 1
88
    assert plugin.active is True
89
90
91
def test_signal_handling(basicApp, EmptyPlugin):
92
    from blinker import NamedSignal
93
    from groundwork.signals import UnknownSignal
94
95
    def sig_reg_test(plugin, **kwargs):
96
        return ["data_1", "data_2"]
97
98
    plugin1 = EmptyPlugin(app=basicApp, name="EmptyPlugin")
99
100
    # This creates a gw receiver and a blinker internal signal called "test"
101
    plugin1.signals.connect("sig_reg_receiver", "sig_reg_test", sig_reg_test, "receiver sig_reg for test")
102
103
    # Check the blinker internal registration
104
    signal_namespace = basicApp.signals._namespace
105
    assert "sig_reg_test" in signal_namespace.keys()
106
    test_signal = signal_namespace["sig_reg_test"]
107
    assert isinstance(test_signal, NamedSignal)
108
109
    # Check the gw registration
110
    plugin_signals = plugin1.signals.get()
111
    # This must be 0, because gw has not registered a signal, this was done in blinker only
112
    assert len(plugin_signals) == 0
113
114
    app_signals = basicApp.signals.get()
115
    assert "sig_reg_test" not in app_signals.keys()
116
117
    # Check if signal sending throws an error (without a registered gw signal)
118
    with pytest.raises(UnknownSignal):
119
        plugin1.signals.send("sig_reg_test")
120
121
    # Check if gw has a registered receiver
122
    plugin_receivers = plugin1.signals.get_receiver()
123
    assert "sig_reg_receiver" in plugin_receivers.keys()
124
125
    # Register the missing gw signal
126
    plugin_signal = plugin1.signals.register("sig_reg_test", "signal for sig_reg_test")
127
    assert plugin_signal is not None
128
129
    # Recheck the gw registration
130
    plugin_signals = plugin1.signals.get()
131
    # This must be 1, because gw has registered a signal now
132
    assert len(plugin_signals) == 1
133
134
    # Sending should work now
135
    return_values = plugin1.signals.send("sig_reg_test")
136
    # Check if the receivers sends back needed data
137
    assert len(return_values) == 1
138
    assert return_values[0][1][0] == "data_1"
139
140
141
def test_signal_handling_via_plugins(basicApp, EmptyPlugin):
142
    def sig_reg_test_1(plugin, **kwargs):
143
        return ["data_1", "data_2"]
144
145
    def sig_reg_test_2(plugin, **kwargs):
146
        return ["data_3", "data_4"]
147
148
    plugin_send = EmptyPlugin(app=basicApp, name="Plugin_send")
149
150
    plugin_receive_pre = EmptyPlugin(app=basicApp, name="Plugin_receive_pre")
151
    plugin_receive_post = EmptyPlugin(app=basicApp, name="Plugin_receive_post")
152
153
    plugin_receive_pre.activate()
154
    plugin_receive_pre.signals.connect("sig_reg_pre_receiver", "signal_test",
155
                                       sig_reg_test_1, "receiver signal_test for test")
156
157
    plugin_send.activate()
158
    plugin_send.signals.register("signal_test", "signal_test doc")
159
160
    return_values = plugin_send.signals.send("signal_test")
161
    assert len(return_values) == 1
162
163
    plugin_receive_post.activate()
164
    plugin_receive_post.signals.connect("sig_reg_post_receiver", "signal_test",
165
                                        sig_reg_test_2, "receiver signal_test for test")
166
167
    return_values = plugin_send.signals.send("signal_test")
168
    assert len(return_values) == 2
169