def test_constructor(self): message_data = { "id": str(uuid.uuid4()), "metadata": { "timestamp": 1460868253255, "publisher": "myapp", "headers": { "request_id": str(uuid.uuid4()), }, }, "format_version": "1.0", "schema": "https://hedwig.automatic.com/schema#/schemas/trip_created/1.0", "data": { "vehicle_id": 'C_1234567890123456', "user_id": 'U_1234567890123456', } } message = Message(message_data) assert message.id == message_data['id'] assert message.metadata == Metadata(message_data['metadata']) assert message.headers == message_data['metadata']['headers'] assert message.schema == 'https://hedwig.automatic.com/schema#/schemas/trip_created/1.0' assert message.data == message_data['data']
def dispatch_mock_sqs_message(message: Message) -> None: from hedwig import consumer sqs_message = mock.Mock() sqs_message.body = json.dumps(message.as_dict()) sqs_message.receipt_handle = 'test-receipt' settings.HEDWIG_PRE_PROCESS_HOOK(sqs_queue_message=sqs_message) consumer.message_handler_sqs(sqs_message)
def test_create_metadata(self, mock_time): mock_time.return_value = time.time() headers = {'foo': 'bar'} assert Message._create_metadata(headers) == { 'timestamp': int(mock_time.return_value * 1000), 'publisher': settings.HEDWIG_PUBLISHER, 'headers': headers, }
def test_new(self, message_data): message = Message.new( MessageType.trip_created, StrictVersion('1.0'), message_data['data'], message_data['id'], message_data['metadata']['headers'], ) assert message.id == message_data['id'] assert message.format_version == Message.FORMAT_CURRENT_VERSION assert message.headers == message_data['metadata']['headers'] assert message.schema == 'https://hedwig.automatic.com/schema#/schemas/trip_created/1.0' assert message.data == message_data['data']
def test_constructor(self): message_data = { "id": str(uuid.uuid4()), "metadata": { "timestamp": 1460868253255, "publisher": "myapp", "headers": { "request_id": str(uuid.uuid4()), }, }, "format_version": "1.0", "schema": "https://hedwig.automatic.com/schema#/schemas/trip.created/1.0", "data": { "from": "*****@*****.**", "subject": "Hello!" } } message = Message(message_data) assert message.id == message_data['id'] assert message.metadata == Metadata(message_data['metadata']) assert message.headers == message_data['metadata']['headers'] assert message.schema == 'https://hedwig.automatic.com/schema#/schemas/trip.created/1.0' assert message.data == message_data['data']
def publish(message: Message) -> None: """ Publishes a message on Hedwig topic """ if settings.HEDWIG_SYNC: dispatch_mock_sqs_message(message) return message_body = message.as_dict() headers = { **settings.HEDWIG_DEFAULT_HEADERS(message=message), **message_body['metadata']['headers'] } # make a copy to prevent changing "headers" variable contents in # pre serialize hook message_body['metadata']['headers'] = copy.deepcopy(headers) settings.HEDWIG_PRE_SERIALIZE_HOOK(message_data=message_body) payload = _convert_to_json(message_body) topic = _get_sns_topic(message) response = _publish_over_sns(topic, payload, headers) _log_published_message(message_body, response['MessageId'])
def test_validate_invalid_version(self, message_data): message_data['format_version'] = '3.0' with pytest.raises(ValidationError): Message(message_data)
def test_validate_missing_data(self, missing_data, message_data): message_data[missing_data] = None with pytest.raises(ValidationError): Message(message_data)
def message(message_data): return Message(message_data)
def _load_and_validate_message(data: dict) -> Message: message = Message(data) message.validate() message.validate_callback() return message
def _message_with_decimal(message_data): message_data = copy.deepcopy(message_data) message_data['data']['user_id'] = Decimal(1469056316326) return Message(message_data)