Skip to content

Commit fcaa53c

Browse files
committed
generalize the tests
1 parent b41d0cc commit fcaa53c

File tree

1 file changed

+46
-81
lines changed

1 file changed

+46
-81
lines changed

β€Žtest/test_ws.py

+46-81
Original file line numberDiff line numberDiff line change
@@ -26,115 +26,80 @@ def test_list(socketio):
2626
reason="VMs have no serial ports",
2727
)
2828
def test_open_serial_default(socketio):
29-
time.sleep(.2)
30-
global message
31-
message = []
32-
socketio.on('message', message_handler)
33-
socketio.emit('command', 'open /dev/ttyACM0 9600')
34-
time.sleep(.5) # give time to message to be filled
35-
assert any("\"IsOpen\": true" in i for i in message)
36-
socketio.emit('command', 'send /dev/ttyACM0 /"ciao/"')
37-
time.sleep(.2)
38-
assert any("send /dev/ttyACM0 /\"ciao/\"" in i for i in message)
39-
assert "ciao" in extract_serial_data(message)
29+
general_test_serial(socketio, "default")
4030

41-
# test with a lot of emoji: they can be messed up
42-
# message = [] # reinitialize the message buffer
43-
socketio.emit('command', 'send /dev/ttyACM0 /"πŸ§€πŸ§€πŸ§€πŸ§€πŸ§€πŸ§€πŸ§€πŸ§€πŸ§€πŸ§€/"')
44-
time.sleep(.2)
45-
print (message)
46-
assert any("send /dev/ttyACM0 /\"πŸ§€πŸ§€πŸ§€πŸ§€πŸ§€πŸ§€πŸ§€πŸ§€πŸ§€πŸ§€/\"" in i for i in message)
47-
emoji_output = extract_serial_data(message)
48-
assert "/\"πŸ§€πŸ§€πŸ§€πŸ§€πŸ§€πŸ§€πŸ§€πŸ§€πŸ§€πŸ§€/\"" in emoji_output # this could be failing because of UTF8 encoding problems
49-
message = []
50-
socketio.emit('command', 'close /dev/ttyACM0')
51-
time.sleep(.2)
52-
assert any("\"IsOpen\": false," in i for i in message)
5331

5432
@pytest.mark.skipif(
5533
running_on_ci(),
5634
reason="VMs have no serial ports",
5735
)
5836
def test_open_serial_timed(socketio):
59-
time.sleep(.2)
60-
global message
61-
message = []
62-
socketio.on('message', message_handler)
63-
socketio.emit('command', 'open /dev/ttyACM0 9600 timed')
64-
time.sleep(.5) # give time to message to be filled
65-
print(message)
66-
assert any("\"IsOpen\": true" in i for i in message)
67-
socketio.emit('command', 'send /dev/ttyACM0 /"ciao/"')
68-
time.sleep(.2)
69-
assert any("send /dev/ttyACM0 /\"ciao/\"" in i for i in message)
70-
assert "ciao" in extract_serial_data(message)
37+
general_test_serial(socketio, "timed")
7138

72-
# test with a lot of emoji: usually they get messed up
73-
message = [] # reinitialize the message buffer
74-
socketio.emit('command', 'send /dev/ttyACM0 /"πŸ§€πŸ§€πŸ§€πŸ§€πŸ§€πŸ§€πŸ§€πŸ§€πŸ§€πŸ§€/"')
75-
time.sleep(.2)
76-
assert any("send /dev/ttyACM0 /\"πŸ§€πŸ§€πŸ§€πŸ§€πŸ§€πŸ§€πŸ§€πŸ§€πŸ§€πŸ§€/\"" in i for i in message)
77-
assert "/\"πŸ§€πŸ§€πŸ§€πŸ§€πŸ§€πŸ§€πŸ§€πŸ§€πŸ§€πŸ§€/\"" in extract_serial_data(message)
78-
message = []
79-
socketio.emit('command', 'close /dev/ttyACM0')
80-
time.sleep(.2)
81-
# print (message)
82-
assert any("\"IsOpen\": false," in i for i in message)
8339

8440
@pytest.mark.skipif(
8541
running_on_ci(),
8642
reason="VMs have no serial ports",
8743
)
8844
def test_open_serial_timedraw(socketio):
89-
global message
90-
message = []
91-
socketio.on('message', message_handler)
92-
socketio.emit('command', 'open /dev/ttyACM0 9600 timedraw')
93-
time.sleep(.5) # give time to message to be filled
94-
assert any("\"IsOpen\": true" in i for i in message)
95-
socketio.emit('command', 'send /dev/ttyACM0 /"ciao/"')
96-
time.sleep(.2)
97-
assert any("send /dev/ttyACM0 /\"ciao/\"" in i for i in message)
98-
assert "ciao" in decode_output(extract_serial_data(message))
45+
general_test_serial(socketio, "timedraw")
9946

100-
# test with a lot of emoji: usually they get messed up
101-
message = [] # reinitialize the message buffer
102-
socketio.emit('command', 'send /dev/ttyACM0 /"πŸ§€πŸ§€πŸ§€πŸ§€πŸ§€πŸ§€πŸ§€πŸ§€πŸ§€πŸ§€/"')
103-
time.sleep(.2)
104-
assert any("send /dev/ttyACM0 /\"πŸ§€πŸ§€πŸ§€πŸ§€πŸ§€πŸ§€πŸ§€πŸ§€πŸ§€πŸ§€/\"" in i for i in message)
105-
# print (message)
106-
assert "/\"πŸ§€πŸ§€πŸ§€πŸ§€πŸ§€πŸ§€πŸ§€πŸ§€πŸ§€πŸ§€/\"" in decode_output(extract_serial_data(message))
107-
socketio.emit('command', 'close /dev/ttyACM0')
108-
time.sleep(.2)
109-
# print (message)
110-
assert any("\"IsOpen\": false," in i for i in message)
11147

11248
@pytest.mark.skipif(
11349
running_on_ci(),
11450
reason="VMs have no serial ports",
11551
)
11652
def test_open_serial_timedbinary(socketio):
53+
general_test_serial(socketio, "timedbinary")
54+
55+
56+
def general_test_serial(socketio, buffertype):
57+
port = "/dev/ttyACM0"
11758
global message
11859
message = []
60+
#in message var we will find the "response"
11961
socketio.on('message', message_handler)
120-
socketio.emit('command', 'open /dev/ttyACM0 9600 timedbinary')
121-
time.sleep(.5) # give time to message to be filled
62+
#open a new serial connection with the specified buffertype, if buffertype s empty it will use the default one
63+
socketio.emit('command', 'open ' + port + ' 9600 ' + buffertype)
64+
# give time to the message var to be filled
65+
time.sleep(.5)
66+
print(message)
67+
# the serial connection should be open now
12268
assert any("\"IsOpen\": true" in i for i in message)
123-
socketio.emit('command', 'send /dev/ttyACM0 /"ciao/"')
124-
time.sleep(.2)
125-
assert any("send /dev/ttyACM0 /\"ciao/\"" in i for i in message)
126-
print (message)
127-
assert "ciao" in decode_output(extract_serial_data(message))
12869

129-
# test with a lot of emoji: usually they get messed up
130-
message = [] # reinitialize the message buffer
131-
socketio.emit('command', 'send /dev/ttyACM0 /"πŸ§€πŸ§€πŸ§€πŸ§€πŸ§€πŸ§€πŸ§€πŸ§€πŸ§€πŸ§€/"')
70+
#test with string
71+
# send the string "ciao" using the serial connection
72+
socketio.emit('command', 'send ' + port + ' /"ciao/"')
73+
time.sleep(1)
74+
print(message)
75+
# check if the send command has been registered
76+
assert any("send " + port + " /\"ciao/\"" in i for i in message)
77+
#check if message has been sent back by the connected board
78+
if buffertype in ("timedbinary", "timedraw"):
79+
output = decode_output(extract_serial_data(message))
80+
elif buffertype in ("default", "timed"):
81+
output = extract_serial_data(message)
82+
assert "ciao" in output
83+
84+
#test with emoji
85+
message = [] # reinitialize the message buffer to have a clean situation
86+
# send a lot of emoji: they can be messed up
87+
socketio.emit('command', 'send ' + port + ' /"πŸ§€πŸ§€πŸ§€πŸ§€πŸ§€πŸ§€πŸ§€πŸ§€πŸ§€πŸ§€/"')
13288
time.sleep(.2)
133-
assert any("send /dev/ttyACM0 /\"πŸ§€πŸ§€πŸ§€πŸ§€πŸ§€πŸ§€πŸ§€πŸ§€πŸ§€πŸ§€/\"" in i for i in message)
134-
assert "/\"πŸ§€πŸ§€πŸ§€πŸ§€πŸ§€πŸ§€πŸ§€πŸ§€πŸ§€πŸ§€/\"" in decode_output(extract_serial_data(message))
135-
socketio.emit('command', 'close /dev/ttyACM0')
89+
print(message)
90+
# check if the send command has been registered
91+
assert any("send " + port + " /\"πŸ§€πŸ§€πŸ§€πŸ§€πŸ§€πŸ§€πŸ§€πŸ§€πŸ§€πŸ§€/\"" in i for i in message)
92+
if buffertype in ("timedbinary", "timedraw"):
93+
output = decode_output(extract_serial_data(message))
94+
elif buffertype in ("default", "timed"):
95+
output = extract_serial_data(message)
96+
assert "/\"πŸ§€πŸ§€πŸ§€πŸ§€πŸ§€πŸ§€πŸ§€πŸ§€πŸ§€πŸ§€/\"" in output
97+
98+
#finally close the serial port
99+
socketio.emit('command', 'close ' + port)
136100
time.sleep(.2)
137-
# print (message)
101+
print (message)
102+
#check if port has been closed
138103
assert any("\"IsOpen\": false," in i for i in message)
139104

140105

0 commit comments

Comments
Β (0)