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()

    _validate_write_request(
        request,
        "my-stream",
        event_type,
        event_id,
        msg.ContentType.Json,
        json.dumps(data).encode("UTF-8"),
    )
async def test_not_authenticated():

    output = TeeQueue()

    event_id = uuid4()
    conversation_id = uuid4()
    error_message = "Dude, like who even are you?"

    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)
    await conversation.respond_to(
        msg.InboundMessage(
            conversation_id,
            msg.TcpCommand.NotAuthenticated,
            error_message.encode("UTF-8"),
        ),
        output,
    )

    with pytest.raises(exn.NotAuthenticated) as exc:
        await conversation.result
        assert exc.message == error_message
async def test_bad_request():

    output = TeeQueue()

    event_id = uuid4()
    conversation_id = uuid4()
    error_message = "That's not an acceptable message, man"

    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)
    await conversation.respond_to(
        msg.InboundMessage(conversation_id, msg.TcpCommand.BadRequest,
                           error_message.encode("UTF-8")),
        output,
    )

    with pytest.raises(exn.BadRequest) as exc:
        await conversation.result
        assert exc.message == error_message
def given_a_write_events_message():

    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)

    return conversation
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""
async def test_write_one_event_binary():

    output = Queue()

    event_id = uuid4()
    conversation_id = uuid4()

    event_type = "pony_jumped_binary"
    data = b"my binary encoded data"
    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()

    _validate_write_request(request, "my-stream", event_type, event_id,
                            msg.ContentType.Binary, data)