From 7a9625cd4451c194943aebb356bf7a0a4b3de6a7 Mon Sep 17 00:00:00 2001 From: Pim Kunis Date: Wed, 4 Oct 2023 12:36:54 +0200 Subject: [PATCH] fix PR comments --- borgmatic/hooks/apprise.py | 2 +- tests/unit/hooks/test_apprise.py | 65 +++++++++++++++++++------------- 2 files changed, 39 insertions(+), 28 deletions(-) diff --git a/borgmatic/hooks/apprise.py b/borgmatic/hooks/apprise.py index 5bb2ce4..212bf7d 100644 --- a/borgmatic/hooks/apprise.py +++ b/borgmatic/hooks/apprise.py @@ -21,7 +21,7 @@ def ping_monitor(hook_config, config, config_filename, state, monitoring_log_lev try: import apprise from apprise import NotifyFormat, NotifyType - except ImportError: + except ImportError: # pragma: no cover logger.warning('Unable to import Apprise in monitoring hook') return diff --git a/tests/unit/hooks/test_apprise.py b/tests/unit/hooks/test_apprise.py index 65204c4..b11f769 100644 --- a/tests/unit/hooks/test_apprise.py +++ b/tests/unit/hooks/test_apprise.py @@ -5,14 +5,22 @@ from flexmock import flexmock import borgmatic.hooks.monitor from borgmatic.hooks import apprise as module -topic = 'borgmatic-unit-testing' +TOPIC = 'borgmatic-unit-testing' + + +def mock_apprise(): + apprise_mock = flexmock( + add=lambda servers: None, notify=lambda title, body, body_format, notify_type: None + ) + flexmock(apprise.Apprise).new_instances(apprise_mock) + return apprise_mock def test_ping_monitor_adheres_dry_run(): - flexmock(apprise.Apprise).should_receive('notify').never() + mock_apprise().should_receive('notify').never() module.ping_monitor( - {'services': [{'url': f'ntfys://{topic}', 'label': 'ntfys'}]}, + {'services': [{'url': f'ntfys://{TOPIC}', 'label': 'ntfys'}]}, {}, 'config.yaml', borgmatic.hooks.monitor.State.FAIL, @@ -22,10 +30,10 @@ def test_ping_monitor_adheres_dry_run(): def test_ping_monitor_does_not_hit_with_no_states(): - flexmock(apprise.Apprise).should_receive('notify').never() + mock_apprise().should_receive('notify').never() module.ping_monitor( - {'services': [{'url': f'ntfys://{topic}', 'label': 'ntfys'}], 'states': []}, + {'services': [{'url': f'ntfys://{TOPIC}', 'label': 'ntfys'}], 'states': []}, {}, 'config.yaml', borgmatic.hooks.monitor.State.FAIL, @@ -35,11 +43,16 @@ def test_ping_monitor_does_not_hit_with_no_states(): def test_ping_monitor_hits_fail_by_default(): - flexmock(apprise.Apprise).should_receive('notify').once() + mock_apprise().should_receive('notify').with_args( + title='A borgmatic FAIL event happened', + body='A borgmatic FAIL event happened', + body_format=NotifyFormat.TEXT, + notify_type=NotifyType.FAILURE, + ).once() for state in borgmatic.hooks.monitor.State: module.ping_monitor( - {'services': [{'url': f'ntfys://{topic}', 'label': 'ntfys'}]}, + {'services': [{'url': f'ntfys://{TOPIC}', 'label': 'ntfys'}]}, {}, 'config.yaml', state, @@ -49,7 +62,7 @@ def test_ping_monitor_hits_fail_by_default(): def test_ping_monitor_hits_with_finish_default_config(): - flexmock(apprise.Apprise).should_receive('notify').with_args( + mock_apprise().should_receive('notify').with_args( title='A borgmatic FINISH event happened', body='A borgmatic FINISH event happened', body_format=NotifyFormat.TEXT, @@ -57,7 +70,7 @@ def test_ping_monitor_hits_with_finish_default_config(): ).once() module.ping_monitor( - {'services': [{'url': f'ntfys://{topic}', 'label': 'ntfys'}], 'states': ['finish']}, + {'services': [{'url': f'ntfys://{TOPIC}', 'label': 'ntfys'}], 'states': ['finish']}, {}, 'config.yaml', borgmatic.hooks.monitor.State.FINISH, @@ -67,7 +80,7 @@ def test_ping_monitor_hits_with_finish_default_config(): def test_ping_monitor_hits_with_start_default_config(): - flexmock(apprise.Apprise).should_receive('notify').with_args( + mock_apprise().should_receive('notify').with_args( title='A borgmatic START event happened', body='A borgmatic START event happened', body_format=NotifyFormat.TEXT, @@ -75,7 +88,7 @@ def test_ping_monitor_hits_with_start_default_config(): ).once() module.ping_monitor( - {'services': [{'url': f'ntfys://{topic}', 'label': 'ntfys'}], 'states': ['start']}, + {'services': [{'url': f'ntfys://{TOPIC}', 'label': 'ntfys'}], 'states': ['start']}, {}, 'config.yaml', borgmatic.hooks.monitor.State.START, @@ -85,7 +98,7 @@ def test_ping_monitor_hits_with_start_default_config(): def test_ping_monitor_hits_with_fail_default_config(): - flexmock(apprise.Apprise).should_receive('notify').with_args( + mock_apprise().should_receive('notify').with_args( title='A borgmatic FAIL event happened', body='A borgmatic FAIL event happened', body_format=NotifyFormat.TEXT, @@ -93,7 +106,7 @@ def test_ping_monitor_hits_with_fail_default_config(): ).once() module.ping_monitor( - {'services': [{'url': f'ntfys://{topic}', 'label': 'ntfys'}], 'states': ['fail']}, + {'services': [{'url': f'ntfys://{TOPIC}', 'label': 'ntfys'}], 'states': ['fail']}, {}, 'config.yaml', borgmatic.hooks.monitor.State.FAIL, @@ -103,7 +116,7 @@ def test_ping_monitor_hits_with_fail_default_config(): def test_ping_monitor_hits_with_log_default_config(): - flexmock(apprise.Apprise).should_receive('notify').with_args( + mock_apprise().should_receive('notify').with_args( title='A borgmatic LOG event happened', body='A borgmatic LOG event happened', body_format=NotifyFormat.TEXT, @@ -111,7 +124,7 @@ def test_ping_monitor_hits_with_log_default_config(): ).once() module.ping_monitor( - {'services': [{'url': f'ntfys://{topic}', 'label': 'ntfys'}], 'states': ['log']}, + {'services': [{'url': f'ntfys://{TOPIC}', 'label': 'ntfys'}], 'states': ['log']}, {}, 'config.yaml', borgmatic.hooks.monitor.State.LOG, @@ -120,8 +133,8 @@ def test_ping_monitor_hits_with_log_default_config(): ) -def test_ping_monitor_with_custom_message_title(): - flexmock(apprise.Apprise).should_receive('notify').with_args( +def test_ping_monitor_passes_through_custom_message_title(): + mock_apprise().should_receive('notify').with_args( title='foo', body='bar', body_format=NotifyFormat.TEXT, @@ -130,7 +143,7 @@ def test_ping_monitor_with_custom_message_title(): module.ping_monitor( { - 'services': [{'url': f'ntfys://{topic}', 'label': 'ntfys'}], + 'services': [{'url': f'ntfys://{TOPIC}', 'label': 'ntfys'}], 'states': ['fail'], 'fail': {'title': 'foo', 'body': 'bar'}, }, @@ -142,8 +155,8 @@ def test_ping_monitor_with_custom_message_title(): ) -def test_ping_monitor_with_custom_message_body(): - flexmock(apprise.Apprise).should_receive('notify').with_args( +def test_ping_monitor_passes_through_custom_message_body(): + mock_apprise().should_receive('notify').with_args( title='', body='baz', body_format=NotifyFormat.TEXT, @@ -152,7 +165,7 @@ def test_ping_monitor_with_custom_message_body(): module.ping_monitor( { - 'services': [{'url': f'ntfys://{topic}', 'label': 'ntfys'}], + 'services': [{'url': f'ntfys://{TOPIC}', 'label': 'ntfys'}], 'states': ['fail'], 'fail': {'body': 'baz'}, }, @@ -164,16 +177,14 @@ def test_ping_monitor_with_custom_message_body(): ) -def test_ping_monitor_multiple_services(): - flexmock(apprise.Apprise).should_receive('add').with_args( - [f'ntfys://{topic}', f'ntfy://{topic}'] - ).once() +def test_ping_monitor_pings_multiple_services(): + mock_apprise().should_receive('add').with_args([f'ntfys://{TOPIC}', f'ntfy://{TOPIC}']).once() module.ping_monitor( { 'services': [ - {'url': f'ntfys://{topic}', 'label': 'ntfys'}, - {'url': f'ntfy://{topic}', 'label': 'ntfy'}, + {'url': f'ntfys://{TOPIC}', 'label': 'ntfys'}, + {'url': f'ntfy://{TOPIC}', 'label': 'ntfy'}, ] }, {},