def test_bad_command_in_pubsub(create_connection, loop, server): conn = yield from create_connection(server.tcp_address, loop=loop) res = yield from conn.execute('subscribe', 'chan:1') assert res == [[b'subscribe', b'chan:1', 1]] msg = "Connection in SUBSCRIBE mode" with pytest.raises_regex(RedisError, msg): yield from conn.execute('select', 1) with pytest.raises_regex(RedisError, msg): conn.execute('get')
def test_bad_command_in_pubsub(create_connection, loop, server): conn = yield from create_connection( server.tcp_address, loop=loop) res = yield from conn.execute('subscribe', 'chan:1') assert res == [[b'subscribe', b'chan:1', 1]] msg = "Connection in SUBSCRIBE mode" with pytest.raises_regex(RedisError, msg): yield from conn.execute('select', 1) with pytest.raises_regex(RedisError, msg): conn.execute('get')
def test_multi_exec(redis, loop): yield from redis.delete('foo', 'bar') tr = redis.multi_exec() f1 = tr.incr('foo') f2 = tr.incr('bar') res = yield from tr.execute() assert res == [1, 1] res2 = yield from asyncio.gather(f1, f2, loop=loop) assert res == res2 tr = redis.multi_exec() f1 = tr.incr('foo') f2 = tr.incr('bar') yield from tr.execute() assert (yield from f1) == 2 assert (yield from f2) == 2 tr = redis.multi_exec() f1 = tr.set('foo', 1.0) f2 = tr.incrbyfloat('foo', 1.2) res = yield from tr.execute() assert res == [True, 2.2] res2 = yield from asyncio.gather(f1, f2, loop=loop) assert res == res2 tr = redis.multi_exec() f1 = tr.incrby('foo', 1.0) with pytest.raises_regex(MultiExecError, "increment must be .* int"): yield from tr.execute() with pytest.raises(TypeError): yield from f1
def test_migrate(redis, create_redis, loop, serverB): yield from add(redis, 'my-key', 123) conn2 = yield from create_redis(serverB.tcp_address, db=2, loop=loop) yield from conn2.delete('my-key') assert (yield from redis.exists('my-key')) assert not (yield from conn2.exists('my-key')) ok = yield from redis.migrate( 'localhost', serverB.tcp_address.port, 'my-key', 2, 1000) assert ok is True assert not (yield from redis.exists('my-key')) assert (yield from conn2.exists('my-key')) with pytest.raises_regex(TypeError, "host .* str"): yield from redis.migrate(None, 1234, 'key', 1, 23) with pytest.raises_regex(TypeError, "args .* None"): yield from redis.migrate('host', '1234', None, 1, 123) with pytest.raises_regex(TypeError, "dest_db .* int"): yield from redis.migrate('host', 123, 'key', 1.0, 123) with pytest.raises_regex(TypeError, "timeout .* int"): yield from redis.migrate('host', '1234', 'key', 2, None) with pytest.raises_regex(ValueError, "Got empty host"): yield from redis.migrate('', '123', 'key', 1, 123) with pytest.raises_regex(ValueError, "dest_db .* greater equal 0"): yield from redis.migrate('host', 6379, 'key', -1, 1000) with pytest.raises_regex(ValueError, "timeout .* greater equal 0"): yield from redis.migrate('host', 6379, 'key', 1, -1000)
def test_migrate(redis, create_redis, loop, serverB): yield from add(redis, 'my-key', 123) conn2 = yield from create_redis(serverB.tcp_address, db=2, loop=loop) yield from conn2.delete('my-key') assert (yield from redis.exists('my-key')) is True assert (yield from conn2.exists('my-key')) is False ok = yield from redis.migrate( 'localhost', serverB.tcp_address.port, 'my-key', 2, 1000) assert ok is True assert (yield from redis.exists('my-key')) is False assert (yield from conn2.exists('my-key')) is True with pytest.raises_regex(TypeError, "host .* str"): yield from redis.migrate(None, 1234, 'key', 1, 23) with pytest.raises_regex(TypeError, "args .* None"): yield from redis.migrate('host', '1234', None, 1, 123) with pytest.raises_regex(TypeError, "dest_db .* int"): yield from redis.migrate('host', 123, 'key', 1.0, 123) with pytest.raises_regex(TypeError, "timeout .* int"): yield from redis.migrate('host', '1234', 'key', 2, None) with pytest.raises_regex(ValueError, "Got empty host"): yield from redis.migrate('', '123', 'key', 1, 123) with pytest.raises_regex(ValueError, "dest_db .* greater equal 0"): yield from redis.migrate('host', 6379, 'key', -1, 1000) with pytest.raises_regex(ValueError, "timeout .* greater equal 0"): yield from redis.migrate('host', 6379, 'key', 1, -1000)
def test_rename(redis, server): yield from add(redis, 'foo', 'bar') yield from redis.delete('bar') res = yield from redis.rename('foo', 'bar') assert res is True with pytest.raises_regex(ReplyError, 'ERR no such key'): yield from redis.rename('foo', 'bar') with pytest.raises(TypeError): yield from redis.rename(None, 'bar') with pytest.raises(TypeError): yield from redis.rename('foo', None) with pytest.raises(ValueError): yield from redis.rename('foo', 'foo') if server.version < (3, 2): with pytest.raises_regex(ReplyError, '.* objects are the same'): yield from redis.rename('bar', b'bar')
def test_config_set(redis): cur_value = yield from redis.config_get('slave-read-only') res = yield from redis.config_set('slave-read-only', 'no') assert res is True res = yield from redis.config_set( 'slave-read-only', cur_value['slave-read-only']) assert res is True with pytest.raises_regex(ReplyError, "Unsupported CONFIG parameter"): yield from redis.config_set('databases', 100) with pytest.raises(TypeError): yield from redis.config_set(100, 'databases')
def test_config_set(redis): cur_value = yield from redis.config_get('slave-read-only') res = yield from redis.config_set('slave-read-only', 'no') assert res is True res = yield from redis.config_set('slave-read-only', cur_value['slave-read-only']) assert res is True with pytest.raises_regex(ReplyError, "Unsupported CONFIG parameter"): yield from redis.config_set('databases', 100) with pytest.raises(TypeError): yield from redis.config_set(100, 'databases')
def test_migrate_keys__errors(redis): with pytest.raises_regex(TypeError, "host .* str"): yield from redis.migrate_keys(None, 1234, 'key', 1, 23) with pytest.raises_regex(TypeError, "keys .* list or tuple"): yield from redis.migrate_keys('host', '1234', None, 1, 123) with pytest.raises_regex(TypeError, "dest_db .* int"): yield from redis.migrate_keys('host', 123, ('key',), 1.0, 123) with pytest.raises_regex(TypeError, "timeout .* int"): yield from redis.migrate_keys('host', '1234', ('key',), 2, None) with pytest.raises_regex(ValueError, "Got empty host"): yield from redis.migrate_keys('', '123', ('key',), 1, 123) with pytest.raises_regex(ValueError, "dest_db .* greater equal 0"): yield from redis.migrate_keys('host', 6379, ('key',), -1, 1000) with pytest.raises_regex(ValueError, "timeout .* greater equal 0"): yield from redis.migrate_keys('host', 6379, ('key',), 1, -1000) with pytest.raises_regex(ValueError, "keys .* empty"): yield from redis.migrate_keys('host', '1234', (), 2, 123)
def test_migrate__exceptions(create_redis, loop, server, serverB): redisA = yield from create_redis(server.tcp_address) redisB = yield from create_redis(serverB.tcp_address, db=2) yield from add(redisA, 'my-key', 123) yield from redisB.delete('my-key') assert (yield from redisA.exists('my-key')) assert not (yield from redisB.exists('my-key')) fut1 = redisB.debug_sleep(2) fut2 = redisA.migrate('localhost', serverB.tcp_address.port, 'my-key', dest_db=2, timeout=10) yield from fut1 with pytest.raises_regex(ReplyError, "IOERR .* timeout .*"): assert not (yield from fut2)
def test_auth(redis): expected_message = "ERR Client sent AUTH, but no password is set" with pytest.raises_regex(ReplyError, expected_message): yield from redis.auth('')