コード例 #1
0
    def setUp(self):
        self._file_data = {
            'destinationTable': 'Files',
            '_id': 1,
            'name': 'filename.ext',
            'subdir': 'filepath',
        }

        self._transaction_id_data = {
            'destinationTable': 'Transactions._id',
            'value': 1,
        }

        self._transaction_key_value_data = {
            'destinationTable': 'TransactionKeyValue',
            'key': 'key',
            'value': 'value',
        }

        self._event_data = []
        self._event_data.append(self._file_data)
        self._event_data.append(self._transaction_id_data)
        self._event_data.append(self._transaction_key_value_data)

        self._event_ok = Event({
            'cloudEventsVersion': SPEC_VERSION,
            'eventID': '1',
            'eventType': CLOUDEVENTS_DEFAULT_EVENT_TYPE_,
            'source': CLOUDEVENTS_DEFAULT_SOURCE_,
            'data': self._event_data,
        })

        self._event_error_invalid_event_type = Event({
            'cloudEventsVersion': SPEC_VERSION,
            'eventID': '1',
            'eventType': 'INVALID',
            'source': CLOUDEVENTS_DEFAULT_SOURCE_,
            'data': self._event_data,
        })

        self._event_error_invalid_source = Event({
            'cloudEventsVersion': SPEC_VERSION,
            'eventID': '1',
            'eventType': CLOUDEVENTS_DEFAULT_EVENT_TYPE_,
            'source': 'INVALID',
            'data': self._event_data,
        })

        self._event_data_error_duplicated_attr = self._event_data.copy()
        self._event_data_error_duplicated_attr.append(self._transaction_id_data)
        self._event_error_duplicated_attr = Event({
            'cloudEventsVersion': SPEC_VERSION,
            'eventID': '1',
            'eventType': CLOUDEVENTS_DEFAULT_EVENT_TYPE_,
            'source': CLOUDEVENTS_DEFAULT_SOURCE_,
            'data': self._event_data_error_duplicated_attr,
        })

        return
コード例 #2
0
    def test_example_event_handler(self) -> None:
        """Test the event handler for Pacifica Downloader Example.

        """

        # Construct the CloudEvents notification object using the JSON-encoded
        # data.
        #
        event = Event(self.event_data)  # type: cloudevents.model.Event

        # Construct the __local__ downloader runner using the name of the base
        # directory. The files to be downloaded are located in the "data"
        # subdirectory.
        #
        downloader_runner = LocalDownloaderRunner(
            os.path.join(self.basedir_name, 'data'))

        # Construct the __local__ uploader runner.
        #
        uploader_runner = LocalUploaderRunner()

        # Construct the event handler using the downloader and uploader runners.
        #
        event_handler = ExampleEventHandler(downloader_runner, uploader_runner)

        # Verify that the event handler successfully handles the CloudEvents
        # notification without raising an exception.
        #
        self.assertEqual(None, event_handler.handle(event))
コード例 #3
0
 def test_exceptions(self):
     """Test the exceptions classes."""
     exception = InvalidModelProxEventHandlerError(
         Event(self.event_data),
         File(name='some_file_name.txt', path='some_file_path.txt'),
         AssertionError('fake error'))
     self.assertEqual(
         'proxymod model for file \'some_file_name.txt\' is invalid: fake error',
         str(exception))
     exception = ConfigNotFoundProxEventHandlerError(
         Event(self.event_data), 'config_1')
     self.assertEqual('proxymod configuration \'config_1\' not found',
                      str(exception))
     exception = InvalidConfigProxEventHandlerError(Event(self.event_data),
                                                    'config_1', {})
     self.assertEqual('proxymod configuration \'config_1\' is invalid',
                      str(exception))
コード例 #4
0
 def test_event_handler(self):
     """Test the event handler."""
     event = Event(self.event_data)
     downloader_runner = LocalDownloaderRunner(
         os.path.join(self.basedir_name, 'data'))
     uploader_runner = LocalUploaderRunner()
     event_handler = ProxEventHandler(downloader_runner, uploader_runner)
     self.assertEqual(None, event_handler.handle(event))
コード例 #5
0
 def _create_scriptlog(event: Event) -> ScriptLog:
     ret = ScriptLog(
         uuid=uuid.UUID(celery.current_task.request.id),
         event=dumps(event.to_dict()),
         script_id=script_config.script_id
     )
     ret.save(force_insert=True)
     return ret
コード例 #6
0
    def test_event_handler(self):
        """Verify the noop event handler does nothing."""
        event = Event({
            'cloudEventsVersion': SPEC_VERSION,
            'eventID': 'ID',
            'eventType': 'io.cloudevents',
            'source': '/cloudevents/io',
            'data': [],
        })

        event_handler = NoopEventHandler()
        event_handler.handle(event)
        self.assertTrue(event_handler)
コード例 #7
0
    def test_event_handler(self):
        event = Event({
            'cloudEventsVersion': SPEC_VERSION,
            'eventID': 'ID',
            'eventType': CLOUDEVENTS_DEFAULT_EVENT_TYPE_,
            'source': CLOUDEVENTS_DEFAULT_SOURCE_,
            'data': [],
        })

        event_handler = NoopEventHandler()

        event_handler_return_value = event_handler.handle(event)

        self.assertEqual(None, event_handler_return_value)

        return
コード例 #8
0
    def test_event_handler(self):
        event = Event({
            'cloudEventsVersion': SPEC_VERSION,
            'eventID': '1',
            'eventType': 'example',
            'source': '/example',
            'data': [],
        })

        event_handler = NoopEventHandler()

        event_handler_return_value = event_handler.handle(event)

        self.assertEqual(None, event_handler_return_value)

        return
コード例 #9
0
def create(event_type,
           event_id,
           source,
           cloud_events_version=SPEC_VERSION,
           event_type_version=None,
           event_time=None,
           schema_url=None,
           content_type=None,
           data=None):
    return Event.create_from_dict(
        dict(event_type=event_type,
             event_id=event_id,
             source=source,
             cloud_events_version=cloud_events_version,
             event_type_version=event_type_version,
             event_time=event_time,
             schema_url=schema_url,
             content_type=content_type,
             data=data))
コード例 #10
0
def parse(data):
    if isinstance(data, str):
        return Event(json.loads(data))
    else:
        return Event(data)
コード例 #11
0
    def __call__(self, event_data: typing.Dict[str, typing.Any]) -> None:
        event = Event(event_data)

        self.event_handler.handle(event)

        return
コード例 #12
0
 def __call__(self, event_data: typing.Dict[str, typing.Any]) -> None:
     """Use event data to call the handler on the event."""
     event = Event(event_data)
     self.event_handler.handle(event)