async def test_parse_date_time(hass, caplog): """Run time parse datetime tests.""" # # Hardcode a location and timezone so we can check sunrise # and sunset. # hass.config.latitude = 38 hass.config.longitude = -122 hass.config.elevation = 0 hass.config.time_zone = "America/Los_Angeles" Function.init(hass) TrigTime.init(hass) # # This set of tests assumes it's currently 13:00 on 2019/9/1 # now = dt(2019, 9, 1, 13, 0, 0, 0) with patch("homeassistant.helpers.condition.dt_util.utcnow", return_value=now), patch("homeassistant.util.dt.utcnow", return_value=now): for test_data in parseDateTimeTests: spec, date_offset, expect = test_data out = TrigTime.parse_date_time(spec, date_offset, now) assert out == expect
def test_timer_active_check(hass, spec, now, expected): """Run time active check tests.""" Function.init(hass) TrigTime.init(hass) out = TrigTime.timer_active_check(spec, now) assert out == expected
def test_install_ast_funcs(ast_functions): # pylint: disable=redefined-outer-name """Test installing ast functions.""" ast_ctx = MagicMock() ast_ctx.func.return_value = "ok" with patch.object(Function, "ast_functions", ast_functions): Function.install_ast_funcs(ast_ctx) assert len(ast_ctx.method_calls) == 3
async def test_eval(hass): """Test interpreter.""" hass.data[DOMAIN] = MockConfigEntry(domain=DOMAIN, data={CONF_ALLOW_ALL_IMPORTS: False}) Function.init(hass) State.init(hass) State.register_functions() for test_data in evalTests: await run_one_test(test_data)
async def test_eval_exceptions(hass): """Test interpreter exceptions.""" hass.data[DOMAIN] = {CONFIG_ENTRY: MockConfigEntry(domain=DOMAIN, data={CONF_ALLOW_ALL_IMPORTS: False})} Function.init(hass) State.init(hass) State.register_functions() TrigTime.init(hass) for test_data in evalTestsExceptions: await run_one_test_exception(test_data)
async def run_one_test(test_data): """Run one interpreter test.""" source, expect = test_data global_ctx = GlobalContext("test", global_sym_table={}, manager=GlobalContextMgr) ast = AstEval("test", global_ctx=global_ctx) Function.install_ast_funcs(ast) ast.parse(source) if ast.get_exception() is not None: print(f"Parsing {source} failed: {ast.get_exception()}") # print(ast.dump()) result = await ast.eval({"sym_local": 10}) assert result == expect
def test_timer_trigger_next_month_rollover(hass): """Run month rollover tests.""" Function.init(hass) TrigTime.init(hass) for test_data in timerTriggerNextTestsMonthRollover: now = dt(2020, 6, 30, 13, 0, 0, 100000) spec, expect_seq = test_data for expect in expect_seq: t_next = TrigTime.timer_trigger_next(spec, now) assert t_next == expect now = t_next
async def run_one_test_exception(test_data): """Run one interpreter test that generates an exception.""" source, expect = test_data global_ctx = GlobalContext("test", global_sym_table={}, manager=GlobalContextMgr) ast = AstEval("test", global_ctx=global_ctx) Function.install_ast_funcs(ast) ast.parse(source) exc = ast.get_exception() if exc is not None: assert exc == expect return await ast.eval() exc = ast.get_exception() if exc is not None: assert exc == expect return assert False
def test_timer_active_check(hass, spec, now, expected): """Run time active check tests.""" # # Hardcode a location and timezone so we can check sunrise # and sunset. # hass.config.latitude = 38 hass.config.longitude = -122 hass.config.elevation = 0 hass.config.time_zone = "America/Los_Angeles" startup_time = dt(2019, 9, 1, 13, 0, 0, 0) Function.init(hass) TrigTime.init(hass) print(f"calling timer_active_check({spec}, {now}, {startup_time})") out = TrigTime.timer_active_check(spec, now, startup_time) assert out == expected
def test_timer_trigger_next(hass): """Run trigger next tests.""" # # Hardcode a location and timezone so we can check sunrise # and sunset. # hass.config.latitude = 38 hass.config.longitude = -122 hass.config.elevation = 0 hass.config.time_zone = "America/Los_Angeles" Function.init(hass) TrigTime.init(hass) for test_data in timerTriggerNextTests: now = dt(2019, 9, 1, 13, 0, 0, 100000) spec, expect_seq = test_data for expect in expect_seq: t_next = TrigTime.timer_trigger_next(spec, now) assert t_next == expect now = t_next
def test_timer_trigger_next(hass): """Run trigger next tests.""" # # Hardcode a location and timezone so we can check sunrise # and sunset. # hass.config.latitude = 38 hass.config.longitude = -122 hass.config.elevation = 0 hass.config.time_zone = "America/Los_Angeles" Function.init(hass) TrigTime.init(hass) for test_data in timerTriggerNextTests: startup_time = now = dt(2019, 9, 1, 13, 0, 0, 100000) spec, expect_seq = test_data for expect in expect_seq: print(f"calling timer_trigger_next({spec}, {now}, {startup_time})") t_next = TrigTime.timer_trigger_next(spec, now, startup_time) assert t_next == expect if t_next is None: break now = t_next + timedelta(microseconds=1)
async def test_misc_errors(hass, caplog): """Test miscellaneous errors.""" await setup_script(hass, None, dt(2020, 7, 1, 11, 59, 59, 999999), "") Function() GlobalContextMgr() State() Event() Event.notify_del("not_in_notify_list", None) trigger.TrigTime() assert "Function class is not meant to be instantiated" in caplog.text assert "GlobalContextMgr class is not meant to be instantiated" in caplog.text assert "State class is not meant to be instantiated" in caplog.text assert "Event class is not meant to be instantiated" in caplog.text assert "TrigTime class is not meant to be instantiated" in caplog.text
async def test_service_call_params(hass): """Test that hass params get set properly on service calls.""" with patch.object(hass.services, "async_call") as call, patch.object( Function, "service_has_service", return_value=True): Function.init(hass) await Function.service_call("test", "test", context=Context(id="test"), blocking=True, limit=1, other_service_data="test") assert call.called assert call.call_args[0] == ("test", "test", { "other_service_data": "test" }) assert call.call_args[1] == { "context": Context(id="test"), "blocking": True, "limit": 1 } call.reset_mock() await Function.service_call("test", "test", context=Context(id="test"), blocking=False, other_service_data="test") assert call.called assert call.call_args[0] == ("test", "test", { "other_service_data": "test" }) assert call.call_args[1] == { "context": Context(id="test"), "blocking": False } call.reset_mock() await Function.get("test.test")(context=Context(id="test"), blocking=True, limit=1, other_service_data="test") assert call.called assert call.call_args[0] == ("test", "test", { "other_service_data": "test" }) assert call.call_args[1] == { "context": Context(id="test"), "blocking": True, "limit": 1 } call.reset_mock() await Function.get("test.test")(context=Context(id="test"), blocking=False, other_service_data="test") assert call.called assert call.call_args[0] == ("test", "test", { "other_service_data": "test" }) assert call.call_args[1] == { "context": Context(id="test"), "blocking": False } # Stop all tasks to avoid conflicts with other tests await Function.reaper_stop()