Skip to content

Commit 3aa50a8

Browse files
committed
Revire TestDevice using raises decorator
1 parent 1eb21f6 commit 3aa50a8

File tree

1 file changed

+166
-203
lines changed

1 file changed

+166
-203
lines changed

tests/devices_tests.py

Lines changed: 166 additions & 203 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,20 @@
99
from minicps.devices import Device, PLC, HMI, Tank, SCADAServer, RTU
1010
from minicps.states import SQLiteState
1111

12-
from nose.tools import eq_
12+
from nose.tools import eq_, raises
1313
from nose.plugins.skip import SkipTest
1414

1515
# NOTE: currently testing only set and get
1616
# TODO: find a way to test also send and recieve outside network emulaiton
1717

1818
class TestDevice():
1919

20-
"""TestDevice: build and input validation tests."""
20+
"""TestDevice: build and input validation tests.
21+
22+
Reading this tests is a good way to review the MiniCPS APIs for the
23+
physical process and network layer.
24+
25+
"""
2126

2227
NAME = 'devices_tests'
2328
STATE = {
@@ -42,207 +47,165 @@ class TestDevice():
4247
'TAG5': '5',
4348
}
4449

45-
def test_validate_device_name(self):
46-
"""Validate device name:
47-
48-
- name is an int
49-
- name is an empty string
50-
"""
51-
52-
try:
53-
device = Device(
54-
name=1,
55-
state=TestDevice.STATE,
56-
protocol=TestDevice.PROTOCOL)
57-
except TypeError as error:
58-
print 'name is an int: ', error
59-
60-
try:
61-
device = Device(
62-
name='',
63-
state=TestDevice.STATE,
64-
protocol=TestDevice.PROTOCOL)
65-
except ValueError as error:
66-
print 'name is empty string: ', error
67-
68-
def test_validate_state(self):
69-
"""Validate device state:
70-
71-
- state is a string
72-
- state is an empty dict
73-
- state has more/less than two keys
74-
- state has a wrong key
75-
- state has an unsupported path extension
76-
- state has an integer name value
77-
"""
78-
79-
try:
80-
device = Device(
81-
name=TestDevice.NAME,
82-
state='state',
83-
protocol=TestDevice.PROTOCOL)
84-
except TypeError as error:
85-
print 'state is string: ', error
86-
87-
try:
88-
device = Device(
89-
name=TestDevice.NAME,
90-
state={},
91-
protocol=TestDevice.PROTOCOL)
92-
except KeyError as error:
93-
print 'state is an empty dict: ', error
94-
95-
try:
96-
device = Device(
97-
name=TestDevice.NAME,
98-
state={
99-
'name': 'table_name', 'path': '/path.db',
100-
'wrong': 'key-val'},
101-
protocol=TestDevice.PROTOCOL)
102-
except KeyError as error:
103-
print 'state has more than 2 keys: ', error
104-
105-
try:
106-
device = Device(
107-
name=TestDevice.NAME,
108-
state={'name': 'table_name'},
109-
protocol=TestDevice.PROTOCOL)
110-
except KeyError as error:
111-
print 'state has less than 2 keys: ', error
112-
113-
try:
114-
device = Device(
115-
name=TestDevice.NAME,
116-
state={
117-
'path': '/bla',
118-
'bla': 'table_name'},
119-
protocol=TestDevice.PROTOCOL)
120-
except KeyError as error:
121-
print 'state has a wrong key: ', error
122-
123-
try:
124-
device = Device(
125-
name=TestDevice.NAME,
126-
state={
127-
'path': 0,
128-
'name': 0},
129-
protocol=TestDevice.PROTOCOL)
130-
except TypeError as error:
131-
print 'state has integer values: ', error
132-
133-
try:
134-
device = Device(
135-
name=TestDevice.NAME,
136-
state={
137-
'path': '/not/supported.ext',
138-
'name': 'table_name'},
139-
protocol=TestDevice.PROTOCOL)
140-
except ValueError as error:
141-
print 'state has an unsupported path extension: ', error
142-
143-
try:
144-
device = Device(
145-
name=TestDevice.NAME,
146-
state={
147-
'path': '/not/supported.ext',
148-
'name': 4},
149-
protocol=TestDevice.PROTOCOL)
150-
except TypeError as error:
151-
print 'state has an integer name: ', error
152-
153-
def test_validate_protocol(self):
154-
"""Validate device protocol:
155-
156-
- protocol is a string
157-
- protocol is an empty dict
158-
- protocol has more/less than three keys
159-
- protocol has a wrong key
160-
- protocol name is not a string
161-
- protocol has an unsupported name
162-
- protocol mode is a float
163-
- protocol mode is a negative int
164-
"""
165-
166-
try:
167-
device = Device(
168-
name=TestDevice.NAME,
169-
state=TestDevice.STATE,
170-
protocol='protocol')
171-
except TypeError as error:
172-
print 'protocol is string: ', error
173-
174-
try:
175-
device = Device(
176-
name=TestDevice.NAME,
177-
state=TestDevice.STATE,
178-
protocol={})
179-
except KeyError as error:
180-
print 'protocol is an empty dict: ', error
181-
182-
try:
183-
device = Device(
184-
name=TestDevice.NAME,
185-
state=TestDevice.STATE,
186-
protocol={
187-
'name': 'enip', 'mode': 0, 'server': '',
188-
'too': 'much'})
189-
except KeyError as error:
190-
print 'protocol has more than 3 keys: ', error
191-
192-
try:
193-
device = Device(
194-
name=TestDevice.NAME,
195-
state=TestDevice.STATE,
196-
protocol={'name': 'enip', 'mode': 0})
197-
except KeyError as error:
198-
print 'protocol has less than 3 keys: ', error
199-
200-
try:
201-
device = Device(
202-
name=TestDevice.NAME,
203-
state=TestDevice.STATE,
204-
protocol={
205-
'name': 'enip', 'mode': 0, 'bla': ''})
206-
except KeyError as error:
207-
print 'protocol has a wrong key: ', error
208-
209-
# protocol['name']
210-
try:
211-
device = Device(
212-
name=TestDevice.NAME,
213-
state=TestDevice.STATE,
214-
protocol={
215-
'name': 1, 'mode': 0, 'server': ''})
216-
except TypeError as error:
217-
print 'protocol name is not a string: ', error
218-
219-
try:
220-
device = Device(
221-
name=TestDevice.NAME,
222-
state=TestDevice.STATE,
223-
protocol={
224-
'name': 'wow', 'mode': 0, 'server': ''})
225-
except ValueError as error:
226-
print 'protocol has an unsupported name: ', error
227-
228-
# protocol['mode']
229-
try:
230-
device = Device(
231-
name=TestDevice.NAME,
232-
state=TestDevice.STATE,
233-
protocol={
234-
'name': 'enip', 'mode': 0.3, 'server': ''})
235-
except TypeError as error:
236-
print 'protocol mode is a float: ', error
237-
238-
try:
239-
device = Device(
240-
name=TestDevice.NAME,
241-
state=TestDevice.STATE,
242-
protocol={
243-
'name': 'enip', 'mode': -3, 'server': ''})
244-
except ValueError as error:
245-
print 'protocol mode is a negative int: ', error
50+
@raises(TypeError)
51+
def test_device_name_is_int(self):
52+
53+
device = Device(
54+
name=1,
55+
state=TestDevice.STATE,
56+
protocol=TestDevice.PROTOCOL)
57+
58+
@raises(ValueError)
59+
def test_device_name_is_empty_string(self):
60+
61+
device = Device(
62+
name='',
63+
state=TestDevice.STATE,
64+
protocol=TestDevice.PROTOCOL)
65+
66+
@raises(TypeError)
67+
def test_device_state_is_string(self):
68+
69+
device = Device(
70+
name=TestDevice.NAME,
71+
state='state',
72+
protocol=TestDevice.PROTOCOL)
73+
74+
@raises(KeyError)
75+
def test_device_state_empty_dict(self):
76+
77+
device = Device(
78+
name=TestDevice.NAME,
79+
state={},
80+
protocol=TestDevice.PROTOCOL)
81+
82+
@raises(KeyError)
83+
def test_device_state_more_than_two_keys(self):
84+
85+
device = Device(
86+
name=TestDevice.NAME,
87+
state={
88+
'name': 'table_name', 'path': '/path.db',
89+
'wrong': 'key-val'},
90+
protocol=TestDevice.PROTOCOL)
91+
92+
@raises(KeyError)
93+
def test_device_state_less_than_two_keys(self):
94+
95+
device = Device(
96+
name=TestDevice.NAME,
97+
state={'name': 'table_name'},
98+
protocol=TestDevice.PROTOCOL)
99+
100+
@raises(KeyError)
101+
def test_device_state_wrong_key(self):
102+
103+
device = Device(
104+
name=TestDevice.NAME,
105+
state={
106+
'path': '/bla',
107+
'bla': 'table_name'},
108+
protocol=TestDevice.PROTOCOL)
109+
110+
@raises(TypeError)
111+
def test_device_state_integer_values(self):
112+
113+
device = Device(
114+
name=TestDevice.NAME,
115+
state={
116+
'path': 0,
117+
'name': 0},
118+
protocol=TestDevice.PROTOCOL)
119+
120+
@raises(ValueError)
121+
def test_device_state_unsupported_path_extension(self):
122+
123+
device = Device(
124+
name=TestDevice.NAME,
125+
state={
126+
'path': '/not/supported.ext',
127+
'name': 'table_name'},
128+
protocol=TestDevice.PROTOCOL)
129+
130+
131+
@raises(TypeError)
132+
def test_device_protocol_is_string(self):
133+
134+
device = Device(
135+
name=TestDevice.NAME,
136+
state=TestDevice.STATE,
137+
protocol='protocol')
138+
139+
@raises(KeyError)
140+
def test_device_protocol_empty_dict(self):
141+
142+
device = Device(
143+
name=TestDevice.NAME,
144+
state=TestDevice.STATE,
145+
protocol={})
146+
147+
@raises(KeyError)
148+
def test_device_protocol_more_than_three_keys(self):
149+
150+
device = Device(
151+
name=TestDevice.NAME,
152+
state=TestDevice.STATE,
153+
protocol={
154+
'name': 'enip', 'mode': 0, 'server': '',
155+
'too': 'much'})
156+
157+
@raises(KeyError)
158+
def test_device_protocol_less_than_three_keys(self):
159+
160+
device = Device(
161+
name=TestDevice.NAME,
162+
state=TestDevice.STATE,
163+
protocol={'name': 'enip', 'mode': 0})
164+
165+
@raises(KeyError)
166+
def test_device_protocol_wrong_key(self):
167+
168+
device = Device(
169+
name=TestDevice.NAME,
170+
state=TestDevice.STATE,
171+
protocol={
172+
'name': 'enip', 'mode': 0, 'bla': ''})
173+
174+
@raises(TypeError)
175+
def test_device_protocol_name_not_string(self):
176+
177+
device = Device(
178+
name=TestDevice.NAME,
179+
state=TestDevice.STATE,
180+
protocol={
181+
'name': 1, 'mode': 0, 'server': ''})
182+
183+
@raises(ValueError)
184+
def test_device_protocol_name_unsupported_name(self):
185+
186+
device = Device(
187+
name=TestDevice.NAME,
188+
state=TestDevice.STATE,
189+
protocol={
190+
'name': 'wow', 'mode': 0, 'server': ''})
191+
192+
@raises(TypeError)
193+
def test_device_protocol_mode_float(self):
194+
195+
device = Device(
196+
name=TestDevice.NAME,
197+
state=TestDevice.STATE,
198+
protocol={
199+
'name': 'enip', 'mode': 0.3, 'server': ''})
200+
201+
@raises(ValueError)
202+
def test_device_protocol_mode_negative_int(self):
203+
204+
device = Device(
205+
name=TestDevice.NAME,
206+
state=TestDevice.STATE,
207+
protocol={
208+
'name': 'enip', 'mode': -3, 'server': ''})
246209

247210
# protocol['server'] TODO
248211
# protocol['client'] TODO

0 commit comments

Comments
 (0)