1
- import asyncio
1
+ """Test the driver correctly parses a tags file and responds with correct data."""
2
2
3
3
import pytest
4
4
7
7
8
8
@pytest .fixture
9
9
def plc_driver ():
10
+ """Confirm the driver correctly initializes without a tags file."""
10
11
return ClickPLC ('fake ip' )
11
12
12
13
13
14
@pytest .fixture
14
15
def tagged_driver ():
16
+ """Confirm the driver correctly initializes with a good tags file."""
15
17
return ClickPLC ('fake ip' , 'tests/plc_tags.csv' )
16
18
19
+
17
20
@pytest .fixture
18
21
def expected_tags ():
22
+ """Return the tags defined in the tags file."""
19
23
return {
20
24
'IO2_24V_OK' : {'address' : {'start' : 16397 }, 'id' : 'C13' , 'type' : 'bool' },
21
25
'IO2_Module_OK' : {'address' : {'start' : 16396 }, 'id' : 'C12' , 'type' : 'bool' },
@@ -38,16 +42,19 @@ def expected_tags():
38
42
39
43
40
44
def test_get_tags (tagged_driver , expected_tags ):
45
+ """Confirm that the driver returns correct values on get() calls."""
41
46
assert expected_tags == tagged_driver .get_tags ()
42
47
43
48
44
49
def test_unsupported_tags ():
50
+ """Confirm the driver detects an improper tags file."""
45
51
with pytest .raises (TypeError , match = 'unsupported data type' ):
46
52
ClickPLC ('fake ip' , 'tests/bad_tags.csv' )
47
53
48
54
49
55
@pytest .mark .asyncio
50
56
async def test_tagged_driver (tagged_driver , expected_tags ):
57
+ """Test a roundtrip with the driver using a tags file."""
51
58
await tagged_driver .set ('VAH_101_OK' , True )
52
59
state = await tagged_driver .get ()
53
60
assert state .get ('VAH_101_OK' )
@@ -57,6 +64,7 @@ async def test_tagged_driver(tagged_driver, expected_tags):
57
64
@pytest .mark .asyncio
58
65
@pytest .mark .parametrize ('prefix' , ['x' , 'y' ])
59
66
async def test_bool_roundtrip (plc_driver , prefix ):
67
+ """Confirm x and y bools are read back correctly after being set."""
60
68
await plc_driver .set (f'{ prefix } 2' , True )
61
69
await plc_driver .set (f'{ prefix } 3' , [False , True ])
62
70
expected = {f'{ prefix } 001' : False , f'{ prefix } 002' : True , f'{ prefix } 003' : False ,
@@ -66,6 +74,7 @@ async def test_bool_roundtrip(plc_driver, prefix):
66
74
67
75
@pytest .mark .asyncio
68
76
async def test_c_roundtrip (plc_driver ):
77
+ """Confirm c bools are read back correctly after being set."""
69
78
await plc_driver .set ('c2' , True )
70
79
await plc_driver .set ('c3' , [False , True ])
71
80
expected = {'c1' : False , 'c2' : True , 'c3' : False , 'c4' : True , 'c5' : False }
@@ -74,6 +83,7 @@ async def test_c_roundtrip(plc_driver):
74
83
75
84
@pytest .mark .asyncio
76
85
async def test_df_roundtrip (plc_driver ):
86
+ """Confirm df floats are read back correctly after being set."""
77
87
await plc_driver .set ('df2' , 2.0 )
78
88
await plc_driver .set ('df3' , [3.0 , 4.0 ])
79
89
expected = {'df1' : 0.0 , 'df2' : 2.0 , 'df3' : 3.0 , 'df4' : 4.0 , 'df5' : 0.0 }
@@ -82,6 +92,7 @@ async def test_df_roundtrip(plc_driver):
82
92
83
93
@pytest .mark .asyncio
84
94
async def test_ds_roundtrip (plc_driver ):
95
+ """Confirm ds ints are read back correctly after being set."""
85
96
await plc_driver .set ('ds2' , 2 )
86
97
await plc_driver .set ('ds3' , [3 , 4 ])
87
98
expected = {'ds1' : 0 , 'ds2' : 2 , 'ds3' : 3 , 'ds4' : 4 , 'ds5' : 0 }
@@ -90,6 +101,7 @@ async def test_ds_roundtrip(plc_driver):
90
101
91
102
@pytest .mark .asyncio
92
103
async def test_get_error_handling (plc_driver ):
104
+ """Confirm the driver gives an error on invalid get() calls."""
93
105
with pytest .raises (ValueError , match = 'An address must be supplied' ):
94
106
await plc_driver .get ()
95
107
with pytest .raises (ValueError , match = 'End address must be greater than start address' ):
@@ -102,68 +114,74 @@ async def test_get_error_handling(plc_driver):
102
114
103
115
@pytest .mark .asyncio
104
116
async def test_set_error_handling (plc_driver ):
117
+ """Confirm the driver gives an error on invalid set() calls."""
105
118
with pytest .raises (ValueError , match = 'foo currently unsupported' ):
106
119
await plc_driver .set ('foo1' , 1 )
107
120
108
121
109
122
@pytest .mark .asyncio
110
123
@pytest .mark .parametrize ('prefix' , ['x' , 'y' ])
111
124
async def test_xy_error_handling (plc_driver , prefix ):
112
- with pytest .raises (ValueError , match = 'address must be \*01-\*16.' ):
125
+ """Ensure errors are handled for invalid requests of x and y registers."""
126
+ with pytest .raises (ValueError , match = r'address must be \*01-\*16.' ):
113
127
await plc_driver .get (f'{ prefix } 17' )
114
- with pytest .raises (ValueError , match = 'address must be in \[001, 816\].' ):
128
+ with pytest .raises (ValueError , match = r 'address must be in \[001, 816\].' ):
115
129
await plc_driver .get (f'{ prefix } 1001' )
116
- with pytest .raises (ValueError , match = 'address must be \*01-\*16.' ):
130
+ with pytest .raises (ValueError , match = r 'address must be \*01-\*16.' ):
117
131
await plc_driver .get (f'{ prefix } 1-{ prefix } 17' )
118
- with pytest .raises (ValueError , match = 'address must be in \[001, 816\].' ):
132
+ with pytest .raises (ValueError , match = r 'address must be in \[001, 816\].' ):
119
133
await plc_driver .get (f'{ prefix } 1-{ prefix } 1001' )
120
- with pytest .raises (ValueError , match = 'address must be \*01-\*16.' ):
134
+ with pytest .raises (ValueError , match = r 'address must be \*01-\*16.' ):
121
135
await plc_driver .set (f'{ prefix } 17' , True )
122
- with pytest .raises (ValueError , match = 'address must be in \[001, 816\].' ):
136
+ with pytest .raises (ValueError , match = r 'address must be in \[001, 816\].' ):
123
137
await plc_driver .set (f'{ prefix } 1001' , True )
124
- with pytest .raises (ValueError , match = 'Data list longer than available addresses.' ):
138
+ with pytest .raises (ValueError , match = r 'Data list longer than available addresses.' ):
125
139
await plc_driver .set (f'{ prefix } 816' , [True , True ])
126
140
127
141
128
142
@pytest .mark .asyncio
129
143
async def test_c_error_handling (plc_driver ):
130
- with pytest .raises (ValueError , match = 'C start address must be 1-2000.' ):
144
+ """Ensure errors are handled for invalid requests of c registers."""
145
+ with pytest .raises (ValueError , match = r'C start address must be 1-2000.' ):
131
146
await plc_driver .get ('c2001' )
132
- with pytest .raises (ValueError , match = 'C end address must be >start and <2000.' ):
147
+ with pytest .raises (ValueError , match = r 'C end address must be >start and <2000.' ):
133
148
await plc_driver .get ('c1-c2001' )
134
- with pytest .raises (ValueError , match = 'C start address must be 1-2000.' ):
149
+ with pytest .raises (ValueError , match = r 'C start address must be 1-2000.' ):
135
150
await plc_driver .set ('c2001' , True )
136
- with pytest .raises (ValueError , match = 'Data list longer than available addresses.' ):
151
+ with pytest .raises (ValueError , match = r 'Data list longer than available addresses.' ):
137
152
await plc_driver .set ('c2000' , [True , True ])
138
153
139
154
140
155
@pytest .mark .asyncio
141
156
async def test_df_error_handling (plc_driver ):
142
- with pytest .raises (ValueError , match = 'DF must be in \[1, 500\]' ):
157
+ """Ensure errors are handled for invalid requests of df registers."""
158
+ with pytest .raises (ValueError , match = r'DF must be in \[1, 500\]' ):
143
159
await plc_driver .get ('df501' )
144
- with pytest .raises (ValueError , match = 'DF end must be in \[1, 500\]' ):
160
+ with pytest .raises (ValueError , match = r 'DF end must be in \[1, 500\]' ):
145
161
await plc_driver .get ('df1-df501' )
146
- with pytest .raises (ValueError , match = 'DF must be in \[1, 500\]' ):
162
+ with pytest .raises (ValueError , match = r 'DF must be in \[1, 500\]' ):
147
163
await plc_driver .set ('df501' , 1.0 )
148
- with pytest .raises (ValueError , match = 'Data list longer than available addresses.' ):
164
+ with pytest .raises (ValueError , match = r 'Data list longer than available addresses.' ):
149
165
await plc_driver .set ('df500' , [1.0 , 2.0 ])
150
166
151
167
152
168
@pytest .mark .asyncio
153
169
async def test_ds_error_handling (plc_driver ):
154
- with pytest .raises (ValueError , match = 'DS must be in \[1, 4500\]' ):
170
+ """Ensure errors are handled for invalid requests of ds registers."""
171
+ with pytest .raises (ValueError , match = r'DS must be in \[1, 4500\]' ):
155
172
await plc_driver .get ('ds4501' )
156
- with pytest .raises (ValueError , match = 'DS end must be in \[1, 4500\]' ):
173
+ with pytest .raises (ValueError , match = r 'DS end must be in \[1, 4500\]' ):
157
174
await plc_driver .get ('ds1-ds4501' )
158
- with pytest .raises (ValueError , match = 'DS must be in \[1, 4500\]' ):
175
+ with pytest .raises (ValueError , match = r 'DS must be in \[1, 4500\]' ):
159
176
await plc_driver .set ('ds4501' , 1 )
160
- with pytest .raises (ValueError , match = 'Data list longer than available addresses.' ):
177
+ with pytest .raises (ValueError , match = r 'Data list longer than available addresses.' ):
161
178
await plc_driver .set ('ds4500' , [1 , 2 ])
162
179
163
180
164
181
@pytest .mark .asyncio
165
182
@pytest .mark .parametrize ('prefix' , ['x' , 'y' , 'c' ])
166
183
async def test_bool_typechecking (plc_driver , prefix ):
184
+ """Ensure errors are handled for set() requests that should be bools."""
167
185
with pytest .raises (ValueError , match = 'Expected .+ as a bool' ):
168
186
await plc_driver .set (f'{ prefix } 1' , 1 )
169
187
with pytest .raises (ValueError , match = 'Expected .+ as a bool' ):
@@ -172,6 +190,7 @@ async def test_bool_typechecking(plc_driver, prefix):
172
190
173
191
@pytest .mark .asyncio
174
192
async def test_df_typechecking (plc_driver ):
193
+ """Ensure errors are handled for set() requests that should be floats."""
175
194
await plc_driver .set ('df1' , 1 )
176
195
with pytest .raises (ValueError , match = 'Expected .+ as a float' ):
177
196
await plc_driver .set ('df1' , True )
@@ -181,6 +200,7 @@ async def test_df_typechecking(plc_driver):
181
200
182
201
@pytest .mark .asyncio
183
202
async def test_ds_typechecking (plc_driver ):
203
+ """Ensure errors are handled for set() requests that should be ints."""
184
204
with pytest .raises (ValueError , match = 'Expected .+ as a int' ):
185
205
await plc_driver .set ('ds1' , 1.0 )
186
206
with pytest .raises (ValueError , match = 'Expected .+ as a int' ):
0 commit comments