def test__has_data(self): proto = siderpy.Protocol() proto._ready.append(1) assert proto._has_data() proto = siderpy.Protocol() proto._unparsed = b"+OK\r\n" assert proto._has_data() proto = siderpy.Protocol() proto._ready.append(1) proto._unparsed = b"+OK\r\n" assert proto._has_data()
def test__feed(self): proto = siderpy.Protocol() assert proto.feed(b"+OK\r\n") is None assert proto._unparsed == b"" assert len(proto._ready) == 1 assert proto._ready.popleft() == b"OK" proto = siderpy.Protocol() assert proto.feed(b"+OK") is None assert proto._unparsed == b"+OK" assert len(proto._ready) == 0 assert proto.feed(b"\r\n") is None assert proto._unparsed == b"" assert len(proto._ready) == 1 assert proto._ready.popleft() == b"OK"
def test_reset(self): proto = siderpy.Protocol() proto._ready.append(1) proto.reset() assert isinstance(proto._ready, collections.deque) assert len(proto._ready) == 0 assert proto._unparsed == b""
def test__parse_array(self): proto = siderpy.Protocol() parser = proto._parse_array() assert isinstance(parser, types.GeneratorType) next(parser) assert parser.send(b"*0\r\n") == ([], b"") assert parser.send(b"*0\r") == (False, b"*0\r") assert parser.send(b"*0\r\n+OK") == ([], b"+OK") assert parser.send(b"*-1\r\n") == (None, b"") assert parser.send(b"*-1\r") == (False, b"*-1\r") assert parser.send(b"*-1\r\n+OK") == (None, b"+OK") assert parser.send(b"*2\r\n$3\r\nfoo\r\n$3\r\nbar\r\n") == ([ b"foo", b"bar" ], b"") assert parser.send(b"*3\r\n:1\r\n:2\r\n:3\r\n") == ([1, 2, 3], b"") assert parser.send( b"*5\r\n:1\r\n:2\r\n:3\r\n:4\r\n$6\r\nfoobar\r\n") == ([ 1, 2, 3, 4, b"foobar" ], b"") assert parser.send(b"*2\r\n$3\r\nfoo\r\n$3\r\n") == (False, b"$3\r\n") assert parser.send(b"$3\r\nbar\r\n") == ([b"foo", b"bar"], b"") data = parser.send(b"*2\r\n*2\r\n:1\r\n:2\r\n*2\r\n+Foo\r\n+Bar\r\n") assert len(data[0]) == 2 assert data[0][0] == [1, 2] assert data[0][1] == [b"Foo", b"Bar"] assert data[1] == b"" data = parser.send(b"*3\r\n$3\r\nfoo\r\n$-1\r\n$3\r\nbar\r\n") assert data[0] == [b"foo", None, b"bar"] and data[1] == b""
def test__init(self): proto = siderpy.Protocol() assert isinstance(proto._reader, hiredis.Reader) assert not hasattr(proto, "_unparsed") assert not hasattr(proto, "_parser") assert not hasattr(proto, "_ready") assert proto.feed == proto._reader.feed assert proto.has_data == proto._reader.has_data
def test__parse_string(self): proto = siderpy.Protocol() parser = proto._parse_string() assert isinstance(parser, types.GeneratorType) next(parser) assert parser.send(b"+OK\r\n") == [b"OK", b""] assert parser.send(b"+OK") == (False, b"+OK") assert parser.send(b"+OK\r\n$6\r") == [b"OK", b"$6\r"]
def test__parse_integer(self): proto = siderpy.Protocol() parser = proto._parse_integer() assert isinstance(parser, types.GeneratorType) next(parser) assert parser.send(b":1000\r\n") == [1000, b""] assert parser.send(b":1000") == (False, b":1000") assert parser.send(b":1000\r\n+OK") == [1000, b"+OK"]
def test__init(self): proto = siderpy.Protocol() assert proto._reader is None assert proto._unparsed == b"" assert isinstance(proto._parser, types.GeneratorType) assert isinstance(proto._ready, collections.deque) and len( proto._ready) == 0 assert proto.feed == proto._feed assert proto.gets == proto._gets assert proto.has_data == proto._has_data
def test__gets(self): proto = siderpy.Protocol() proto._ready.append(b"OK") assert proto.gets() == b"OK" assert len(proto._ready) == 0 proto._ready.append(siderpy.RedisError("Err")) assert isinstance(proto.gets(), siderpy.RedisError) assert len(proto._ready) == 0
def test__parse_bulk_string(self): proto = siderpy.Protocol() parser = proto._parse_bulk_string() assert isinstance(parser, types.GeneratorType) next(parser) assert parser.send(b"$6\r\nfoobar\r\n") == (b"foobar", b"") assert parser.send(b"$6\r\nfoo") == (False, b"$6\r\nfoo") assert parser.send(b"$6\r\nfoobar") == (False, b"$6\r\nfoobar") assert parser.send(b"$6\r\nfoobar\r\n+OK") == (b"foobar", b"+OK") assert parser.send(b"$-1\r\n") == (None, b"") assert parser.send(b"$0\r\n\r\n") == (b"", b"")
def test_make_cmd(self): proto = siderpy.Protocol() assert proto.make_cmd("get", ["key"]) == b"*2\r\n$3\r\nget\r\n$3\r\nkey\r\n" assert proto.make_cmd( "set", ["key", "value" ]) == b"*3\r\n$3\r\nset\r\n$3\r\nkey\r\n$5\r\nvalue\r\n" assert proto.make_cmd( "set", ["key", b"value" ]) == b"*3\r\n$3\r\nset\r\n$3\r\nkey\r\n$5\r\nvalue\r\n" assert proto.make_cmd( "set", ["key", 1]) == b"*3\r\n$3\r\nset\r\n$3\r\nkey\r\n$1\r\n1\r\n"
def test__parse_error(self): proto = siderpy.Protocol() parser = proto._parse_error() assert isinstance(parser, types.GeneratorType) next(parser) data = parser.send(b"-Err\r\n") assert data and isinstance(data[0], siderpy.RedisError) and data[1] == b"" data = parser.send(b"-Err") assert data == (False, b"-Err") data = parser.send(b"-Err\r\n+OK") assert data and isinstance(data[0], siderpy.RedisError) and data[1] == b"+OK"
def test_make_cmd(self): proto = siderpy.Protocol() assert proto.make_cmd("get", ["key"]) == b"*2\r\n$3\r\nget\r\n$3\r\nkey\r\n" assert proto.make_cmd( "set", ["key", "value" ]) == b"*3\r\n$3\r\nset\r\n$3\r\nkey\r\n$5\r\nvalue\r\n" assert proto.make_cmd( "set", ["key", b"value" ]) == b"*3\r\n$3\r\nset\r\n$3\r\nkey\r\n$5\r\nvalue\r\n" assert proto.make_cmd( "set", ["key", 1]) == b"*3\r\n$3\r\nset\r\n$3\r\nkey\r\n$1\r\n1\r\n" with pytest.raises(TypeError): assert proto.make_cmd("blpop", ["key", None])
def test__parse(self): proto = siderpy.Protocol() parser = proto._parse() assert isinstance(parser, types.GeneratorType) next(parser) assert parser.send(b"+OK") == (False, b"+OK") assert parser.send(b"+OK\r\n") == [b"OK", b""] data = parser.send(b"-Err\r\n") assert isinstance(data[0], siderpy.RedisError) and data[1] == b"" assert parser.send(b":1000") == (False, b":1000") assert parser.send(b":1000\r\n") == [1000, b""] assert parser.send(b":1000\r\n+OK\r\n") == [1000, b"+OK\r\n"] assert parser.send(b"$6\r\nfoobar\r\n") == (b"foobar", b"") assert parser.send(b"*2\r\n$3\r\nfoo\r\n$3\r\nbar\r\n") == ([ b"foo", b"bar" ], b"")
def test__str(self): proto = siderpy.Protocol() assert str(proto) == "<siderpy.Protocol hiredis=False [{}]>".format( hex(id(proto)))
def test_reset(self): proto = siderpy.Protocol() reader = proto._reader proto.reset() assert proto._reader is not reader