bt2: add user attributes property support
[babeltrace.git] / tests / bindings / python / bt2 / test_stream_class.py
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
19 import unittest
20 from utils import run_in_component_init
21
22
23 class StreamClassTestCase(unittest.TestCase):
24 def setUp(self):
25 def f(comp_self):
26 tc = comp_self._create_trace_class(assigns_automatic_stream_class_id=True)
27 cc = comp_self._create_clock_class()
28 return tc, cc
29
30 self._tc, self._cc = run_in_component_init(f)
31 self._trace = self._tc()
32
33 def test_create_default(self):
34 sc = self._tc.create_stream_class()
35
36 self.assertIsNone(sc.name)
37 self.assertIsNone(sc.packet_context_field_class)
38 self.assertIsNone(sc.event_common_context_field_class)
39 self.assertIsNone(sc.default_clock_class)
40 self.assertTrue(sc.assigns_automatic_event_class_id)
41 self.assertTrue(sc.assigns_automatic_stream_id)
42 self.assertFalse(sc.supports_packets)
43 self.assertFalse(sc.packets_have_beginning_default_clock_snapshot)
44 self.assertFalse(sc.packets_have_end_default_clock_snapshot)
45 self.assertFalse(sc.supports_discarded_events)
46 self.assertFalse(sc.discarded_events_have_default_clock_snapshots)
47 self.assertFalse(sc.supports_discarded_packets)
48 self.assertFalse(sc.discarded_packets_have_default_clock_snapshots)
49 self.assertEqual(len(sc.user_attributes), 0)
50
51 def test_create_name(self):
52 sc = self._tc.create_stream_class(name='bozo')
53 self.assertEqual(sc.name, 'bozo')
54
55 def test_create_invalid_name(self):
56 with self.assertRaises(TypeError):
57 self._tc.create_stream_class(name=17)
58
59 def test_create_packet_context_field_class(self):
60 fc = self._tc.create_structure_field_class()
61 sc = self._tc.create_stream_class(
62 packet_context_field_class=fc, supports_packets=True
63 )
64 self.assertEqual(sc.packet_context_field_class, fc)
65
66 def test_create_invalid_packet_context_field_class(self):
67 with self.assertRaises(TypeError):
68 self._tc.create_stream_class(packet_context_field_class=22)
69
70 def test_create_invalid_packet_context_field_class_no_packets(self):
71 fc = self._tc.create_structure_field_class()
72
73 with self.assertRaises(ValueError):
74 self._tc.create_stream_class(packet_context_field_class=fc)
75
76 def test_create_event_common_context_field_class(self):
77 fc = self._tc.create_structure_field_class()
78 sc = self._tc.create_stream_class(event_common_context_field_class=fc)
79 self.assertEqual(sc.event_common_context_field_class, fc)
80
81 def test_create_invalid_event_common_context_field_class(self):
82 with self.assertRaises(TypeError):
83 self._tc.create_stream_class(event_common_context_field_class=22)
84
85 def test_create_default_clock_class(self):
86 sc = self._tc.create_stream_class(default_clock_class=self._cc)
87 self.assertEqual(sc.default_clock_class.addr, self._cc.addr)
88
89 def test_create_invalid_default_clock_class(self):
90 with self.assertRaises(TypeError):
91 self._tc.create_stream_class(default_clock_class=12)
92
93 def test_create_user_attributes(self):
94 sc = self._tc.create_stream_class(user_attributes={'salut': 23})
95 self.assertEqual(sc.user_attributes, {'salut': 23})
96
97 def test_create_invalid_user_attributes(self):
98 with self.assertRaises(TypeError):
99 self._tc.create_stream_class(user_attributes=object())
100
101 def test_create_invalid_user_attributes_value_type(self):
102 with self.assertRaises(TypeError):
103 self._tc.create_stream_class(user_attributes=23)
104
105 def test_automatic_stream_ids(self):
106 sc = self._tc.create_stream_class(assigns_automatic_stream_id=True)
107 self.assertTrue(sc.assigns_automatic_stream_id)
108
109 stream = self._trace.create_stream(sc)
110 self.assertIsNotNone(stream.id)
111
112 def test_automatic_stream_ids_raises(self):
113 sc = self._tc.create_stream_class(assigns_automatic_stream_id=True)
114 self.assertTrue(sc.assigns_automatic_stream_id)
115
116 with self.assertRaises(ValueError):
117 self._trace.create_stream(sc, id=123)
118
119 def test_no_automatic_stream_ids(self):
120 sc = self._tc.create_stream_class(assigns_automatic_stream_id=False)
121 self.assertFalse(sc.assigns_automatic_stream_id)
122
123 stream = self._trace.create_stream(sc, id=333)
124 self.assertEqual(stream.id, 333)
125
126 def test_no_automatic_stream_ids_raises(self):
127 sc = self._tc.create_stream_class(assigns_automatic_stream_id=False)
128 self.assertFalse(sc.assigns_automatic_stream_id)
129
130 with self.assertRaises(ValueError):
131 self._trace.create_stream(sc)
132
133 def test_automatic_event_class_ids(self):
134 sc = self._tc.create_stream_class(assigns_automatic_event_class_id=True)
135 self.assertTrue(sc.assigns_automatic_event_class_id)
136
137 ec = sc.create_event_class()
138 self.assertIsNotNone(ec.id)
139
140 def test_automatic_event_class_ids_raises(self):
141 sc = self._tc.create_stream_class(assigns_automatic_event_class_id=True)
142 self.assertTrue(sc.assigns_automatic_event_class_id)
143
144 with self.assertRaises(ValueError):
145 sc.create_event_class(id=123)
146
147 def test_no_automatic_event_class_ids(self):
148 sc = self._tc.create_stream_class(assigns_automatic_event_class_id=False)
149 self.assertFalse(sc.assigns_automatic_event_class_id)
150
151 ec = sc.create_event_class(id=333)
152 self.assertEqual(ec.id, 333)
153
154 def test_no_automatic_event_class_ids_raises(self):
155 sc = self._tc.create_stream_class(assigns_automatic_event_class_id=False)
156 self.assertFalse(sc.assigns_automatic_event_class_id)
157
158 with self.assertRaises(ValueError):
159 sc.create_event_class()
160
161 def test_supports_packets_without_cs(self):
162 sc = self._tc.create_stream_class(
163 default_clock_class=self._cc, supports_packets=True
164 )
165 self.assertTrue(sc.supports_packets)
166 self.assertFalse(sc.packets_have_beginning_default_clock_snapshot)
167 self.assertFalse(sc.packets_have_end_default_clock_snapshot)
168
169 def test_supports_packets_with_begin_cs(self):
170 sc = self._tc.create_stream_class(
171 default_clock_class=self._cc,
172 supports_packets=True,
173 packets_have_beginning_default_clock_snapshot=True,
174 )
175 self.assertTrue(sc.supports_packets)
176 self.assertTrue(sc.packets_have_beginning_default_clock_snapshot)
177 self.assertFalse(sc.packets_have_end_default_clock_snapshot)
178
179 def test_supports_packets_with_end_cs(self):
180 sc = self._tc.create_stream_class(
181 default_clock_class=self._cc,
182 supports_packets=True,
183 packets_have_end_default_clock_snapshot=True,
184 )
185 self.assertTrue(sc.supports_packets)
186 self.assertFalse(sc.packets_have_beginning_default_clock_snapshot)
187 self.assertTrue(sc.packets_have_end_default_clock_snapshot)
188
189 def test_supports_packets_raises_type_error(self):
190 with self.assertRaises(TypeError):
191 sc = self._tc.create_stream_class(
192 default_clock_class=self._cc, supports_packets=23
193 )
194
195 def test_packets_have_begin_default_cs_raises_type_error(self):
196 with self.assertRaises(TypeError):
197 sc = self._tc.create_stream_class(
198 default_clock_class=self._cc,
199 packets_have_beginning_default_clock_snapshot=23,
200 )
201
202 def test_packets_have_end_default_cs_raises_type_error(self):
203 with self.assertRaises(TypeError):
204 sc = self._tc.create_stream_class(
205 default_clock_class=self._cc, packets_have_end_default_clock_snapshot=23
206 )
207
208 def test_does_not_support_packets_raises_with_begin_cs(self):
209 with self.assertRaises(ValueError):
210 sc = self._tc.create_stream_class(
211 default_clock_class=self._cc,
212 packets_have_beginning_default_clock_snapshot=True,
213 )
214
215 def test_does_not_support_packets_raises_with_end_cs(self):
216 with self.assertRaises(ValueError):
217 sc = self._tc.create_stream_class(
218 default_clock_class=self._cc,
219 packets_have_end_default_clock_snapshot=True,
220 )
221
222 def test_supports_discarded_events_without_cs(self):
223 sc = self._tc.create_stream_class(
224 default_clock_class=self._cc, supports_discarded_events=True
225 )
226 self.assertTrue(sc.supports_discarded_events)
227 self.assertFalse(sc.discarded_events_have_default_clock_snapshots)
228
229 def test_supports_discarded_events_with_cs(self):
230 sc = self._tc.create_stream_class(
231 default_clock_class=self._cc,
232 supports_discarded_events=True,
233 discarded_events_have_default_clock_snapshots=True,
234 )
235 self.assertTrue(sc.supports_discarded_events)
236 self.assertTrue(sc.discarded_events_have_default_clock_snapshots)
237
238 def test_supports_discarded_events_raises_type_error(self):
239 with self.assertRaises(TypeError):
240 sc = self._tc.create_stream_class(
241 default_clock_class=self._cc, supports_discarded_events=23
242 )
243
244 def test_discarded_events_have_default_cs_raises_type_error(self):
245 with self.assertRaises(TypeError):
246 sc = self._tc.create_stream_class(
247 default_clock_class=self._cc,
248 discarded_events_have_default_clock_snapshots=23,
249 )
250
251 def test_does_not_support_discarded_events_raises_with_cs(self):
252 with self.assertRaises(ValueError):
253 sc = self._tc.create_stream_class(
254 default_clock_class=self._cc,
255 discarded_events_have_default_clock_snapshots=True,
256 )
257
258 def test_supports_discarded_packets_without_cs(self):
259 sc = self._tc.create_stream_class(
260 default_clock_class=self._cc,
261 supports_discarded_packets=True,
262 supports_packets=True,
263 )
264 self.assertTrue(sc.supports_discarded_packets)
265 self.assertFalse(sc.discarded_packets_have_default_clock_snapshots)
266
267 def test_supports_discarded_packets_with_cs(self):
268 sc = self._tc.create_stream_class(
269 default_clock_class=self._cc,
270 supports_discarded_packets=True,
271 discarded_packets_have_default_clock_snapshots=True,
272 supports_packets=True,
273 )
274 self.assertTrue(sc.supports_discarded_packets)
275 self.assertTrue(sc.discarded_packets_have_default_clock_snapshots)
276
277 def test_supports_discarded_packets_raises_without_packet_support(self):
278 with self.assertRaises(ValueError):
279 sc = self._tc.create_stream_class(
280 default_clock_class=self._cc, supports_discarded_packets=True
281 )
282
283 def test_supports_discarded_packets_raises_type_error(self):
284 with self.assertRaises(TypeError):
285 sc = self._tc.create_stream_class(
286 default_clock_class=self._cc,
287 supports_discarded_packets=23,
288 supports_packets=True,
289 )
290
291 def test_discarded_packets_have_default_cs_raises_type_error(self):
292 with self.assertRaises(TypeError):
293 sc = self._tc.create_stream_class(
294 default_clock_class=self._cc,
295 discarded_packets_have_default_clock_snapshots=23,
296 supports_packets=True,
297 )
298
299 def test_does_not_support_discarded_packets_raises_with_cs(self):
300 with self.assertRaises(ValueError):
301 sc = self._tc.create_stream_class(
302 default_clock_class=self._cc,
303 discarded_packets_have_default_clock_snapshots=True,
304 supports_packets=True,
305 )
306
307 def test_trace_class(self):
308 sc = self._tc.create_stream_class()
309 self.assertEqual(sc.trace_class.addr, self._tc.addr)
310
311 def _create_stream_class_with_event_classes(self):
312 sc = self._tc.create_stream_class(assigns_automatic_event_class_id=False)
313 ec1 = sc.create_event_class(id=23)
314 ec2 = sc.create_event_class(id=17)
315 return sc, ec1, ec2
316
317 def test_getitem(self):
318 sc, ec1, ec2 = self._create_stream_class_with_event_classes()
319
320 self.assertEqual(sc[23].addr, ec1.addr)
321 self.assertEqual(sc[17].addr, ec2.addr)
322
323 def test_getitem_wrong_key_type(self):
324 sc, _, _ = self._create_stream_class_with_event_classes()
325
326 with self.assertRaises(TypeError):
327 sc['event23']
328
329 def test_getitem_wrong_key(self):
330 sc, _, _ = self._create_stream_class_with_event_classes()
331
332 with self.assertRaises(KeyError):
333 sc[19]
334
335 def test_len(self):
336 sc, _, _ = self._create_stream_class_with_event_classes()
337
338 self.assertEqual(len(sc), 2)
339
340 def test_iter(self):
341 sc, _, _ = self._create_stream_class_with_event_classes()
342
343 ec_ids = sorted(sc)
344 self.assertEqual(ec_ids, [17, 23])
This page took 0.036745 seconds and 5 git commands to generate.