1
+ import pytest
2
+ from custom_components .first_bus .config import (validate_config )
3
+ from custom_components .first_bus .const import CONFIG_NAME , CONFIG_STOP , CONFIG_BUSES
4
+
5
+ @pytest .mark .asyncio
6
+ async def test_when_data_valid_then_no_errors_returned ():
7
+ # Arrange
8
+ original_config = {
9
+ CONFIG_NAME : "test" ,
10
+ CONFIG_STOP : "123" ,
11
+ CONFIG_BUSES : "12 A,12B,12"
12
+ }
13
+
14
+ # Act
15
+ (errors , config ) = validate_config (original_config )
16
+
17
+ # Assert
18
+ assert CONFIG_NAME not in errors
19
+ assert CONFIG_STOP not in errors
20
+ assert CONFIG_BUSES not in errors
21
+
22
+ assert CONFIG_NAME in config
23
+ assert config [CONFIG_NAME ] == original_config [CONFIG_NAME ]
24
+ assert CONFIG_STOP in config
25
+ assert config [CONFIG_STOP ] == original_config [CONFIG_STOP ]
26
+ assert CONFIG_BUSES in config
27
+ assert config [CONFIG_BUSES ] == ["12 A" , "12B" , "12" ]
28
+
29
+ @pytest .mark .asyncio
30
+ async def test_when_buses_not_present_then_buses_empty_array ():
31
+ # Arrange
32
+ original_config = {
33
+ CONFIG_NAME : "test" ,
34
+ CONFIG_STOP : "123"
35
+ }
36
+
37
+ # Act
38
+ (errors , config ) = validate_config (original_config )
39
+
40
+ # Assert
41
+ assert CONFIG_NAME not in errors
42
+ assert CONFIG_STOP not in errors
43
+ assert CONFIG_BUSES not in errors
44
+
45
+ assert CONFIG_NAME in config
46
+ assert config [CONFIG_NAME ] == original_config [CONFIG_NAME ]
47
+ assert CONFIG_STOP in config
48
+ assert config [CONFIG_STOP ] == original_config [CONFIG_STOP ]
49
+ assert CONFIG_BUSES in config
50
+ assert config [CONFIG_BUSES ] == []
51
+
52
+ @pytest .mark .asyncio
53
+ async def test_when_buses_none_then_buses_empty_array ():
54
+ # Arrange
55
+ original_config = {
56
+ CONFIG_NAME : "test" ,
57
+ CONFIG_STOP : "123" ,
58
+ CONFIG_BUSES : None
59
+ }
60
+
61
+ # Act
62
+ (errors , config ) = validate_config (original_config )
63
+
64
+ # Assert
65
+ assert CONFIG_NAME not in errors
66
+ assert CONFIG_STOP not in errors
67
+ assert CONFIG_BUSES not in errors
68
+
69
+ assert CONFIG_NAME in config
70
+ assert config [CONFIG_NAME ] == original_config [CONFIG_NAME ]
71
+ assert CONFIG_STOP in config
72
+ assert config [CONFIG_STOP ] == original_config [CONFIG_STOP ]
73
+ assert CONFIG_BUSES in config
74
+ assert config [CONFIG_BUSES ] == []
75
+
76
+ @pytest .mark .asyncio
77
+ async def test_when_buses_empty_then_buses_empty_array ():
78
+ # Arrange
79
+ original_config = {
80
+ CONFIG_NAME : "test" ,
81
+ CONFIG_STOP : "123" ,
82
+ CONFIG_BUSES : ""
83
+ }
84
+
85
+ # Act
86
+ (errors , config ) = validate_config (original_config )
87
+
88
+ # Assert
89
+ assert CONFIG_NAME not in errors
90
+ assert CONFIG_STOP not in errors
91
+ assert CONFIG_BUSES not in errors
92
+
93
+ assert CONFIG_NAME in config
94
+ assert config [CONFIG_NAME ] == original_config [CONFIG_NAME ]
95
+ assert CONFIG_STOP in config
96
+ assert config [CONFIG_STOP ] == original_config [CONFIG_STOP ]
97
+ assert CONFIG_BUSES in config
98
+ assert config [CONFIG_BUSES ] == []
99
+
100
+ @pytest .mark .asyncio
101
+ @pytest .mark .parametrize ("bus_value" ,[
102
+ ("A-B" ),
103
+ ("12,12B," ),
104
+ ])
105
+ async def test_when_buses_not_valid_then_buses_empty_array (bus_value : str ):
106
+ # Arrange
107
+ original_config = {
108
+ CONFIG_NAME : "test" ,
109
+ CONFIG_STOP : "123" ,
110
+ CONFIG_BUSES : bus_value
111
+ }
112
+
113
+ # Act
114
+ (errors , config ) = validate_config (original_config )
115
+
116
+ # Assert
117
+ assert CONFIG_NAME not in errors
118
+ assert CONFIG_STOP not in errors
119
+ assert CONFIG_BUSES in errors
120
+ assert errors [CONFIG_BUSES ] == "invalid_buses"
121
+
122
+ assert CONFIG_NAME in config
123
+ assert config [CONFIG_NAME ] == original_config [CONFIG_NAME ]
124
+ assert CONFIG_STOP in config
125
+ assert config [CONFIG_STOP ] == original_config [CONFIG_STOP ]
126
+ assert CONFIG_BUSES in config
127
+ assert config [CONFIG_BUSES ] == original_config [CONFIG_BUSES ]
0 commit comments