tests: use assertRaisesRegex instead of assertRaises in test_stream_class.py
[babeltrace.git] / tests / bindings / python / bt2 / test_stream_class.py
CommitLineData
32d2d479
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
060aee52 20from utils import run_in_component_init
9cbe0c59
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):
060aee52
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
9cbe0c59 41 self.assertIs(type(sc), bt2_stream_class._StreamClass)
060aee52
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)
37a93d41 48 self.assertFalse(sc.supports_packets)
5ef34326
PP
49 self.assertFalse(sc.packets_have_beginning_default_clock_snapshot)
50 self.assertFalse(sc.packets_have_end_default_clock_snapshot)
77037b2b
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)
b2df5780 55 self.assertEqual(len(sc.user_attributes), 0)
060aee52
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):
d5a22ce8 62 with self.assertRaisesRegex(TypeError, "'int' is not a 'str' object"):
060aee52 63 self._tc.create_stream_class(name=17)
9cf643d1 64
060aee52
SM
65 def test_create_packet_context_field_class(self):
66 fc = self._tc.create_structure_field_class()
61d96b89
FD
67 sc = self._tc.create_stream_class(
68 packet_context_field_class=fc, supports_packets=True
69 )
060aee52 70 self.assertEqual(sc.packet_context_field_class, fc)
9cbe0c59
FD
71 self.assertIs(
72 type(sc.packet_context_field_class), bt2_field_class._StructureFieldClass
73 )
9cf643d1 74
060aee52 75 def test_create_invalid_packet_context_field_class(self):
d5a22ce8
SM
76 with self.assertRaisesRegex(
77 TypeError,
78 "'int' is not a '<class 'bt2.field_class._StructureFieldClass'>' object",
79 ):
060aee52 80 self._tc.create_stream_class(packet_context_field_class=22)
9cf643d1 81
37a93d41
PP
82 def test_create_invalid_packet_context_field_class_no_packets(self):
83 fc = self._tc.create_structure_field_class()
84
d5a22ce8
SM
85 with self.assertRaisesRegex(
86 ValueError,
87 "cannot have a packet context field class without supporting packets",
88 ):
37a93d41
PP
89 self._tc.create_stream_class(packet_context_field_class=fc)
90
060aee52
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)
9cbe0c59
FD
95 self.assertIs(
96 type(sc.event_common_context_field_class),
97 bt2_field_class._StructureFieldClass,
98 )
f6a5e476 99
060aee52 100 def test_create_invalid_event_common_context_field_class(self):
d5a22ce8
SM
101 with self.assertRaisesRegex(
102 TypeError,
103 "'int' is not a '<class 'bt2.field_class._StructureFieldClass'>' object",
104 ):
060aee52 105 self._tc.create_stream_class(event_common_context_field_class=22)
9cf643d1 106
060aee52
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)
9cbe0c59 110 self.assertIs(type(sc.default_clock_class), bt2_clock_class._ClockClass)
9cf643d1 111
060aee52 112 def test_create_invalid_default_clock_class(self):
d5a22ce8
SM
113 with self.assertRaisesRegex(
114 TypeError, "'int' is not a '<class 'bt2.clock_class._ClockClass'>' object"
115 ):
060aee52 116 self._tc.create_stream_class(default_clock_class=12)
9cf643d1 117
b2df5780
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):
d5a22ce8
SM
123 with self.assertRaisesRegex(
124 TypeError, "cannot create value object from 'object' object"
125 ):
b2df5780
PP
126 self._tc.create_stream_class(user_attributes=object())
127
128 def test_create_invalid_user_attributes_value_type(self):
d5a22ce8
SM
129 with self.assertRaisesRegex(
130 TypeError,
131 "'SignedIntegerValue' is not a '<class 'bt2.value.MapValue'>' object",
132 ):
b2df5780
PP
133 self._tc.create_stream_class(user_attributes=23)
134
060aee52
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
060aee52
SM
139 stream = self._trace.create_stream(sc)
140 self.assertIsNotNone(stream.id)
9cf643d1 141
060aee52
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
d5a22ce8
SM
146 with self.assertRaisesRegex(
147 ValueError, "id provided, but stream class assigns automatic stream ids"
148 ):
060aee52
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
d5a22ce8
SM
162 with self.assertRaisesRegex(
163 ValueError,
164 "id not provided, but stream class does not assign automatic stream ids",
165 ):
060aee52
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
d5a22ce8
SM
179 with self.assertRaisesRegex(
180 ValueError,
181 "id provided, but stream class assigns automatic event class ids",
182 ):
060aee52
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
060aee52
SM
189 ec = sc.create_event_class(id=333)
190 self.assertEqual(ec.id, 333)
9cf643d1 191
060aee52
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
d5a22ce8
SM
196 with self.assertRaisesRegex(
197 ValueError,
198 "id not provided, but stream class does not assign automatic event class ids",
199 ):
060aee52
SM
200 sc.create_event_class()
201
37a93d41 202 def test_supports_packets_without_cs(self):
61d96b89
FD
203 sc = self._tc.create_stream_class(
204 default_clock_class=self._cc, supports_packets=True
205 )
37a93d41
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):
61d96b89
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 )
37a93d41 216 self.assertTrue(sc.supports_packets)
5ef34326 217 self.assertTrue(sc.packets_have_beginning_default_clock_snapshot)
37a93d41 218 self.assertFalse(sc.packets_have_end_default_clock_snapshot)
060aee52 219
37a93d41 220 def test_supports_packets_with_end_cs(self):
61d96b89
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 )
37a93d41
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):
d5a22ce8 231 with self.assertRaisesRegex(TypeError, "'int' is not a 'bool' object"):
36153ada 232 self._tc.create_stream_class(
61d96b89
FD
233 default_clock_class=self._cc, supports_packets=23
234 )
060aee52 235
37a93d41 236 def test_packets_have_begin_default_cs_raises_type_error(self):
d5a22ce8 237 with self.assertRaisesRegex(TypeError, "'int' is not a 'bool' object"):
36153ada 238 self._tc.create_stream_class(
61d96b89
FD
239 default_clock_class=self._cc,
240 packets_have_beginning_default_clock_snapshot=23,
241 )
060aee52 242
37a93d41 243 def test_packets_have_end_default_cs_raises_type_error(self):
d5a22ce8 244 with self.assertRaisesRegex(TypeError, "'int' is not a 'bool' object"):
36153ada 245 self._tc.create_stream_class(
61d96b89
FD
246 default_clock_class=self._cc, packets_have_end_default_clock_snapshot=23
247 )
37a93d41
PP
248
249 def test_does_not_support_packets_raises_with_begin_cs(self):
d5a22ce8
SM
250 with self.assertRaisesRegex(
251 ValueError,
252 "cannot not support packets, but have packet beginning default clock snapshot",
253 ):
36153ada 254 self._tc.create_stream_class(
61d96b89
FD
255 default_clock_class=self._cc,
256 packets_have_beginning_default_clock_snapshot=True,
257 )
37a93d41
PP
258
259 def test_does_not_support_packets_raises_with_end_cs(self):
d5a22ce8
SM
260 with self.assertRaisesRegex(
261 ValueError,
262 "cannot not support packets, but have packet end default clock snapshots",
263 ):
36153ada 264 self._tc.create_stream_class(
61d96b89
FD
265 default_clock_class=self._cc,
266 packets_have_end_default_clock_snapshot=True,
267 )
060aee52 268
77037b2b 269 def test_supports_discarded_events_without_cs(self):
61d96b89
FD
270 sc = self._tc.create_stream_class(
271 default_clock_class=self._cc, supports_discarded_events=True
272 )
77037b2b
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):
61d96b89
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 )
77037b2b
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):
d5a22ce8 286 with self.assertRaisesRegex(TypeError, "'int' is not a 'bool' object"):
36153ada 287 self._tc.create_stream_class(
61d96b89
FD
288 default_clock_class=self._cc, supports_discarded_events=23
289 )
77037b2b
PP
290
291 def test_discarded_events_have_default_cs_raises_type_error(self):
d5a22ce8 292 with self.assertRaisesRegex(TypeError, "'int' is not a 'bool' object"):
36153ada 293 self._tc.create_stream_class(
61d96b89
FD
294 default_clock_class=self._cc,
295 discarded_events_have_default_clock_snapshots=23,
296 )
77037b2b
PP
297
298 def test_does_not_support_discarded_events_raises_with_cs(self):
d5a22ce8
SM
299 with self.assertRaisesRegex(
300 ValueError,
301 "cannot not support discarded events, but have default clock snapshots for discarded event messages",
302 ):
36153ada 303 self._tc.create_stream_class(
61d96b89
FD
304 default_clock_class=self._cc,
305 discarded_events_have_default_clock_snapshots=True,
306 )
77037b2b
PP
307
308 def test_supports_discarded_packets_without_cs(self):
61d96b89
FD
309 sc = self._tc.create_stream_class(
310 default_clock_class=self._cc,
311 supports_discarded_packets=True,
312 supports_packets=True,
313 )
77037b2b
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):
61d96b89
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 )
77037b2b
PP
324 self.assertTrue(sc.supports_discarded_packets)
325 self.assertTrue(sc.discarded_packets_have_default_clock_snapshots)
326
37a93d41 327 def test_supports_discarded_packets_raises_without_packet_support(self):
d5a22ce8
SM
328 with self.assertRaisesRegex(
329 ValueError, "cannot support discarded packets, but not support packets"
330 ):
36153ada 331 self._tc.create_stream_class(
61d96b89
FD
332 default_clock_class=self._cc, supports_discarded_packets=True
333 )
37a93d41 334
77037b2b 335 def test_supports_discarded_packets_raises_type_error(self):
d5a22ce8 336 with self.assertRaisesRegex(TypeError, "'int' is not a 'bool' object"):
36153ada 337 self._tc.create_stream_class(
61d96b89
FD
338 default_clock_class=self._cc,
339 supports_discarded_packets=23,
340 supports_packets=True,
341 )
77037b2b
PP
342
343 def test_discarded_packets_have_default_cs_raises_type_error(self):
d5a22ce8 344 with self.assertRaisesRegex(TypeError, "'int' is not a 'bool' object"):
36153ada 345 self._tc.create_stream_class(
61d96b89
FD
346 default_clock_class=self._cc,
347 discarded_packets_have_default_clock_snapshots=23,
348 supports_packets=True,
349 )
77037b2b
PP
350
351 def test_does_not_support_discarded_packets_raises_with_cs(self):
d5a22ce8
SM
352 with self.assertRaisesRegex(
353 ValueError,
354 "cannot not support discarded packets, but have default clock snapshots for discarded packet messages",
355 ):
36153ada 356 self._tc.create_stream_class(
61d96b89
FD
357 default_clock_class=self._cc,
358 discarded_packets_have_default_clock_snapshots=True,
359 supports_packets=True,
360 )
77037b2b 361
060aee52
SM
362 def test_trace_class(self):
363 sc = self._tc.create_stream_class()
364 self.assertEqual(sc.trace_class.addr, self._tc.addr)
9cbe0c59 365 self.assertIs(type(sc.trace_class), bt2_trace_class._TraceClass)
060aee52
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):
060aee52
SM
374 sc, ec1, ec2 = self._create_stream_class_with_event_classes()
375
376 self.assertEqual(sc[23].addr, ec1.addr)
9cbe0c59 377 self.assertEqual(type(sc[23]), bt2_event_class._EventClass)
060aee52 378 self.assertEqual(sc[17].addr, ec2.addr)
9cbe0c59 379 self.assertEqual(type(sc[17]), bt2_event_class._EventClass)
9cf643d1
PP
380
381 def test_getitem_wrong_key_type(self):
060aee52
SM
382 sc, _, _ = self._create_stream_class_with_event_classes()
383
d5a22ce8 384 with self.assertRaisesRegex(TypeError, "'str' is not an 'int' object"):
060aee52 385 sc['event23']
9cf643d1
PP
386
387 def test_getitem_wrong_key(self):
060aee52
SM
388 sc, _, _ = self._create_stream_class_with_event_classes()
389
d5a22ce8 390 with self.assertRaisesRegex(KeyError, '19'):
060aee52 391 sc[19]
9cf643d1
PP
392
393 def test_len(self):
060aee52
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):
060aee52
SM
399 sc, _, _ = self._create_stream_class_with_event_classes()
400
401 ec_ids = sorted(sc)
402 self.assertEqual(ec_ids, [17, 23])
3db06b1d
SM
403
404
405if __name__ == '__main__':
406 unittest.main()
This page took 0.068402 seconds and 4 git commands to generate.