def test_set_item_tags(self): """ Setting item tags """ key1 = 'somekey' data1 = 'some date to put to cache' key2 = 'other' data2 = 'some other data' redis = Redis('test') redis.set(key1, data1) redis.set(key2, data2) tags = ['tag1', 'tag2'] redis.set_tags(key1, tags) redis.set_tags(key2, tags) set1 = redis.get_tagged_items('tag1') set2 = redis.get_tagged_items('tag2') # assert tags created and contain item self.assertTrue(type(set1) is set) self.assertTrue(redis.get_full_item_key(key1) in set1) self.assertTrue(redis.get_full_item_key(key2) in set1) self.assertTrue(type(set2) is set) self.assertTrue(redis.get_full_item_key(key1) in set2) self.assertTrue(redis.get_full_item_key(key2) in set2)
def test_can_set(self): """ Simple item set """ key = 'somekey' data = 'some date to put to cache' redis = Redis('test') redis.set(key, data) full_key = redis.get_full_item_key(key) self.assertIsNotNone(redis.get_redis().hget(full_key, 'data'))
def test_check_item_existence(self): """ Can check item existence in cache """ key = 'somekey' data = 'some date to put to cache' redis = Redis('test') redis.set(key, data) self.assertTrue(redis.exists(key)) self.assertFalse(redis.exists('no-item'))
def test_can_delete_by_key(self): """ Deleting item by key """ key = 'itemkey' data = 'initial item data' redis = Redis('test') redis.set(key, data) self.assertIsNotNone(redis.get(key)) result = redis.delete(key) self.assertTrue(result) self.assertIsNone(redis.get(key))
def test_can_set_with_expiration(self): """ Doing set item with expiration """ key = 'somekey' data = 'some date to put to cache' expire = '+1second' redis = Redis('test') redis.set(key, data, expires_at=expire) full_key = redis.get_full_item_key(key) time.sleep(1.1) self.assertIsNone(redis.get_redis().hget(full_key, 'data'))
def test_can_set_with_ttl(self): """ Doing set item with custom ttl""" ttl = 1 key = 'somekey' data = 'some date to put to cache' redis = Redis('test') redis.set(key, data, ttl=ttl) full_key = redis.get_full_item_key(key) time.sleep(1) self.assertIsNone(redis.get_redis().hget(full_key, 'data'))
def test_can_set_with_tags(self): """ Doing set item with tags """ key = 'somekey' data = 'some date to put to cache' tags = ['tag'] redis = Redis('test') redis.set(key, data, tags=tags) item_tags = redis.get_item_tags(key) tagged_items = redis.get_tagged_items('tag') self.assertIn('tag', item_tags) self.assertIn(redis.get_full_item_key(key), tagged_items)
def test_can_get_by_key(self): """ Getting item by key """ key = 'itemkey' data = 'initial item data' redis = Redis('test') self.assertTrue(redis.set(key, data)) self.assertEqual(data, redis.get(key))
def test_get_item_tags(self): """ Getting item tags """ key = 'somekey' data = 'some date to put to cache' redis = Redis('test') redis.set(key, data) tags = ['tag1', 'tag2'] redis.set_tags(key, tags) # assert item tagged item_tags = redis.get_item_tags(key) self.assertTrue(type(item_tags) is list) self.assertIn('tag1', item_tags) self.assertIn('tag2', item_tags) self.assertIsNone(redis.get_item_tags('no-item'))
def test_can_delete_all(self): """ Deleting all items under namespace """ key1 = 'itemkey' data1 = 'initial item data' key2 = 'itemkey2' data2 = 'some more initial item data' redis = Redis('test') redis.set(key1, data1) redis.set(key2, data2) self.assertIsNotNone(redis.get(key1)) self.assertIsNotNone(redis.get(key2)) redis.delete_all() self.assertIsNone(redis.get(key1)) self.assertIsNone(redis.get(key2))
def test_can_optimize(self): """ Performing storage optimization """ redis = Redis('test') data = 'this data will be used for everything' key1 = 'item1' tags1 = ['tag1', 'tag2', 'tag3'] key2 = 'item2' tags2 = ['tag1', 'tag2', 'tag3'] key3 = 'item3' tags3 = ['tag3', 'tag4', 'tag5'] key4 = 'item4' tags4 = ['tag3', 'tag4', 'tag5'] redis.set(key1, data, ttl=1, tags=tags1) redis.set(key2, data, ttl=1, tags=tags2) redis.set(key3, data, tags=tags3) redis.set(key4, data, tags=tags4) redis.get_redis().delete(redis.get_tag_set_key('tag4')) redis.get_redis().delete(redis.get_tag_set_key('tag5')) time.sleep(1.1) redis.optimize() # missing items should be removed from tags self.assertNotIn(redis.get_full_item_key(key1), redis.get_tagged_items('tag3')) self.assertNotIn(redis.get_full_item_key(key2), redis.get_tagged_items('tag3')) # empty tags should be removed self.assertFalse(redis.get_tagged_items('tag1')) self.assertFalse(redis.get_tagged_items('tag2')) # missing tags should be removed from items self.assertNotIn('tag4', redis.get_item_tags('item3')) self.assertNotIn('tag4', redis.get_item_tags('item4')) self.assertNotIn('tag5', redis.get_item_tags('item3')) self.assertNotIn('tag5', redis.get_item_tags('item4'))
def test_can_delete_by_tags_with_disjunction(self): """ Deleting by tags with disjunction """ key1 = 'itemkey' data1 = 'initial item data' key2 = 'itemkey2' data2 = 'more initial item data' key3 = 'itemkey3' data3 = 'and some more initial item data' tags1 = ['tag1', 'tag2', 'tag3'] tags2 = ['tag3', 'tag4'] redis = Redis('test') redis.set(key1, data1, tags=tags1) redis.set(key2, data2, tags=tags1) redis.set(key3, data3, tags=tags2) redis.delete(tags=tags1, disjunction=True) self.assertIsNone(redis.get(key1)) self.assertIsNone(redis.get(key2)) self.assertIsNone(redis.get(key3))
def test_can_delete_by_tags(self): """ Deleting items by tags """ key1 = 'itemkey' data1 = 'initial item data' key2 = 'itemkey2' data2 = 'more initial item data' key3 = 'itemkey3' data3 = 'and some more initial item data' tags1 = ['tag1', 'tag2'] tags2 = ['tag3', 'tag4'] redis = Redis('test') redis.set(key1, data1, tags=tags1) redis.set(key2, data2, tags=tags1) redis.set(key3, data3, tags=tags2) redis.delete(tags=tags1) self.assertIsNone(redis.get(key1)) self.assertIsNone(redis.get(key2)) self.assertIsNotNone(redis.get(key3))