Skip to content

A live bird collage card for your Home Assistant dashboard.

add basic test

frederik2649ae972d4b
diff --git a/tests/helpers.py b/tests/helpers.py
new file mode 100644
index 00000000..679abb45
--- /dev/null
+++ b/tests/helpers.py
@@ -0,0 +1,28 @@
+import os
+
+TESTDATA = os.path.join(os.path.dirname(__file__), 'testdata')
+
+
+class Settings(dict):
+ def getint(self, key):
+ return int(self.get(key))
+
+ def getfloat(self, key):
+ return float(self.get(key))
+
+ @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,
+ }
+ return cls(settings)
diff --git a/tests/test_server.py b/tests/test_server.py
new file mode 100644
index 00000000..8487f8ac
--- /dev/null
+++ b/tests/test_server.py
@@ -0,0 +1,40 @@
+import os
+import unittest
+from unittest.mock import patch
+
+from scripts.server import run_analysis
+from scripts.utils.classes import ParseFileName
+from tests.helpers import TESTDATA, Settings
+
+
+class TestRunAnalysis(unittest.TestCase):
+
+ @patch('scripts.utils.helpers._load_settings')
+ @patch('scripts.server.loadCustomSpeciesList')
+ def test_run_analysis(self, mock_loadCustomSpeciesList, mock_load_settings):
+ # Mock the settings and species list
+ mock_load_settings.return_value = Settings.with_defaults()
+ mock_loadCustomSpeciesList.return_value = []
+
+ # Test file
+ test_file = ParseFileName(os.path.join(TESTDATA, '2024-02-24-birdnet-16:19:37.wav'))
+
+ # Expected results
+ expected_results = [
+ {"confidence": 0.912, 'sci_name': 'Pica pica'},
+ {"confidence": 0.9316, 'sci_name': 'Pica pica'},
+ {"confidence": 0.8857, 'sci_name': 'Pica pica'}
+ ]
+
+ # Run the analysis
+ detections = run_analysis(test_file)
+
+ # Assertions
+ self.assertEqual(len(detections), len(expected_results))
+ for det, expected in zip(detections, expected_results):
+ self.assertAlmostEqual(det.confidence, expected['confidence'], delta=1e-4)
+ self.assertEqual(det.scientific_name, expected['sci_name'])
+
+
+if __name__ == '__main__':
+ unittest.main()