Skip to content

Commit

Permalink
Refactor test_dbus-opendtu.py to remove unused mock_constants paramet…
Browse files Browse the repository at this point in the history
…er in test_register_service method
  • Loading branch information
0x7878 committed Nov 12, 2024
1 parent 1e1190b commit 9642657
Showing 1 changed file with 8 additions and 9 deletions.
17 changes: 8 additions & 9 deletions test_dbus-opendtu.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
""" Unit tests for the dbus_opendtu.py module """

import unittest
from unittest.mock import patch, MagicMock
import sys
import os

# Mocking the dbus and other dependencies before importing the module to test
sys.modules['dbus'] = MagicMock()
sys.modules['vedbus'] = MagicMock()
sys.modules['gi.repository'] = MagicMock()
sys.modules['dbus.mainloop.glib'] = MagicMock()

# Add the directory containing dbus_opendtu.py to the Python path
sys.path.insert(0, os.path.abspath(os.path.dirname(__file__)))
from dbus_opendtu import register_service # pylint: disable=E0401,C0413 # noqa: E402

from dbus_opendtu import register_service

class TestRegisterService(unittest.TestCase):
""" Test cases for the register_service function """

@patch('dbus_opendtu.config', {'DEFAULT': {'DTU': 'opendtu'}})
@patch('dbus_opendtu.DbusService')
@patch('dbus_opendtu.get_config_value')
@patch('dbus_opendtu.constants')

def test_register_service(self, mock_get_config_value, mock_dbus_service):
""" Test the register_service function """
def get_config_value_side_effect(key, *args, **kwargs):
if isinstance(key, dict):
key = key.get('key', 'mock_value')
Expand All @@ -31,7 +31,6 @@ def get_config_value_side_effect(key, *args, **kwargs):
}.get(key, 'mock_value')

mock_get_config_value.side_effect = get_config_value_side_effect
#mock_dbus_service.return_value = MagicMock()
mock_dbus_service_instance = mock_dbus_service.return_value
mock_dbus_service_instance.get_number_of_inverters.return_value = 1

Expand All @@ -43,9 +42,9 @@ def get_config_value_side_effect(key, *args, **kwargs):
# Additional assertions
mock_dbus_service.assert_called_once_with(
servicename="mock_value",
paths=unittest.mock.ANY,
actual_inverter=0,
)


if __name__ == '__main__':
unittest.main()
unittest.main()

0 comments on commit 9642657

Please sign in to comment.