11"""Test the pyscript component."""
2- from ast import literal_eval
32import asyncio
4- from datetime import datetime as dt
53import pathlib
64import time
5+ from ast import literal_eval
6+ from datetime import datetime as dt
77
8- from custom_components .pyscript .const import DOMAIN
9- import custom_components .pyscript .trigger as trigger
10-
8+ import pytest
119from homeassistant import loader
1210from homeassistant .const import EVENT_HOMEASSISTANT_STARTED , EVENT_STATE_CHANGED
1311from homeassistant .setup import async_setup_component
12+ from pytest_homeassistant .async_mock import MagicMock , Mock , mock_open , patch
13+
14+ import custom_components .pyscript .trigger as trigger
15+ from custom_components .pyscript .const import DOMAIN
16+ from custom_components .pyscript .function import Function
17+
18+
19+ @pytest .fixture ()
20+ def ast_functions ():
21+ return {
22+ "domain_ast.func_name" : lambda ast_ctx : ast_ctx .func (),
23+ "domain_ast.other_func" : lambda ast_ctx : ast_ctx .func (),
24+ }
25+
26+
27+ @pytest .fixture
28+ def functions ():
29+ mock_func = Mock ()
30+
31+ return {
32+ "domain.func_1" : mock_func ,
33+ "domain.func_2" : mock_func ,
34+ "helpers.get_today" : mock_func ,
35+ "helpers.entity_id" : mock_func ,
36+ }
37+
1438
15- from pytest_homeassistant .async_mock import mock_open , patch
39+ @pytest .fixture
40+ def services ():
41+ return {
42+ "domain" : {"turn_on" : None , "turn_off" : None , "toggle" : None },
43+ "helpers" : {"set_state" : None , "restart" : None },
44+ }
45+
46+
47+ def test_install_ast_funcs (ast_functions ):
48+ ast_ctx = MagicMock ()
49+ ast_ctx .func .return_value = "ok"
50+
51+ with patch .object (Function , "ast_functions" , ast_functions ):
52+ Function .install_ast_funcs (ast_ctx )
53+ assert len (ast_ctx .method_calls ) == 3
54+
55+
56+ @pytest .mark .parametrize (
57+ "root,expected" ,
58+ [
59+ ("helpers" , {"helpers.entity_id" , "helpers.get_today" }),
60+ (
61+ "domain" ,
62+ {
63+ "domain.func_2" ,
64+ "domain_ast.func_name" ,
65+ "domain_ast.other_func" ,
66+ "domain.func_1" ,
67+ },
68+ ),
69+ ("domain_" , {"domain_ast.func_name" , "domain_ast.other_func" }),
70+ ("domain_ast.func" , {"domain_ast.func_name" }),
71+ ("no match" , set ()),
72+ ],
73+ ids = lambda x : x if not isinstance (x , (set ,)) else f"set({ len (x )} )" ,
74+ )
75+ async def test_func_completions (ast_functions , functions , root , expected ):
76+ with patch .object (Function , "ast_functions" , ast_functions ), patch .object (
77+ Function , "functions" , functions
78+ ):
79+ words = await Function .func_completions (root )
80+ assert words == expected
81+
82+
83+ @pytest .mark .parametrize (
84+ "root,expected" ,
85+ [
86+ ("do" , {"domain" }),
87+ ("domain.t" , {"domain.toggle" , "domain.turn_on" , "domain.turn_off" }),
88+ ("domain.turn" , {"domain.turn_on" , "domain.turn_off" }),
89+ ("helpers.set" , {"helpers.set_state" }),
90+ ("no match" , set ()),
91+ ],
92+ ids = lambda x : x if not isinstance (x , (set ,)) else f"set({ len (x )} )" ,
93+ )
94+ async def test_service_completions (root , expected , hass , services ):
95+ with patch .object (
96+ hass .services , "async_services" , return_value = services
97+ ), patch .object (Function , "hass" , hass ):
98+ words = await Function .service_completions (root )
99+ assert words == expected
16100
17101
18102async def setup_script (hass , notify_q , now , source ):
@@ -34,14 +118,10 @@ async def setup_script(hass, notify_q, now, source):
34118
35119 with patch (
36120 "homeassistant.loader.async_get_integration" , return_value = integration ,
37- ), patch (
38- "custom_components.pyscript.os.path.isdir" , return_value = True
39- ), patch (
121+ ), patch ("custom_components.pyscript.os.path.isdir" , return_value = True ), patch (
40122 "custom_components.pyscript.glob.iglob" , return_value = scripts
41123 ), patch (
42- "custom_components.pyscript.open" ,
43- mock_open (read_data = source ),
44- create = True ,
124+ "custom_components.pyscript.open" , mock_open (read_data = source ), create = True ,
45125 ), patch (
46126 "custom_components.pyscript.trigger.dt_now" , return_value = now
47127 ):
@@ -157,7 +237,8 @@ def func4(trigger_type=None, event_type=None, **kwargs):
157237 seq_num += 1
158238 res = task.wait_until(state_trigger="pyscript.f4var2 == '10'", timeout=10)
159239 log.info(f"func4 trigger_type = {res}")
160- pyscript.done = [seq_num, res, pyscript.setVar1, pyscript.setVar1.attr1, state.get("pyscript.setVar1.attr2"), pyscript.setVar2, state.get("pyscript.setVar3")]
240+ pyscript.done = [seq_num, res, pyscript.setVar1, pyscript.setVar1.attr1, state.get("pyscript.setVar1.attr2"),
241+ pyscript.setVar2, state.get("pyscript.setVar3")]
161242
162243 seq_num += 1
163244 #
0 commit comments