def test_unicode_channel_message_handler(self, r): p = r.pubsub(ignore_subscribe_messages=True) channel = u('uni') + unichr(4456) + u('code') channels = {channel: self.message_handler} p.subscribe(**channels) assert r.publish(channel, 'test message') == 1 assert wait_for_message(p) is None assert self.message == make_message('message', channel, 'test message')
def test_unicode_pattern_message_handler(self, r): p = r.pubsub(ignore_subscribe_messages=True) pattern = u('uni') + unichr(4456) + u('*') channel = u('uni') + unichr(4456) + u('code') p.psubscribe(**{pattern: self.message_handler}) assert r.publish(channel, 'test message') == 1 assert wait_for_message(p) is None assert self.message == make_message('pmessage', channel, 'test message', pattern=pattern)
def test_simple_encoding(self): unicode_string = unichr(3456) + u('abcd') + unichr(3421) self.client.set('unicode-string', unicode_string) cached_val = self.client.get('unicode-string') self.assertEquals( unicode.__name__, type(cached_val).__name__, 'Cache returned value with type "%s", expected "%s"' % (type(cached_val).__name__, unicode.__name__)) self.assertEqual(unicode_string, cached_val)
def make_subscribe_test_data(pubsub, type): if type == 'channel': return { 'p': pubsub, 'sub_type': 'subscribe', 'unsub_type': 'unsubscribe', 'sub_func': pubsub.subscribe, 'unsub_func': pubsub.unsubscribe, 'keys': ['foo', 'bar', u('uni') + unichr(4456) + u('code')] } elif type == 'pattern': return { 'p': pubsub, 'sub_type': 'psubscribe', 'unsub_type': 'punsubscribe', 'sub_func': pubsub.psubscribe, 'unsub_func': pubsub.punsubscribe, 'keys': ['f*', 'b*', u('uni') + unichr(4456) + u('*')] } assert False, 'invalid subscribe type: %s' % type
def test_list_or_args(self): bfoo = b('foo') ufoo = u('foo') # first record is a text instance self.assertEquals(list_or_args(ufoo, []), [ufoo]) self.assertEquals(list_or_args(ufoo, [ufoo]), [ufoo, ufoo]) # first record is a list self.assertEquals(list_or_args([ufoo], [ufoo]), [ufoo, ufoo]) # first record is a binary instance self.assertEquals(list_or_args(bfoo, []), [bfoo]) self.assertEquals(list_or_args(bfoo, [bfoo]), [bfoo, bfoo])
def test_get_and_set(self, r): # get and set can't be tested independently of each other assert r.get("a") is None byte_string = b("value") integer = 5 unicode_string = unichr(3456) + u("abcd") + unichr(3421) assert r.set("byte_string", byte_string) assert r.set("integer", 5) assert r.set("unicode_string", unicode_string) assert r.get("byte_string") == byte_string assert r.get("integer") == b(str(integer)) assert r.get("unicode_string").decode("utf-8") == unicode_string
def test_get_and_set(self, r): # get and set can't be tested independently of each other assert r.get('a') is None byte_string = b('value') integer = 5 unicode_string = unichr(3456) + u('abcd') + unichr(3421) assert r.set('byte_string', byte_string) assert r.set('integer', 5) assert r.set('unicode_string', unicode_string) assert r.get('byte_string') == byte_string assert r.get('integer') == b(str(integer)) assert r.get('unicode_string').decode('utf-8') == unicode_string
def test_exec_error_in_no_transaction_pipeline_unicode_command(self, r): key = unichr(3456) + u('abcd') + unichr(3421) r[key] = 1 with r.pipeline(transaction=False) as pipe: pipe.llen(key) pipe.expire(key, 100) with pytest.raises(ResponseError) as ex: pipe.execute() expected = unicode('Command # 1 (LLEN {0}) of pipeline caused error: ').format(key) assert unicode(ex.value).startswith(expected) assert r[key] == b('1')
def test_exec_error_in_no_transaction_pipeline_unicode_command(self, nr): key = unichr(3456) + u('abcd') + unichr(3421) nr[key] = 1 with nr.pipeline(transaction=False) as pipe: pipe.llen(key) pipe.expire(key, 100) with pytest.raises(redis.ResponseError) as ex: pipe.execute() expected = unicode('Command # 1 (LLEN %s) of pipeline caused ' 'error: ') % add_namespace(key) assert unicode(ex.value).startswith(expected) assert nr[key] == b('1')
def test_exec_error_in_no_transaction_pipeline_unicode_command(self): key = unichr(3456) + u('abcd') + unichr(3421) self.rc[key] = 1 with self.rc.pipeline(transaction=False) as pipe: pipe.llen(key) pipe.expire(key, 100) with pytest.raises(redis.ResponseError) as ex: pipe.execute() print ex.value expected = unicode('Command # 1 (LLEN %s) of pipeline caused ' 'error: ') % key assert unicode(ex.value).startswith(expected) assert self.rc[key] == b('1')
def test_channel_message_handler(self, r): p = r.pubsub(ignore_subscribe_messages=True) p.subscribe(**{self.channel: self.message_handler}) r.publish(self.channel, self.data) assert wait_for_message(p) is None assert self.message == self.make_message('message', self.channel, self.data) # test that we reconnected to the correct channel p.connection.disconnect() assert wait_for_message(p) is None # should reconnect new_data = self.data + u('new data') r.publish(self.channel, new_data) assert wait_for_message(p) is None assert self.message == self.make_message('message', self.channel, new_data)
def test_exec_error_in_no_transaction_pipeline_unicode_command(self): key = unichr(3456) + u('abcd') + unichr(3421) yield self.client.set(key, 1) with self.client.pipeline(transaction=False) as pipe: pipe.llen(key) pipe.expire(key, 100) expected = unicode('Command # 1 (LLEN %s) of pipeline caused ' 'error: ') % key try : yield pipe.execute() except redis.ResponseError as ex: self.assertTrue(unicode(ex).startswith(expected)) res = yield self.client.get(key) self.assertEqual(res, b('1'))
def test_slowlog_get(self, r, slowlog): assert r.slowlog_reset() unicode_string = unichr(3456) + u('abcd') + unichr(3421) r.get(unicode_string) slowlog = r.slowlog_get() assert isinstance(slowlog, list) assert len(slowlog) == 2 get_command = slowlog[0] assert isinstance(get_command['start_time'], int) assert isinstance(get_command['duration'], int) assert get_command['command'] == \ b(' ').join((b('GET'), unicode_string.encode('utf-8'))) slowlog_reset_command = slowlog[1] assert isinstance(slowlog_reset_command['start_time'], int) assert isinstance(slowlog_reset_command['duration'], int) assert slowlog_reset_command['command'] == b('SLOWLOG RESET')
def test_get_and_set(self): # get and set can't be tested independently of each other r = self.r res = yield r.get('a') self.assertTrue(res is None) byte_string = b('value') integer = 5 unicode_string = unichr(3456) + u('abcd') + unichr(3421) yield r.set('byte_string', byte_string) yield r.set('integer', 5) yield r.set('unicode_string', unicode_string) res = yield r.get('byte_string') self.assertEqual(res, byte_string) res = yield r.get('integer') self.assertEqual(res, b(str(integer))) res = yield r.get('unicode_string') self.assertEqual(res.decode('utf-8'), unicode_string)
def test_pattern_message_handler(self, o): p = o.pubsub(ignore_subscribe_messages=True) p.psubscribe(**{self.pattern: self.message_handler}) o.publish(self.channel, self.data) assert wait_for_message(p) is None assert self.message == self.make_message('pmessage', self.channel, self.data, pattern=self.pattern) # test that we reconnected to the correct pattern p.connection.disconnect() assert wait_for_message(p) is None # should reconnect new_data = self.data + u('new data') o.publish(self.channel, new_data) assert wait_for_message(p) is None assert self.message == self.make_message('pmessage', self.channel, new_data, pattern=self.pattern)
def test_slowlog_get(self, r, slowlog): assert r.slowlog_reset() unicode_string = unichr(3456) + u('abcd') + unichr(3421) r.get(unicode_string) slowlog = r.slowlog_get() assert isinstance(slowlog, list) commands = [log['command'] for log in slowlog] get_command = b(' ').join((b('GET'), unicode_string.encode('utf-8'))) assert get_command in commands assert b('SLOWLOG RESET') in commands # the order should be ['GET <uni string>', 'SLOWLOG RESET'], # but if other clients are executing commands at the same time, there # could be commands, before, between, or after, so just check that # the two we care about are in the appropriate order. assert commands.index(get_command) < commands.index(b('SLOWLOG RESET')) # make sure other attributes are typed correctly assert isinstance(slowlog[0]['start_time'], int) assert isinstance(slowlog[0]['duration'], int)
def test_pattern_message_handler(self, nr): p = nr.pubsub(ignore_subscribe_messages=True) p.psubscribe(**{self.pattern: self.message_handler}) nr.publish(self.channel, self.data) assert wait_for_message(p) is None assert self.message == self.make_message('pmessage', self.channel, self.data, pattern=self.pattern) # test that we reconnected to the correct pattern p.connection.disconnect() assert wait_for_message(p) is None # should reconnect new_data = self.data + u('new data') nr.publish(self.channel, new_data) assert wait_for_message(p) is None assert self.message == self.make_message('pmessage', self.channel, new_data, pattern=self.pattern)
def test_slowlog_get(self, r): # fail on purpose on travis assert [ r.config_get('slowlog-log-slower-than'), r.config_get('slowlog-max-len')] == [-10, -10] assert r.slowlog_reset() unicode_string = unichr(3456) + u('abcd') + unichr(3421) r.get(unicode_string) slowlog = r.slowlog_get() assert isinstance(slowlog, list) assert len(slowlog) == 2 get_command = slowlog[0] assert isinstance(get_command['start_time'], int) assert isinstance(get_command['duration'], int) assert get_command['command'] == \ b(' ').join((b('GET'), unicode_string.encode('utf-8'))) slowlog_reset_command = slowlog[1] assert isinstance(slowlog_reset_command['start_time'], int) assert isinstance(slowlog_reset_command['duration'], int) assert slowlog_reset_command['command'] == b('SLOWLOG RESET')
class TestPubSubAutoDecoding(object): "These tests only validate that we get unicode values back" channel = u('uni') + unichr(4456) + u('code') pattern = u('uni') + unichr(4456) + u('*') data = u('abc') + unichr(4458) + u('123') def make_message(self, type, channel, data, pattern=None): return { 'type': type, 'channel': channel, 'pattern': pattern, 'data': data } def setup_method(self, method): self.message = None def message_handler(self, message): self.message = message @pytest.fixture() def r(self, request): return _redis_client(request=request, decode_responses=True) def test_channel_subscribe_unsubscribe(self, r): p = r.pubsub() p.subscribe(self.channel) assert wait_for_message(p) == self.make_message( 'subscribe', self.channel, 1) p.unsubscribe(self.channel) assert wait_for_message(p) == self.make_message( 'unsubscribe', self.channel, 0) def test_pattern_subscribe_unsubscribe(self, r): p = r.pubsub() p.psubscribe(self.pattern) assert wait_for_message(p) == self.make_message( 'psubscribe', self.pattern, 1) p.punsubscribe(self.pattern) assert wait_for_message(p) == self.make_message( 'punsubscribe', self.pattern, 0) def test_channel_publish(self, r): p = r.pubsub(ignore_subscribe_messages=True) p.subscribe(self.channel) r.publish(self.channel, self.data) assert wait_for_message(p) == self.make_message( 'message', self.channel, self.data) def test_pattern_publish(self, r): p = r.pubsub(ignore_subscribe_messages=True) p.psubscribe(self.pattern) r.publish(self.channel, self.data) assert wait_for_message(p) == self.make_message('pmessage', self.channel, self.data, pattern=self.pattern) def test_channel_message_handler(self, r): p = r.pubsub(ignore_subscribe_messages=True) p.subscribe(**{self.channel: self.message_handler}) r.publish(self.channel, self.data) assert wait_for_message(p) is None assert self.message == self.make_message('message', self.channel, self.data) # test that we reconnected to the correct channel p.connection.disconnect() assert wait_for_message(p) is None # should reconnect new_data = self.data + u('new data') r.publish(self.channel, new_data) assert wait_for_message(p) is None assert self.message == self.make_message('message', self.channel, new_data) def test_pattern_message_handler(self, r): p = r.pubsub(ignore_subscribe_messages=True) p.psubscribe(**{self.pattern: self.message_handler}) r.publish(self.channel, self.data) assert wait_for_message(p) is None assert self.message == self.make_message('pmessage', self.channel, self.data, pattern=self.pattern) # test that we reconnected to the correct pattern p.connection.disconnect() assert wait_for_message(p) is None # should reconnect new_data = self.data + u('new data') r.publish(self.channel, new_data) assert wait_for_message(p) is None assert self.message == self.make_message('pmessage', self.channel, new_data, pattern=self.pattern)
def test_list_encoding(self): unicode_string = unichr(3456) + u('abcd') + unichr(3421) result = [unicode_string, unicode_string, unicode_string] for i in range(len(result)): self.client.rpush('a', unicode_string) self.assertEquals(self.client.lrange('a', 0, -1), result)
class TestPubSubAutoDecoding(object): "These tests only validate that we get unicode values back" channel = u('uni') + unichr(4456) + u('code') pattern = u('uni') + unichr(4456) + u('*') data = u('abc') + unichr(4458) + u('123') def make_message(self, type, channel, data, pattern=None): return { 'type': type, 'channel': channel, 'pattern': pattern, 'data': data } def setup_method(self, *args): self.message = None def message_handler(self, message): self.message = message def test_channel_subscribe_unsubscribe(self, o): p = o.pubsub() p.subscribe(self.channel) assert wait_for_message(p) == self.make_message( 'subscribe', self.channel, 1) p.unsubscribe(self.channel) assert wait_for_message(p) == self.make_message( 'unsubscribe', self.channel, 0) @pytest.mark.xfail(reason="Pattern pubsub do not work currently") def test_pattern_subscribe_unsubscribe(self, o): p = o.pubsub() p.psubscribe(self.pattern) assert wait_for_message(p) == self.make_message( 'psubscribe', self.pattern, 1) p.punsubscribe(self.pattern) assert wait_for_message(p) == self.make_message( 'punsubscribe', self.pattern, 0) def test_channel_publish(self, o): p = o.pubsub(ignore_subscribe_messages=True) p.subscribe(self.channel) o.publish(self.channel, self.data) assert wait_for_message(p) == self.make_message( 'message', self.channel, self.data) @pytest.mark.xfail(reason="Pattern pubsub do not work currently") def test_pattern_publish(self, o): p = o.pubsub(ignore_subscribe_messages=True) p.psubscribe(self.pattern) o.publish(self.channel, self.data) assert wait_for_message(p) == self.make_message('pmessage', self.channel, self.data, pattern=self.pattern) def test_channel_message_handler(self, o): p = o.pubsub(ignore_subscribe_messages=True) p.subscribe(**{self.channel: self.message_handler}) o.publish(self.channel, self.data) assert wait_for_message(p) is None assert self.message == self.make_message('message', self.channel, self.data) # test that we reconnected to the correct channel p.connection.disconnect() assert wait_for_message(p) is None # should reconnect new_data = self.data + u('new data') o.publish(self.channel, new_data) assert wait_for_message(p) is None assert self.message == self.make_message('message', self.channel, new_data) @pytest.mark.xfail(reason="Pattern pubsub do not work currently") def test_pattern_message_handler(self, o): p = o.pubsub(ignore_subscribe_messages=True) p.psubscribe(**{self.pattern: self.message_handler}) o.publish(self.channel, self.data) assert wait_for_message(p) is None assert self.message == self.make_message('pmessage', self.channel, self.data, pattern=self.pattern) # test that we reconnected to the correct pattern p.connection.disconnect() assert wait_for_message(p) is None # should reconnect new_data = self.data + u('new data') o.publish(self.channel, new_data) assert wait_for_message(p) is None assert self.message == self.make_message('pmessage', self.channel, new_data, pattern=self.pattern)
def test_list_encoding(self, r): unicode_string = unichr(3456) + u('abcd') + unichr(3421) result = [unicode_string, unicode_string, unicode_string] r.rpush('a', *result) assert r.lrange('a', 0, -1) == result
def test_simple_encoding(self, r): unicode_string = unichr(3456) + u('abcd') + unichr(3421) r['unicode-string'] = unicode_string cached_val = r['unicode-string'] assert isinstance(cached_val, unicode) assert unicode_string == cached_val
def test_list_encoding(self): unicode_string = unichr(3456) + u("abcd") + unichr(3421) result = [unicode_string, unicode_string, unicode_string] for i in range(len(result)): self.client.rpush("a", unicode_string) self.assertEquals(self.client.lrange("a", 0, -1), result)
def test_object_value(self, r): unicode_string = unichr(3456) + u('abcd') + unichr(3421) r['unicode-string'] = Exception(unicode_string) cached_val = r['unicode-string'] assert isinstance(cached_val, unicode) assert unicode_string == cached_val