Skip to content

Commit f9f0bf0

Browse files
committed
Refactor message type accessor, improve tests
Signed-off-by: Pierre R. Mai <[email protected]>
1 parent 91cc877 commit f9f0bf0

File tree

3 files changed

+34
-2
lines changed

3 files changed

+34
-2
lines changed

osi3trace/osi2read.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
python3 osi2read.py -d trace.osi -o myreadableosifile
66
"""
77

8-
from osi3trace.osi_trace import OSITrace, MESSAGES_TYPE
8+
from osi3trace.osi_trace import OSITrace
99
import argparse
1010
import pathlib
1111

@@ -28,7 +28,7 @@ def command_line_arguments():
2828
"--type",
2929
"-t",
3030
help="Name of the type used to serialize data.",
31-
choices=list(MESSAGES_TYPE.keys()),
31+
choices=OSITrace.message_types(),
3232
default="SensorView",
3333
type=str,
3434
required=False,

osi3trace/osi_trace.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ def map_message_type(type_name):
3737
"""Map the type name to the protobuf message type."""
3838
return MESSAGES_TYPE[type_name]
3939

40+
@staticmethod
41+
def message_types():
42+
"""Message types that OSITrace supports."""
43+
return list(MESSAGES_TYPE.keys())
44+
4045
def __init__(self, path=None, type_name="SensorView", cache_messages=False):
4146
self.type = self.map_message_type(type_name)
4247
self.file = None

tests/test_osi_trace.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,10 @@ def test_osi_trace_sv(self):
2626
trace = OSITrace(path_input)
2727
with open(path_output, "wt") as f:
2828
for message in trace:
29+
self.assertIsInstance(message, SensorView)
2930
f.write(str(message))
31+
32+
self.assertEqual(len(trace.retrieve_offsets()), 10)
3033
trace.close()
3134

3235
self.assertTrue(os.path.exists(path_output))
@@ -40,7 +43,10 @@ def test_osi_trace_gt(self):
4043
trace = OSITrace(path_input, "GroundTruth")
4144
with open(path_output, "wt") as f:
4245
for message in trace:
46+
self.assertIsInstance(message, GroundTruth)
4347
f.write(str(message))
48+
49+
self.assertEqual(len(trace.retrieve_offsets()), 10)
4450
trace.close()
4551

4652
self.assertTrue(os.path.exists(path_output))
@@ -54,7 +60,10 @@ def test_osi_trace_sd(self):
5460
trace = OSITrace(path_input, "SensorData")
5561
with open(path_output, "wt") as f:
5662
for message in trace:
63+
self.assertIsInstance(message, SensorData)
5764
f.write(str(message))
65+
66+
self.assertEqual(len(trace.retrieve_offsets()), 10)
5867
trace.close()
5968

6069
self.assertTrue(os.path.exists(path_output))
@@ -68,7 +77,10 @@ def test_osi_trace_svc(self):
6877
trace = OSITrace(path_input, "SensorViewConfiguration")
6978
with open(path_output, "wt") as f:
7079
for message in trace:
80+
self.assertIsInstance(message, SensorViewConfiguration)
7181
f.write(str(message))
82+
83+
self.assertEqual(len(trace.retrieve_offsets()), 1)
7284
trace.close()
7385

7486
self.assertTrue(os.path.exists(path_output))
@@ -82,7 +94,10 @@ def test_osi_trace_tu(self):
8294
trace = OSITrace(path_input, "TrafficUpdate")
8395
with open(path_output, "wt") as f:
8496
for message in trace:
97+
self.assertIsInstance(message, TrafficUpdate)
8598
f.write(str(message))
99+
100+
self.assertEqual(len(trace.retrieve_offsets()), 10)
86101
trace.close()
87102

88103
self.assertTrue(os.path.exists(path_output))
@@ -96,7 +111,10 @@ def test_osi_trace_tc(self):
96111
trace = OSITrace(path_input, "TrafficCommand")
97112
with open(path_output, "wt") as f:
98113
for message in trace:
114+
self.assertIsInstance(message, TrafficCommand)
99115
f.write(str(message))
116+
117+
self.assertEqual(len(trace.retrieve_offsets()), 10)
100118
trace.close()
101119

102120
self.assertTrue(os.path.exists(path_output))
@@ -110,7 +128,10 @@ def test_osi_trace_tcu(self):
110128
trace = OSITrace(path_input, "TrafficCommandUpdate")
111129
with open(path_output, "wt") as f:
112130
for message in trace:
131+
self.assertIsInstance(message, TrafficCommandUpdate)
113132
f.write(str(message))
133+
134+
self.assertEqual(len(trace.retrieve_offsets()), 10)
114135
trace.close()
115136

116137
self.assertTrue(os.path.exists(path_output))
@@ -124,7 +145,10 @@ def test_osi_trace_mr(self):
124145
trace = OSITrace(path_input, "MotionRequest")
125146
with open(path_output, "wt") as f:
126147
for message in trace:
148+
self.assertIsInstance(message, MotionRequest)
127149
f.write(str(message))
150+
151+
self.assertEqual(len(trace.retrieve_offsets()), 10)
128152
trace.close()
129153

130154
self.assertTrue(os.path.exists(path_output))
@@ -138,7 +162,10 @@ def test_osi_trace_su(self):
138162
trace = OSITrace(path_input, "StreamingUpdate")
139163
with open(path_output, "wt") as f:
140164
for message in trace:
165+
self.assertIsInstance(message, StreamingUpdate)
141166
f.write(str(message))
167+
168+
self.assertEqual(len(trace.retrieve_offsets()), 10)
142169
trace.close()
143170

144171
self.assertTrue(os.path.exists(path_output))

0 commit comments

Comments
 (0)