Пример #1
0
class TestStringScalar(object):
    def setup_method(self, method):
        self.redis = RedisWrapper(Redis())
        self.test_key = 'test_key'
        self.str = Scalar(self.redis, self.test_key)
        self.redis.flushdb()
        self.redis.method_calls = []

    def test_simple_assign(self):
        self.str.value = '3'
        assert self.redis.get('test_key') == '3'

    def test_length(self):
        assert len(self.str) == 0
        self.str.value = '3'
        assert len(self.str) == 1

    def test_contains(self):
        self.str.value = '123'
        assert '2' in self.str

    def test_append(self):
        self.str += '123'
        assert self.redis.get('test_key') == '123'

    def test_slicing(self):
        self.str.value = '1234567'
        assert self.str[0:0] == ''
        assert self.str[:] == '1234567'
        assert self.str[0:-1] == '123456'
        assert self.str[:-1] == '123456'
        assert self.str[-1:] == '7'
Пример #2
0
class TestList(object):
    def setup_method(self, method):
        self.redis = RedisWrapper(Redis())
        self.test_key = 'test_key'
        self.list = List(self.redis, self.test_key)
        self.redis.flushdb()
        self.redis.method_calls = []

    def test_length_initially_zero(self):
        assert len(self.list) == 0

    def test_append_value_increases_length(self):
        self.list.append(1)
        assert len(self.list) == 1

    def test_append_saves_values_in_redis(self):
        self.list.append(1)
        assert self.redis.lrange('test_key', 0, 1) == ['1']

    def test_remove(self):
        self.list.append(1)

        self.list.remove(1)
        assert len(self.list) == 0

    def test_contains(self):
        self.list.append(1)
        assert '1' in self.list
        assert '2' not in self.list

    def test_iterator(self):
        self.list.append(1)
        self.list.append(2)
        assert [i for i in self.list] == ['1', '2']

    def test_insert(self):
        self.list.append(1)
        self.list.insert(0, 2)

        assert [i for i in self.list] == ['2', '1']

    def test_pop(self):
        self.list.append(1)
        assert self.list.pop() == '1'

    def test_popping_first_item_uses_lpop(self):
        self.list.append(1)
        self.list.append(2)
        self.list.append(3)
        self.redis.method_calls = []
        assert self.list.pop(0) == '1'
        assert self.redis.method_calls == ['lpop']

    def test_redis_type(self):
        self.list.append(1)
        assert self.list.redis_type == 'list'
Пример #3
0
class TestBooleanList(object):
    def setup_method(self, method):
        self.redis = RedisWrapper(Redis())
        self.redis.flushdb()
        self.test_key = 'test_key'
        self.list = List(self.redis, self.test_key, type=bool)
        self.list.append(True)
        self.list.append(False)
        self.list.append(True)
        self.list.append(True)
        self.redis.method_calls = []

    def test_length_returns_list_length(self):
        assert len(self.list) == 4

    def test_get_list_item_by_range(self):
        assert self.list[0:-1] == [True, False, True]

    def test_get_list_item(self):
        assert self.list[1] == False

    def test_set_list_item(self):
        self.list[2] = False
        assert self.list[2] == False

    def test_set_items_by_range(self):
        self.list[2:] = False
        assert self.list[2] == False
        assert self.list[3] == False

    def test_pop(self):
        self.list.pop() == True

    def test_remove(self):
        self.list.remove(False)
        assert len(self.list) == 3

    def test_contains(self):
        assert True in self.list
        assert False in self.list
        assert 5 not in self.list

    def test_iterator(self):
        assert [i for i in self.list] == [True, False, True, True]

    def test_insert(self):
        self.list.insert(0, False)

        assert [i for i in self.list] == [False, True, False, True, True]

    def test_popping_first_item_uses_lpop(self):
        self.redis.method_calls = []
        assert self.list.pop(0) == True
        assert self.redis.method_calls == ['lpop']
Пример #4
0
class TestIntegerList(object):
    def setup_method(self, method):
        self.redis = RedisWrapper(Redis())
        self.redis.flushdb()
        self.test_key = 'test_key'
        self.list = List(self.redis, self.test_key, type=int)
        self.list.append(1)
        self.list.append(2)
        self.list.append(3)
        self.list.append(4)
        self.redis.method_calls = []

    def test_length_returns_list_length(self):
        assert len(self.list) == 4

    def test_get_list_item_by_range(self):
        assert self.list[0:-1] == [1, 2, 3]

    def test_get_list_item(self):
        assert self.list[1] == 2

    def test_set_list_item(self):
        self.list[2] = 123
        assert self.list[2] == 123

    def test_set_items_by_range(self):
        self.list[1:2] = 5
        assert self.list[1] == 5
        assert self.list[2] == 5

    def test_pop(self):
        self.list.pop() == 4

    def test_remove(self):
        self.list.remove(1)
        assert len(self.list) == 3

    def test_contains(self):
        assert 1 in self.list
        assert 5 not in self.list

    def test_iterator(self):
        assert [i for i in self.list] == [1, 2, 3, 4]

    def test_insert(self):
        self.list.insert(0, 2)

        assert [i for i in self.list] == [2, 1, 2, 3, 4]

    def test_popping_first_item_uses_lpop(self):
        self.redis.method_calls = []
        assert self.list.pop(0) == 1
        assert self.redis.method_calls == ['lpop']
Пример #5
0
class TestIntegerDict(object):
    def setup_method(self, method):
        self.redis = RedisWrapper(Redis())
        self.redis.flushdb()
        self.test_key = 'test_key'
        self.dict = Dict(self.redis, self.test_key, type=int)

    def test_length_initially_zero(self):
        assert len(self.dict) == 0

    def test_add_value_increases_length(self):
        self.dict['a'] = 1
        assert len(self.dict) == 1

    def test_set_saves_values_in_redis(self):
        self.dict['a'] = 1
        assert self.redis.hkeys('test_key') == ['a']

    def test_remove(self):
        self.dict['a'] = 1

        del self.dict['a']
        assert len(self.dict) == 0

    def test_contains(self):
        self.dict['a'] = 1
        assert 'a' in self.dict
        assert 'b' not in self.dict

    def test_iterator(self):
        self.dict['a'] = 1
        self.dict['b'] = 2
        assert [key for key in self.dict] == ['a', 'b']

    def test_items(self):
        self.dict['a'] = 1
        self.dict['b'] = 2
        assert self.dict.items() == [('a', 1), ('b', 2)]

    def test_values(self):
        self.dict['a'] = 1
        self.dict['b'] = 2
        assert self.dict.values() == [1, 2]

    def test_keys(self):
        self.dict['a'] = 1
        self.dict['b'] = 2
        assert self.dict.keys() == ['a', 'b']

    def test_redis_type(self):
        self.dict['a'] = 1
        assert self.dict.redis_type == 'hash'
class TestListSlicing(object):
    def setup_method(self, method):
        self.redis = RedisWrapper(Redis())
        self.redis.flushdb()
        self.test_key = "test_key"
        self.list = List(self.redis, self.test_key)
        self.list.append(1)
        self.list.append(2)
        self.list.append(3)
        self.list.append(4)

    def test_get_list_item_by_range(self):
        assert self.list[0:-1] == ["1", "2", "3"]

    def test_get_list_item_by_range_with_open_end(self):
        assert self.list[0:] == ["1", "2", "3", "4"]

    def test_get_list_item_by_range_with_open_start(self):
        assert self.list[:2] == ["1", "2"]

    def test_get_list_item_by_range_with_open_start_and_end(self):
        assert self.list[:] == ["1", "2", "3", "4"]

    def test_get_list_item(self):
        assert self.list[1] == "2"

    def test_set_list_item(self):
        self.list[2] = 123
        assert self.list[2] == "123"

    def test_set_items_by_range(self):
        self.list[1:2] = 5
        assert self.list[1] == "5"
        assert self.list[2] == "5"

    def test_set_list_item_by_range_with_open_end(self):
        self.list[2:] = "5"
        assert self.list[:] == ["1", "2", "5", "5"]

    def test_set_list_item_by_range_with_open_start(self):
        self.list[:2] = "5"
        assert self.list[:2] == ["5", "5"]

    def test_set_list_item_by_range_with_open_start_and_end(self):
        self.list[:] = "5"
        assert self.list[:] == ["5", "5", "5", "5"]

    def test_pop(self):
        self.list.pop() == "4"
 def setup_method(self, method):
     self.redis = RedisWrapper(Redis())
     self.redis.flushdb()
     self.test_key = "test_key"
     self.list = List(self.redis, self.test_key)
     self.list.append(1)
     self.list.append(2)
     self.list.append(3)
     self.list.append(4)
Пример #8
0
 def setup_method(self, method):
     self.redis = RedisWrapper(Redis())
     self.redis.flushdb()
     self.test_key = 'test_key'
     self.list = List(self.redis, self.test_key, type=int)
     self.list.append(1)
     self.list.append(2)
     self.list.append(3)
     self.list.append(4)
     self.redis.method_calls = []
Пример #9
0
 def setup_method(self, method):
     self.redis = RedisWrapper(Redis())
     self.redis.flushdb()
     self.test_key = 'test_key'
     self.list = List(self.redis, self.test_key, type=bool)
     self.list.append(True)
     self.list.append(False)
     self.list.append(True)
     self.list.append(True)
     self.redis.method_calls = []
Пример #10
0
class TestIntegerScalar(object):
    def setup_method(self, method):
        self.redis = RedisWrapper(Redis())
        self.test_key = 'test_key'
        self.int = Scalar(self.redis, self.test_key, type=int)
        self.redis.flushdb()
        self.redis.method_calls = []

    def test_simple_assign(self):
        self.int.value = 3
        assert self.int.value == 3

    def test_length(self):
        assert len(self.int) == 0
        self.int.value = '3'
        assert len(self.int) == 1

    def test_append(self):
        self.int.value = 2
        self.int += 3
        assert self.int.value == 5
Пример #11
0
 def setup_method(self, method):
     self.redis = RedisWrapper(Redis())
     self.redis.flushdb()
     self.test_key = 'test_key'
     self.dict = Dict(self.redis, self.test_key)
Пример #12
0
 def setup_method(self, method):
     self.redis = RedisWrapper(Redis())
     self.test_key = 'test_key'
     self.list = List(self.redis, self.test_key)
     self.redis.flushdb()
     self.redis.method_calls = []
Пример #13
0
 def setup_method(self, method):
     self.redis = RedisWrapper(Redis())
     self.test_key = 'test_key'
     self.int = Scalar(self.redis, self.test_key, type=int)
     self.redis.flushdb()
     self.redis.method_calls = []