fakeredis 0.5.0 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.gitignore +4 -0
- data/.travis.yml +16 -5
- data/LICENSE +1 -1
- data/README.md +18 -2
- data/fakeredis.gemspec +1 -1
- data/gemfiles/redisrb-master.gemfile +14 -0
- data/lib/fakeredis/bitop_command.rb +56 -0
- data/lib/fakeredis/expiring_hash.rb +1 -1
- data/lib/fakeredis/minitest.rb +24 -0
- data/lib/fakeredis/rspec.rb +1 -0
- data/lib/fakeredis/sort_method.rb +3 -2
- data/lib/fakeredis/transaction_commands.rb +1 -1
- data/lib/fakeredis/version.rb +1 -1
- data/lib/fakeredis/zset.rb +7 -1
- data/lib/redis/connection/memory.rb +424 -38
- data/spec/bitop_command_spec.rb +209 -0
- data/spec/compatibility_spec.rb +1 -1
- data/spec/connection_spec.rb +20 -20
- data/spec/hashes_spec.rb +123 -57
- data/spec/keys_spec.rb +197 -80
- data/spec/lists_spec.rb +61 -34
- data/spec/memory_spec.rb +60 -7
- data/spec/server_spec.rb +24 -24
- data/spec/sets_spec.rb +95 -46
- data/spec/sort_method_spec.rb +6 -0
- data/spec/sorted_sets_spec.rb +288 -150
- data/spec/spec_helper.rb +1 -0
- data/spec/strings_spec.rb +83 -78
- data/spec/subscription_spec.rb +107 -0
- data/spec/support/shared_examples/bitwise_operation.rb +59 -0
- data/spec/support/shared_examples/sortable.rb +20 -16
- data/spec/transactions_spec.rb +20 -12
- data/spec/upcase_method_name_spec.rb +2 -2
- metadata +14 -5
@@ -0,0 +1,209 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module FakeRedis
|
4
|
+
describe "#bitop" do
|
5
|
+
before(:all) do
|
6
|
+
@client = Redis.new
|
7
|
+
end
|
8
|
+
|
9
|
+
before(:each) do
|
10
|
+
@client.discard rescue nil
|
11
|
+
end
|
12
|
+
|
13
|
+
it 'raises an argument error when passed unsupported operation' do
|
14
|
+
lambda { @client.bitop("meh", "dest1", "key1") }.should raise_error(Redis::CommandError)
|
15
|
+
end
|
16
|
+
|
17
|
+
describe "or" do
|
18
|
+
it_should_behave_like "a bitwise operation", "or"
|
19
|
+
|
20
|
+
it "should apply bitwise or operation" do
|
21
|
+
@client.setbit("key1", 0, 0)
|
22
|
+
@client.setbit("key1", 1, 1)
|
23
|
+
@client.setbit("key1", 2, 1)
|
24
|
+
@client.setbit("key1", 3, 0)
|
25
|
+
|
26
|
+
@client.setbit("key2", 0, 1)
|
27
|
+
@client.setbit("key2", 1, 1)
|
28
|
+
@client.setbit("key2", 2, 0)
|
29
|
+
@client.setbit("key2", 3, 0)
|
30
|
+
|
31
|
+
@client.bitop("or", "dest1", "key1", "key2").should be == 1
|
32
|
+
@client.bitcount("dest1").should be == 3
|
33
|
+
@client.getbit("dest1", 0).should be == 1
|
34
|
+
@client.getbit("dest1", 1).should be == 1
|
35
|
+
@client.getbit("dest1", 2).should be == 1
|
36
|
+
@client.getbit("dest1", 3).should be == 0
|
37
|
+
end
|
38
|
+
|
39
|
+
it "should apply bitwise or operation with empty values" do
|
40
|
+
@client.setbit("key1", 1, 1)
|
41
|
+
|
42
|
+
@client.bitop("or", "dest1", "key1", "nothing_here1", "nothing_here2").should be == 1
|
43
|
+
@client.bitcount("dest1").should be == 1
|
44
|
+
@client.getbit("dest1", 0).should be == 0
|
45
|
+
@client.getbit("dest1", 1).should be == 1
|
46
|
+
@client.getbit("dest1", 2).should be == 0
|
47
|
+
end
|
48
|
+
|
49
|
+
it "should apply bitwise or operation with multiple keys" do
|
50
|
+
@client.setbit("key1", 1, 1)
|
51
|
+
@client.setbit("key1", 3, 1)
|
52
|
+
|
53
|
+
@client.setbit("key2", 5, 1)
|
54
|
+
@client.setbit("key2", 10, 1)
|
55
|
+
|
56
|
+
@client.setbit("key3", 13, 1)
|
57
|
+
@client.setbit("key3", 15, 1)
|
58
|
+
|
59
|
+
@client.bitop("or", "dest1", "key1", "key2", "key3").should be == 2
|
60
|
+
@client.bitcount("dest1").should be == 6
|
61
|
+
@client.getbit("dest1", 1).should be == 1
|
62
|
+
@client.getbit("dest1", 3).should be == 1
|
63
|
+
@client.getbit("dest1", 5).should be == 1
|
64
|
+
@client.getbit("dest1", 10).should be == 1
|
65
|
+
@client.getbit("dest1", 13).should be == 1
|
66
|
+
@client.getbit("dest1", 15).should be == 1
|
67
|
+
@client.getbit("dest1", 2).should be == 0
|
68
|
+
@client.getbit("dest1", 12).should be == 0
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
describe "and" do
|
73
|
+
it_should_behave_like "a bitwise operation", "and"
|
74
|
+
|
75
|
+
it "should apply bitwise and operation" do
|
76
|
+
@client.setbit("key1", 0, 1)
|
77
|
+
@client.setbit("key1", 1, 1)
|
78
|
+
@client.setbit("key1", 2, 0)
|
79
|
+
|
80
|
+
@client.setbit("key2", 0, 0)
|
81
|
+
@client.setbit("key2", 1, 1)
|
82
|
+
@client.setbit("key2", 2, 1)
|
83
|
+
|
84
|
+
@client.bitop("and", "dest1", "key1", "key2").should be == 1
|
85
|
+
@client.bitcount("dest1").should be == 1
|
86
|
+
@client.getbit("dest1", 0).should be == 0
|
87
|
+
@client.getbit("dest1", 1).should be == 1
|
88
|
+
@client.getbit("dest1", 2).should be == 0
|
89
|
+
end
|
90
|
+
|
91
|
+
it "should apply bitwise and operation with empty values" do
|
92
|
+
@client.setbit("key1", 1, 1)
|
93
|
+
|
94
|
+
@client.bitop("and", "dest1", "key1", "nothing_here").should be == 1
|
95
|
+
@client.bitcount("dest1").should be == 1
|
96
|
+
@client.getbit("dest1", 0).should be == 0
|
97
|
+
@client.getbit("dest1", 1).should be == 1
|
98
|
+
@client.getbit("dest1", 2).should be == 0
|
99
|
+
end
|
100
|
+
|
101
|
+
it "should apply bitwise and operation with multiple keys" do
|
102
|
+
@client.setbit("key1", 1, 1)
|
103
|
+
@client.setbit("key1", 2, 1)
|
104
|
+
@client.setbit("key1", 3, 1)
|
105
|
+
@client.setbit("key1", 4, 1)
|
106
|
+
|
107
|
+
@client.setbit("key2", 2, 1)
|
108
|
+
@client.setbit("key2", 3, 1)
|
109
|
+
@client.setbit("key2", 4, 1)
|
110
|
+
@client.setbit("key2", 5, 1)
|
111
|
+
|
112
|
+
@client.setbit("key3", 2, 1)
|
113
|
+
@client.setbit("key3", 4, 1)
|
114
|
+
@client.setbit("key3", 5, 1)
|
115
|
+
@client.setbit("key3", 6, 1)
|
116
|
+
|
117
|
+
@client.bitop("and", "dest1", "key1", "key2", "key3").should be == 1
|
118
|
+
@client.bitcount("dest1").should be == 2
|
119
|
+
@client.getbit("dest1", 1).should be == 0
|
120
|
+
@client.getbit("dest1", 2).should be == 1
|
121
|
+
@client.getbit("dest1", 3).should be == 0
|
122
|
+
@client.getbit("dest1", 4).should be == 1
|
123
|
+
@client.getbit("dest1", 5).should be == 0
|
124
|
+
@client.getbit("dest1", 6).should be == 0
|
125
|
+
end
|
126
|
+
end
|
127
|
+
|
128
|
+
describe "xor" do
|
129
|
+
it_should_behave_like "a bitwise operation", "xor"
|
130
|
+
|
131
|
+
it "should apply bitwise xor operation" do
|
132
|
+
@client.setbit("key1", 0, 0)
|
133
|
+
@client.setbit("key1", 1, 1)
|
134
|
+
@client.setbit("key1", 2, 0)
|
135
|
+
@client.setbit("key1", 3, 0)
|
136
|
+
|
137
|
+
@client.setbit("key2", 0, 1)
|
138
|
+
@client.setbit("key2", 1, 1)
|
139
|
+
@client.setbit("key2", 2, 1)
|
140
|
+
@client.setbit("key2", 3, 0)
|
141
|
+
|
142
|
+
@client.bitop("xor", "dest1", "key1", "key2").should be == 1
|
143
|
+
@client.bitcount("dest1").should be == 2
|
144
|
+
@client.getbit("dest1", 0).should be == 1
|
145
|
+
@client.getbit("dest1", 1).should be == 0
|
146
|
+
@client.getbit("dest1", 2).should be == 1
|
147
|
+
@client.getbit("dest1", 3).should be == 0
|
148
|
+
end
|
149
|
+
|
150
|
+
it "should apply bitwise xor operation with empty values" do
|
151
|
+
@client.setbit("key1", 1, 1)
|
152
|
+
|
153
|
+
@client.bitop("xor", "dest1", "key1", "nothing_here1", "nothing_here2").should be == 1
|
154
|
+
@client.bitcount("dest1").should be == 1
|
155
|
+
@client.getbit("dest1", 0).should be == 0
|
156
|
+
@client.getbit("dest1", 1).should be == 1
|
157
|
+
@client.getbit("dest1", 2).should be == 0
|
158
|
+
end
|
159
|
+
|
160
|
+
it "should apply bitwise xor operation with multiple keys" do
|
161
|
+
@client.setbit("key1", 1, 1)
|
162
|
+
@client.setbit("key1", 3, 1)
|
163
|
+
@client.setbit("key1", 5, 1)
|
164
|
+
@client.setbit("key1", 6, 1)
|
165
|
+
|
166
|
+
@client.setbit("key2", 2, 1)
|
167
|
+
@client.setbit("key2", 3, 1)
|
168
|
+
@client.setbit("key2", 4, 1)
|
169
|
+
@client.setbit("key2", 6, 1)
|
170
|
+
|
171
|
+
@client.bitop("xor", "dest1", "key1", "key2").should be == 1
|
172
|
+
@client.bitcount("dest1").should be == 4
|
173
|
+
@client.getbit("dest1", 1).should be == 1
|
174
|
+
@client.getbit("dest1", 2).should be == 1
|
175
|
+
@client.getbit("dest1", 3).should be == 0
|
176
|
+
@client.getbit("dest1", 4).should be == 1
|
177
|
+
@client.getbit("dest1", 5).should be == 1
|
178
|
+
@client.getbit("dest1", 6).should be == 0
|
179
|
+
end
|
180
|
+
end
|
181
|
+
|
182
|
+
describe "not" do
|
183
|
+
it 'raises an argument error when not passed any keys' do
|
184
|
+
lambda { @client.bitop("not", "destkey") }.should raise_error(Redis::CommandError)
|
185
|
+
end
|
186
|
+
|
187
|
+
it 'raises an argument error when not passed too many keys' do
|
188
|
+
lambda { @client.bitop("not", "destkey", "key1", "key2") }.should raise_error(Redis::CommandError)
|
189
|
+
end
|
190
|
+
|
191
|
+
it "should apply bitwise negation operation" do
|
192
|
+
@client.setbit("key1", 1, 1)
|
193
|
+
@client.setbit("key1", 3, 1)
|
194
|
+
@client.setbit("key1", 5, 1)
|
195
|
+
|
196
|
+
@client.bitop("not", "dest1", "key1").should be == 1
|
197
|
+
@client.bitcount("dest1").should be == 5
|
198
|
+
@client.getbit("dest1", 0).should be == 1
|
199
|
+
@client.getbit("dest1", 1).should be == 0
|
200
|
+
@client.getbit("dest1", 2).should be == 1
|
201
|
+
@client.getbit("dest1", 3).should be == 0
|
202
|
+
@client.getbit("dest1", 4).should be == 1
|
203
|
+
@client.getbit("dest1", 5).should be == 0
|
204
|
+
@client.getbit("dest1", 6).should be == 1
|
205
|
+
@client.getbit("dest1", 7).should be == 1
|
206
|
+
end
|
207
|
+
end
|
208
|
+
end
|
209
|
+
end
|
data/spec/compatibility_spec.rb
CHANGED
data/spec/connection_spec.rb
CHANGED
@@ -9,7 +9,7 @@ module FakeRedis
|
|
9
9
|
|
10
10
|
if fakeredis?
|
11
11
|
it "should authenticate to the server" do
|
12
|
-
@client.auth("pass").
|
12
|
+
expect(@client.auth("pass")).to eq("OK")
|
13
13
|
end
|
14
14
|
|
15
15
|
it "should re-use the same instance with the same host & port" do
|
@@ -18,40 +18,40 @@ module FakeRedis
|
|
18
18
|
@client3 = Redis.new(:host => "localhost", :port => 5678)
|
19
19
|
|
20
20
|
@client1.set("key1", "1")
|
21
|
-
@client2.get("key1").
|
22
|
-
@client3.get("key1").
|
21
|
+
expect(@client2.get("key1")).to eq("1")
|
22
|
+
expect(@client3.get("key1")).to be_nil
|
23
23
|
|
24
24
|
@client2.set("key2", "2")
|
25
|
-
@client1.get("key2").
|
26
|
-
@client3.get("key2").
|
25
|
+
expect(@client1.get("key2")).to eq("2")
|
26
|
+
expect(@client3.get("key2")).to be_nil
|
27
27
|
|
28
28
|
@client3.set("key3", "3")
|
29
|
-
@client1.get("key3").
|
30
|
-
@client2.get("key3").
|
29
|
+
expect(@client1.get("key3")).to be_nil
|
30
|
+
expect(@client2.get("key3")).to be_nil
|
31
31
|
|
32
|
-
@client1.dbsize.
|
33
|
-
@client2.dbsize.
|
34
|
-
@client3.dbsize.
|
32
|
+
expect(@client1.dbsize).to eq(2)
|
33
|
+
expect(@client2.dbsize).to eq(2)
|
34
|
+
expect(@client3.dbsize).to eq(1)
|
35
35
|
end
|
36
36
|
|
37
37
|
it "should connect to a specific database" do
|
38
38
|
@client1 = Redis.new(:host => "localhost", :port => 1234, :db => 0)
|
39
39
|
@client1.set("key1", "1")
|
40
40
|
@client1.select(0)
|
41
|
-
@client1.get("key1").
|
41
|
+
expect(@client1.get("key1")).to eq("1")
|
42
42
|
|
43
43
|
@client2 = Redis.new(:host => "localhost", :port => 1234, :db => 1)
|
44
44
|
@client2.set("key1", "1")
|
45
45
|
@client2.select(1)
|
46
|
-
@client2.get("key1").
|
46
|
+
expect(@client2.get("key1")).to eq("1")
|
47
47
|
end
|
48
48
|
|
49
49
|
it "should not error with shutdown" do
|
50
|
-
|
50
|
+
expect { @client.shutdown }.not_to raise_error
|
51
51
|
end
|
52
52
|
|
53
53
|
it "should not error with quit" do
|
54
|
-
|
54
|
+
expect { @client.quit }.not_to raise_error
|
55
55
|
end
|
56
56
|
end
|
57
57
|
|
@@ -60,26 +60,26 @@ module FakeRedis
|
|
60
60
|
@client2 = Redis.new(:host => "localhost", :port => 6379, :db => 2)
|
61
61
|
|
62
62
|
@client1.set("key1", "one")
|
63
|
-
@client1.get("key1").
|
63
|
+
expect(@client1.get("key1")).to eq("one")
|
64
64
|
|
65
65
|
@client2.set("key2", "two")
|
66
|
-
@client2.get("key2").
|
66
|
+
expect(@client2.get("key2")).to eq("two")
|
67
67
|
|
68
|
-
@client1.get("key1").
|
68
|
+
expect(@client1.get("key1")).to eq("one")
|
69
69
|
end
|
70
70
|
|
71
71
|
it "should not error with a disconnected client" do
|
72
72
|
@client1 = Redis.new
|
73
73
|
@client1.client.disconnect
|
74
|
-
@client1.get("key1").
|
74
|
+
expect(@client1.get("key1")).to be_nil
|
75
75
|
end
|
76
76
|
|
77
77
|
it "should echo the given string" do
|
78
|
-
@client.echo("something").
|
78
|
+
expect(@client.echo("something")).to eq("something")
|
79
79
|
end
|
80
80
|
|
81
81
|
it "should ping the server" do
|
82
|
-
@client.ping.
|
82
|
+
expect(@client.ping).to eq("PONG")
|
83
83
|
end
|
84
84
|
end
|
85
85
|
end
|
data/spec/hashes_spec.rb
CHANGED
@@ -9,168 +9,179 @@ module FakeRedis
|
|
9
9
|
it "should delete a hash field" do
|
10
10
|
@client.hset("key1", "k1", "val1")
|
11
11
|
@client.hset("key1", "k2", "val2")
|
12
|
-
@client.hdel("key1", "k1").
|
12
|
+
expect(@client.hdel("key1", "k1")).to be(1)
|
13
13
|
|
14
|
-
@client.hget("key1", "k1").
|
15
|
-
@client.hget("key1", "k2").
|
14
|
+
expect(@client.hget("key1", "k1")).to be_nil
|
15
|
+
expect(@client.hget("key1", "k2")).to eq("val2")
|
16
|
+
end
|
17
|
+
|
18
|
+
it "should delete array of fields" do
|
19
|
+
@client.hset("key1", "k1", "val1")
|
20
|
+
@client.hset("key1", "k2", "val2")
|
21
|
+
@client.hset("key1", "k3", "val3")
|
22
|
+
expect(@client.hdel("key1", ["k1", "k2"])).to be(2)
|
23
|
+
|
24
|
+
expect(@client.hget("key1", "k1")).to be_nil
|
25
|
+
expect(@client.hget("key1", "k2")).to be_nil
|
26
|
+
expect(@client.hget("key1", "k3")).to eq("val3")
|
16
27
|
end
|
17
28
|
|
18
29
|
it "should remove a hash with no keys left" do
|
19
30
|
@client.hset("key1", "k1", "val1")
|
20
31
|
@client.hset("key1", "k2", "val2")
|
21
|
-
@client.hdel("key1", "k1").
|
22
|
-
@client.hdel("key1", "k2").
|
32
|
+
expect(@client.hdel("key1", "k1")).to be(1)
|
33
|
+
expect(@client.hdel("key1", "k2")).to be(1)
|
23
34
|
|
24
|
-
@client.exists("key1").
|
35
|
+
expect(@client.exists("key1")).to eq(false)
|
25
36
|
end
|
26
37
|
|
27
38
|
it "should convert key to a string for hset" do
|
28
39
|
m = double("key")
|
29
|
-
m.
|
40
|
+
allow(m).to receive(:to_s).and_return("foo")
|
30
41
|
|
31
42
|
@client.hset("key1", m, "bar")
|
32
|
-
@client.hget("key1", "foo").
|
43
|
+
expect(@client.hget("key1", "foo")).to eq("bar")
|
33
44
|
end
|
34
45
|
|
35
46
|
it "should convert key to a string for hget" do
|
36
47
|
m = double("key")
|
37
|
-
m.
|
48
|
+
allow(m).to receive(:to_s).and_return("foo")
|
38
49
|
|
39
50
|
@client.hset("key1", "foo", "bar")
|
40
|
-
@client.hget("key1", m).
|
51
|
+
expect(@client.hget("key1", m)).to eq("bar")
|
41
52
|
end
|
42
53
|
|
43
54
|
it "should determine if a hash field exists" do
|
44
55
|
@client.hset("key1", "index", "value")
|
45
56
|
|
46
|
-
@client.hexists("key1", "index").
|
47
|
-
@client.hexists("key2", "i2").
|
57
|
+
expect(@client.hexists("key1", "index")).to be true
|
58
|
+
expect(@client.hexists("key2", "i2")).to be false
|
48
59
|
end
|
49
60
|
|
50
61
|
it "should get the value of a hash field" do
|
51
62
|
@client.hset("key1", "index", "value")
|
52
63
|
|
53
|
-
@client.hget("key1", "index").
|
64
|
+
expect(@client.hget("key1", "index")).to eq("value")
|
54
65
|
end
|
55
66
|
|
56
67
|
it "should get all the fields and values in a hash" do
|
57
68
|
@client.hset("key1", "i1", "val1")
|
58
69
|
@client.hset("key1", "i2", "val2")
|
59
70
|
|
60
|
-
@client.hgetall("key1").
|
71
|
+
expect(@client.hgetall("key1")).to eq({"i1" => "val1", "i2" => "val2"})
|
61
72
|
end
|
62
73
|
|
63
74
|
it "should increment the integer value of a hash field by the given number" do
|
64
75
|
@client.hset("key1", "cont1", "5")
|
65
|
-
@client.hincrby("key1", "cont1", "5").
|
66
|
-
@client.hget("key1", "cont1").
|
76
|
+
expect(@client.hincrby("key1", "cont1", "5")).to eq(10)
|
77
|
+
expect(@client.hget("key1", "cont1")).to eq("10")
|
67
78
|
end
|
68
79
|
|
69
80
|
it "should increment non existing hash keys" do
|
70
|
-
@client.hget("key1", "cont2").
|
71
|
-
@client.hincrby("key1", "cont2", "5").
|
81
|
+
expect(@client.hget("key1", "cont2")).to be_nil
|
82
|
+
expect(@client.hincrby("key1", "cont2", "5")).to eq(5)
|
72
83
|
end
|
73
84
|
|
74
85
|
it "should increment the float value of a hash field by the given float" do
|
75
86
|
@client.hset("key1", "cont1", 5.0)
|
76
|
-
@client.hincrbyfloat("key1", "cont1", 4.1).
|
77
|
-
@client.hget("key1", "cont1").
|
87
|
+
expect(@client.hincrbyfloat("key1", "cont1", 4.1)).to eq(9.1)
|
88
|
+
expect(@client.hget("key1", "cont1")).to eq("9.1")
|
78
89
|
end
|
79
90
|
|
80
91
|
it "should increment non existing hash keys" do
|
81
|
-
@client.hget("key1", "cont2").
|
82
|
-
@client.hincrbyfloat("key1", "cont2", 5.5).
|
92
|
+
expect(@client.hget("key1", "cont2")).to be_nil
|
93
|
+
expect(@client.hincrbyfloat("key1", "cont2", 5.5)).to eq(5.5)
|
83
94
|
end
|
84
95
|
|
85
96
|
it "should get all the fields in a hash" do
|
86
97
|
@client.hset("key1", "i1", "val1")
|
87
98
|
@client.hset("key1", "i2", "val2")
|
88
99
|
|
89
|
-
@client.hkeys("key1").
|
90
|
-
@client.hkeys("key2").
|
100
|
+
expect(@client.hkeys("key1")).to match_array(["i1", "i2"])
|
101
|
+
expect(@client.hkeys("key2")).to eq([])
|
91
102
|
end
|
92
103
|
|
93
104
|
it "should get the number of fields in a hash" do
|
94
105
|
@client.hset("key1", "i1", "val1")
|
95
106
|
@client.hset("key1", "i2", "val2")
|
96
107
|
|
97
|
-
@client.hlen("key1").
|
108
|
+
expect(@client.hlen("key1")).to eq(2)
|
98
109
|
end
|
99
110
|
|
100
111
|
it "should get the values of all the given hash fields" do
|
101
112
|
@client.hset("key1", "i1", "val1")
|
102
113
|
@client.hset("key1", "i2", "val2")
|
103
114
|
|
104
|
-
@client.hmget("key1", "i1", "i2", "i3").
|
105
|
-
@client.hmget("key1", ["i1", "i2", "i3"]).
|
115
|
+
expect(@client.hmget("key1", "i1", "i2", "i3")).to match_array(["val1", "val2", nil])
|
116
|
+
expect(@client.hmget("key1", ["i1", "i2", "i3"])).to match_array(["val1", "val2", nil])
|
106
117
|
|
107
|
-
@client.hmget("key2", "i1", "i2").
|
118
|
+
expect(@client.hmget("key2", "i1", "i2")).to eq([nil, nil])
|
108
119
|
end
|
109
120
|
|
110
121
|
it "should throw an argument error when you don't ask for any keys" do
|
111
|
-
|
122
|
+
expect { @client.hmget("key1") }.to raise_error(Redis::CommandError)
|
112
123
|
end
|
113
124
|
|
114
125
|
it "should reject an empty list of values" do
|
115
|
-
|
116
|
-
@client.exists("key").
|
126
|
+
expect { @client.hmset("key") }.to raise_error(Redis::CommandError)
|
127
|
+
expect(@client.exists("key")).to be false
|
117
128
|
end
|
118
129
|
|
119
130
|
it "rejects an insert with a key but no value" do
|
120
|
-
|
121
|
-
|
122
|
-
@client.exists("key").
|
131
|
+
expect { @client.hmset("key", 'foo') }.to raise_error(Redis::CommandError)
|
132
|
+
expect { @client.hmset("key", 'foo', 3, 'bar') }.to raise_error(Redis::CommandError)
|
133
|
+
expect(@client.exists("key")).to be false
|
123
134
|
end
|
124
135
|
|
125
136
|
it "should reject the wrong number of arguments" do
|
126
|
-
|
137
|
+
expect { @client.hmset("hash", "foo1", "bar1", "foo2", "bar2", "foo3") }.to raise_error(Redis::CommandError, "ERR wrong number of arguments for HMSET")
|
127
138
|
end
|
128
139
|
|
129
140
|
it "should set multiple hash fields to multiple values" do
|
130
141
|
@client.hmset("key", "k1", "value1", "k2", "value2")
|
131
142
|
|
132
|
-
@client.hget("key", "k1").
|
133
|
-
@client.hget("key", "k2").
|
143
|
+
expect(@client.hget("key", "k1")).to eq("value1")
|
144
|
+
expect(@client.hget("key", "k2")).to eq("value2")
|
134
145
|
end
|
135
146
|
|
136
147
|
it "should set multiple hash fields from a ruby hash to multiple values" do
|
137
148
|
@client.mapped_hmset("foo", :k1 => "value1", :k2 => "value2")
|
138
149
|
|
139
|
-
@client.hget("foo", "k1").
|
140
|
-
@client.hget("foo", "k2").
|
150
|
+
expect(@client.hget("foo", "k1")).to eq("value1")
|
151
|
+
expect(@client.hget("foo", "k2")).to eq("value2")
|
141
152
|
end
|
142
153
|
|
143
154
|
it "should set the string value of a hash field" do
|
144
|
-
@client.hset("key1", "k1", "val1").
|
145
|
-
@client.hset("key1", "k1", "val1").
|
155
|
+
expect(@client.hset("key1", "k1", "val1")).to eq(true)
|
156
|
+
expect(@client.hset("key1", "k1", "val1")).to eq(false)
|
146
157
|
|
147
|
-
@client.hget("key1", "k1").
|
158
|
+
expect(@client.hget("key1", "k1")).to eq("val1")
|
148
159
|
end
|
149
160
|
|
150
161
|
it "should set the value of a hash field, only if the field does not exist" do
|
151
162
|
@client.hset("key1", "k1", "val1")
|
152
|
-
@client.hsetnx("key1", "k1", "value").
|
153
|
-
@client.hsetnx("key1", "k2", "val2").
|
154
|
-
@client.hsetnx("key1", :k1, "value").
|
155
|
-
@client.hsetnx("key1", :k3, "val3").
|
156
|
-
|
157
|
-
@client.hget("key1", "k1").
|
158
|
-
@client.hget("key1", "k2").
|
159
|
-
@client.hget("key1", "k3").
|
163
|
+
expect(@client.hsetnx("key1", "k1", "value")).to eq(false)
|
164
|
+
expect(@client.hsetnx("key1", "k2", "val2")).to eq(true)
|
165
|
+
expect(@client.hsetnx("key1", :k1, "value")).to eq(false)
|
166
|
+
expect(@client.hsetnx("key1", :k3, "val3")).to eq(true)
|
167
|
+
|
168
|
+
expect(@client.hget("key1", "k1")).to eq("val1")
|
169
|
+
expect(@client.hget("key1", "k2")).to eq("val2")
|
170
|
+
expect(@client.hget("key1", "k3")).to eq("val3")
|
160
171
|
end
|
161
172
|
|
162
173
|
it "should get all the values in a hash" do
|
163
174
|
@client.hset("key1", "k1", "val1")
|
164
175
|
@client.hset("key1", "k2", "val2")
|
165
176
|
|
166
|
-
@client.hvals("key1").
|
177
|
+
expect(@client.hvals("key1")).to match_array(["val1", "val2"])
|
167
178
|
end
|
168
179
|
|
169
180
|
it "should accept a list of array pairs as arguments and not throw an invalid argument number error" do
|
170
181
|
@client.hmset("key1", [:k1, "val1"], [:k2, "val2"], [:k3, "val3"])
|
171
|
-
@client.hget("key1", :k1).
|
172
|
-
@client.hget("key1", :k2).
|
173
|
-
@client.hget("key1", :k3).
|
182
|
+
expect(@client.hget("key1", :k1)).to eq("val1")
|
183
|
+
expect(@client.hget("key1", :k2)).to eq("val2")
|
184
|
+
expect(@client.hget("key1", :k3)).to eq("val3")
|
174
185
|
end
|
175
186
|
|
176
187
|
it "should reject a list of arrays that contain an invalid number of arguments" do
|
@@ -179,17 +190,72 @@ module FakeRedis
|
|
179
190
|
|
180
191
|
it "should convert a integer field name to string for hdel" do
|
181
192
|
@client.hset("key1", "1", 1)
|
182
|
-
@client.hdel("key1", 1).
|
193
|
+
expect(@client.hdel("key1", 1)).to be(1)
|
183
194
|
end
|
184
195
|
|
185
196
|
it "should convert a integer field name to string for hexists" do
|
186
197
|
@client.hset("key1", "1", 1)
|
187
|
-
@client.hexists("key1", 1).
|
198
|
+
expect(@client.hexists("key1", 1)).to be true
|
188
199
|
end
|
189
200
|
|
190
201
|
it "should convert a integer field name to string for hincrby" do
|
191
202
|
@client.hset("key1", 1, 0)
|
192
|
-
@client.hincrby("key1", 1, 1).
|
203
|
+
expect(@client.hincrby("key1", 1, 1)).to be(1)
|
204
|
+
end
|
205
|
+
|
206
|
+
describe "#hscan" do
|
207
|
+
it 'with no arguments and few items, returns all items' do
|
208
|
+
@client.hmset('hash', 'name', 'Jack', 'age', '33')
|
209
|
+
result = @client.hscan('hash', 0)
|
210
|
+
|
211
|
+
expect(result[0]).to eq('0')
|
212
|
+
expect(result[1]).to eq([['name', 'Jack'], ['age', '33']])
|
213
|
+
end
|
214
|
+
|
215
|
+
it 'with a count should return that number of members or more' do
|
216
|
+
@client.hmset('hash',
|
217
|
+
'a', '1', 'b', '2', 'c', '3', 'd', '4', 'e', '5', 'f', '6', 'g', '7'
|
218
|
+
)
|
219
|
+
result = @client.hscan('hash', 0, count: 3)
|
220
|
+
expect(result[0]).to eq('3')
|
221
|
+
expect(result[1]).to eq([
|
222
|
+
['a', '1'],
|
223
|
+
['b', '2'],
|
224
|
+
['c', '3'],
|
225
|
+
])
|
226
|
+
end
|
227
|
+
|
228
|
+
it 'returns items starting at the provided cursor' do
|
229
|
+
@client.hmset('hash',
|
230
|
+
'a', '1', 'b', '2', 'c', '3', 'd', '4', 'e', '5', 'f', '6', 'g', '7'
|
231
|
+
)
|
232
|
+
result = @client.hscan('hash', 2, count: 3)
|
233
|
+
expect(result[0]).to eq('5')
|
234
|
+
expect(result[1]).to eq([
|
235
|
+
['c', '3'],
|
236
|
+
['d', '4'],
|
237
|
+
['e', '5']
|
238
|
+
])
|
239
|
+
end
|
240
|
+
|
241
|
+
it 'with match, returns items matching the given pattern' do
|
242
|
+
@client.hmset('hash',
|
243
|
+
'aa', '1', 'b', '2', 'cc', '3', 'd', '4', 'ee', '5', 'f', '6', 'gg', '7'
|
244
|
+
)
|
245
|
+
result = @client.hscan('hash', 2, count: 3, match: '??')
|
246
|
+
expect(result[0]).to eq('5')
|
247
|
+
expect(result[1]).to eq([
|
248
|
+
['cc', '3'],
|
249
|
+
['ee', '5']
|
250
|
+
])
|
251
|
+
end
|
252
|
+
|
253
|
+
it 'returns an empty result if the key is not found' do
|
254
|
+
result = @client.hscan('hash', 0)
|
255
|
+
|
256
|
+
expect(result[0]).to eq('0')
|
257
|
+
expect(result[1]).to eq([])
|
258
|
+
end
|
193
259
|
end
|
194
260
|
end
|
195
261
|
end
|