tests: use assertRaisesRegex instead of assertRaises in test_stream_class.py
[babeltrace.git] / tests / bindings / python / bt2 / test_stream_class.py
CommitLineData
d2d857a8
MJ
1#
2# Copyright (C) 2019 EfficiOS Inc.
3#
4# This program is free software; you can redistribute it and/or
5# modify it under the terms of the GNU General Public License
6# as published by the Free Software Foundation; only version 2
7# of the License.
8#
9# This program is distributed in the hope that it will be useful,
10# but WITHOUT ANY WARRANTY; without even the implied warranty of
11# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12# GNU General Public License for more details.
13#
14# You should have received a copy of the GNU General Public License
15# along with this program; if not, write to the Free Software
16# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17#
18
9cf643d1 19import unittest
3cdfbaea 20from utils import run_in_component_init
f0a42b33
FD
21from bt2 import stream_class as bt2_stream_class
22from bt2 import trace_class as bt2_trace_class
23from bt2 import clock_class as bt2_clock_class
24from bt2 import event_class as bt2_event_class
25from bt2 import field_class as bt2_field_class
9cf643d1
PP
26
27
28class StreamClassTestCase(unittest.TestCase):
29 def setUp(self):
3cdfbaea
SM
30 def f(comp_self):
31 tc = comp_self._create_trace_class(assigns_automatic_stream_class_id=True)
32 cc = comp_self._create_clock_class()
33 return tc, cc
34
35 self._tc, self._cc = run_in_component_init(f)
36 self._trace = self._tc()
37
38 def test_create_default(self):
39 sc = self._tc.create_stream_class()
40
f0a42b33 41 self.assertIs(type(sc), bt2_stream_class._StreamClass)
3cdfbaea
SM
42 self.assertIsNone(sc.name)
43 self.assertIsNone(sc.packet_context_field_class)
44 self.assertIsNone(sc.event_common_context_field_class)
45 self.assertIsNone(sc.default_clock_class)
46 self.assertTrue(sc.assigns_automatic_event_class_id)
47 self.assertTrue(sc.assigns_automatic_stream_id)
26fc5aed 48 self.assertFalse(sc.supports_packets)
9b24b6aa
PP
49 self.assertFalse(sc.packets_have_beginning_default_clock_snapshot)
50 self.assertFalse(sc.packets_have_end_default_clock_snapshot)
2e90378a
PP
51 self.assertFalse(sc.supports_discarded_events)
52 self.assertFalse(sc.discarded_events_have_default_clock_snapshots)
53 self.assertFalse(sc.supports_discarded_packets)
54 self.assertFalse(sc.discarded_packets_have_default_clock_snapshots)
5783664e 55 self.assertEqual(len(sc.user_attributes), 0)
3cdfbaea
SM
56
57 def test_create_name(self):
58 sc = self._tc.create_stream_class(name='bozo')
59 self.assertEqual(sc.name, 'bozo')
60
61 def test_create_invalid_name(self):
91910435 62 with self.assertRaisesRegex(TypeError, "'int' is not a 'str' object"):
3cdfbaea 63 self._tc.create_stream_class(name=17)
9cf643d1 64
3cdfbaea
SM
65 def test_create_packet_context_field_class(self):
66 fc = self._tc.create_structure_field_class()
cfbd7cf3
FD
67 sc = self._tc.create_stream_class(
68 packet_context_field_class=fc, supports_packets=True
69 )
3cdfbaea 70 self.assertEqual(sc.packet_context_field_class, fc)
f0a42b33
FD
71 self.assertIs(
72 type(sc.packet_context_field_class), bt2_field_class._StructureFieldClass
73 )
9cf643d1 74
3cdfbaea 75 def test_create_invalid_packet_context_field_class(self):
91910435
SM
76 with self.assertRaisesRegex(
77 TypeError,
78 "'int' is not a '<class 'bt2.field_class._StructureFieldClass'>' object",
79 ):
3cdfbaea 80 self._tc.create_stream_class(packet_context_field_class=22)
9cf643d1 81
26fc5aed
PP
82 def test_create_invalid_packet_context_field_class_no_packets(self):
83 fc = self._tc.create_structure_field_class()
84
91910435
SM
85 with self.assertRaisesRegex(
86 ValueError,
87 "cannot have a packet context field class without supporting packets",
88 ):
26fc5aed
PP
89 self._tc.create_stream_class(packet_context_field_class=fc)
90
3cdfbaea
SM
91 def test_create_event_common_context_field_class(self):
92 fc = self._tc.create_structure_field_class()
93 sc = self._tc.create_stream_class(event_common_context_field_class=fc)
94 self.assertEqual(sc.event_common_context_field_class, fc)
f0a42b33
FD
95 self.assertIs(
96 type(sc.event_common_context_field_class),
97 bt2_field_class._StructureFieldClass,
98 )
811644b8 99
3cdfbaea 100 def test_create_invalid_event_common_context_field_class(self):
91910435
SM
101 with self.assertRaisesRegex(
102 TypeError,
103 "'int' is not a '<class 'bt2.field_class._StructureFieldClass'>' object",
104 ):
3cdfbaea 105 self._tc.create_stream_class(event_common_context_field_class=22)
9cf643d1 106
3cdfbaea
SM
107 def test_create_default_clock_class(self):
108 sc = self._tc.create_stream_class(default_clock_class=self._cc)
109 self.assertEqual(sc.default_clock_class.addr, self._cc.addr)
f0a42b33 110 self.assertIs(type(sc.default_clock_class), bt2_clock_class._ClockClass)
9cf643d1 111
3cdfbaea 112 def test_create_invalid_default_clock_class(self):
91910435
SM
113 with self.assertRaisesRegex(
114 TypeError, "'int' is not a '<class 'bt2.clock_class._ClockClass'>' object"
115 ):
3cdfbaea 116 self._tc.create_stream_class(default_clock_class=12)
9cf643d1 117
5783664e
PP
118 def test_create_user_attributes(self):
119 sc = self._tc.create_stream_class(user_attributes={'salut': 23})
120 self.assertEqual(sc.user_attributes, {'salut': 23})
121
122 def test_create_invalid_user_attributes(self):
91910435
SM
123 with self.assertRaisesRegex(
124 TypeError, "cannot create value object from 'object' object"
125 ):
5783664e
PP
126 self._tc.create_stream_class(user_attributes=object())
127
128 def test_create_invalid_user_attributes_value_type(self):
91910435
SM
129 with self.assertRaisesRegex(
130 TypeError,
131 "'SignedIntegerValue' is not a '<class 'bt2.value.MapValue'>' object",
132 ):
5783664e
PP
133 self._tc.create_stream_class(user_attributes=23)
134
3cdfbaea
SM
135 def test_automatic_stream_ids(self):
136 sc = self._tc.create_stream_class(assigns_automatic_stream_id=True)
137 self.assertTrue(sc.assigns_automatic_stream_id)
9cf643d1 138
3cdfbaea
SM
139 stream = self._trace.create_stream(sc)
140 self.assertIsNotNone(stream.id)
9cf643d1 141
3cdfbaea
SM
142 def test_automatic_stream_ids_raises(self):
143 sc = self._tc.create_stream_class(assigns_automatic_stream_id=True)
144 self.assertTrue(sc.assigns_automatic_stream_id)
145
91910435
SM
146 with self.assertRaisesRegex(
147 ValueError, "id provided, but stream class assigns automatic stream ids"
148 ):
3cdfbaea
SM
149 self._trace.create_stream(sc, id=123)
150
151 def test_no_automatic_stream_ids(self):
152 sc = self._tc.create_stream_class(assigns_automatic_stream_id=False)
153 self.assertFalse(sc.assigns_automatic_stream_id)
154
155 stream = self._trace.create_stream(sc, id=333)
156 self.assertEqual(stream.id, 333)
157
158 def test_no_automatic_stream_ids_raises(self):
159 sc = self._tc.create_stream_class(assigns_automatic_stream_id=False)
160 self.assertFalse(sc.assigns_automatic_stream_id)
161
91910435
SM
162 with self.assertRaisesRegex(
163 ValueError,
164 "id not provided, but stream class does not assign automatic stream ids",
165 ):
3cdfbaea
SM
166 self._trace.create_stream(sc)
167
168 def test_automatic_event_class_ids(self):
169 sc = self._tc.create_stream_class(assigns_automatic_event_class_id=True)
170 self.assertTrue(sc.assigns_automatic_event_class_id)
171
172 ec = sc.create_event_class()
173 self.assertIsNotNone(ec.id)
174
175 def test_automatic_event_class_ids_raises(self):
176 sc = self._tc.create_stream_class(assigns_automatic_event_class_id=True)
177 self.assertTrue(sc.assigns_automatic_event_class_id)
178
91910435
SM
179 with self.assertRaisesRegex(
180 ValueError,
181 "id provided, but stream class assigns automatic event class ids",
182 ):
3cdfbaea
SM
183 sc.create_event_class(id=123)
184
185 def test_no_automatic_event_class_ids(self):
186 sc = self._tc.create_stream_class(assigns_automatic_event_class_id=False)
187 self.assertFalse(sc.assigns_automatic_event_class_id)
9cf643d1 188
3cdfbaea
SM
189 ec = sc.create_event_class(id=333)
190 self.assertEqual(ec.id, 333)
9cf643d1 191
3cdfbaea
SM
192 def test_no_automatic_event_class_ids_raises(self):
193 sc = self._tc.create_stream_class(assigns_automatic_event_class_id=False)
194 self.assertFalse(sc.assigns_automatic_event_class_id)
9cf643d1 195
91910435
SM
196 with self.assertRaisesRegex(
197 ValueError,
198 "id not provided, but stream class does not assign automatic event class ids",
199 ):
3cdfbaea
SM
200 sc.create_event_class()
201
26fc5aed 202 def test_supports_packets_without_cs(self):
cfbd7cf3
FD
203 sc = self._tc.create_stream_class(
204 default_clock_class=self._cc, supports_packets=True
205 )
26fc5aed
PP
206 self.assertTrue(sc.supports_packets)
207 self.assertFalse(sc.packets_have_beginning_default_clock_snapshot)
208 self.assertFalse(sc.packets_have_end_default_clock_snapshot)
209
210 def test_supports_packets_with_begin_cs(self):
cfbd7cf3
FD
211 sc = self._tc.create_stream_class(
212 default_clock_class=self._cc,
213 supports_packets=True,
214 packets_have_beginning_default_clock_snapshot=True,
215 )
26fc5aed 216 self.assertTrue(sc.supports_packets)
9b24b6aa 217 self.assertTrue(sc.packets_have_beginning_default_clock_snapshot)
26fc5aed 218 self.assertFalse(sc.packets_have_end_default_clock_snapshot)
3cdfbaea 219
26fc5aed 220 def test_supports_packets_with_end_cs(self):
cfbd7cf3
FD
221 sc = self._tc.create_stream_class(
222 default_clock_class=self._cc,
223 supports_packets=True,
224 packets_have_end_default_clock_snapshot=True,
225 )
26fc5aed
PP
226 self.assertTrue(sc.supports_packets)
227 self.assertFalse(sc.packets_have_beginning_default_clock_snapshot)
228 self.assertTrue(sc.packets_have_end_default_clock_snapshot)
229
230 def test_supports_packets_raises_type_error(self):
91910435 231 with self.assertRaisesRegex(TypeError, "'int' is not a 'bool' object"):
082db648 232 self._tc.create_stream_class(
cfbd7cf3
FD
233 default_clock_class=self._cc, supports_packets=23
234 )
3cdfbaea 235
26fc5aed 236 def test_packets_have_begin_default_cs_raises_type_error(self):
91910435 237 with self.assertRaisesRegex(TypeError, "'int' is not a 'bool' object"):
082db648 238 self._tc.create_stream_class(
cfbd7cf3
FD
239 default_clock_class=self._cc,
240 packets_have_beginning_default_clock_snapshot=23,
241 )
3cdfbaea 242
26fc5aed 243 def test_packets_have_end_default_cs_raises_type_error(self):
91910435 244 with self.assertRaisesRegex(TypeError, "'int' is not a 'bool' object"):
082db648 245 self._tc.create_stream_class(
cfbd7cf3
FD
246 default_clock_class=self._cc, packets_have_end_default_clock_snapshot=23
247 )
26fc5aed
PP
248
249 def test_does_not_support_packets_raises_with_begin_cs(self):
91910435
SM
250 with self.assertRaisesRegex(
251 ValueError,
252 "cannot not support packets, but have packet beginning default clock snapshot",
253 ):
082db648 254 self._tc.create_stream_class(
cfbd7cf3
FD
255 default_clock_class=self._cc,
256 packets_have_beginning_default_clock_snapshot=True,
257 )
26fc5aed
PP
258
259 def test_does_not_support_packets_raises_with_end_cs(self):
91910435
SM
260 with self.assertRaisesRegex(
261 ValueError,
262 "cannot not support packets, but have packet end default clock snapshots",
263 ):
082db648 264 self._tc.create_stream_class(
cfbd7cf3
FD
265 default_clock_class=self._cc,
266 packets_have_end_default_clock_snapshot=True,
267 )
3cdfbaea 268
2e90378a 269 def test_supports_discarded_events_without_cs(self):
cfbd7cf3
FD
270 sc = self._tc.create_stream_class(
271 default_clock_class=self._cc, supports_discarded_events=True
272 )
2e90378a
PP
273 self.assertTrue(sc.supports_discarded_events)
274 self.assertFalse(sc.discarded_events_have_default_clock_snapshots)
275
276 def test_supports_discarded_events_with_cs(self):
cfbd7cf3
FD
277 sc = self._tc.create_stream_class(
278 default_clock_class=self._cc,
279 supports_discarded_events=True,
280 discarded_events_have_default_clock_snapshots=True,
281 )
2e90378a
PP
282 self.assertTrue(sc.supports_discarded_events)
283 self.assertTrue(sc.discarded_events_have_default_clock_snapshots)
284
285 def test_supports_discarded_events_raises_type_error(self):
91910435 286 with self.assertRaisesRegex(TypeError, "'int' is not a 'bool' object"):
082db648 287 self._tc.create_stream_class(
cfbd7cf3
FD
288 default_clock_class=self._cc, supports_discarded_events=23
289 )
2e90378a
PP
290
291 def test_discarded_events_have_default_cs_raises_type_error(self):
91910435 292 with self.assertRaisesRegex(TypeError, "'int' is not a 'bool' object"):
082db648 293 self._tc.create_stream_class(
cfbd7cf3
FD
294 default_clock_class=self._cc,
295 discarded_events_have_default_clock_snapshots=23,
296 )
2e90378a
PP
297
298 def test_does_not_support_discarded_events_raises_with_cs(self):
91910435
SM
299 with self.assertRaisesRegex(
300 ValueError,
301 "cannot not support discarded events, but have default clock snapshots for discarded event messages",
302 ):
082db648 303 self._tc.create_stream_class(
cfbd7cf3
FD
304 default_clock_class=self._cc,
305 discarded_events_have_default_clock_snapshots=True,
306 )
2e90378a
PP
307
308 def test_supports_discarded_packets_without_cs(self):
cfbd7cf3
FD
309 sc = self._tc.create_stream_class(
310 default_clock_class=self._cc,
311 supports_discarded_packets=True,
312 supports_packets=True,
313 )
2e90378a
PP
314 self.assertTrue(sc.supports_discarded_packets)
315 self.assertFalse(sc.discarded_packets_have_default_clock_snapshots)
316
317 def test_supports_discarded_packets_with_cs(self):
cfbd7cf3
FD
318 sc = self._tc.create_stream_class(
319 default_clock_class=self._cc,
320 supports_discarded_packets=True,
321 discarded_packets_have_default_clock_snapshots=True,
322 supports_packets=True,
323 )
2e90378a
PP
324 self.assertTrue(sc.supports_discarded_packets)
325 self.assertTrue(sc.discarded_packets_have_default_clock_snapshots)
326
26fc5aed 327 def test_supports_discarded_packets_raises_without_packet_support(self):
91910435
SM
328 with self.assertRaisesRegex(
329 ValueError, "cannot support discarded packets, but not support packets"
330 ):
082db648 331 self._tc.create_stream_class(
cfbd7cf3
FD
332 default_clock_class=self._cc, supports_discarded_packets=True
333 )
26fc5aed 334
2e90378a 335 def test_supports_discarded_packets_raises_type_error(self):
91910435 336 with self.assertRaisesRegex(TypeError, "'int' is not a 'bool' object"):
082db648 337 self._tc.create_stream_class(
cfbd7cf3
FD
338 default_clock_class=self._cc,
339 supports_discarded_packets=23,
340 supports_packets=True,
341 )
2e90378a
PP
342
343 def test_discarded_packets_have_default_cs_raises_type_error(self):
91910435 344 with self.assertRaisesRegex(TypeError, "'int' is not a 'bool' object"):
082db648 345 self._tc.create_stream_class(
cfbd7cf3
FD
346 default_clock_class=self._cc,
347 discarded_packets_have_default_clock_snapshots=23,
348 supports_packets=True,
349 )
2e90378a
PP
350
351 def test_does_not_support_discarded_packets_raises_with_cs(self):
91910435
SM
352 with self.assertRaisesRegex(
353 ValueError,
354 "cannot not support discarded packets, but have default clock snapshots for discarded packet messages",
355 ):
082db648 356 self._tc.create_stream_class(
cfbd7cf3
FD
357 default_clock_class=self._cc,
358 discarded_packets_have_default_clock_snapshots=True,
359 supports_packets=True,
360 )
2e90378a 361
3cdfbaea
SM
362 def test_trace_class(self):
363 sc = self._tc.create_stream_class()
364 self.assertEqual(sc.trace_class.addr, self._tc.addr)
f0a42b33 365 self.assertIs(type(sc.trace_class), bt2_trace_class._TraceClass)
3cdfbaea
SM
366
367 def _create_stream_class_with_event_classes(self):
368 sc = self._tc.create_stream_class(assigns_automatic_event_class_id=False)
369 ec1 = sc.create_event_class(id=23)
370 ec2 = sc.create_event_class(id=17)
371 return sc, ec1, ec2
9cf643d1
PP
372
373 def test_getitem(self):
3cdfbaea
SM
374 sc, ec1, ec2 = self._create_stream_class_with_event_classes()
375
376 self.assertEqual(sc[23].addr, ec1.addr)
f0a42b33 377 self.assertEqual(type(sc[23]), bt2_event_class._EventClass)
3cdfbaea 378 self.assertEqual(sc[17].addr, ec2.addr)
f0a42b33 379 self.assertEqual(type(sc[17]), bt2_event_class._EventClass)
9cf643d1
PP
380
381 def test_getitem_wrong_key_type(self):
3cdfbaea
SM
382 sc, _, _ = self._create_stream_class_with_event_classes()
383
91910435 384 with self.assertRaisesRegex(TypeError, "'str' is not an 'int' object"):
3cdfbaea 385 sc['event23']
9cf643d1
PP
386
387 def test_getitem_wrong_key(self):
3cdfbaea
SM
388 sc, _, _ = self._create_stream_class_with_event_classes()
389
91910435 390 with self.assertRaisesRegex(KeyError, '19'):
3cdfbaea 391 sc[19]
9cf643d1
PP
392
393 def test_len(self):
3cdfbaea
SM
394 sc, _, _ = self._create_stream_class_with_event_classes()
395
396 self.assertEqual(len(sc), 2)
9cf643d1
PP
397
398 def test_iter(self):
3cdfbaea
SM
399 sc, _, _ = self._create_stream_class_with_event_classes()
400
401 ec_ids = sorted(sc)
402 self.assertEqual(ec_ids, [17, 23])
d14ddbba
SM
403
404
405if __name__ == '__main__':
406 unittest.main()
This page took 0.069982 seconds and 4 git commands to generate.