def test_action(self): # action event data a_timestamp = EpochNSClock.generate_timestamp() a_name = NAME_A a_success = True a_for_event = CompositeEvent( timestamp=EpochNSClock.generate_timestamp(), name=NAME_B, history=BoboHistory(), data={} ) a_exception = EXCEPTION_A a_description = DESCRIPTION_A a_data = DATA_A a_event_id = EVENT_ID_A # create dict representation of action event a_dict = { ActionEvent.TIMESTAMP: a_timestamp, ActionEvent.NAME: a_name, ActionEvent.SUCCESS: a_success, ActionEvent.FOR_EVENT: a_for_event.to_dict(), ActionEvent.EXCEPTION: a_exception, ActionEvent.DESCRIPTION: a_description, ActionEvent.DATA: a_data, ActionEvent.EVENT_ID: a_event_id } # build actual action event from dict a_event_1 = BoboRuleBuilder.action(a_dict) a_event_2 = BoboRuleBuilder.event(a_dict) for a_event in [a_event_1, a_event_2]: self.assertEqual(a_event.timestamp, a_timestamp) self.assertEqual(a_event.name, a_name) self.assertEqual(a_event.success, a_success) self.assertEqual(a_event.exception, a_exception) self.assertEqual(a_event.description, a_description) self.assertDictEqual(a_event.data, a_data) self.assertEqual(a_event.event_id, a_event_id) c_event = a_event.for_event self.assertIsInstance(c_event, CompositeEvent) self.assertEqual(c_event.timestamp, a_for_event.timestamp) self.assertEqual(c_event.name, a_for_event.name) self.assertIsNone(c_event.history.first) self.assertDictEqual(c_event.data, a_for_event.data) self.assertEqual(c_event.event_id, a_for_event.event_id)
def _handle_action(self, data: str) -> None: json_data = json.loads(data) event = BoboRuleBuilder.action(json_data[bdc.EVENT]) for subscriber in self._subs: subscriber.on_dist_action(event=event)
def test_action_none(self): # action event data a_timestamp = EpochNSClock.generate_timestamp() a_name = NAME_A a_success = True a_for_event = CompositeEvent( timestamp=EpochNSClock.generate_timestamp(), name=NAME_B, history=BoboHistory(), data={} ) a_exception = EXCEPTION_A a_description = DESCRIPTION_A a_data = DATA_A a_event_id = EVENT_ID_A with self.assertRaises(RuntimeError): BoboRuleBuilder.action({ ActionEvent.TIMESTAMP: None, ActionEvent.NAME: a_name, ActionEvent.SUCCESS: a_success, ActionEvent.FOR_EVENT: a_for_event.to_dict(), ActionEvent.EXCEPTION: a_exception, ActionEvent.DESCRIPTION: a_description, ActionEvent.DATA: a_data, ActionEvent.EVENT_ID: a_event_id }) with self.assertRaises(RuntimeError): BoboRuleBuilder.action({ ActionEvent.TIMESTAMP: a_timestamp, ActionEvent.NAME: None, ActionEvent.SUCCESS: a_success, ActionEvent.FOR_EVENT: a_for_event.to_dict(), ActionEvent.EXCEPTION: a_exception, ActionEvent.DESCRIPTION: a_description, ActionEvent.DATA: a_data, ActionEvent.EVENT_ID: a_event_id }) with self.assertRaises(RuntimeError): BoboRuleBuilder.action({ ActionEvent.TIMESTAMP: a_timestamp, ActionEvent.NAME: a_name, ActionEvent.SUCCESS: None, ActionEvent.FOR_EVENT: a_for_event.to_dict(), ActionEvent.EXCEPTION: a_exception, ActionEvent.DESCRIPTION: a_description, ActionEvent.DATA: a_data, ActionEvent.EVENT_ID: a_event_id }) with self.assertRaises(RuntimeError): BoboRuleBuilder.action({ ActionEvent.TIMESTAMP: a_timestamp, ActionEvent.NAME: a_name, ActionEvent.SUCCESS: a_success, ActionEvent.FOR_EVENT: None, ActionEvent.EXCEPTION: a_exception, ActionEvent.DESCRIPTION: a_description, ActionEvent.DATA: a_data, ActionEvent.EVENT_ID: a_event_id }) with self.assertRaises(RuntimeError): BoboRuleBuilder.action({ ActionEvent.TIMESTAMP: a_timestamp, ActionEvent.NAME: a_name, ActionEvent.SUCCESS: a_success, ActionEvent.FOR_EVENT: a_for_event.to_dict(), ActionEvent.EXCEPTION: a_exception, ActionEvent.DESCRIPTION: a_description, ActionEvent.DATA: a_data, ActionEvent.EVENT_ID: None })