cleanup: factor out passing in settings
This commit is contained in:
+18
-11
@@ -13,16 +13,23 @@ class Settings(dict):
|
||||
@classmethod
|
||||
def with_defaults(cls):
|
||||
settings = {
|
||||
'OVERLAP': 0.0,
|
||||
'LATITUDE': 50,
|
||||
'LONGITUDE': 5,
|
||||
'CONFIDENCE': 0.7,
|
||||
'DATABASE_LANG': 'en',
|
||||
'PRIVACY_THRESHOLD': 0,
|
||||
'EXTRACTION_LENGTH': 6,
|
||||
'MODEL': 'BirdNET_GLOBAL_6K_V2.4_Model_FP16',
|
||||
'DATA_MODEL_VERSION': 1,
|
||||
'SENSITIVITY': 1.25,
|
||||
'SF_THRESH': 0.003,
|
||||
"OVERLAP": 0.0,
|
||||
"LATITUDE": 50,
|
||||
"LONGITUDE": 5,
|
||||
"CONFIDENCE": 0.7,
|
||||
"DATABASE_LANG": "en",
|
||||
"PRIVACY_THRESHOLD": 0,
|
||||
"EXTRACTION_LENGTH": 6,
|
||||
"MODEL": "BirdNET_GLOBAL_6K_V2.4_Model_FP16",
|
||||
"DATA_MODEL_VERSION": 1,
|
||||
"SENSITIVITY": 1.25,
|
||||
"SF_THRESH": 0.003,
|
||||
"APPRISE_NOTIFICATION_TITLE": "New backyard bird!",
|
||||
"APPRISE_NOTIFY_EACH_DETECTION": "0",
|
||||
"APPRISE_NOTIFY_NEW_SPECIES": "0",
|
||||
"APPRISE_NOTIFY_NEW_SPECIES_EACH_DAY": "0",
|
||||
"APPRISE_MINIMUM_SECONDS_BETWEEN_NOTIFICATIONS_PER_SPECIES": "0",
|
||||
"APPRISE_ONLY_NOTIFY_SPECIES_NAMES": "",
|
||||
"APPRISE_ONLY_NOTIFY_SPECIES_NAMES_2": ""
|
||||
}
|
||||
return cls(settings)
|
||||
|
||||
@@ -1,15 +1,20 @@
|
||||
import os
|
||||
import sqlite3
|
||||
from datetime import datetime
|
||||
import unittest
|
||||
from datetime import datetime
|
||||
from unittest.mock import patch
|
||||
|
||||
from scripts.utils import db
|
||||
from scripts.utils import notifications
|
||||
from scripts.utils.notifications import sendAppriseNotifications
|
||||
|
||||
from tests.helpers import Settings
|
||||
|
||||
class TestAppriseNotifications(unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
db.DB_PATH = self.db_file
|
||||
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
cls.db_file = "test.db"
|
||||
@@ -59,32 +64,33 @@ class TestAppriseNotifications(unittest.TestCase):
|
||||
if os.path.exists(self.apprise_config_file):
|
||||
os.remove(self.apprise_config_file)
|
||||
|
||||
def get_default_params(self):
|
||||
return {
|
||||
"sci_name": "Myiarchus crinitus",
|
||||
"com_name": "Great Crested Flycatcher",
|
||||
"confidence": "0.91",
|
||||
"confidencepct": "91",
|
||||
"path": "filename",
|
||||
"date": "1666-06-06",
|
||||
"time_of_day": "06:06:06",
|
||||
"week": "06",
|
||||
"latitude": "-1",
|
||||
"longitude": "-1",
|
||||
"cutoff": "0.7",
|
||||
"sens": "1.25",
|
||||
"overlap": "0.0"
|
||||
}
|
||||
|
||||
@patch('scripts.utils.helpers._load_settings')
|
||||
@patch('scripts.utils.notifications.notify')
|
||||
def test_notifications(self, mock_notify):
|
||||
def test_notifications(self, mock_notify, mock_load_settings):
|
||||
self.create_test_db()
|
||||
self.create_apprise_config()
|
||||
settings_dict = {
|
||||
"APPRISE_NOTIFICATION_TITLE": "New backyard bird!",
|
||||
"APPRISE_NOTIFY_EACH_DETECTION": "0",
|
||||
"APPRISE_NOTIFY_NEW_SPECIES": "0",
|
||||
"APPRISE_NOTIFY_NEW_SPECIES_EACH_DAY": "0",
|
||||
"APPRISE_MINIMUM_SECONDS_BETWEEN_NOTIFICATIONS_PER_SPECIES": "0"
|
||||
}
|
||||
sendAppriseNotifications("Myiarchus crinitus",
|
||||
"Great Crested Flycatcher",
|
||||
"0.91",
|
||||
"91",
|
||||
"filename",
|
||||
"1666-06-06",
|
||||
"06:06:06",
|
||||
"06",
|
||||
"-1",
|
||||
"-1",
|
||||
"0.7",
|
||||
"1.25",
|
||||
"0.0",
|
||||
settings_dict,
|
||||
self.db_file)
|
||||
notifications.DB_PATH = self.db_file
|
||||
settings_dict = Settings.with_defaults()
|
||||
|
||||
mock_load_settings.return_value = settings_dict
|
||||
sendAppriseNotifications(**self.get_default_params())
|
||||
|
||||
# No active apprise notifications configured. Confirm no notifications.
|
||||
self.assertEqual(mock_notify.call_count, 0)
|
||||
@@ -92,21 +98,8 @@ class TestAppriseNotifications(unittest.TestCase):
|
||||
# Add daily notification.
|
||||
mock_notify.reset_mock()
|
||||
settings_dict["APPRISE_NOTIFY_NEW_SPECIES_EACH_DAY"] = "1"
|
||||
sendAppriseNotifications("Myiarchus crinitus",
|
||||
"Great Crested Flycatcher",
|
||||
"0.91",
|
||||
"91",
|
||||
"filename",
|
||||
"1666-06-06",
|
||||
"06:06:06",
|
||||
"06",
|
||||
"-1",
|
||||
"-1",
|
||||
"0.7",
|
||||
"1.25",
|
||||
"0.0",
|
||||
settings_dict,
|
||||
self.db_file)
|
||||
mock_load_settings.return_value = settings_dict
|
||||
sendAppriseNotifications(**self.get_default_params())
|
||||
|
||||
self.assertEqual(mock_notify.call_count, 1)
|
||||
self.assertEqual(
|
||||
@@ -117,21 +110,8 @@ class TestAppriseNotifications(unittest.TestCase):
|
||||
# Add new species notification.
|
||||
mock_notify.reset_mock()
|
||||
settings_dict["APPRISE_NOTIFY_NEW_SPECIES"] = "1"
|
||||
sendAppriseNotifications("Myiarchus crinitus",
|
||||
"Great Crested Flycatcher",
|
||||
"0.91",
|
||||
"91",
|
||||
"filename",
|
||||
"1666-06-06",
|
||||
"06:06:06",
|
||||
"06",
|
||||
"-1",
|
||||
"-1",
|
||||
"0.7",
|
||||
"1.25",
|
||||
"0.0",
|
||||
settings_dict,
|
||||
self.db_file)
|
||||
mock_load_settings.return_value = settings_dict
|
||||
sendAppriseNotifications(**self.get_default_params())
|
||||
|
||||
self.assertEqual(mock_notify.call_count, 2)
|
||||
self.assertEqual(
|
||||
@@ -146,24 +126,57 @@ class TestAppriseNotifications(unittest.TestCase):
|
||||
# Add each species notification.
|
||||
mock_notify.reset_mock()
|
||||
settings_dict["APPRISE_NOTIFY_EACH_DETECTION"] = "1"
|
||||
sendAppriseNotifications("Myiarchus crinitus",
|
||||
"Great Crested Flycatcher",
|
||||
"0.91",
|
||||
"91",
|
||||
"filename",
|
||||
"1666-06-06",
|
||||
"06:06:06",
|
||||
"06",
|
||||
"-1",
|
||||
"-1",
|
||||
"0.7",
|
||||
"1.25",
|
||||
"0.0",
|
||||
settings_dict,
|
||||
self.db_file)
|
||||
mock_load_settings.return_value = settings_dict
|
||||
sendAppriseNotifications(**self.get_default_params())
|
||||
|
||||
self.assertEqual(mock_notify.call_count, 3)
|
||||
|
||||
@patch('scripts.utils.helpers._load_settings')
|
||||
@patch('scripts.utils.notifications.notify')
|
||||
def test_notifications_excluded(self, mock_notify, mock_load_settings):
|
||||
self.create_test_db()
|
||||
self.create_apprise_config()
|
||||
notifications.DB_PATH = self.db_file
|
||||
settings_dict = Settings.with_defaults()
|
||||
settings_dict["APPRISE_NOTIFY_EACH_DETECTION"] = "1"
|
||||
|
||||
settings_dict['APPRISE_ONLY_NOTIFY_SPECIES_NAMES'] = 'Quailfinch'
|
||||
mock_load_settings.return_value = settings_dict
|
||||
sendAppriseNotifications(**self.get_default_params())
|
||||
|
||||
# Not excluded. Confirm notifications.
|
||||
self.assertEqual(mock_notify.call_count, 1)
|
||||
|
||||
mock_notify.reset_mock()
|
||||
settings_dict['APPRISE_ONLY_NOTIFY_SPECIES_NAMES'] = 'Quailfinch,Great Crested Flycatcher'
|
||||
mock_load_settings.return_value = settings_dict
|
||||
sendAppriseNotifications(**self.get_default_params())
|
||||
|
||||
self.assertEqual(mock_notify.call_count, 0)
|
||||
|
||||
@patch('scripts.utils.helpers._load_settings')
|
||||
@patch('scripts.utils.notifications.notify')
|
||||
def test_notifications_included(self, mock_notify, mock_load_settings):
|
||||
self.create_test_db()
|
||||
self.create_apprise_config()
|
||||
notifications.DB_PATH = self.db_file
|
||||
settings_dict = Settings.with_defaults()
|
||||
settings_dict["APPRISE_NOTIFY_EACH_DETECTION"] = "1"
|
||||
|
||||
settings_dict['APPRISE_ONLY_NOTIFY_SPECIES_NAMES_2'] = 'Quailfinch'
|
||||
mock_load_settings.return_value = settings_dict
|
||||
sendAppriseNotifications(**self.get_default_params())
|
||||
|
||||
# No wanted species. Confirm no notifications.
|
||||
self.assertEqual(mock_notify.call_count, 0)
|
||||
|
||||
mock_notify.reset_mock()
|
||||
settings_dict['APPRISE_ONLY_NOTIFY_SPECIES_NAMES_2'] = 'Quailfinch,Great Crested Flycatcher'
|
||||
mock_load_settings.return_value = settings_dict
|
||||
sendAppriseNotifications(**self.get_default_params())
|
||||
|
||||
self.assertEqual(mock_notify.call_count, 1)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
|
||||
Reference in New Issue
Block a user