1+ from unittest import mock
2+ import pytest
3+ from custom_components .first_bus .config import async_validate_main_config
4+ from custom_components .first_bus .const import CONFIG_NAME , CONFIG_STOP , CONFIG_BUSES
5+ from custom_components .first_bus .api_client import FirstBusApiClient
6+
7+ @pytest .mark .asyncio
8+ async def test_when_data_valid_then_no_errors_returned ():
9+ # Arrange
10+ original_config = {
11+ CONFIG_NAME : "test" ,
12+ CONFIG_STOP : "123" ,
13+ CONFIG_BUSES : "12 A,12B,12"
14+ }
15+
16+ async def async_mocked_get_bus_times (* args , ** kwargs ):
17+ return []
18+
19+ # Act
20+ with mock .patch .multiple (FirstBusApiClient , async_get_bus_times = async_mocked_get_bus_times ):
21+ (errors , config ) = await async_validate_main_config (original_config )
22+
23+ # Assert
24+ assert CONFIG_NAME not in errors
25+ assert CONFIG_STOP not in errors
26+ assert CONFIG_BUSES not in errors
27+
28+ assert CONFIG_NAME in config
29+ assert config [CONFIG_NAME ] == original_config [CONFIG_NAME ]
30+ assert CONFIG_STOP in config
31+ assert config [CONFIG_STOP ] == original_config [CONFIG_STOP ]
32+ assert CONFIG_BUSES in config
33+ assert config [CONFIG_BUSES ] == ["12 A" , "12B" , "12" ]
34+
35+ @pytest .mark .asyncio
36+ async def test_when_buses_not_present_then_buses_empty_array ():
37+ # Arrange
38+ original_config = {
39+ CONFIG_NAME : "test" ,
40+ CONFIG_STOP : "123"
41+ }
42+
43+ async def async_mocked_get_bus_times (* args , ** kwargs ):
44+ return []
45+
46+ # Act
47+ with mock .patch .multiple (FirstBusApiClient , async_get_bus_times = async_mocked_get_bus_times ):
48+ (errors , config ) = await async_validate_main_config (original_config )
49+
50+ # Assert
51+ assert CONFIG_NAME not in errors
52+ assert CONFIG_STOP not in errors
53+ assert CONFIG_BUSES not in errors
54+
55+ assert CONFIG_NAME in config
56+ assert config [CONFIG_NAME ] == original_config [CONFIG_NAME ]
57+ assert CONFIG_STOP in config
58+ assert config [CONFIG_STOP ] == original_config [CONFIG_STOP ]
59+ assert CONFIG_BUSES in config
60+ assert config [CONFIG_BUSES ] == []
61+
62+ @pytest .mark .asyncio
63+ async def test_when_buses_none_then_buses_empty_array ():
64+ # Arrange
65+ original_config = {
66+ CONFIG_NAME : "test" ,
67+ CONFIG_STOP : "123" ,
68+ CONFIG_BUSES : None
69+ }
70+
71+ async def async_mocked_get_bus_times (* args , ** kwargs ):
72+ return []
73+
74+ # Act
75+ with mock .patch .multiple (FirstBusApiClient , async_get_bus_times = async_mocked_get_bus_times ):
76+ (errors , config ) = await async_validate_main_config (original_config )
77+
78+ # Assert
79+ assert CONFIG_NAME not in errors
80+ assert CONFIG_STOP not in errors
81+ assert CONFIG_BUSES not in errors
82+
83+ assert CONFIG_NAME in config
84+ assert config [CONFIG_NAME ] == original_config [CONFIG_NAME ]
85+ assert CONFIG_STOP in config
86+ assert config [CONFIG_STOP ] == original_config [CONFIG_STOP ]
87+ assert CONFIG_BUSES in config
88+ assert config [CONFIG_BUSES ] == []
89+
90+ @pytest .mark .asyncio
91+ async def test_when_buses_empty_then_buses_empty_array ():
92+ # Arrange
93+ original_config = {
94+ CONFIG_NAME : "test" ,
95+ CONFIG_STOP : "123" ,
96+ CONFIG_BUSES : ""
97+ }
98+
99+ async def async_mocked_get_bus_times (* args , ** kwargs ):
100+ return []
101+
102+ # Act
103+ with mock .patch .multiple (FirstBusApiClient , async_get_bus_times = async_mocked_get_bus_times ):
104+ (errors , config ) = await async_validate_main_config (original_config )
105+
106+ # Assert
107+ assert CONFIG_NAME not in errors
108+ assert CONFIG_STOP not in errors
109+ assert CONFIG_BUSES not in errors
110+
111+ assert CONFIG_NAME in config
112+ assert config [CONFIG_NAME ] == original_config [CONFIG_NAME ]
113+ assert CONFIG_STOP in config
114+ assert config [CONFIG_STOP ] == original_config [CONFIG_STOP ]
115+ assert CONFIG_BUSES in config
116+ assert config [CONFIG_BUSES ] == []
117+
118+ @pytest .mark .asyncio
119+ @pytest .mark .parametrize ("bus_value" ,[
120+ ("A-B" ),
121+ ("12,12B," ),
122+ ])
123+ async def test_when_buses_not_valid_then_buses_empty_array (bus_value : str ):
124+ # Arrange
125+ original_config = {
126+ CONFIG_NAME : "test" ,
127+ CONFIG_STOP : "123" ,
128+ CONFIG_BUSES : bus_value
129+ }
130+
131+ async def async_mocked_get_bus_times (* args , ** kwargs ):
132+ return []
133+
134+ # Act
135+ with mock .patch .multiple (FirstBusApiClient , async_get_bus_times = async_mocked_get_bus_times ):
136+ (errors , config ) = await async_validate_main_config (original_config )
137+
138+ # Assert
139+ assert CONFIG_NAME not in errors
140+ assert CONFIG_STOP not in errors
141+ assert CONFIG_BUSES in errors
142+ assert errors [CONFIG_BUSES ] == "invalid_buses"
143+
144+ assert CONFIG_NAME in config
145+ assert config [CONFIG_NAME ] == original_config [CONFIG_NAME ]
146+ assert CONFIG_STOP in config
147+ assert config [CONFIG_STOP ] == original_config [CONFIG_STOP ]
148+ assert CONFIG_BUSES in config
149+ assert config [CONFIG_BUSES ] == original_config [CONFIG_BUSES ]
150+
151+ @pytest .mark .asyncio
152+ async def test_when_bus_stop_is_invalid_then_no_errors_returned ():
153+ # Arrange
154+ original_config = {
155+ CONFIG_NAME : "test" ,
156+ CONFIG_STOP : "123" ,
157+ CONFIG_BUSES : ""
158+ }
159+
160+ async def async_mocked_get_bus_times (* args , ** kwargs ):
161+ return None
162+
163+ # Act
164+ with mock .patch .multiple (FirstBusApiClient , async_get_bus_times = async_mocked_get_bus_times ):
165+ (errors , config ) = await async_validate_main_config (original_config )
166+
167+ # Assert
168+ assert CONFIG_NAME not in errors
169+ assert CONFIG_BUSES not in errors
170+ assert CONFIG_STOP in errors
171+ assert errors [CONFIG_STOP ] == "invalid_stop"
172+
173+ assert CONFIG_NAME in config
174+ assert config [CONFIG_NAME ] == original_config [CONFIG_NAME ]
175+ assert CONFIG_STOP in config
176+ assert config [CONFIG_STOP ] == original_config [CONFIG_STOP ]
177+ assert CONFIG_BUSES in config
178+ assert config [CONFIG_BUSES ] == []
0 commit comments