1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
|
require 'test_helper'
describe "Redis::Store::Namespace" do
def setup
@namespace = "theplaylist"
@store = Redis::Store.new :namespace => @namespace, :serializer => nil
@client = @store.instance_variable_get(:@client)
@rabbit = "bunny"
@default_store = Redis::Store.new
@other_store = Redis::Store.new :namespace => 'other'
end
def teardown
@store.flushdb
@store.quit
@default_store.flushdb
@default_store.quit
@other_store.flushdb
@other_store.quit
end
it "only decorates instances that need to be namespaced" do
store = Redis::Store.new
client = store.instance_variable_get(:@client)
client.expects(:call).with([:get, "rabbit"])
store.get("rabbit")
end
it "doesn't namespace a key which is already namespaced" do
@store.send(:interpolate, "#{@namespace}:rabbit").must_equal("#{@namespace}:rabbit")
end
it "should only delete namespaced keys" do
@default_store.set 'abc', 'cba'
@store.set 'def', 'fed'
@store.flushdb
@store.get('def').must_equal(nil)
@default_store.get('abc').must_equal('cba')
end
it "should not try to delete missing namespaced keys" do
empty_store = Redis::Store.new :namespace => 'empty'
empty_store.flushdb
empty_store.keys.must_be_empty
end
it "should work with dynamic namespace" do
$ns = "ns1"
dyn_store = Redis::Store.new :namespace => -> { $ns }
dyn_store.set 'key', 'x'
$ns = "ns2"
dyn_store.set 'key', 'y'
$ns = "ns3"
dyn_store.set 'key', 'z'
dyn_store.flushdb
r3 = dyn_store.get 'key'
$ns = "ns2"
r2 = dyn_store.get 'key'
$ns = "ns1"
r1 = dyn_store.get 'key'
r1.must_equal('x') && r2.must_equal('y') && r3.must_equal(nil)
end
it "namespaces setex and ttl" do
@store.flushdb
@other_store.flushdb
@store.setex('foo', 30, 'bar')
@store.ttl('foo').must_be_close_to(30)
@store.get('foo').must_equal('bar')
@other_store.ttl('foo').must_equal(-2)
@other_store.get('foo').must_be_nil
end
describe 'method calls' do
let(:store){Redis::Store.new :namespace => @namespace, :serializer => nil}
let(:client){store.instance_variable_get(:@client)}
it "should namespace get" do
client.expects(:call).with([:get, "#{@namespace}:rabbit"]).once
store.get("rabbit")
end
it "should namespace set" do
client.expects(:call).with([:set, "#{@namespace}:rabbit", @rabbit])
store.set "rabbit", @rabbit
end
it "should namespace setnx" do
client.expects(:call).with([:setnx, "#{@namespace}:rabbit", @rabbit])
store.setnx "rabbit", @rabbit
end
it "should namespace del with single key" do
client.expects(:call).with([:del, "#{@namespace}:rabbit"])
store.del "rabbit"
end
it "should namespace del with multiple keys" do
client.expects(:call).with([:del, "#{@namespace}:rabbit", "#{@namespace}:white_rabbit"])
store.del "rabbit", "white_rabbit"
end
it "should namespace keys" do
store.set "rabbit", @rabbit
store.keys("rabb*").must_equal [ "rabbit" ]
end
it "should namespace exists" do
client.expects(:call).with([:exists, "#{@namespace}:rabbit"])
store.exists "rabbit"
end
it "should namespace incrby" do
client.expects(:call).with([:incrby, "#{@namespace}:counter", 1])
store.incrby "counter", 1
end
it "should namespace decrby" do
client.expects(:call).with([:decrby, "#{@namespace}:counter", 1])
store.decrby "counter", 1
end
it "should namespace mget" do
client.expects(:call).with([:mget, "#{@namespace}:rabbit", "#{@namespace}:white_rabbit"])
store.mget "rabbit", "white_rabbit"
end
it "should namespace expire" do
client.expects(:call).with([:expire, "#{@namespace}:rabbit", 60]).once
store.expire("rabbit",60)
end
it "should namespace ttl" do
client.expects(:call).with([:ttl, "#{@namespace}:rabbit"]).once
store.ttl("rabbit")
end
end
end
|