bt2: Add remaining trace-ir `*Const` classes and adapt tests
[babeltrace.git] / tests / bindings / python / bt2 / test_trace.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
335a2da5 19import uuid
9cf643d1 20import unittest
f0a42b33 21import utils
8c2367b8 22from utils import get_default_trace_class
f0a42b33
FD
23from bt2 import trace_class as bt2_trace_class
24from bt2 import value as bt2_value
25from bt2 import trace as bt2_trace
26from bt2 import stream as bt2_stream
9cf643d1
PP
27
28
29class TraceTestCase(unittest.TestCase):
30 def setUp(self):
8c2367b8 31 self._tc = get_default_trace_class()
9cf643d1
PP
32
33 def test_create_default(self):
8c2367b8 34 trace = self._tc()
335a2da5
PP
35 self.assertIsNone(trace.name)
36 self.assertIsNone(trace.uuid)
b7bc733b 37 self.assertEqual(len(trace.environment), 0)
5783664e 38 self.assertEqual(len(trace.user_attributes), 0)
9cf643d1 39
8c2367b8 40 def test_create_invalid_name(self):
9cf643d1 41 with self.assertRaises(TypeError):
8c2367b8 42 self._tc(name=17)
9cf643d1 43
5783664e
PP
44 def test_create_user_attributes(self):
45 trace = self._tc(user_attributes={'salut': 23})
46 self.assertEqual(trace.user_attributes, {'salut': 23})
f0a42b33 47 self.assertIs(type(trace.user_attributes), bt2_value.MapValue)
5783664e
PP
48
49 def test_create_invalid_user_attributes(self):
50 with self.assertRaises(TypeError):
51 self._tc(user_attributes=object())
52
53 def test_create_invalid_user_attributes_value_type(self):
54 with self.assertRaises(TypeError):
55 self._tc(user_attributes=23)
56
05abc762 57 def test_attr_trace_class(self):
335a2da5 58 trace = self._tc()
05abc762 59 self.assertEqual(trace.cls.addr, self._tc.addr)
f0a42b33
FD
60 self.assertIs(type(trace.cls), bt2_trace_class._TraceClass)
61
62 def test_const_attr_trace_class(self):
63 trace = utils.get_const_stream_beginning_message().stream.trace
64 self.assertIs(type(trace.cls), bt2_trace_class._TraceClassConst)
05abc762 65
335a2da5
PP
66 def test_attr_name(self):
67 trace = self._tc(name='mein trace')
68 self.assertEqual(trace.name, 'mein trace')
69
70 def test_attr_uuid(self):
71 trace = self._tc(uuid=uuid.UUID('da7d6b6f-3108-4706-89bd-ab554732611b'))
72 self.assertEqual(trace.uuid, uuid.UUID('da7d6b6f-3108-4706-89bd-ab554732611b'))
73
74 def test_env_get(self):
b7bc733b 75 trace = self._tc(environment={'hello': 'you', 'foo': -5})
f0a42b33
FD
76 self.assertIs(type(trace.environment), bt2_trace._TraceEnvironment)
77 self.assertIs(type(trace.environment['foo']), bt2_value.SignedIntegerValue)
b7bc733b
PP
78 self.assertEqual(trace.environment['hello'], 'you')
79 self.assertEqual(trace.environment['foo'], -5)
335a2da5 80
f0a42b33
FD
81 def test_env_iter(self):
82 trace = self._tc(environment={'hello': 'you', 'foo': -5})
83 values = set(trace.environment)
84 self.assertEqual(values, {'hello', 'foo'})
85
86 def test_const_env_get(self):
87 trace = utils.get_const_stream_beginning_message().stream.trace
88 self.assertIs(type(trace.environment), bt2_trace._TraceEnvironmentConst)
89 self.assertIs(
90 type(trace.environment['patate']), bt2_value._SignedIntegerValueConst
91 )
92
93 def test_env_iter(self):
94 trace = utils.get_const_stream_beginning_message().stream.trace
95 values = set(trace.environment)
96 self.assertEqual(values, {'patate'})
97
98 def test_const_env_set(self):
99 trace = utils.get_const_stream_beginning_message().stream.trace
100 with self.assertRaises(TypeError):
101 trace.environment['patate'] = 33
102
335a2da5 103 def test_env_get_non_existent(self):
b7bc733b 104 trace = self._tc(environment={'hello': 'you', 'foo': -5})
335a2da5
PP
105
106 with self.assertRaises(KeyError):
b7bc733b 107 trace.environment['lel']
335a2da5 108
8c2367b8
SM
109 def test_len(self):
110 trace = self._tc()
111 sc = self._tc.create_stream_class()
112 self.assertEqual(len(trace), 0)
9cf643d1 113
8c2367b8
SM
114 trace.create_stream(sc)
115 self.assertEqual(len(trace), 1)
9cf643d1 116
8c2367b8
SM
117 def _create_trace_with_some_streams(self):
118 sc = self._tc.create_stream_class(assigns_automatic_stream_id=False)
119 trace = self._tc()
120 trace.create_stream(sc, id=12)
121 trace.create_stream(sc, id=15)
122 trace.create_stream(sc, id=17)
9cf643d1 123
8c2367b8 124 return trace
9cf643d1
PP
125
126 def test_iter(self):
8c2367b8
SM
127 trace = self._create_trace_with_some_streams()
128 stream_ids = set(trace)
129 self.assertEqual(stream_ids, {12, 15, 17})
9cf643d1 130
8c2367b8
SM
131 def test_getitem(self):
132 trace = self._create_trace_with_some_streams()
8c2367b8 133 self.assertEqual(trace[12].id, 12)
f0a42b33
FD
134 self.assertIs(type(trace[12]), bt2_stream._Stream)
135
136 def test_const_getitem(self):
137 trace = utils.get_const_stream_beginning_message().stream.trace
138 self.assertIs(type(trace[0]), bt2_stream._StreamConst)
9cf643d1 139
8c2367b8
SM
140 def test_getitem_invalid_key(self):
141 trace = self._create_trace_with_some_streams()
9cf643d1 142 with self.assertRaises(KeyError):
8c2367b8 143 trace[18]
9cf643d1 144
8c2367b8
SM
145 def test_destruction_listener(self):
146 def on_trace_class_destruction(trace_class):
147 nonlocal trace_class_destroyed
148 trace_class_destroyed = True
811644b8 149
8c2367b8
SM
150 def on_trace_destruction(trace):
151 nonlocal trace_destroyed
152 trace_destroyed = True
811644b8 153
8c2367b8
SM
154 trace_destroyed = False
155 trace_class_destroyed = False
811644b8 156
8c2367b8
SM
157 trace_class = get_default_trace_class()
158 stream_class = trace_class.create_stream_class()
159 trace = trace_class()
160 stream = trace.create_stream(stream_class)
811644b8 161
8c2367b8
SM
162 trace_class.add_destruction_listener(on_trace_class_destruction)
163 trace.add_destruction_listener(on_trace_destruction)
811644b8 164
8c2367b8
SM
165 self.assertFalse(trace_class_destroyed)
166 self.assertFalse(trace_destroyed)
9cf643d1 167
8c2367b8 168 del trace
9cf643d1 169
8c2367b8
SM
170 self.assertFalse(trace_class_destroyed)
171 self.assertFalse(trace_destroyed)
9cf643d1 172
8c2367b8 173 del stream
9cf643d1 174
8c2367b8
SM
175 self.assertFalse(trace_class_destroyed)
176 self.assertTrue(trace_destroyed)
9cf643d1 177
8c2367b8 178 del trace_class
9cf643d1 179
8c2367b8
SM
180 self.assertFalse(trace_class_destroyed)
181 self.assertTrue(trace_destroyed)
9cf643d1 182
8c2367b8 183 del stream_class
9cf643d1 184
8c2367b8
SM
185 self.assertTrue(trace_class_destroyed)
186 self.assertTrue(trace_destroyed)
This page took 0.055274 seconds and 4 git commands to generate.