Skip to content

Commit

Permalink
Fix bug in 'flush' CLI command
Browse files Browse the repository at this point in the history
  • Loading branch information
epandurski committed Nov 22, 2018
1 parent bd12809 commit c92e166
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 11 deletions.
8 changes: 4 additions & 4 deletions flask_signalbus/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,16 +156,16 @@ def get_signal_models(self):
)
]

def flush(self, model=None):
def flush(self, models=None):
"""Send all pending signals over the message bus.
:param model: If passed, flushes only signals of the specified type.
:type model: `signal-model` or `None`
:param models: If passed, flushes only signals of the specified types.
:type models: list(`signal-model`) or `None`
:return: The total number of signals that have been sent
"""

models_to_flush = [model] if model else self.get_signal_models()
models_to_flush = self.get_signal_models() if models is None else models
try:
pks_to_flush = {}
for model in models_to_flush:
Expand Down
8 changes: 3 additions & 5 deletions flask_signalbus/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,13 @@ def flush(signal_names, exclude):
click.echo('Warning: Specified both SIGNAL_NAMES and exclude option.')
if signal_names:
wrong_signal_names = signal_names - {m.__name__ for m in models_to_flush}
models_to_flush = [m for m in models_to_flush if type(m).__name__ in signal_names]
models_to_flush = [m for m in models_to_flush if m.__name__ in signal_names]
else:
wrong_signal_names = exclude - {m.__name__ for m in models_to_flush}
for name in wrong_signal_names:
click.echo('Warning: A signal with name "{}" does not exist.'.format(name))
models_to_flush = [m for m in models_to_flush if type(m).__name__ not in exclude]
signal_count = 0
for model in models_to_flush:
signal_count += signalbus.flush()
models_to_flush = [m for m in models_to_flush if m.__name__ not in exclude]
signal_count = signalbus.flush(models_to_flush)
if signal_count == 1:
click.echo('{} signal has been successfully processed.'.format(signal_count))
elif signal_count > 1:
Expand Down
4 changes: 2 additions & 2 deletions tests/test_basics.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@ def test_create_signalbus_directly_no_app():

def test_flush_signal_model(app, signalbus, Signal):
assert len(signalbus.get_signal_models()) == 1
signalbus.flush(Signal)
signalbus.flush([Signal])


def test_flush_nonsignal_model(app, signalbus, NonSignal):
assert len(signalbus.get_signal_models()) == 0
with pytest.raises(RuntimeError):
signalbus.flush(NonSignal)
signalbus.flush([NonSignal])


def test_flush_all_signal_models(app, signalbus, Signal, NonSignal):
Expand Down
28 changes: 28 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,34 @@ def test_flush_pending(app, signalbus_with_pending_signal):
assert 'processed' in result.output


def test_flush_pending_explicit(app, signalbus_with_pending_signal):
runner = app.test_cli_runner()
result = runner.invoke(args=['signalbus', 'flush', 'Signal'])
assert '1' in result.output
assert 'processed' in result.output


def test_flush_pending_explicit_wrong_name(app, signalbus_with_pending_signal):
runner = app.test_cli_runner()
result = runner.invoke(args=['signalbus', 'flush', 'WrongSignalName'])
assert not ('1' in result.output and 'processed' in result.output)
assert 'Warning' in result.output


def test_flush_pending_exclude(app, signalbus_with_pending_signal):
runner = app.test_cli_runner()
result = runner.invoke(args=['signalbus', 'flush', '--exclude', 'Signal'])
assert not result.output


def test_flush_pending_exclude_wrong_name(app, signalbus_with_pending_signal):
runner = app.test_cli_runner()
result = runner.invoke(args=['signalbus', 'flush', '--exclude', 'WrongSignalName'])
assert 'Warning' in result.output
assert '1' in result.output
assert 'processed' in result.output


def test_flush_error(app, signalbus_with_pending_error):
runner = app.test_cli_runner()
result = runner.invoke(args=['signalbus', 'flush'])
Expand Down

0 comments on commit c92e166

Please sign in to comment.