From: Simon Marchi Date: Mon, 13 May 2019 17:10:40 +0000 (-0400) Subject: bt2: Adapt test_event_class.py and make it pass X-Git-Url: http://git.efficios.com/?p=babeltrace.git;a=commitdiff_plain;h=65531d5502d759226b18a2974dd77c78d58ab4dc bt2: Adapt test_event_class.py and make it pass This patch updates event_class.py to be more in line with the current API and updates test_event_class.py accordingly. Everything related to equality, copy and deepcopy is removed. An event class is always created from an existing stream class, so it is not longer possible to create an event class out of thin air. We only support passing parameters to the event class when creating it, not assigning them afterwards. Change-Id: I2f1ad9f98f25e3e2dfdea511a1410529d014745b Signed-off-by: Simon Marchi Signed-off-by: Francis Deslauriers Reviewed-on: https://review.lttng.org/c/babeltrace/+/1296 Tested-by: jenkins Reviewed-by: Philippe Proulx --- diff --git a/bindings/python/bt2/bt2/event_class.py b/bindings/python/bt2/bt2/event_class.py index 72d63ecb..d63c9347 100644 --- a/bindings/python/bt2/bt2/event_class.py +++ b/bindings/python/bt2/bt2/event_class.py @@ -22,10 +22,8 @@ from bt2 import native_bt, object, utils import bt2.field_class -import collections.abc import bt2.value import bt2.event -import copy import bt2 @@ -51,62 +49,39 @@ class EventClass(object._SharedObject): _get_ref = staticmethod(native_bt.event_class_get_ref) _put_ref = staticmethod(native_bt.event_class_put_ref) - def __init__(self, name, id=None, log_level=None, emf_uri=None, - context_field_class=None, payload_field_class=None): - utils._check_str(name) - ptr = native_bt.event_class_create(name) - - if ptr is None: - raise bt2.CreationError('cannot create event class object') - - super().__init__(ptr) - - if id is not None: - self.id = id - - if log_level is not None: - self.log_level = log_level - - if emf_uri is not None: - self.emf_uri = emf_uri - - if context_field_class is not None: - self.context_field_class = context_field_class - - if payload_field_class is not None: - self.payload_field_class = payload_field_class - @property def stream_class(self): - sc_ptr = native_bt.event_class_get_stream_class(self._ptr) + sc_ptr = native_bt.event_class_borrow_stream_class(self._ptr) if sc_ptr is not None: - return bt2.StreamClass._create_from_ptr(sc_ptr) + return bt2.stream_class.StreamClass._create_from_ptr_and_get_ref(sc_ptr) @property def name(self): return native_bt.event_class_get_name(self._ptr) + def _name(self, name): + utils._check_str(name) + return native_bt.event_class_set_name(self._ptr, name) + + _name = property(fset=_name) + @property def id(self): id = native_bt.event_class_get_id(self._ptr) return id if id >= 0 else None - @id.setter - def id(self, id): - utils._check_int64(id) - ret = native_bt.event_class_set_id(self._ptr, id) - utils._handle_ret(ret, "cannot set event class object's ID") - @property def log_level(self): - log_level = native_bt.event_class_get_log_level(self._ptr) - return log_level if log_level >= 0 else None + is_available, log_level = native_bt.event_class_get_log_level(self._ptr) + + if is_available != native_bt.PROPERTY_AVAILABILITY_AVAILABLE: + return None - @log_level.setter - def log_level(self, log_level): + return _EVENT_CLASS_LOG_LEVEL_TO_OBJ[log_level] + + def _log_level(self, log_level): log_levels = ( - EventClassLogLevel.UNSPECIFIED, EventClassLogLevel.EMERGENCY, EventClassLogLevel.ALERT, EventClassLogLevel.CRITICAL, @@ -127,110 +102,70 @@ class EventClass(object._SharedObject): if log_level not in log_levels: raise ValueError("'{}' is not a valid log level".format(log_level)) - ret = native_bt.event_class_set_log_level(self._ptr, log_level) - utils._handle_ret(ret, "cannot set event class object's log level") + native_bt.event_class_set_log_level(self._ptr, log_level) + + _log_level = property(fset=_log_level) @property def emf_uri(self): return native_bt.event_class_get_emf_uri(self._ptr) - @emf_uri.setter - def emf_uri(self, emf_uri): + def _emf_uri(self, emf_uri): utils._check_str(emf_uri) ret = native_bt.event_class_set_emf_uri(self._ptr, emf_uri) utils._handle_ret(ret, "cannot set event class object's EMF URI") + _emf_uri = property(fset=_emf_uri) + @property - def context_field_class(self): - fc_ptr = native_bt.event_class_get_context_type(self._ptr) + def specific_context_field_class(self): + fc_ptr = native_bt.event_class_borrow_specific_context_field_class_const(self._ptr) if fc_ptr is None: return - return bt2.field_class._create_from_ptr(fc_ptr) - - @context_field_class.setter - def context_field_class(self, context_field_class): - context_field_class_ptr = None + return bt2.field_class._create_field_class_from_ptr_and_get_ref(fc_ptr) + def _specific_context_field_class(self, context_field_class): if context_field_class is not None: - utils._check_type(context_field_class, bt2.field_class._FieldClass) - context_field_class_ptr = context_field_class._ptr + utils._check_type(context_field_class, bt2.field_class._StructureFieldClass) + ret = native_bt.event_class_set_specific_context_field_class(self._ptr, context_field_class._ptr) + utils._handle_ret(ret, "cannot set event class object's context field class") - ret = native_bt.event_class_set_context_type(self._ptr, context_field_class_ptr) - utils._handle_ret(ret, "cannot set event class object's context field class") + _specific_context_field_class = property(fset=_specific_context_field_class) @property def payload_field_class(self): - fc_ptr = native_bt.event_class_get_payload_type(self._ptr) + fc_ptr = native_bt.event_class_borrow_payload_field_class_const(self._ptr) if fc_ptr is None: return - return bt2.field_class._create_from_ptr(fc_ptr) - - @payload_field_class.setter - def payload_field_class(self, payload_field_class): - payload_field_class_ptr = None + return bt2.field_class._create_field_class_from_ptr_and_get_ref(fc_ptr) + def _payload_field_class(self, payload_field_class): if payload_field_class is not None: - utils._check_type(payload_field_class, bt2.field_class._FieldClass) - payload_field_class_ptr = payload_field_class._ptr - - ret = native_bt.event_class_set_payload_type(self._ptr, payload_field_class_ptr) - utils._handle_ret(ret, "cannot set event class object's payload field class") - - def __call__(self): - event_ptr = native_bt.event_create(self._ptr) - - if event_ptr is None: - raise bt2.CreationError('cannot create event field object') - - return bt2.event._create_from_ptr(event_ptr) - - def __eq__(self, other): - if type(other) is not type(self): - return False - - if self.addr == other.addr: - return True - - self_props = ( - self.name, - self.id, - self.log_level, - self.emf_uri, - self.context_field_class, - self.payload_field_class - ) - other_props = ( - other.name, - other.id, - other.log_level, - other.emf_uri, - other.context_field_class, - other.payload_field_class - ) - return self_props == other_props - - def _copy(self, fc_copy_func): - cpy = EventClass(self.name) - cpy.id = self.id - - if self.log_level is not None: - cpy.log_level = self.log_level - - if self.emf_uri is not None: - cpy.emf_uri = self.emf_uri - - cpy.context_field_class = fc_copy_func(self.context_field_class) - cpy.payload_field_class = fc_copy_func(self.payload_field_class) - return cpy - - def __copy__(self): - return self._copy(lambda fc: fc) - - def __deepcopy__(self, memo): - cpy = self._copy(copy.deepcopy) - memo[id(self)] = cpy - return cpy + utils._check_type(payload_field_class, bt2.field_class._StructureFieldClass) + ret = native_bt.event_class_set_payload_field_class(self._ptr, payload_field_class._ptr) + utils._handle_ret(ret, "cannot set event class object's payload field class") + + _payload_field_class = property(fset=_payload_field_class) + + +_EVENT_CLASS_LOG_LEVEL_TO_OBJ = { + native_bt.EVENT_CLASS_LOG_LEVEL_EMERGENCY: EventClassLogLevel.EMERGENCY, + native_bt.EVENT_CLASS_LOG_LEVEL_ALERT: EventClassLogLevel.ALERT, + native_bt.EVENT_CLASS_LOG_LEVEL_CRITICAL: EventClassLogLevel.CRITICAL, + native_bt.EVENT_CLASS_LOG_LEVEL_ERROR: EventClassLogLevel.ERROR, + native_bt.EVENT_CLASS_LOG_LEVEL_WARNING: EventClassLogLevel.WARNING, + native_bt.EVENT_CLASS_LOG_LEVEL_NOTICE: EventClassLogLevel.NOTICE, + native_bt.EVENT_CLASS_LOG_LEVEL_INFO: EventClassLogLevel.INFO, + native_bt.EVENT_CLASS_LOG_LEVEL_DEBUG_SYSTEM: EventClassLogLevel.DEBUG_SYSTEM, + native_bt.EVENT_CLASS_LOG_LEVEL_DEBUG_PROGRAM: EventClassLogLevel.DEBUG_PROGRAM, + native_bt.EVENT_CLASS_LOG_LEVEL_DEBUG_PROCESS: EventClassLogLevel.DEBUG_PROCESS, + native_bt.EVENT_CLASS_LOG_LEVEL_DEBUG_MODULE: EventClassLogLevel.DEBUG_MODULE, + native_bt.EVENT_CLASS_LOG_LEVEL_DEBUG_UNIT: EventClassLogLevel.DEBUG_UNIT, + native_bt.EVENT_CLASS_LOG_LEVEL_DEBUG_FUNCTION: EventClassLogLevel.DEBUG_FUNCTION, + native_bt.EVENT_CLASS_LOG_LEVEL_DEBUG_LINE: EventClassLogLevel.DEBUG_LINE, + native_bt.EVENT_CLASS_LOG_LEVEL_DEBUG: EventClassLogLevel.DEBUG, +} diff --git a/tests/bindings/python/bt2/test_event_class.py b/tests/bindings/python/bt2/test_event_class.py index 153a21d4..4d2fe60a 100644 --- a/tests/bindings/python/bt2/test_event_class.py +++ b/tests/bindings/python/bt2/test_event_class.py @@ -1,217 +1,78 @@ -from bt2 import value import unittest -import copy import bt2 +from utils import get_default_trace_class -@unittest.skip("this is broken") class EventClassTestCase(unittest.TestCase): def setUp(self): - self._context_fc = bt2.StructureFieldClass() - self._context_fc.append_field('allo', bt2.StringFieldClass()) - self._context_fc.append_field('zola', bt2.IntegerFieldClass(18)) - self._payload_fc = bt2.StructureFieldClass() - self._payload_fc.append_field('zoom', bt2.StringFieldClass()) - self._ec = bt2.EventClass('my_event') - self._ec.id = 18 - self._ec.emf_uri = 'yes' - self._ec.log_level = bt2.EventClassLogLevel.INFO - self._ec.context_field_class = self._context_fc - self._ec.payload_field_class = self._payload_fc - - def tearDown(self): - del self._context_fc - del self._payload_fc - del self._ec - - def test_create(self): - self.assertEqual(self._ec.name, 'my_event') - self.assertEqual(self._ec.id, 18) - self.assertEqual(self._ec.context_field_class, self._context_fc) - self.assertEqual(self._ec.payload_field_class, self._payload_fc) - self.assertEqual(self._ec.emf_uri, 'yes') - self.assertEqual(self._ec.log_level, bt2.EventClassLogLevel.INFO) - - def test_create_invalid_no_name(self): - with self.assertRaises(TypeError): - bt2.EventClass() - - def test_create_full(self): - ec = bt2.EventClass(name='name', id=23, - context_field_class=self._context_fc, - payload_field_class=self._payload_fc, - emf_uri='my URI', - log_level=bt2.EventClassLogLevel.WARNING) - self.assertEqual(ec.name, 'name') - self.assertEqual(ec.id, 23) - self.assertEqual(ec.context_field_class, self._context_fc) - self.assertEqual(ec.payload_field_class, self._payload_fc) - self.assertEqual(ec.emf_uri, 'my URI') - self.assertEqual(ec.log_level, bt2.EventClassLogLevel.WARNING) - - def test_assign_id(self): - self._ec.id = 1717 - self.assertEqual(self._ec.id, 1717) - - def test_assign_invalid_id(self): - with self.assertRaises(TypeError): - self._ec.id = 'lel' + self._tc = get_default_trace_class() + + self._context_fc = self._tc.create_structure_field_class() + self._context_fc.append_field('allo', self._tc.create_string_field_class()) + self._context_fc.append_field('zola', self._tc.create_signed_integer_field_class(18)) + + self._payload_fc = self._tc.create_structure_field_class() + self._payload_fc.append_field('zoom', self._tc.create_string_field_class()) - def test_assign_context_field_class(self): - self._ec.context_field_class = self._payload_fc - self.assertEqual(self._ec.context_field_class, self._payload_fc) + self._stream_class = self._tc.create_stream_class(assigns_automatic_event_class_id=True) - def test_assign_no_context_field_class(self): - self._ec.context_field_class = None - self.assertIsNone(self._ec.context_field_class) + def test_create_default(self): + ec = self._stream_class.create_event_class() - def test_assign_invalid_context_field_class(self): + self.assertIsNone(ec.name, 'my_event') + self.assertTrue(type(ec.id), int) + self.assertIsNone(ec.specific_context_field_class) + self.assertIsNone(ec.payload_field_class) + self.assertIsNone(ec.emf_uri) + self.assertIsNone(ec.log_level) + + def test_create_invalid_id(self): + sc = self._tc.create_stream_class(assigns_automatic_event_class_id=False) with self.assertRaises(TypeError): - self._ec.context_field_class = 'lel' + sc.create_event_class(id='lel') + + def test_create_specific_context_field_class(self): + fc = self._tc.create_structure_field_class() + ec = self._stream_class.create_event_class(specific_context_field_class=fc) + self.assertEqual(ec.specific_context_field_class.addr, fc.addr) - def test_assign_payload_field_class(self): - self._ec.payload_field_class = self._payload_fc - self.assertEqual(self._ec.payload_field_class, self._payload_fc) + def test_create_invalid_specific_context_field_class(self): + with self.assertRaises(TypeError): + self._stream_class.create_event_class(specific_context_field_class='lel') - def test_assign_no_payload_field_class(self): - self._ec.payload_field_class = None - self.assertIsNone(self._ec.payload_field_class) + def test_create_payload_field_class(self): + fc = self._tc.create_structure_field_class() + ec = self._stream_class.create_event_class(payload_field_class=fc) + self.assertEqual(ec.payload_field_class.addr, fc.addr) - def test_assign_invalid_payload_field_class(self): + def test_create_invalid_payload_field_class(self): with self.assertRaises(TypeError): - self._ec.payload_field_class = 'lel' - - def test_stream_class_prop_no_sc(self): - self.assertIsNone(self._ec.stream_class) - - def test_stream_class_prop(self): - sc = bt2.StreamClass() - sc.add_event_class(self._ec) - self.assertEqual(self._ec.stream_class.addr, sc.addr) - - def _test_copy(self, cpy): - self.assertIsNot(cpy, self._ec) - self.assertNotEqual(cpy.addr, self._ec.addr) - self.assertEqual(cpy, self._ec) - - def test_copy(self): - cpy = copy.copy(self._ec) - self._test_copy(cpy) - self.assertEqual(self._ec.context_field_class.addr, cpy.context_field_class.addr) - self.assertEqual(self._ec.payload_field_class.addr, cpy.payload_field_class.addr) - - def test_deepcopy(self): - cpy = copy.deepcopy(self._ec) - self._test_copy(cpy) - self.assertNotEqual(self._ec.context_field_class.addr, cpy.context_field_class.addr) - self.assertNotEqual(self._ec.payload_field_class.addr, cpy.payload_field_class.addr) - - def test_assign_emf_uri(self): - self._ec.emf_uri = 'salut' - self.assertEqual(self._ec.emf_uri, 'salut') - - def test_assign_invalid_emf_uri(self): + self._stream_class.create_event_class(payload_field_class='lel') + + def test_create_name(self): + ec = self._stream_class.create_event_class(name='viande à chien') + self.assertEqual(ec.name, 'viande à chien') + + def test_create_invalid_name(self): + with self.assertRaises(TypeError): + self._stream_class.create_event_class(name=2) + + def test_emf_uri(self): + ec = self._stream_class.create_event_class(emf_uri='salut') + self.assertEqual(ec.emf_uri, 'salut') + + def test_create_invalid_emf_uri(self): with self.assertRaises(TypeError): - self._ec.emf_uri = 23 + self._stream_class.create_event_class(emf_uri=23) - def test_assign_log_level(self): - self._ec.log_level = bt2.EventClassLogLevel.EMERGENCY - self.assertEqual(self._ec.log_level, bt2.EventClassLogLevel.EMERGENCY) + def test_create_log_level(self): + ec = self._stream_class.create_event_class(log_level=bt2.EventClassLogLevel.EMERGENCY) + self.assertEqual(ec.log_level, bt2.EventClassLogLevel.EMERGENCY) - def test_assign_invalid_log_level(self): + def test_create_invalid_log_level(self): with self.assertRaises(ValueError): - self._ec.log_level = 'zoom' - - def test_eq(self): - ec1 = bt2.EventClass(name='name', id=23, - context_field_class=self._context_fc, - payload_field_class=self._payload_fc, - emf_uri='my URI', - log_level=bt2.EventClassLogLevel.WARNING) - ec2 = bt2.EventClass(name='name', id=23, - context_field_class=self._context_fc, - payload_field_class=self._payload_fc, - emf_uri='my URI', - log_level=bt2.EventClassLogLevel.WARNING) - self.assertEqual(ec1, ec2) - - def test_ne_name(self): - ec1 = bt2.EventClass(name='name1', id=23, - context_field_class=self._context_fc, - payload_field_class=self._payload_fc, - emf_uri='my URI', - log_level=bt2.EventClassLogLevel.WARNING) - ec2 = bt2.EventClass(name='name', id=23, - context_field_class=self._context_fc, - payload_field_class=self._payload_fc, - emf_uri='my URI', - log_level=bt2.EventClassLogLevel.WARNING) - self.assertNotEqual(ec1, ec2) - - def test_ne_id(self): - ec1 = bt2.EventClass(name='name', id=24, - context_field_class=self._context_fc, - payload_field_class=self._payload_fc, - emf_uri='my URI', - log_level=bt2.EventClassLogLevel.WARNING) - ec2 = bt2.EventClass(name='name', id=23, - context_field_class=self._context_fc, - payload_field_class=self._payload_fc, - emf_uri='my URI', - log_level=bt2.EventClassLogLevel.WARNING) - self.assertNotEqual(ec1, ec2) - - def test_ne_context_field_class(self): - ec1 = bt2.EventClass(name='name', id=23, - context_field_class=self._payload_fc, - payload_field_class=self._payload_fc, - emf_uri='my URI', - log_level=bt2.EventClassLogLevel.WARNING) - ec2 = bt2.EventClass(name='name', id=23, - context_field_class=self._context_fc, - payload_field_class=self._payload_fc, - emf_uri='my URI', - log_level=bt2.EventClassLogLevel.WARNING) - self.assertNotEqual(ec1, ec2) - - def test_ne_payload_field_class(self): - ec1 = bt2.EventClass(name='name', id=23, - context_field_class=self._context_fc, - payload_field_class=self._context_fc, - emf_uri='my URI', - log_level=bt2.EventClassLogLevel.WARNING) - ec2 = bt2.EventClass(name='name', id=23, - context_field_class=self._context_fc, - payload_field_class=self._payload_fc, - emf_uri='my URI', - log_level=bt2.EventClassLogLevel.WARNING) - self.assertNotEqual(ec1, ec2) - - def test_ne_emf_uri(self): - ec1 = bt2.EventClass(name='name', id=23, - context_field_class=self._context_fc, - payload_field_class=self._payload_fc, - emf_uri='my URI', - log_level=bt2.EventClassLogLevel.WARNING) - ec2 = bt2.EventClass(name='name', id=23, - context_field_class=self._context_fc, - payload_field_class=self._payload_fc, - emf_uri='my UR', - log_level=bt2.EventClassLogLevel.WARNING) - self.assertNotEqual(ec1, ec2) - - def test_ne_log_level(self): - ec1 = bt2.EventClass(name='name', id=23, - context_field_class=self._context_fc, - payload_field_class=self._payload_fc, - emf_uri='my URI', - log_level=bt2.EventClassLogLevel.WARNING) - ec2 = bt2.EventClass(name='name', id=23, - context_field_class=self._context_fc, - payload_field_class=self._payload_fc, - emf_uri='my URI', - log_level=bt2.EventClassLogLevel.ERROR) - self.assertNotEqual(ec1, ec2) - - def test_eq_invalid(self): - self.assertFalse(self._ec == 23) + self._stream_class.create_event_class(log_level='zoom') + + def test_stream_class(self): + ec = self._stream_class.create_event_class() + self.assertEqual(ec.stream_class.addr, self._stream_class.addr)