def test_extend(self): self.list.clear() data = RandData(str).list(10) self.list.extend(data) self.assertEqual(len(self.list), 10) for x in data: self.assertIn(x, self.list)
def test_getsetitem(self): self.list.clear() data = RandData(str).list(10) self.list.extend(data) test_str = gen_rand_str() self.list[2] = test_str self.assertEqual(self.list[2], test_str)
def reset(self, count=10, type=int): self.set.clear() self.data = OrderedDict([ (k, v) for k, v in RandData(type).dict(count).items() ]) self.data_count = count self.set.update(self.data)
def test_delitem(self): self.list.clear() data = RandData(str).list(10) self.list.extend(data) item = self.list[2] del self.list[2] self.assertNotIn(item, self.list)
def reset(self, count=10, type=int): self.set.clear() self.set_2.clear() self.set_3.clear() self.data = RandData(type).set(count) self.data_count = count self.set.update(self.data) self.set_2.update(self.data)
def test_ttl(self): self.list.clear() data = RandData(str).list(10) self.list.extend(data) self.list.set_ttl(1) self.assertGreater(self.list.ttl(), 0.30) time.sleep(1.10) self.assertEqual(len(self.list), 0)
def test_pexpire_at(self): self.list.clear() data = RandData(str).list(10) self.list.extend(data) expire_at = datetime.datetime.now() + datetime.timedelta(seconds=1) self.list.pexpire_at(expire_at.timestamp() * 1000) self.assertGreater(self.list.pttl(), 300) time.sleep(2) self.assertEqual(len(self.list), 0)
def test_slice(self): self.list.clear() data = RandData(str).list(10) self.list.extend(data) self.assertListEqual(self.list[2:8], data[2:8]) self.assertListEqual(self.list[:-1], data[:-1]) self.assertListEqual(self.list[-4:-1], data[-4:-1]) self.assertEqual(self.list[-3], data[-3]) self.assertEqual(self.list[2], data[2])
def test_iter(self): self.list.clear() data = RandData(str).list(30) self.list.extend(data) checker = [] add_check = checker.append for x in self.list.iter(0, 10): self.assertIn(x, data) add_check(x) self.assertListEqual(self.list.all, checker)
class TestJSONRedisMap(unittest.TestCase): map = RedisMap("json_map", prefix="rs:unit_tests:", serialize=True) is_str = False def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.addCleanup(self.map.clear) def cast(self, obj): return str(obj) if self.is_str else obj def reset(self, count=10, type=int): self.map.clear() self.data = RandData(type).dict(count, 1) self.data_count = count self.map.update(self.data) def test_prefix(self): self.assertEqual(self.map.prefix, 'rs:unit_tests') self.assertEqual(self.map.name, 'json_map') self.assertEqual(self.map.key_prefix, 'rs:unit_tests:json_map') def test_incr_decr(self): self.reset() self.map.incr('views', 1) self.assertEqual(self.map['views'], self.cast(1)) self.map.incr('views', 3) self.assertEqual(self.map['views'], self.cast(4)) self.map.decr('views', 1) self.assertEqual(self.map['views'], self.cast(3)) def test_get(self): self.reset() self.map["hello"] = "world" self.assertEqual(self.map.get("hello"), 'world') self.assertEqual(self.map.get('world', 'hello'), 'hello') def test_get_key(self): self.assertEqual( self.map.get_key('views'), "{}:{}:{}".format(self.map.prefix, self.map.name, 'views')) def test_items(self): self.reset() self.assertDictEqual( {k: v for k, v in self.map.items()}, {k: self.cast(v) for k, v in self.data.items()}) def test_values(self): self.reset() self.assertSetEqual( set(self.map.values()), set(map(self.cast, self.data.values()))) def test_iter(self): self.reset() self.assertSetEqual( set(k for k in self.map.iter()), set(self.cast(k) for k in self.data.keys())) def test_iter_match(self): self.reset(count=10) self.assertSetEqual( set(k for k in self.map.iter("a*")), set(self.cast(k) for k in self.data.keys() if k.startswith('a'))) def test_mget(self): self.reset(0) self.map.update({ 'test1': 1, 'test2': 2, 'test3': 3, 'test4': 4, 'test5': 5}) self.assertListEqual( self.map.mget('test2', 'test3', 'test4'), [self.cast(2), self.cast(3), self.cast(4)]) def test_pop(self): self.reset() self.map['hello'] = 'world' self.assertEqual(self.map.pop('hello'), 'world') self.assertNotIn('hello', self.map) def test_delete(self): self.reset() self.map['hello'] = 'world' self.assertEqual(self.map['hello'], 'world') del self.map['hello'] self.assertNotIn('hello', self.map) def test_scan(self): self.reset() new_keys = [] cursor = '0' while cursor: cursor, keys = self.map.scan(count=1, cursor=int(cursor)) if keys: new_keys.extend(keys) self.assertSetEqual( set(self.map.get_key(k) for k in self.data.keys()), set(new_keys)) def test_set(self): self.reset() self.map.set("hello", "world") self.assertIn("hello", self.map) def test_setex(self): self.reset() self.map.setex("hello", "world", 1) self.assertIn("hello", self.map) time.sleep(1.25) self.assertNotIn("hello", self.map) self.map.psetex("hello", "world", 1000) self.assertIn("hello", self.map) time.sleep(1.25) self.assertNotIn("hello", self.map)
class TestJSONRedisDefaultHash(unittest.TestCase): hash = RedisDefaultHash( "json_hash", prefix="rs:unit_tests:", serialize=True) is_str = False def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.addCleanup(self.hash.clear) def cast(self, obj): return str(obj) if self.is_str else obj def reset(self, count=10, type=int): self.hash.clear() self.data = RandData(type).dict(count, 1) self.data_len = len(self.data) self.hash.update(self.data) def test_prefix(self): self.assertEqual(self.hash.prefix, 'rs:unit_tests') self.assertEqual(self.hash.name, 'json_hash') self.assertEqual(self.hash.key_prefix, 'rs:unit_tests:json_hash') def test_incr_decr(self): self.reset() self.hash.incr('views', 1) self.assertEqual(self.hash['views'], self.cast(1)) self.hash.incr('views', 3) self.assertEqual(self.hash['views'], self.cast(4)) self.hash.decr('views', 1) self.assertEqual(self.hash['views'], self.cast(3)) def test_get(self): self.reset() self.hash["hello"] = "world" self.assertEqual(self.hash.get("hello"), 'world') self.assertEqual(self.hash.get('world', 'hello'), 'hello') self.assertEqual(self.hash['world'], {}) def test_items(self): self.reset() self.assertDictEqual( {k: v for k, v in self.hash.items()}, {k: self.cast(v) for k, v in self.data.items()}) def test_values(self): self.reset() self.assertSetEqual( set(self.hash.values()), set(map(self.cast, self.data.values()))) def test_iter(self): self.reset() self.assertSetEqual( set(k for k in self.hash.iter()), set(self.cast(k) for k in self.data.keys())) def test_iter_match(self): self.reset(count=10) self.assertSetEqual( set(k for k in self.hash.iter("a*")), set(self.cast(k) for k in self.data.keys() if k.startswith('a'))) def test_mget(self): self.reset(0) self.hash.update({ 'test1': 1, 'test2': 2, 'test3': 3, 'test4': 4, 'test5': 5}) self.assertListEqual( self.hash.mget('test2', 'test3', 'test4'), [self.cast(2), self.cast(3), self.cast(4)]) def test_pop(self): self.reset() self.hash['hello'] = 'world' self.assertEqual(self.hash.pop('hello'), 'world') self.assertNotIn('hello', self.hash) def test_delete(self): self.reset() self.hash['hello'] = 'world' self.assertEqual(self.hash['hello'], 'world') del self.hash['hello'] self.assertNotIn('hello', self.hash) def test_scan(self): self.reset() new_keys = [] cursor = '0' while cursor: cursor, keys = self.hash.scan(count=1, cursor=int(cursor)) if keys: new_keys.extend(keys) self.assertSetEqual( set(k for k in self.data.keys()), set(new_keys)) def test_set(self): self.reset() self.hash.set("hello", "world") self.assertIn("hello", self.hash) def test_len(self): self.reset(100) self.assertEqual(len(self.hash), self.data_len) self.reset(1000) self.assertEqual(len(self.hash), self.data_len) rem = [k for k in list(self.hash)[:250]] self.hash.remove(*rem) self.assertEqual(len(self.hash), self.data_len - len(rem)) def test_pttl(self): self.reset() self.hash.set_pttl(1000) self.assertGreater(self.hash.pttl(), 300) time.sleep(1.10) self.assertEqual(len(self.hash), 0) def test_ttl(self): self.reset() self.hash.set_ttl(1) self.assertGreater(self.hash.ttl(), 0.30) time.sleep(1.10) self.assertEqual(len(self.hash), 0) def test_ttl(self): self.reset() self.hash.set_ttl(1) self.assertGreater(self.hash.ttl(), 0.30) time.sleep(1.10) self.assertEqual(len(self.hash), 0) def test_expire_at(self): self.reset() expire_at = datetime.datetime.now() + datetime.timedelta(seconds=1) self.hash.expire_at(expire_at.timestamp()) self.assertGreater(self.hash.ttl(), 0.30) time.sleep(2) self.assertEqual(len(self.hash), 0) def test_pexpire_at(self): self.reset() expire_at = datetime.datetime.now() + datetime.timedelta(seconds=1) self.hash.pexpire_at(expire_at.timestamp() * 1000) self.assertGreater(self.hash.pttl(), 300) time.sleep(2) self.assertEqual(len(self.hash), 0)
def test_pop(self): self.list.clear() data = RandData(str).list(10) self.list.extend(data) self.assertEqual(self.list.pop(9), data[9]) self.assertNotIn(data[9], self.list)
def test_all(self): self.list.clear() data = RandData(str).list(10) self.list.extend(data) self.assertListEqual(self.list.all, data)
def reset(self, count=10, type=int): self.hash.clear() self.data = RandData(type).dict(count, 1) self.data_len = len(self.data) self.hash.update(self.data)
def reset(self, count=10, type=int): self.map.clear() self.data = RandData(type).dict(count, 1) self.data_count = count self.map.update(self.data)
def test_push(self): self.list.clear() data = RandData(str).list(10) self.list.extend(data) self.list.push(10) self.assertEqual(self.list[0], self.cast(10))
def test_trim(self): self.list.clear() data = RandData(str).list(10) self.list.extend(data) self.list.trim(2, 7) self.assertListEqual(self.list.all, data[2:8])
def test_reversed(self): self.list.clear() data = RandData(str).list(10) self.list.extend(data) self.assertListEqual([x for x in reversed(self.list)], [x for x in reversed(data)])