async def test_merge_mapping( sut_before_init: Controller, mocker: MockerFixture, mapping: List[str], merge_mapping: List[str], actions_output: List[str], error_expected: bool, ): actions_input = ["action1", "action2", "action3"] actions = {action: action for action in actions_input} predefined_actions = {action: lambda: None for action in actions_input} if mapping: sut_before_init.args["mapping"] = {item: item for item in mapping} if merge_mapping: sut_before_init.args["merge_mapping"] = { item: item for item in merge_mapping } mocker.patch.object(sut_before_init, "get_default_actions_mapping", return_value=actions) mocker.patch.object( sut_before_init, "get_predefined_actions_mapping", return_value=predefined_actions, ) # SUT with wrap_exetuction(error_expected=error_expected, exception=ValueError): await sut_before_init.initialize() # Checks if not error_expected: assert list(sut_before_init.actions_mapping.keys()) == actions_output
async def test_check_domain( sut: MyTypeController, monkeypatch: MonkeyPatch, entity: str, domains: List[str], entities: List[str], error_expected: bool, ): sut.domains = domains expected_error_message = "" if error_expected: if entities == []: expected_error_message = ( f"'{entity}' must be from one of the following domains " f"{domains} (e.g. {domains[0]}.bedroom)") else: expected_error_message = ( f"All entities from '{entity}' must be from one of the " f"following domains {domains} (e.g. {domains[0]}.bedroom)") monkeypatch.setattr(sut, "get_state", fake_fn(to_return=entities, async_=True)) with wrap_exetuction(error_expected=error_expected, exception=ValueError) as err_info: await sut.check_domain(entity) if err_info is not None: assert str(err_info.value) == expected_error_message
async def test_get_entity_state( sut: MyTypeController, mocker: MockerFixture, monkeypatch: MonkeyPatch, entity_input: str, entities: List[str], expected_calls: int, ): stub_get_state = mocker.stub() async def fake_get_state(entity, attribute=None): stub_get_state(entity, attribute=attribute) return entities monkeypatch.setattr(sut, "get_state", fake_get_state) with wrap_exetuction(error_expected=expected_calls is None, exception=ValueError): await sut.get_entity_state(entity_input, "attribute_test") if expected_calls is not None: if expected_calls == 1: stub_get_state.assert_called_once_with(entity_input, attribute="attribute_test") elif expected_calls == 2: stub_get_state.call_count == 2 stub_get_state.assert_any_call(entity_input, attribute="entity_id") stub_get_state.assert_any_call("entity.test", attribute="attribute_test")
async def test_init(sut_before_init: MyTypeController, args: Dict[str, Any], error_expected: bool): sut_before_init.args = args with wrap_exetuction(error_expected=error_expected, exception=ValueError): await sut_before_init.init() if not error_expected: assert sut_before_init.entity.name == ENTITY_NAME if isinstance(args[ENTITY_ARG], dict): assert sut_before_init.entity.attr_test == args[ENTITY_ARG].get( "attr_test", DEFAULT_ATTR_TEST)
async def test_get_attribute( sut: LightController, attribute_input: str, color_mode: ColorMode, supported_features: int, expected_attribute: str, error_expected: bool, ): sut.feature_support._supported_features = supported_features sut.entity = LightEntity(name=ENTITY_NAME, color_mode=color_mode) with wrap_exetuction(error_expected=error_expected, exception=ValueError): output = await sut.get_attribute(attribute_input) if not error_expected: assert output == expected_attribute
def test_get_integration( fake_controller: Controller, mocker: MockerFixture, integration_input: Union[str, Dict[str, Any]], integration_name_expected: str, args_expected: Dict[str, Any], error_expected: bool, ): get_integrations_spy = mocker.spy(integration_module, "get_integrations") with wrap_exetuction(error_expected=error_expected, exception=ValueError): integration = fake_controller.get_integration(integration_input) if not error_expected: get_integrations_spy.assert_called_once_with(fake_controller, args_expected) assert integration.name == integration_name_expected
async def test_init( sut_before_init: LightController, light_input: Union[str, Dict[str, str]], expected_name: str, expected_color_mode: str, error_expected: bool, ): sut_before_init.args["light"] = light_input # SUT with wrap_exetuction(error_expected=error_expected, exception=ValueError): await sut_before_init.init() # Checks if not error_expected: assert sut_before_init.entity.name == expected_name assert sut_before_init.entity.color_mode == expected_color_mode
def test_get_action( sut: Controller, test_input: TypeAction, expected: ActionFunction, error_expected: bool, ): with wrap_exetuction(error_expected=error_expected, exception=ValueError) as err_info: output = sut.get_action(test_input) if err_info is not None: assert ( str(err_info.value) == "The action value from the action mapping should be a list or a function" ) else: assert output == expected
async def test_init( sut_before_init: CoverController, open_position: int, close_position: int, error_expected: bool, ): sut_before_init.args = { "open_position": open_position, "close_position": close_position, } with wrap_exetuction(error_expected=error_expected, exception=ValueError): await sut_before_init.init() if not error_expected: assert sut_before_init.open_position == open_position assert sut_before_init.close_position == close_position
async def test_initialize( sut_before_init: Controller, mocker: MockerFixture, controller_input: Union[str, List[str]], actions_input: List[str], included_actions: Optional[List[str]], excluded_actions: Optional[List[str]], actions_output: List[str], error_expected: bool, ): actions = {action: action for action in actions_input} predefined_actions = {action: lambda: None for action in actions_input} sut_before_init.args["controller"] = controller_input integration_mock = IntegrationMock(INTEGRATION_TEST_NAME, sut_before_init, mocker) mocker.patch.object(sut_before_init, "get_integration", return_value=integration_mock) if included_actions: sut_before_init.args["actions"] = included_actions if excluded_actions: sut_before_init.args["excluded_actions"] = excluded_actions mocker.patch.object(sut_before_init, "get_default_actions_mapping", return_value=actions) mocker.patch.object( sut_before_init, "get_predefined_actions_mapping", return_value=predefined_actions, ) get_default_actions_mapping = mocker.spy(sut_before_init, "get_default_actions_mapping") # SUT with wrap_exetuction(error_expected=error_expected, exception=ValueError): await sut_before_init.initialize() # Checks if not error_expected: get_default_actions_mapping.assert_called_once() for controller_id in controller_input: integration_mock.listen_changes.assert_any_call(controller_id) assert integration_mock.listen_changes.call_count == len( controller_input) assert list(sut_before_init.actions_mapping.keys()) == actions_output
async def test_get_value_attribute( sut: LightController, monkeypatch: MonkeyPatch, attribute_input: str, direction_input: Literal["up", "down"], light_state: str, expected_output: Union[int, float, str], error_expected: bool, ): sut.smooth_power_on = True async def fake_get_entity_state(entity, attribute=None): if entity == "light" and attribute is None: return light_state return expected_output monkeypatch.setattr(sut, "get_entity_state", fake_get_entity_state) with wrap_exetuction(error_expected=error_expected, exception=ValueError): output = await sut.get_value_attribute(attribute_input, direction_input) if not error_expected: assert output == float(expected_output)
def test_get_option(sut: Controller, option: str, options: List[str], error_expected: bool): with wrap_exetuction(error_expected=error_expected, exception=ValueError): sut.get_option(option, options)
def test_get_color_wheel(colors: Colors, error_expected: bool): with wrap_exetuction(error_expected=error_expected, exception=ValueError): colors = get_color_wheel(colors)