From 5e4fb95dee0e9d7c6bbb77ab0458649efdfd1ac3 Mon Sep 17 00:00:00 2001 From: Yossi Gottlieb Date: Tue, 19 May 2015 10:21:18 +0300 Subject: [PATCH] Add basic config_set / config_get support. --- mockredis/client.py | 22 ++++++++++++++++++++++ mockredis/tests/test_config.py | 20 ++++++++++++++++++++ 2 files changed, 42 insertions(+) create mode 100644 mockredis/tests/test_config.py diff --git a/mockredis/client.py b/mockredis/client.py index ab2a010..05c2439 100644 --- a/mockredis/client.py +++ b/mockredis/client.py @@ -53,6 +53,7 @@ def __init__(self, self.blocking_sleep_interval = blocking_sleep_interval # The 'Redis' store self.redis = defaultdict(dict) + self.redis_config = defaultdict(dict) self.timeouts = defaultdict(dict) # The 'PubSub' store self.pubsub = defaultdict(list) @@ -1396,6 +1397,27 @@ def _normalize_command_response(self, command, response): return response + # Config Set/Get commands # + + def config_set(self, name, value): + """ + Set a configuration parameter. + """ + self.redis_config[name] = value + + def config_get(self, pattern='*'): + """ + Get one or more configuration parameters. + """ + result = {} + for name, value in self.redis_config.items(): + if fnmatch.fnmatch(name, pattern): + try: + result[name] = int(value) + except ValueError: + result[name] = value + return result + # PubSub commands # def publish(self, channel, message): diff --git a/mockredis/tests/test_config.py b/mockredis/tests/test_config.py new file mode 100644 index 0000000..24a7f94 --- /dev/null +++ b/mockredis/tests/test_config.py @@ -0,0 +1,20 @@ +from nose.tools import eq_, ok_ + +from mockredis.tests.fixtures import setup, teardown + + +class TestRedisConfig(object): + """Redis config set/get tests""" + + def setup(self): + setup(self) + + def teardown(self): + teardown(self) + + def test_config_set(self): + eq_(self.redis.config_get('config-param'), {}) + self.redis.config_set('config-param', 'value') + eq_(self.redis.config_get('config-param'), {'config-param': 'value'}) + eq_(self.redis.config_get('config*'), {'config-param': 'value'}) +