Skip to content

Commit

Permalink
More refinements to unit testing.
Browse files Browse the repository at this point in the history
  • Loading branch information
DaveL17 committed Oct 19, 2024
1 parent 51692a4 commit f1b5519
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
__copyright__ = "Copyright 2024 DaveL17"
__license__ = "MIT"
__title__ = "DLFramework"
__version__ = "0.1.06"
__version__ = "0.1.07"

# supported operators for eval expressions
OPERATORS = {
Expand Down Expand Up @@ -64,7 +64,7 @@ def __init__(self, plugin):
self.pluginPrefs = plugin.pluginPrefs
self.plugin.plugin_file_handler.setFormatter(logging.Formatter(fmt=LOG_FORMAT, datefmt='%Y-%m-%d %H:%M:%S'))

def environment(self):
def environment(self) -> str:
self.plugin.logger.debug("DLFramework pluginEnvironment method called.")
environment_state = ""
spacer = " " * 35
Expand All @@ -83,7 +83,7 @@ def environment(self):

return environment_state
# =============================================================================
def pluginEnvironment(self): # noqa
def pluginEnvironment(self) -> None: # noqa
"""
The pluginEnvironment method prints selected information about the pluginEnvironment that the plugin is running
in. It pulls some of this information from the calling plugin and some from the server pluginEnvironment. It
Expand All @@ -95,7 +95,7 @@ def pluginEnvironment(self): # noqa
indigo.server.log(environment_state)

# =============================================================================
def pluginEnvironmentLogger(self): # noqa
def pluginEnvironmentLogger(self) -> None: # noqa
"""
The pluginEnvironmentLogger method prints selected information about the pluginEnvironment that the plugin is
running in. It pulls some of this information from the calling plugin and some from the server
Expand Down Expand Up @@ -130,7 +130,7 @@ def pluginErrorHandler(self, sub_error): # noqa
self.plugin.logger.critical("!" * 80)

# =============================================================================
def convertDebugLevel(self, debug_val: str = ""): # noqa
def convertDebugLevel(self, debug_val: str = "") -> int: # noqa
"""
The convertDebugLevel method is used to standardize the various implementations of debug level settings across
plugins. Its main purpose is to convert an old string-based setting to account for older plugin versions. Over
Expand All @@ -155,7 +155,7 @@ def convertDebugLevel(self, debug_val: str = ""): # noqa

# =============================================================================
@staticmethod
def deviceList(dev_filter: str = ""): # noqa
def deviceList(dev_filter: str = "") -> list: # noqa
"""
Returns a list of tuples containing Indigo devices for use in config dialogs (etc.)
Expand All @@ -168,7 +168,7 @@ def deviceList(dev_filter: str = ""): # noqa

# =============================================================================
@staticmethod
def deviceListEnabled(dev_filter: str = ""): # noqa
def deviceListEnabled(dev_filter: str = "") -> list: # noqa
"""
Returns a list of tuples containing Indigo devices for use in config dialogs (etc.) Returns enabled devices
only.
Expand All @@ -181,7 +181,7 @@ def deviceListEnabled(dev_filter: str = ""): # noqa
return devices_list

@staticmethod
def time_list():
def time_list() -> list:
"""
Generate a list of hours for plugin control menus
Expand All @@ -193,7 +193,7 @@ def time_list():

# =============================================================================
@staticmethod
def variableList(): # noqa
def variableList() -> list: # noqa
"""
Returns a list of tuples containing Indigo variables for use in config dialogs (etc.)
Expand All @@ -205,7 +205,7 @@ def variableList(): # noqa

# =============================================================================
@staticmethod
def deviceAndVariableList(): # noqa
def deviceAndVariableList() -> list: # noqa
"""
Returns a list of tuples containing Indigo devices and variables for use in config dialogs
(etc.)
Expand All @@ -221,7 +221,7 @@ def deviceAndVariableList(): # noqa

# =============================================================================
@staticmethod
def deviceAndVariableListClean(): # noqa
def deviceAndVariableListClean() -> list: # noqa
"""
Returns a list of tuples containing Indigo devices and variables for use in config dialogs (etc.)
Expand All @@ -234,7 +234,7 @@ def deviceAndVariableListClean(): # noqa

# =============================================================================
@staticmethod
def launchWebPage(launch_url: str = ""): # noqa
def launchWebPage(launch_url: str = "") -> None: # noqa
"""
The launchWebPage method is used to direct a call to the registered default browser and open the page
referenced by the parameter 'URL'.
Expand All @@ -246,7 +246,7 @@ def launchWebPage(launch_url: str = ""): # noqa

# =============================================================================
@staticmethod
def generatorStateOrValue(dev_id: int = 0): # noqa
def generatorStateOrValue(dev_id: int = 0) -> list: # noqa
"""
The generator_state_or_value() method returns a list to populate the relevant device states or variable value
to populate a menu control.
Expand Down Expand Up @@ -279,7 +279,7 @@ def generatorStateOrValue(dev_id: int = 0): # noqa
return [(0, 'Pick a Device or Variable')]

# =============================================================================
def audit_server_version(self, min_ver: int = 0):
def audit_server_version(self, min_ver: int = 0) -> None:
"""
Audit Indigo Version
Expand All @@ -297,7 +297,7 @@ def audit_server_version(self, min_ver: int = 0):
self.plugin.logger.debug("Indigo server version OK.")

# =============================================================================
def audit_os_version(self, min_ver: int = 0):
def audit_os_version(self, min_ver: int = 0) -> None:
"""
Audit Operating System Version
Expand Down Expand Up @@ -336,7 +336,7 @@ def __init__(self, plugin):
self.pluginPrefs = plugin.pluginPrefs

# =============================================================================
def dateFormat(self): # noqa
def dateFormat(self) -> str: # noqa
"""
The dateFormat method takes the user configuration preference for date and time display and converts them to a
valid datetime() format specifier.
Expand All @@ -352,15 +352,18 @@ def dateFormat(self): # noqa
return date_formatter[self.pluginPrefs['uiDateFormat']]

# =============================================================================
def timeFormat(self): # noqa
def timeFormat(self) -> str: # noqa
"""
The timeFormat method takes the user configuration preference for date and time display and converts them to a
valid datetime() format specifier.
:return:
"""

time_formatter = {'military': '%H:%M', 'standard': '%I:%M', 'standard_am_pm': '%I:%M %p'}
time_formatter = {
'military': '%H:%M',
'standard': '%I:%M',
'standard_am_pm': '%I:%M %p'}
return time_formatter[self.pluginPrefs['uiTimeFormat']]


Expand All @@ -387,7 +390,7 @@ def __init__(self, plugin):
self.pluginPrefs = plugin.pluginPrefs

# =============================================================================
def eval_expr(self, expr):
def eval_expr(self, expr: int):
"""
Title Placeholder
Expand Down Expand Up @@ -415,11 +418,14 @@ def __eval(self, node):
if isinstance(node, ast.Num): # <number>
value = node.n
elif isinstance(node, ast.BinOp): # <left> <operator> <right>
value = OPERATORS[type(node.op)](self.__eval(node.left), self.__eval(node.right))
value = OPERATORS.get(type(node.op))(self.__eval(node.left), self.__eval(node.right)) # noqa
elif isinstance(node, ast.UnaryOp): # <operator> <operand> e.g., -1
value = OPERATORS[type(node.op)](self.__eval(node.operand))
value = OPERATORS.get(type(node.op))(self.__eval(node.operand)) # noqa
else:
raise TypeError(node)
if not value:
# value will be none if the operator provided is not in the "approved" OPERATORS.
raise TypeError(node)

return value
except (TypeError, KeyError):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0"?>
<Template>

<!-- This template is no longer needed because Indigo will notify users of plugin updates. -->
<Field id="notificationsHeaderSpace" type="label" fontSize="mini"/>

<Field id="notificationsLabel" type="label" alignText="right">
Expand Down
22 changes: 16 additions & 6 deletions Announcements.indigoPlugin/Contents/Server Plugin/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -1274,9 +1274,19 @@ def my_tests(self, action=None):
If the unit test module returns True, then all tests have passed.
"""
from Tests import test_plugin, test_devices
tests = test_plugin.TestPlugin()
if tests.my_tests(self):
self.logger.warning("All plugin tests passed.")
tests = test_devices.TestDevices()
if tests.my_tests(self):
self.logger.warning("All devices tests passed.")
plugin_tests = test_plugin.TestPlugin()
device_tests = test_devices.TestDevices()

def process_test_result(result, name):
if result[0] is True:
self.logger.warning(f"{name} tests passed.")
else:
self.logger.warning(f"{result[1]}")

# ===================================== Plugin tests =====================================
test = plugin_tests.my_tests(self)
process_test_result(test, "Test Plugin")

# ===================================== Device Tests =====================================
test = device_tests.my_tests(self)
process_test_result(test, "Test Devices")

0 comments on commit f1b5519

Please sign in to comment.