TasmotaManager/tests/test_pattern_matching.py
2025-10-28 00:21:08 +00:00

156 lines
6.9 KiB
Python
Executable File

#!/usr/bin/env python3
"""
Test script to verify the regex pattern matching functionality in TasmotaManager.py.
This script tests both the is_hostname_unknown and is_device_excluded functions with
various patterns and parameters to ensure they work correctly after refactoring.
"""
import logging
import unittest
from unittest.mock import patch, MagicMock
# Configure logging
logging.basicConfig(
level=logging.DEBUG,
format='%(asctime)s - %(levelname)s - %(message)s',
datefmt='%Y-%m-%d %H:%M:%S'
)
logger = logging.getLogger(__name__)
# Import TasmotaManager class
import sys
import os
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
from TasmotaManager import TasmotaDiscovery
class TestPatternMatching(unittest.TestCase):
"""Test cases for pattern matching functionality."""
def setUp(self):
"""Set up test environment."""
self.discovery = TasmotaDiscovery(debug=True)
# Create a mock config
self.discovery.config = {
'unifi': {
'network_filter': {
'test_network': {
'exclude_patterns': [
"^homeassistant*",
"^.*sonos.*",
"^printer$"
],
'unknown_device_patterns': [
"^tasmota_*",
"^tasmota-*",
"^esp-*",
"^ESP-*"
]
}
}
}
}
def test_is_hostname_unknown_basic(self):
"""Test basic hostname matching in is_hostname_unknown."""
# Should match
self.assertTrue(self.discovery.is_hostname_unknown("tasmota_123"))
self.assertTrue(self.discovery.is_hostname_unknown("tasmota-456"))
self.assertTrue(self.discovery.is_hostname_unknown("esp-abcd"))
self.assertTrue(self.discovery.is_hostname_unknown("ESP-EFGH"))
# Should not match
self.assertFalse(self.discovery.is_hostname_unknown("mydevice"))
self.assertFalse(self.discovery.is_hostname_unknown("not-tasmota"))
self.assertFalse(self.discovery.is_hostname_unknown("espresso"))
logger.info("Basic hostname matching tests passed")
def test_is_hostname_unknown_with_ip(self):
"""Test is_hostname_unknown with IP parameter."""
# Should always return True when IP is provided
self.assertTrue(self.discovery.is_hostname_unknown("", ip="192.168.1.100"))
self.assertTrue(self.discovery.is_hostname_unknown("mydevice", ip="192.168.1.100"))
logger.info("Hostname matching with IP parameter tests passed")
def test_is_hostname_unknown_with_unifi_flag(self):
"""Test is_hostname_unknown with from_unifi_os flag."""
# This just tests that the flag is accepted, actual Unifi bug handling would need more testing
self.assertTrue(self.discovery.is_hostname_unknown("tasmota_123", from_unifi_os=True))
self.assertFalse(self.discovery.is_hostname_unknown("mydevice", from_unifi_os=True))
logger.info("Hostname matching with Unifi OS flag tests passed")
def test_is_hostname_unknown_with_custom_patterns(self):
"""Test is_hostname_unknown with custom patterns."""
custom_patterns = ["^custom-*", "^test-*"]
# Should match custom patterns
self.assertTrue(self.discovery.is_hostname_unknown("custom-device", patterns=custom_patterns))
self.assertTrue(self.discovery.is_hostname_unknown("test-device", patterns=custom_patterns))
# Should not match default patterns when custom patterns are provided
self.assertFalse(self.discovery.is_hostname_unknown("tasmota_123", patterns=custom_patterns))
logger.info("Hostname matching with custom patterns tests passed")
def test_is_device_excluded_basic(self):
"""Test basic device exclusion in is_device_excluded."""
# Should match exclude patterns
self.assertTrue(self.discovery.is_device_excluded("homeassistant"))
self.assertTrue(self.discovery.is_device_excluded("homeassistant-server"))
self.assertTrue(self.discovery.is_device_excluded("sonos-speaker"))
self.assertTrue(self.discovery.is_device_excluded("mysonosspeaker"))
self.assertTrue(self.discovery.is_device_excluded("printer"))
# Should not match exclude patterns
self.assertFalse(self.discovery.is_device_excluded("tasmota_123"))
self.assertFalse(self.discovery.is_device_excluded("esp-abcd"))
self.assertFalse(self.discovery.is_device_excluded("mydevice"))
self.assertFalse(self.discovery.is_device_excluded("printerx")) # printer$ should match exactly
logger.info("Basic device exclusion tests passed")
def test_is_device_excluded_with_hostname(self):
"""Test device exclusion with hostname parameter."""
# Should match exclude patterns in hostname
self.assertTrue(self.discovery.is_device_excluded("mydevice", "homeassistant.local"))
self.assertTrue(self.discovery.is_device_excluded("mydevice", "sonos.local"))
# Should not match exclude patterns
self.assertFalse(self.discovery.is_device_excluded("mydevice", "tasmota.local"))
logger.info("Device exclusion with hostname tests passed")
def test_is_device_excluded_with_custom_patterns(self):
"""Test device exclusion with custom patterns."""
custom_patterns = ["^custom-*", "^.*test.*"]
# Should match custom patterns
self.assertTrue(self.discovery.is_device_excluded("custom-device", patterns=custom_patterns))
self.assertTrue(self.discovery.is_device_excluded("mytest", patterns=custom_patterns))
self.assertTrue(self.discovery.is_device_excluded("testdevice", patterns=custom_patterns))
# Should not match default patterns when custom patterns are provided
self.assertFalse(self.discovery.is_device_excluded("homeassistant", patterns=custom_patterns))
self.assertFalse(self.discovery.is_device_excluded("sonos-speaker", patterns=custom_patterns))
logger.info("Device exclusion with custom patterns tests passed")
def test_is_device_excluded_with_log_level(self):
"""Test device exclusion with different log levels."""
# Test with different log levels
self.assertTrue(self.discovery.is_device_excluded("homeassistant", log_level="info"))
self.assertTrue(self.discovery.is_device_excluded("sonos-speaker", log_level="warning"))
self.assertTrue(self.discovery.is_device_excluded("printer", log_level="error"))
logger.info("Device exclusion with different log levels tests passed")
def main():
"""Run the tests."""
unittest.main()
if __name__ == "__main__":
main()