diff --git a/aprsd/plugins/fortune.py b/aprsd/plugins/fortune.py index 0ac6bb1..d0f44ca 100644 --- a/aprsd/plugins/fortune.py +++ b/aprsd/plugins/fortune.py @@ -8,7 +8,7 @@ from aprsd.utils import trace LOG = logging.getLogger("APRSD") -DEFAULT_FORTUNE_PATH = '/usr/games/fortune' +DEFAULT_FORTUNE_PATH = "/usr/games/fortune" class FortunePlugin(plugin.APRSDRegexCommandPluginBase): @@ -45,7 +45,7 @@ class FortunePlugin(plugin.APRSDRegexCommandPluginBase): command, shell=True, timeout=3, - universal_newlines=True, + text=True, ) output = ( output.replace("\r", "") diff --git a/aprsd/plugins/location.py b/aprsd/plugins/location.py index 4855979..879f12b 100644 --- a/aprsd/plugins/location.py +++ b/aprsd/plugins/location.py @@ -2,8 +2,10 @@ import logging import re import time -from geopy.geocoders import ArcGIS, AzureMaps, Baidu, Bing, GoogleV3 -from geopy.geocoders import HereV7, Nominatim, OpenCage, TomTom, What3WordsV3, Woosmap +from geopy.geocoders import ( + ArcGIS, AzureMaps, Baidu, Bing, GoogleV3, HereV7, Nominatim, OpenCage, + TomTom, What3WordsV3, Woosmap, +) from oslo_config import cfg from aprsd import packets, plugin, plugin_utils @@ -39,8 +41,8 @@ class USGov: result = plugin_utils.get_weather_gov_for_gps(lat, lon) # LOG.info(f"WEATHER: {result}") # LOG.info(f"area description {result['location']['areaDescription']}") - if 'location' in result: - loc = UsLocation(result['location']['areaDescription']) + if "location" in result: + loc = UsLocation(result["location"]["areaDescription"]) else: loc = UsLocation("Unknown Location") diff --git a/tests/client/test_factory.py b/tests/client/test_factory.py new file mode 100644 index 0000000..4c2257e --- /dev/null +++ b/tests/client/test_factory.py @@ -0,0 +1,75 @@ +import unittest +from unittest import mock + +from aprsd.client.factory import Client, ClientFactory + + +class MockClient: + """Mock client for testing.""" + + @classmethod + def is_enabled(cls): + return True + + @classmethod + def is_configured(cls): + return True + + +class TestClientFactory(unittest.TestCase): + """Test cases for ClientFactory.""" + + def setUp(self): + """Set up test fixtures.""" + self.factory = ClientFactory() + # Clear any registered clients from previous tests + self.factory.clients = [] + + def test_singleton(self): + """Test that ClientFactory is a singleton.""" + factory2 = ClientFactory() + self.assertEqual(self.factory, factory2) + + def test_register_client(self): + """Test registering a client.""" + self.factory.register(MockClient) + self.assertIn(MockClient, self.factory.clients) + + def test_register_invalid_client(self): + """Test registering an invalid client raises error.""" + invalid_client = mock.MagicMock(spec=Client) + with self.assertRaises(ValueError): + self.factory.register(invalid_client) + + def test_create_client(self): + """Test creating a client.""" + self.factory.register(MockClient) + client = self.factory.create() + self.assertIsInstance(client, MockClient) + + def test_create_no_clients(self): + """Test creating a client with no registered clients.""" + with self.assertRaises(Exception): + self.factory.create() + + def test_is_client_enabled(self): + """Test checking if any client is enabled.""" + self.factory.register(MockClient) + self.assertTrue(self.factory.is_client_enabled()) + + def test_is_client_enabled_none(self): + """Test checking if any client is enabled when none are.""" + MockClient.is_enabled = classmethod(lambda cls: False) + self.factory.register(MockClient) + self.assertFalse(self.factory.is_client_enabled()) + + def test_is_client_configured(self): + """Test checking if any client is configured.""" + self.factory.register(MockClient) + self.assertTrue(self.factory.is_client_configured()) + + def test_is_client_configured_none(self): + """Test checking if any client is configured when none are.""" + MockClient.is_configured = classmethod(lambda cls: False) + self.factory.register(MockClient) + self.assertFalse(self.factory.is_client_configured()) diff --git a/tests/test_main.py b/tests/test_main.py index ab1303d..276a42c 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -1,15 +1,9 @@ -import sys import unittest +from unittest import mock from aprsd.plugins import email -if sys.version_info >= (3, 2): - from unittest import mock -else: - from unittest import mock - - class TestMain(unittest.TestCase): @mock.patch("aprsd.plugins.email._imap_connect") @mock.patch("aprsd.plugins.email._smtp_connect")