async def start(self, output: Queue) -> None: msg = proto.WriteEvents() msg.event_stream_id = self.stream msg.require_master = self.require_master msg.expected_version = self.expected_version for event in self.events: e = msg.events.add() e.event_id = event.id.bytes_le e.event_type = event.type if isinstance(event.data, str): e.data_content_type = ContentType.Json e.data = event.data.encode("UTF-8") elif event.data: e.data_content_type = ContentType.Json e.data = json.dumps(event.data).encode("UTF-8") else: e.data_content_type = ContentType.Binary e.data = bytes() if event.metadata: e.metadata_content_type = ContentType.Json e.metadata = json.dumps(event.metadata).encode("UTF-8") else: e.metadata_content_type = ContentType.Binary e.metadata = bytes() data = msg.SerializeToString() await output.put( OutboundMessage(self.conversation_id, TcpCommand.WriteEvents, data, self.credential))
def _validate_write_request(request, stream_id, event_type, event_id, content_type, data): payload = proto.WriteEvents() payload.ParseFromString(request.payload) assert payload.event_stream_id == stream_id assert payload.expected_version == msg.ExpectedVersion.Any assert len(payload.events) == 1 assert not payload.require_master [evt] = payload.events assert evt.event_id == event_id.bytes_le assert evt.event_type == event_type assert evt.data_content_type == content_type assert evt.data == data assert evt.metadata_content_type == msg.ContentType.Binary assert evt.metadata == b""
async def test_write_one_event(): output = Queue() event_id = uuid4() conversation_id = uuid4() event_type = "pony_jumped" data = {"pony_name": "Burning Sulphur", "distance": 6} event_data = msg.NewEventData(event_id, event_type, data, None) conversation = WriteEvents("my-stream", [event_data], conversation_id=conversation_id) await conversation.start(output) request = await output.get() assert request.conversation_id == conversation_id assert request.command == msg.TcpCommand.WriteEvents assert not conversation.is_complete assert not conversation.result.done() payload = proto.WriteEvents() payload.ParseFromString(request.payload) assert payload.event_stream_id == "my-stream" assert payload.expected_version == msg.ExpectedVersion.Any assert len(payload.events) == 1 assert not payload.require_master [evt] = payload.events assert evt.event_id == event_id.bytes_le assert evt.event_type == "pony_jumped" assert evt.data_content_type == msg.ContentType.Json assert evt.data == json.dumps(data).encode("UTF-8") assert evt.metadata_content_type == msg.ContentType.Binary assert evt.metadata == b""