Page MenuHomePhabricator (Chris)

redis_spec.lua
No OneTemporary

Authored By
Unknown
Size
10 KB
Referenced Files
None
Subscribers
None

redis_spec.lua

--[[
Tests for Redis module using mock_redis.
Tests basic operations, scan vs keys, list ops, hash ops, set ops.
]]
describe('core.redis (mock)', function()
local mock_redis = require('spec.helpers.mock_redis')
local redis
before_each(function()
redis = mock_redis.new()
end)
after_each(function()
redis.reset()
end)
describe('string operations', function()
it('should set and get a value', function()
redis.set('key1', 'value1')
assert.are.equal('value1', redis.get('key1'))
end)
it('should return nil for non-existent key', function()
assert.is_nil(redis.get('nonexistent'))
end)
it('should overwrite existing values', function()
redis.set('key1', 'v1')
redis.set('key1', 'v2')
assert.are.equal('v2', redis.get('key1'))
end)
it('should set with expiry via setex', function()
redis.setex('key1', 60, 'value1')
assert.are.equal('value1', redis.get('key1'))
assert.are.equal(60, redis.ttls['key1'])
end)
it('should setnx only when key does not exist', function()
local r1 = redis.setnx('key1', 'first')
assert.are.equal(1, r1)
assert.are.equal('first', redis.get('key1'))
local r2 = redis.setnx('key1', 'second')
assert.are.equal(0, r2)
assert.are.equal('first', redis.get('key1'))
end)
it('should convert values to strings on set', function()
redis.set('num', 42)
assert.are.equal('42', redis.get('num'))
end)
end)
describe('del()', function()
it('should delete a key', function()
redis.set('key1', 'value1')
redis.del('key1')
assert.is_nil(redis.get('key1'))
end)
it('should not error when deleting non-existent key', function()
assert.has_no.errors(function()
redis.del('nonexistent')
end)
end)
it('should also delete sets and hashes', function()
redis.sadd('myset', 'val')
redis.hset('myhash', 'field', 'val')
redis.del('myset')
redis.del('myhash')
assert.are.same({}, redis.smembers('myset'))
assert.are.same({}, redis.hgetall('myhash'))
end)
end)
describe('exists()', function()
it('should return 1 for existing key', function()
redis.set('key1', 'value1')
assert.are.equal(1, redis.exists('key1'))
end)
it('should return 0 for non-existent key', function()
assert.are.equal(0, redis.exists('nonexistent'))
end)
end)
describe('expire()', function()
it('should record TTL for a key', function()
redis.set('key1', 'value1')
redis.expire('key1', 300)
assert.are.equal(300, redis.ttls['key1'])
end)
end)
describe('incr / incrby', function()
it('should increment from 0 for new key', function()
local val = redis.incr('counter')
assert.are.equal(1, val)
end)
it('should increment existing value', function()
redis.set('counter', '5')
local val = redis.incr('counter')
assert.are.equal(6, val)
end)
it('should increment by arbitrary amount', function()
local val = redis.incrby('counter', 10)
assert.are.equal(10, val)
val = redis.incrby('counter', 5)
assert.are.equal(15, val)
end)
it('should handle negative incrby', function()
redis.set('counter', '10')
local val = redis.incrby('counter', -3)
assert.are.equal(7, val)
end)
end)
describe('hash operations', function()
it('should set and get hash fields', function()
redis.hset('user:1', 'name', 'Alice')
assert.are.equal('Alice', redis.hget('user:1', 'name'))
end)
it('should return nil for non-existent hash field', function()
assert.is_nil(redis.hget('user:1', 'name'))
end)
it('should return nil for non-existent hash key', function()
redis.hset('user:1', 'name', 'Alice')
assert.is_nil(redis.hget('user:1', 'email'))
end)
it('should delete hash fields', function()
redis.hset('user:1', 'name', 'Alice')
redis.hdel('user:1', 'name')
assert.is_nil(redis.hget('user:1', 'name'))
end)
it('should return all hash fields', function()
redis.hset('user:1', 'name', 'Alice')
redis.hset('user:1', 'age', '30')
local all = redis.hgetall('user:1')
assert.are.equal('Alice', all['name'])
assert.are.equal('30', all['age'])
end)
it('should return empty table for non-existent hash', function()
local all = redis.hgetall('nonexistent')
assert.are.same({}, all)
end)
it('should check field existence', function()
redis.hset('user:1', 'name', 'Alice')
assert.is_true(redis.hexists('user:1', 'name'))
assert.is_falsy(redis.hexists('user:1', 'email'))
end)
it('should increment hash field', function()
local val = redis.hincrby('user:1', 'count', 1)
assert.are.equal(1, val)
val = redis.hincrby('user:1', 'count', 5)
assert.are.equal(6, val)
end)
it('should convert hash values to strings', function()
redis.hset('h', 'num', 42)
assert.are.equal('42', redis.hget('h', 'num'))
end)
end)
describe('set operations', function()
it('should add and check membership', function()
redis.sadd('myset', 'a')
assert.are.equal(1, redis.sismember('myset', 'a'))
assert.are.equal(0, redis.sismember('myset', 'b'))
end)
it('should remove members', function()
redis.sadd('myset', 'a')
redis.srem('myset', 'a')
assert.are.equal(0, redis.sismember('myset', 'a'))
end)
it('should return all members', function()
redis.sadd('myset', 'a')
redis.sadd('myset', 'b')
redis.sadd('myset', 'c')
local members = redis.smembers('myset')
assert.are.equal(3, #members)
end)
it('should return empty table for non-existent set', function()
local members = redis.smembers('nonexistent')
assert.are.same({}, members)
end)
it('should not duplicate members', function()
redis.sadd('myset', 'a')
redis.sadd('myset', 'a')
local members = redis.smembers('myset')
assert.are.equal(1, #members)
end)
it('should count members with scard', function()
redis.sadd('myset', 'a')
redis.sadd('myset', 'b')
assert.are.equal(2, redis.scard('myset'))
end)
end)
describe('list operations', function()
it('should push and retrieve list items', function()
redis.rpush('mylist', 'a')
redis.rpush('mylist', 'b')
redis.rpush('mylist', 'c')
local items = redis.lrange('mylist', 0, -1)
assert.are.equal(3, #items)
assert.are.equal('a', items[1])
assert.are.equal('b', items[2])
assert.are.equal('c', items[3])
end)
it('should return empty list for non-existent key', function()
local items = redis.lrange('nonexistent', 0, -1)
assert.are.same({}, items)
end)
it('should support partial range', function()
redis.rpush('mylist', 'a')
redis.rpush('mylist', 'b')
redis.rpush('mylist', 'c')
local items = redis.lrange('mylist', 0, 1)
assert.are.equal(2, #items)
assert.are.equal('a', items[1])
assert.are.equal('b', items[2])
end)
end)
describe('scan / keys', function()
it('should find keys matching a pattern', function()
redis.set('stats:msg:100:2024-01-01:111', '5')
redis.set('stats:msg:100:2024-01-01:222', '3')
redis.set('stats:cmd:ping:100:2024-01-01', '2')
local msg_keys = redis.scan('stats:msg:*')
assert.are.equal(2, #msg_keys)
end)
it('should return empty table when no keys match', function()
redis.set('foo', 'bar')
local results = redis.scan('baz:*')
assert.are.same({}, results)
end)
it('keys() should delegate to scan()', function()
redis.set('test:1', 'a')
redis.set('test:2', 'b')
local results = redis.keys('test:*')
assert.are.equal(2, #results)
end)
end)
describe('command recording', function()
it('should record all commands issued', function()
redis.set('k', 'v')
redis.get('k')
redis.del('k')
assert.are.equal(3, #redis.commands)
assert.are.equal('set', redis.commands[1].cmd)
assert.are.equal('get', redis.commands[2].cmd)
assert.are.equal('del', redis.commands[3].cmd)
end)
it('should track command arguments', function()
redis.setex('key', 60, 'val')
assert.are.same({ 'key', 60, 'val' }, redis.commands[1].args)
end)
it('has_command should find recorded commands', function()
redis.set('k', 'v')
assert.is_true(redis.has_command('set'))
assert.is_false(redis.has_command('del'))
end)
it('count_commands should count occurrences', function()
redis.get('a')
redis.get('b')
redis.set('c', 'd')
assert.are.equal(2, redis.count_commands('get'))
assert.are.equal(1, redis.count_commands('set'))
end)
end)
describe('reset()', function()
it('should clear all state', function()
redis.set('k', 'v')
redis.sadd('s', 'v')
redis.hset('h', 'f', 'v')
redis.reset()
assert.are.same({}, redis.store)
assert.are.same({}, redis.sets)
assert.are.same({}, redis.hashes)
assert.are.same({}, redis.commands)
end)
end)
end)

File Metadata

Mime Type
application/javascript
Expires
Sat, Nov 14, 10:12 PM (13 h, 18 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
132531
Default Alt Text
redis_spec.lua (10 KB)

Event Timeline