Skip to content

Commit

Permalink
tests: isolate mock data between tests (#155)
Browse files Browse the repository at this point in the history
Mock data was previously static dictionaries shared across all TeslaMock
instances, so accidental dependencies were introduced between the tests.  Now
each mock instance has its own deep copy.
  • Loading branch information
graham33 committed Feb 20, 2021
1 parent 1432c04 commit 12c02bc
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 36 deletions.
74 changes: 41 additions & 33 deletions tests/tesla_mock.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
Tesla mock.
"""

import copy

from teslajsonpy.connection import Connection
from teslajsonpy.controller import Controller

Expand Down Expand Up @@ -42,6 +44,21 @@ def __init__(self, monkeypatch) -> None:
)
self._monkeypatch.setattr(Controller, "update", self.mock_update)

self._drive_state = copy.deepcopy(DRIVE_STATE)
self._climate_state = copy.deepcopy(CLIMATE_STATE)
self._charge_state = copy.deepcopy(CHARGE_STATE)
self._gui_settings = copy.deepcopy(GUI_SETTINGS)
self._vehicle_state = copy.deepcopy(VEHICLE_STATE)
self._vehicle_config = copy.deepcopy(VEHICLE_CONFIG)

self._vehicle = copy.deepcopy(VEHICLE)
self._vehicle["drive_state"] = self._drive_state
self._vehicle["climate_state"] = self._climate_state
self._vehicle["charge_state"] = self._charge_state
self._vehicle["gui_settings"] = self._gui_settings
self._vehicle["vehicle_state"] = self._vehicle_state
self._vehicle["vehicle_config"] = self._vehicle_config

def mock_connect(self, *args, **kwargs):
# pylint: disable=unused-argument
""" Mock controller's connect method."""
Expand Down Expand Up @@ -102,30 +119,25 @@ async def controller_command():
""" Monkeypatch for controller.command()."""
return RESULT_OK

@staticmethod
def controller_get_charging_params():
def controller_get_charging_params(self):
""" Monkeypatch for controller.get_charging_params()."""
return CHARGE_STATE
return self._charge_state

@staticmethod
def controller_get_climate_params():
def controller_get_climate_params(self):
""" Monkeypatch for controller.get_climate_params()."""
return CLIMATE_STATE
return self._climate_state

@staticmethod
def controller_get_drive_params():
def controller_get_drive_params(self):
""" Monkeypatch for controller.get_drive_params()."""
return DRIVE_STATE
return self._drive_state

@staticmethod
def controller_get_gui_params():
def controller_get_gui_params(self):
""" Monkeypatch for controller.get_gui_params()."""
return GUI_SETTINGS
return self._gui_settings

@staticmethod
def controller_get_state_params():
def controller_get_state_params(self):
""" Monkeypatch for controller.get_state_params()."""
return VEHICLE_STATE
return self._vehicle_state

@staticmethod
def controller_get_vehicles():
Expand All @@ -142,25 +154,21 @@ def connection_generate_oauth():
""" Monkeypatch for connection.generate_oauth()."""
return

@staticmethod
def data_request_vehicle():
def data_request_vehicle(self):
""" Simulates the result of vehicle data request. """
return VEHICLE
return self._vehicle

@staticmethod
def data_request_charge_state():
def data_request_charge_state(self):
""" Simulates the result of charge state data request. """
return CHARGE_STATE
return self._charge_state

@staticmethod
def data_request_climate_state():
def data_request_climate_state(self):
""" Simulates the result of climate state data request. """
return CLIMATE_STATE
return self._climate_state

@staticmethod
def data_request_vehicle_state():
def data_request_vehicle_state(self):
""" Simulates the result of vehicle state data request. """
return VEHICLE_STATE
return self._vehicle_state

@staticmethod
def command_ok():
Expand Down Expand Up @@ -399,10 +407,10 @@ def command_ok():
"api_version": 7,
"backseat_token": None,
"backseat_token_updated_at": None,
"drive_state": DRIVE_STATE,
"climate_state": CLIMATE_STATE,
"charge_state": CHARGE_STATE,
"gui_settings": GUI_SETTINGS,
"vehicle_state": VEHICLE_STATE,
"vehicle_config": VEHICLE_CONFIG,
"drive_state": None,
"climate_state": None,
"charge_state": None,
"gui_settings": None,
"vehicle_state": None,
"vehicle_config": None,
}
6 changes: 3 additions & 3 deletions tests/unit_tests/homeassistant/test_online_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ async def test_get_value_after_update(monkeypatch):
_controller = Controller(None)
monkeypatch.setitem(_controller.car_online, "5YJSA11111111111", True)
monkeypatch.setitem(
_controller.car_state, "5YJSA11111111111", TeslaMock.data_request_vehicle()
_controller.car_state, "5YJSA11111111111", _mock.data_request_vehicle()
)

_data = _mock.data_request_vehicle()
Expand All @@ -62,7 +62,7 @@ async def test_get_value_on(monkeypatch):
_controller = Controller(None)
monkeypatch.setitem(_controller.car_online, "5YJSA11111111111", True)
monkeypatch.setitem(
_controller.car_state, "5YJSA11111111111", TeslaMock.data_request_vehicle()
_controller.car_state, "5YJSA11111111111", _mock.data_request_vehicle()
)

_data = _mock.data_request_vehicle()
Expand All @@ -84,7 +84,7 @@ async def test_get_value_off(monkeypatch):
_controller = Controller(None)
monkeypatch.setitem(_controller.car_online, "5YJSA11111111111", False)
monkeypatch.setitem(
_controller.car_state, "5YJSA11111111111", TeslaMock.data_request_vehicle()
_controller.car_state, "5YJSA11111111111", _mock.data_request_vehicle()
)

_data = _mock.data_request_vehicle()
Expand Down

0 comments on commit 12c02bc

Please sign in to comment.