Exemplo n.º 1
0
async def test_dispatcher_receive():
    mock_reader = Mock(spec=asyncio.StreamReader)
    d = Dispatcher(None, mock_reader, None)
    d._reader = mock_reader
    stream = SBytes(b"\x85\x00\x00\x00\x00\x02\x01\x00\x02\xaa\xbb")
    mock_reader.readexactly = get_mock_coro(lambda x: stream.grab(x))
    # mock_reader.returns = b'\01\84\00\00\08\00'
    data = await d._receive(lambda x: [1, 2, 0, 2, 2])
    assert data[5] == b"\xaa\xbb"
Exemplo n.º 2
0
def test_messages_errormsg_build_err():
    with pytest.raises(
        exceptions.InternalDriverError, match=r"unknown error_code=feebad"
    ):
        messages.ErrorMessage.build(
            1, 2, 3, SBytes(b"\x00\xfe\xeb\xad\x00"),
        )
Exemplo n.º 3
0
def test_messages_event_build_good_function():
    body = b"\x00\rSCHEMA_CHANGE\x00\x07CREATED\x00\x08FUNCTION\x00\x07mykeysp\x00\x07mytable\x00\x02\x00\x03cat\x00\x04book"
    msg = messages.EventMessage.build(1, 2, 3, SBytes(body),)
    assert (
        msg.event.options["argument_types"] == ["cat", "book"]
        and msg.event.target == SchemaChangeTarget.FUNCTION
    )
Exemplo n.º 4
0
def test_messages_event_build_badtargete():
    with pytest.raises(
        exceptions.UnknownPayloadException, match=r"got unexpected target=K3YSPACE"
    ):
        body = b"\x00\rSCHEMA_CHANGE\x00\x07CREATED\x00\x08K3YSPACE\x00\x0ctestkeyspace"
        messages.EventMessage.build(
            1, 2, 3, SBytes(body),
        )
Exemplo n.º 5
0
def test_messages_event_build_badevent():
    with pytest.raises(
        exceptions.UnknownPayloadException, match=r"got unexpected event=SCHEMA_change"
    ):
        body = b"\x00\rSCHEMA_change\x00\x07CREATED\x00\x08KEYSPACE\x00\x0ctestkeyspace"
        messages.EventMessage.build(
            1, 2, 3, SBytes(body),
        )
Exemplo n.º 6
0
def test_messages_rowresults_noglobal():
    body = (
        b"\x00\x00\x00\x02\x00\x00\x00\x01\x00\x00\x00\x03\x00\x08uprofile\x00\x04user\x00"
        + b"\x07user_id\x00\t\x00\tuser_name\x00\r\x00\nuser_bcity\x00\r\x00\x00\x00\x01\x00"
        + b"\x00\x00\x04\x00\x00\x00\x04\x00\x00\x00\x06Ehtevs\x00\x00\x00\x04Pune"
    )
    msg = messages.ResultMessage.build(1, 2, 3, SBytes(body),)
    assert msg.rows.col_specs[0]["name"] == "user_id"
Exemplo n.º 7
0
def test_messages_event_build_good_table():
    body = (
        b"\x00\rSCHEMA_CHANGE\x00\x07CREATED\x00\x05TABLE\x00\x07mykeysp\x00\x07mytable"
    )
    msg = messages.EventMessage.build(1, 2, 3, SBytes(body),)
    assert (
        msg.event.options["target_name"] == "mytable"
        and msg.event.target == SchemaChangeTarget.TABLE
    )
Exemplo n.º 8
0
def test_messages_response_create_bad():
    with pytest.raises(
        exceptions.InternalDriverError, match=r"subclass should implement method"
    ):

        class MyResponse(messages.ResponseMessage):
            pass

        MyResponse.create(1, 1, 1, SBytes(b""))
Exemplo n.º 9
0
def test_messages_response_create_remains():
    with pytest.raises(exceptions.InternalDriverError, match=r"left data remains"):

        class MyResponse(messages.ResponseMessage):
            @staticmethod
            def build(version, flags, strem_id, body):
                return MyResponse(1, 2, 3)

        MyResponse.create(1, 1, 1, SBytes(b"asdf"))
Exemplo n.º 10
0
def test_messages_response_create_good():
    class MyResponse(messages.ResponseMessage):
        @staticmethod
        def build(version, flags, strem_id, body):
            body.grab(4)
            return MyResponse(1, 2, 3)

    msg = MyResponse.create(1, 1, 1, SBytes(b"asdf"))
    assert msg.version == 1
Exemplo n.º 11
0
def test_protocol_header_bad():
    with pytest.raises(
        exceptions.VersionMismatchException,
        match=r"received incorrect version from server",
    ):
        p = protocol.Protocol()
        p.version = 2
        stream = SBytes(b"\x83\x00\x00\x00\x00\x00\x00\x00\x01")
        p.decode_header(stream)
Exemplo n.º 12
0
def test_codecs_decode_string_multimap():
    body = SBytes(
        b"\00\03\00\02as\00\02\00\02df\00\03zxc\00\01z\00\03\00\04zxcv\00\01c\00\02vv\00\05last.\00\03\00\03one\00\03two\00\05three"
    )
    assert codecs.decode_string_multimap(body) == {
        "as": ["df", "zxc"],
        "z": ["zxcv", "c", "vv"],
        "last.": ["one", "two", "three"],
    }
Exemplo n.º 13
0
def test_messages_errormsg_build_good():
    msg = messages.ErrorMessage.build(
        1,
        2,
        3,
        SBytes(
            b'\x00\x00"\x00\x00;Invalid STRING constant (hillary) for "user_id" of type int'
        ),
    )
    assert msg.error_code == constants.ErrorCode.INVALID
Exemplo n.º 14
0
def test_messages_rowresults_pages():
    body = (
        b"\x00\x00\x00\x02\x00\x00\x00\x07\x00\x00\x00\x03\x00\x00\x00\x18\x04\x00\x00\x00"
        + b"\x04\x08\x00\x06Ehtevs\xf0\x7f\xff\xff\xfb\xf0\x7f\xff\xff\xfe\x00\x00\x00\x01"
        + b"\x00\x00\x00\x04\x00\x00\x00\x04\x00\x00\x00\x06Ehtevs\x00\x00\x00\x04Pune"
    )
    msg = messages.ResultMessage.build(1, 2, 3, SBytes(body),)
    assert (
        msg.rows.paging_state
        == b"\x04\x00\x00\x00\x04\x08\x00\x06Ehtevs\xf0\x7f\xff\xff\xfb\xf0\x7f\xff\xff\xfe"
    )
Exemplo n.º 15
0
def test_messages_response_create_emtpy():
    with pytest.raises(
        exceptions.InternalDriverError, match=r"didn't generate a response message"
    ):

        class MyResponse(messages.ResponseMessage):
            @staticmethod
            def build(version, flags, strem_id, body):
                return None

        MyResponse.create(1, 1, 1, SBytes(b""))
Exemplo n.º 16
0
def test_messages_rowresults_alltypes():
    body = (
        b"\x00\x00\x00\x02\x00\x00\x00\x01\x00\x00\x00\x13\x00\x08uprofile\x00\x08"
        + b"alltypes\x00\x05myint\x00\t\x00\x07myascii\x00\x01\x00\x08mybigint\x00\x02\x00"
        + b"\x06myblob\x00\x03\x00\tmyboolean\x00\x04\x00\x06mydate\x00\x11\x00\tmydecimal"
        + b"\x00\x06\x00\x08mydouble\x00\x07\x00\x07myfloat\x00\x08\x00\x06myinet\x00\x10"
        + b"\x00\nmysmallint\x00\x13\x00\x06mytext\x00\r\x00\x06mytime\x00\x12\x00\x0b"
        + b"mytimestamp\x00\x0b\x00\nmytimeuuid\x00\x0f\x00\tmytinyint\x00\x14\x00\x06myuuid"
        + b"\x00\x0c\x00\tmyvarchar\x00\r\x00\x08myvarint\x00\x0e\x00\x00\x00\x01\x00\x00\x00"
        + b"\x04\x00\x00\x00\n\x00\x00\x00\x011\x00\x00\x00\x08\x00\x00\x00\x00\x00\x00\x00"
        + b"\x02\x00\x00\x00\x02\x03\x06\x00\x00\x00\x01\x00\x00\x00\x00\x04\x80\x00G5\x00"
        + b"\x00\x00\t\x00\x00\x00\x08\r\xf9\x03C?\x00\x00\x00\x08@\x1c~M\xe3\xb8\xa1\x9d\x00"
        + b"\x00\x00\x04A\x05\x82\xe4\x00\x00\x00\x10&\x07\xf8\xb0@\x06\x08\x13\x00\x00\x00"
        + b"\x00\x00\x00 \x0e\x00\x00\x00\x02\x00\x0b\x00\x00\x00\x0212\x00\x00\x00\x08\x00"
        + b"\x00\x00\x00\x00\x00\x00\r\x00\x00\x00\x08\x00\x00\x01n\xb8@\xa3\x1b\x00\x00\x00"
        + b"\x10v\x92\x80\xc8\x12\xf0\x11\xea\x88\x99`\xa4L\xe9tb\x00\x00\x00\x01\x10\x00\x00"
        + b"\x00\x10\xf9&0\xa6\xd9\x94D\x0e\xa2\xdc\xfek(\xe98)\x00\x00\x00\x0218\x00\x00\x00\x01\x13"
    )
    msg = messages.ResultMessage.build(1, 2, 3, SBytes(body),)
    assert msg.rows[0] == Row(
        myint=10,
        myascii="1",
        mybigint=2,
        myblob=b"\x03\x06",
        myboolean=False,
        mydate=datetime.date(2019, 11, 29),
        mydecimal=decimal.Decimal("600.12315455"),
        mydouble=7.123344,
        myfloat=8.34445571899414,
        myinet=ipaddress.IPv6Address("2607:f8b0:4006:813::200e"),
        mysmallint=11,
        mytext="12",
        mytime=13,
        mytimestamp=datetime.datetime(2019, 11, 29, 17, 41, 14, 139000),
        mytimeuuid=uuid.UUID("769280c8-12f0-11ea-8899-60a44ce97462"),
        mytinyint=16,
        myuuid=uuid.UUID("f92630a6-d994-440e-a2dc-fe6b28e93829"),
        myvarchar="18",
        myvarint=19,
    )
Exemplo n.º 17
0
def test_messages_preparedresults_meta():
    body = (
        b"\x00\x00\x00\x04\x00\x10\xac\xfc\x0fW\xa9\x9c\x1cr\xaf\xcaP9<\xd2c\x8d\x00\x00\x00"
        + b"\x01\x00\x00\x00\x03\x00\x00\x00\x01\x00\x00\x00\x08uprofile\x00\x04user\x00\x07user_id\x00"
        + b"\t\x00\tuser_name\x00\r\x00\nuser_bcity\x00\r\x00\x00\x00\x04\x00\x00\x00\x00"
    )
    msg = messages.ResultMessage.build(1, 2, 3, SBytes(body),)
    assert msg.col_specs == [
        {"ksname": "uprofile", "name": "user_id", "option_id": 9, "tablename": "user"},
        {
            "ksname": "uprofile",
            "name": "user_name",
            "option_id": 13,
            "tablename": "user",
        },
        {
            "ksname": "uprofile",
            "name": "user_bcity",
            "option_id": 13,
            "tablename": "user",
        },
    ]
Exemplo n.º 18
0
def test_messages_rowresults_global():
    body = (
        b"\x00\x00\x00\x02\x00\x00\x00\x01\x00\x00\x00\x03\x00\x08uprofile\x00\x04user"
        + b"\x00\x07user_id\x00\t\x00\tuser_name\x00\r\x00\nuser_bcity\x00\r\x00\x00\x00\x01"
        + b"\x00\x00\x00\x04\x00\x00\x00\x04\x00\x00\x00\x06Ehtevs\x00\x00\x00\x04Pune"
    )
    msg = messages.ResultMessage.build(1, 2, 3, SBytes(body),)
    assert msg.rows.col_specs == [
        {"ksname": "uprofile", "name": "user_id", "option_id": 9, "tablename": "user"},
        {
            "ksname": "uprofile",
            "name": "user_name",
            "option_id": 13,
            "tablename": "user",
        },
        {
            "ksname": "uprofile",
            "name": "user_bcity",
            "option_id": 13,
            "tablename": "user",
        },
    ]
Exemplo n.º 19
0
def test_codecs_decode_int_bytes_zero():
    body = SBytes(b"\00\00\00\00")
    assert codecs.decode_int_bytes(body) == b""
Exemplo n.º 20
0
def test_codecs_decode_short_bytes_empty():
    body = SBytes(b"\00\00")
    assert codecs.decode_short_bytes(body) == b""
Exemplo n.º 21
0
def test_codecs_decode_short_bytes():
    body = SBytes(b"\00\04asdf")
    assert codecs.decode_short_bytes(body) == b"asdf"
Exemplo n.º 22
0
def test_codecs_decode_int():
    body = SBytes(b"\00\04\02\04")
    assert codecs.decode_int(body) == 262660
Exemplo n.º 23
0
def test_codecs_decode_short():
    body = SBytes(b"\00\04")
    assert codecs.decode_short(body) == 4
Exemplo n.º 24
0
def test_codecs_decode_string():
    body = SBytes(b"\00\04asdf")
    assert codecs.decode_string(body) == "asdf"
Exemplo n.º 25
0
def test_codecs_decode_length_bytes_zero():
    body = SBytes(b"\x01\x02\x03\x04")
    value = codecs.decode_length_bytes(body, 0)
    assert value == b"" and body.remaining == b"\x01\x02\x03\x04"
Exemplo n.º 26
0
def test_codecs_decode_byte_good():
    body = SBytes(b"\xe0\x04")
    value = codecs.decode_byte(body)
    assert value == 224 and body.remaining == b"\x04"
Exemplo n.º 27
0
def test_codecs_decode_consistency_bad():
    with pytest.raises(InternalDriverError, match=r"unknown consistency=abba"):
        body = SBytes(b"\xab\xba")
        codecs.decode_consistency(body)
Exemplo n.º 28
0
def test_codecs_decode_consistency_good():
    body = SBytes(b"\x00\x04")
    obj = codecs.decode_consistency(body)
    assert obj == Consistency.QUORUM
Exemplo n.º 29
0
def test_codecs_decode_strings_list():
    body = SBytes(b"\00\02\00\02as\00\03dfg")
    assert codecs.decode_strings_list(body) == ["as", "dfg"]
Exemplo n.º 30
0
def test_codecs_decode_int_bytes_invalid():
    with pytest.raises(InternalDriverError, match=r"unhandled inet length=5"):
        body = SBytes(b"\x05\x09\x09\x09\x09\x00\x00\x01\xBB")
        codecs.decode_inet(body)