| Conditions | 26 |
| Total Lines | 96 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like test_multi_plugin_sending() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | import pytest |
||
| 173 | def test_multi_plugin_sending(basicApp, EmptyPlugin): |
||
| 174 | """ |
||
| 175 | Nearly some test as "test_signal_handling_via_plugins", but with an adjustable amount of plugins for sending |
||
| 176 | and receiving. |
||
| 177 | |||
| 178 | Registers receivers before the signal gets registers itself and afterwards. |
||
| 179 | Checks if all receivers get correctly called, if signal is send. |
||
| 180 | |||
| 181 | This tests will normally fail, if Weaknamespace is used in groundwork/signals.py: |
||
| 182 | from blinker import WeakNamespace as Namespace |
||
| 183 | |||
| 184 | So use always Namespace and if need do clean ups on groundwork site. |
||
| 185 | """ |
||
| 186 | |||
| 187 | amount_pre_plugins = 30 |
||
| 188 | amount_post_plugins = 10 |
||
| 189 | amount_send_plugins = 30 |
||
| 190 | |||
| 191 | def create_receiver_function(start): |
||
| 192 | |||
| 193 | def func_template(*args, **kwargs): |
||
| 194 | return ["data_{0}_A".format(start), |
||
| 195 | "data_{0}_B".format(start)] |
||
| 196 | |||
| 197 | return func_template |
||
| 198 | |||
| 199 | # PRE receivers |
||
| 200 | plugin_receive_pre = [] |
||
| 201 | for i in range(0, amount_pre_plugins): |
||
| 202 | plugin_receive_pre.append(EmptyPlugin(app=basicApp, name="Plugin_receive_pre_{0}".format(i))) |
||
| 203 | plugin_receive_pre[i].activate() |
||
| 204 | plugin_receive_pre[i].signals.connect("sig_reg_pre_receiver_{0}".format(i), "signal_test", |
||
| 205 | create_receiver_function(i), |
||
| 206 | "receiver signal_test_{0} for test".format(i)) |
||
| 207 | |||
| 208 | # Count gw internal registered receivers |
||
| 209 | amount = 0 |
||
| 210 | for receiver in basicApp.signals.receivers.keys(): |
||
| 211 | if "sig_reg_pre_receiver" in receiver: |
||
| 212 | amount += 1 |
||
| 213 | assert amount == amount_pre_plugins |
||
| 214 | |||
| 215 | # Senders |
||
| 216 | plugin_send = [] |
||
| 217 | for i in range(0, amount_send_plugins): |
||
| 218 | plugin_send.append(EmptyPlugin(app=basicApp, name="Plugin_send_{0}".format(i))) |
||
| 219 | plugin_send[i].activate() |
||
| 220 | if i == 0: |
||
| 221 | # We only need to register our signal once |
||
| 222 | plugin_send[0].signals.register("signal_test", "signal_test") |
||
| 223 | assert amount_pre_plugins == len(basicApp.signals.signals["signal_test"]._signal.receivers) |
||
| 224 | |||
| 225 | # Check, if for our signal all receivers have been registered |
||
| 226 | print("Registered receivers for signal_test: {0}".format( |
||
| 227 | len(basicApp.signals.signals["signal_test"]._signal.receivers))) |
||
| 228 | assert amount_pre_plugins == len(basicApp.signals.signals["signal_test"]._signal.receivers) |
||
| 229 | |||
| 230 | # Send signal |
||
| 231 | for index, plugin in enumerate(plugin_send): |
||
| 232 | print(" {0} sending...".format(index)) |
||
| 233 | return_values = plugin.signals.send("signal_test") |
||
| 234 | |||
| 235 | # check return length and content |
||
| 236 | assert len(return_values) == amount_pre_plugins |
||
| 237 | for i in range(0, amount_pre_plugins): |
||
| 238 | found = False |
||
| 239 | for value in return_values: |
||
| 240 | if value[1][0] == "data_{0}_A".format(i) and value[1][1] == "data_{0}_B".format(i): |
||
| 241 | found = True |
||
| 242 | break |
||
| 243 | assert found is True |
||
| 244 | |||
| 245 | # Register POST receivers |
||
| 246 | plugin_receive_post = [] |
||
| 247 | for i in range(0, amount_post_plugins): |
||
| 248 | plugin_receive_post.append(EmptyPlugin(app=basicApp, name="Plugin_receive_post_{0}".format(i))) |
||
| 249 | plugin_receive_post[i].activate() |
||
| 250 | plugin_receive_post[i].signals.connect("sig_reg_post_receiver_{0}".format(i), "signal_test", |
||
| 251 | create_receiver_function(amount_pre_plugins + i), |
||
| 252 | "receiver signal_test_{0} for test".format(i)) |
||
| 253 | |||
| 254 | # Send again a signal and check return values |
||
| 255 | # Send signal |
||
| 256 | for index, plugin in enumerate(plugin_send): |
||
| 257 | print(" {0} sending again...".format(index)) |
||
| 258 | return_values = plugin.signals.send("signal_test") |
||
| 259 | |||
| 260 | # check return length and content |
||
| 261 | assert len(return_values) == amount_pre_plugins + amount_post_plugins |
||
| 262 | for i in range(0, amount_pre_plugins + amount_post_plugins): |
||
| 263 | found = False |
||
| 264 | for value in return_values: |
||
| 265 | if value[1][0] == "data_{0}_A".format(i) and value[1][1] == "data_{0}_B".format(i): |
||
| 266 | found = True |
||
| 267 | break |
||
| 268 | assert found is True |
||
| 269 |
The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:
If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.