bt2: Add remaining trace-ir `*Const` classes and adapt tests
[babeltrace.git] / tests / bindings / python / bt2 / test_event_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 import bt2
21 from utils import get_default_trace_class
22 from bt2 import stream_class as bt2_stream_class
23 from bt2 import event_class as bt2_event_class
24 from bt2 import field_class as bt2_field_class
25 from bt2 import value as bt2_value
26 from utils import TestOutputPortMessageIterator
27
28
29 def _create_const_event_class(tc, stream_class):
30 fc1 = tc.create_structure_field_class()
31 fc2 = tc.create_structure_field_class()
32 event_class = stream_class.create_event_class(
33 payload_field_class=fc1, specific_context_field_class=fc2
34 )
35
36 class MyIter(bt2._UserMessageIterator):
37 def __init__(self, self_port_output):
38
39 trace = tc()
40 stream = trace.create_stream(stream_class)
41 self._msgs = [
42 self._create_stream_beginning_message(stream),
43 self._create_event_message(event_class, stream),
44 ]
45
46 def __next__(self):
47 if len(self._msgs) == 0:
48 raise StopIteration
49
50 return self._msgs.pop(0)
51
52 class MySrc(bt2._UserSourceComponent, message_iterator_class=MyIter):
53 def __init__(self, params, obj):
54 self._add_output_port('out', params)
55
56 graph = bt2.Graph()
57 src_comp = graph.add_component(MySrc, 'my_source', None)
58 msg_iter = TestOutputPortMessageIterator(graph, src_comp.output_ports['out'])
59
60 # Ignore first message, stream beginning
61 _ = next(msg_iter)
62
63 event_msg = next(msg_iter)
64
65 return event_msg.event.cls
66
67
68 class EventClassTestCase(unittest.TestCase):
69 def setUp(self):
70 self._tc = get_default_trace_class()
71
72 self._context_fc = self._tc.create_structure_field_class()
73 self._context_fc.append_member('allo', self._tc.create_string_field_class())
74 self._context_fc.append_member(
75 'zola', self._tc.create_signed_integer_field_class(18)
76 )
77
78 self._payload_fc = self._tc.create_structure_field_class()
79 self._payload_fc.append_member('zoom', self._tc.create_string_field_class())
80
81 self._stream_class = self._tc.create_stream_class(
82 assigns_automatic_event_class_id=True
83 )
84
85 def test_create_default(self):
86 ec = self._stream_class.create_event_class()
87
88 self.assertIs(type(ec), bt2_event_class._EventClass)
89 self.assertIsNone(ec.name, 'my_event')
90 self.assertTrue(type(ec.id), int)
91 self.assertIsNone(ec.specific_context_field_class)
92 self.assertIsNone(ec.payload_field_class)
93 self.assertIsNone(ec.emf_uri)
94 self.assertIsNone(ec.log_level)
95 self.assertEqual(len(ec.user_attributes), 0)
96
97 def test_create_invalid_id(self):
98 sc = self._tc.create_stream_class(assigns_automatic_event_class_id=False)
99 with self.assertRaises(TypeError):
100 sc.create_event_class(id='lel')
101
102 def test_create_specific_context_field_class(self):
103 fc = self._tc.create_structure_field_class()
104 ec = self._stream_class.create_event_class(specific_context_field_class=fc)
105 self.assertEqual(ec.specific_context_field_class.addr, fc.addr)
106 self.assertIs(
107 type(ec.specific_context_field_class), bt2_field_class._StructureFieldClass
108 )
109
110 def test_const_create_specific_context_field_class(self):
111 ec_const = _create_const_event_class(self._tc, self._stream_class)
112 self.assertIs(
113 type(ec_const.specific_context_field_class),
114 bt2_field_class._StructureFieldClassConst,
115 )
116
117 def test_create_invalid_specific_context_field_class(self):
118 with self.assertRaises(TypeError):
119 self._stream_class.create_event_class(specific_context_field_class='lel')
120
121 def test_create_payload_field_class(self):
122 fc = self._tc.create_structure_field_class()
123 ec = self._stream_class.create_event_class(payload_field_class=fc)
124 self.assertEqual(ec.payload_field_class.addr, fc.addr)
125 self.assertIs(
126 type(ec.payload_field_class), bt2_field_class._StructureFieldClass
127 )
128
129 def test_const_create_payload_field_class(self):
130 ec_const = _create_const_event_class(self._tc, self._stream_class)
131 self.assertIs(
132 type(ec_const.payload_field_class),
133 bt2_field_class._StructureFieldClassConst,
134 )
135
136 def test_create_invalid_payload_field_class(self):
137 with self.assertRaises(TypeError):
138 self._stream_class.create_event_class(payload_field_class='lel')
139
140 def test_create_name(self):
141 ec = self._stream_class.create_event_class(name='viande à chien')
142 self.assertEqual(ec.name, 'viande à chien')
143
144 def test_create_invalid_name(self):
145 with self.assertRaises(TypeError):
146 self._stream_class.create_event_class(name=2)
147
148 def test_emf_uri(self):
149 ec = self._stream_class.create_event_class(emf_uri='salut')
150 self.assertEqual(ec.emf_uri, 'salut')
151
152 def test_create_invalid_emf_uri(self):
153 with self.assertRaises(TypeError):
154 self._stream_class.create_event_class(emf_uri=23)
155
156 def test_create_log_level(self):
157 ec = self._stream_class.create_event_class(
158 log_level=bt2.EventClassLogLevel.EMERGENCY
159 )
160 self.assertEqual(ec.log_level, bt2.EventClassLogLevel.EMERGENCY)
161
162 def test_create_invalid_log_level(self):
163 with self.assertRaises(ValueError):
164 self._stream_class.create_event_class(log_level='zoom')
165
166 def test_create_user_attributes(self):
167 ec = self._stream_class.create_event_class(user_attributes={'salut': 23})
168 self.assertEqual(ec.user_attributes, {'salut': 23})
169 self.assertIs(type(ec.user_attributes), bt2_value.MapValue)
170
171 def test_const_create_user_attributes(self):
172 ec_const = _create_const_event_class(self._tc, self._stream_class)
173 self.assertIs(type(ec_const.user_attributes), bt2_value._MapValueConst)
174
175 def test_create_invalid_user_attributes(self):
176 with self.assertRaises(TypeError):
177 self._stream_class.create_event_class(user_attributes=object())
178
179 def test_create_invalid_user_attributes_value_type(self):
180 with self.assertRaises(TypeError):
181 self._stream_class.create_event_class(user_attributes=23)
182
183 def test_stream_class(self):
184 ec = self._stream_class.create_event_class()
185 self.assertEqual(ec.stream_class.addr, self._stream_class.addr)
186 self.assertIs(type(ec.stream_class), bt2_stream_class._StreamClass)
187
188 def test_const_stream_class(self):
189 ec_const = _create_const_event_class(self._tc, self._stream_class)
190 self.assertIs(type(ec_const.stream_class), bt2_stream_class._StreamClassConst)
This page took 0.033184 seconds and 4 git commands to generate.