fakeredis 0.7.0 → 0.9.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 +5 -5
- data/.github/workflows/codeql.yml +74 -0
- data/.github/workflows/ruby.yml +33 -0
- data/.gitignore +0 -1
- data/Gemfile +0 -6
- data/LICENSE +1 -1
- data/README.md +4 -3
- data/fakeredis.gemspec +2 -2
- data/lib/fakeredis/command_executor.rb +0 -6
- data/lib/fakeredis/expiring_hash.rb +2 -4
- data/lib/fakeredis/geo_commands.rb +142 -0
- data/lib/fakeredis/geo_set.rb +84 -0
- data/lib/fakeredis/minitest.rb +1 -1
- data/lib/fakeredis/sort_method.rb +0 -1
- data/lib/fakeredis/version.rb +1 -1
- data/lib/fakeredis/zset.rb +2 -2
- data/lib/fakeredis.rb +16 -0
- data/lib/redis/connection/memory.rb +261 -62
- data/spec/bitop_command_spec.rb +71 -71
- data/spec/fakeredis_spec.rb +52 -0
- data/spec/geo_set_spec.rb +164 -0
- data/spec/hashes_spec.rb +46 -5
- data/spec/hyper_log_logs_spec.rb +50 -0
- data/spec/keys_spec.rb +92 -34
- data/spec/lists_spec.rb +20 -3
- data/spec/memory_spec.rb +4 -8
- data/spec/server_spec.rb +18 -4
- data/spec/sets_spec.rb +23 -2
- data/spec/sorted_sets_spec.rb +180 -2
- data/spec/spec_helper.rb +5 -19
- data/spec/strings_spec.rb +42 -2
- data/spec/subscription_spec.rb +31 -31
- data/spec/support/shared_examples/bitwise_operation.rb +12 -12
- data/spec/transactions_spec.rb +14 -1
- metadata +20 -21
- data/.travis.yml +0 -22
- data/gemfiles/redisrb-master.gemfile +0 -14
data/spec/bitop_command_spec.rb
CHANGED
@@ -11,7 +11,7 @@ module FakeRedis
|
|
11
11
|
end
|
12
12
|
|
13
13
|
it 'raises an argument error when passed unsupported operation' do
|
14
|
-
|
14
|
+
expect { @client.bitop("meh", "dest1", "key1") }.to raise_error(Redis::CommandError)
|
15
15
|
end
|
16
16
|
|
17
17
|
describe "or" do
|
@@ -28,22 +28,22 @@ module FakeRedis
|
|
28
28
|
@client.setbit("key2", 2, 0)
|
29
29
|
@client.setbit("key2", 3, 0)
|
30
30
|
|
31
|
-
@client.bitop("or", "dest1", "key1", "key2").
|
32
|
-
@client.bitcount("dest1").
|
33
|
-
@client.getbit("dest1", 0).
|
34
|
-
@client.getbit("dest1", 1).
|
35
|
-
@client.getbit("dest1", 2).
|
36
|
-
@client.getbit("dest1", 3).
|
31
|
+
expect(@client.bitop("or", "dest1", "key1", "key2")).to eq(1)
|
32
|
+
expect(@client.bitcount("dest1")).to eq(3)
|
33
|
+
expect(@client.getbit("dest1", 0)).to eq(1)
|
34
|
+
expect(@client.getbit("dest1", 1)).to eq(1)
|
35
|
+
expect(@client.getbit("dest1", 2)).to eq(1)
|
36
|
+
expect(@client.getbit("dest1", 3)).to eq(0)
|
37
37
|
end
|
38
38
|
|
39
39
|
it "should apply bitwise or operation with empty values" do
|
40
40
|
@client.setbit("key1", 1, 1)
|
41
41
|
|
42
|
-
@client.bitop("or", "dest1", "key1", "nothing_here1", "nothing_here2").
|
43
|
-
@client.bitcount("dest1").
|
44
|
-
@client.getbit("dest1", 0).
|
45
|
-
@client.getbit("dest1", 1).
|
46
|
-
@client.getbit("dest1", 2).
|
42
|
+
expect(@client.bitop("or", "dest1", "key1", "nothing_here1", "nothing_here2")).to eq(1)
|
43
|
+
expect(@client.bitcount("dest1")).to eq(1)
|
44
|
+
expect(@client.getbit("dest1", 0)).to eq(0)
|
45
|
+
expect(@client.getbit("dest1", 1)).to eq(1)
|
46
|
+
expect(@client.getbit("dest1", 2)).to eq(0)
|
47
47
|
end
|
48
48
|
|
49
49
|
it "should apply bitwise or operation with multiple keys" do
|
@@ -56,16 +56,16 @@ module FakeRedis
|
|
56
56
|
@client.setbit("key3", 13, 1)
|
57
57
|
@client.setbit("key3", 15, 1)
|
58
58
|
|
59
|
-
@client.bitop("or", "dest1", "key1", "key2", "key3").
|
60
|
-
@client.bitcount("dest1").
|
61
|
-
@client.getbit("dest1", 1).
|
62
|
-
@client.getbit("dest1", 3).
|
63
|
-
@client.getbit("dest1", 5).
|
64
|
-
@client.getbit("dest1", 10).
|
65
|
-
@client.getbit("dest1", 13).
|
66
|
-
@client.getbit("dest1", 15).
|
67
|
-
@client.getbit("dest1", 2).
|
68
|
-
@client.getbit("dest1", 12).
|
59
|
+
expect(@client.bitop("or", "dest1", "key1", "key2", "key3")).to eq(2)
|
60
|
+
expect(@client.bitcount("dest1")).to eq(6)
|
61
|
+
expect(@client.getbit("dest1", 1)).to eq(1)
|
62
|
+
expect(@client.getbit("dest1", 3)).to eq(1)
|
63
|
+
expect(@client.getbit("dest1", 5)).to eq(1)
|
64
|
+
expect(@client.getbit("dest1", 10)).to eq(1)
|
65
|
+
expect(@client.getbit("dest1", 13)).to eq(1)
|
66
|
+
expect(@client.getbit("dest1", 15)).to eq(1)
|
67
|
+
expect(@client.getbit("dest1", 2)).to eq(0)
|
68
|
+
expect(@client.getbit("dest1", 12)).to eq(0)
|
69
69
|
end
|
70
70
|
end
|
71
71
|
|
@@ -81,21 +81,21 @@ module FakeRedis
|
|
81
81
|
@client.setbit("key2", 1, 1)
|
82
82
|
@client.setbit("key2", 2, 1)
|
83
83
|
|
84
|
-
@client.bitop("and", "dest1", "key1", "key2").
|
85
|
-
@client.bitcount("dest1").
|
86
|
-
@client.getbit("dest1", 0).
|
87
|
-
@client.getbit("dest1", 1).
|
88
|
-
@client.getbit("dest1", 2).
|
84
|
+
expect(@client.bitop("and", "dest1", "key1", "key2")).to eq(1)
|
85
|
+
expect(@client.bitcount("dest1")).to eq(1)
|
86
|
+
expect(@client.getbit("dest1", 0)).to eq(0)
|
87
|
+
expect(@client.getbit("dest1", 1)).to eq(1)
|
88
|
+
expect(@client.getbit("dest1", 2)).to eq(0)
|
89
89
|
end
|
90
90
|
|
91
91
|
it "should apply bitwise and operation with empty values" do
|
92
92
|
@client.setbit("key1", 1, 1)
|
93
93
|
|
94
|
-
@client.bitop("and", "dest1", "key1", "nothing_here").
|
95
|
-
@client.bitcount("dest1").
|
96
|
-
@client.getbit("dest1", 0).
|
97
|
-
@client.getbit("dest1", 1).
|
98
|
-
@client.getbit("dest1", 2).
|
94
|
+
expect(@client.bitop("and", "dest1", "key1", "nothing_here")).to eq(1)
|
95
|
+
expect(@client.bitcount("dest1")).to eq(1)
|
96
|
+
expect(@client.getbit("dest1", 0)).to eq(0)
|
97
|
+
expect(@client.getbit("dest1", 1)).to eq(1)
|
98
|
+
expect(@client.getbit("dest1", 2)).to eq(0)
|
99
99
|
end
|
100
100
|
|
101
101
|
it "should apply bitwise and operation with multiple keys" do
|
@@ -114,14 +114,14 @@ module FakeRedis
|
|
114
114
|
@client.setbit("key3", 5, 1)
|
115
115
|
@client.setbit("key3", 6, 1)
|
116
116
|
|
117
|
-
@client.bitop("and", "dest1", "key1", "key2", "key3").
|
118
|
-
@client.bitcount("dest1").
|
119
|
-
@client.getbit("dest1", 1).
|
120
|
-
@client.getbit("dest1", 2).
|
121
|
-
@client.getbit("dest1", 3).
|
122
|
-
@client.getbit("dest1", 4).
|
123
|
-
@client.getbit("dest1", 5).
|
124
|
-
@client.getbit("dest1", 6).
|
117
|
+
expect(@client.bitop("and", "dest1", "key1", "key2", "key3")).to eq(1)
|
118
|
+
expect(@client.bitcount("dest1")).to eq(2)
|
119
|
+
expect(@client.getbit("dest1", 1)).to eq(0)
|
120
|
+
expect(@client.getbit("dest1", 2)).to eq(1)
|
121
|
+
expect(@client.getbit("dest1", 3)).to eq(0)
|
122
|
+
expect(@client.getbit("dest1", 4)).to eq(1)
|
123
|
+
expect(@client.getbit("dest1", 5)).to eq(0)
|
124
|
+
expect(@client.getbit("dest1", 6)).to eq(0)
|
125
125
|
end
|
126
126
|
end
|
127
127
|
|
@@ -139,22 +139,22 @@ module FakeRedis
|
|
139
139
|
@client.setbit("key2", 2, 1)
|
140
140
|
@client.setbit("key2", 3, 0)
|
141
141
|
|
142
|
-
@client.bitop("xor", "dest1", "key1", "key2").
|
143
|
-
@client.bitcount("dest1").
|
144
|
-
@client.getbit("dest1", 0).
|
145
|
-
@client.getbit("dest1", 1).
|
146
|
-
@client.getbit("dest1", 2).
|
147
|
-
@client.getbit("dest1", 3).
|
142
|
+
expect(@client.bitop("xor", "dest1", "key1", "key2")).to eq(1)
|
143
|
+
expect(@client.bitcount("dest1")).to eq(2)
|
144
|
+
expect(@client.getbit("dest1", 0)).to eq(1)
|
145
|
+
expect(@client.getbit("dest1", 1)).to eq(0)
|
146
|
+
expect(@client.getbit("dest1", 2)).to eq(1)
|
147
|
+
expect(@client.getbit("dest1", 3)).to eq(0)
|
148
148
|
end
|
149
149
|
|
150
150
|
it "should apply bitwise xor operation with empty values" do
|
151
151
|
@client.setbit("key1", 1, 1)
|
152
152
|
|
153
|
-
@client.bitop("xor", "dest1", "key1", "nothing_here1", "nothing_here2").
|
154
|
-
@client.bitcount("dest1").
|
155
|
-
@client.getbit("dest1", 0).
|
156
|
-
@client.getbit("dest1", 1).
|
157
|
-
@client.getbit("dest1", 2).
|
153
|
+
expect(@client.bitop("xor", "dest1", "key1", "nothing_here1", "nothing_here2")).to eq(1)
|
154
|
+
expect(@client.bitcount("dest1")).to eq(1)
|
155
|
+
expect(@client.getbit("dest1", 0)).to eq(0)
|
156
|
+
expect(@client.getbit("dest1", 1)).to eq(1)
|
157
|
+
expect(@client.getbit("dest1", 2)).to eq(0)
|
158
158
|
end
|
159
159
|
|
160
160
|
it "should apply bitwise xor operation with multiple keys" do
|
@@ -168,24 +168,24 @@ module FakeRedis
|
|
168
168
|
@client.setbit("key2", 4, 1)
|
169
169
|
@client.setbit("key2", 6, 1)
|
170
170
|
|
171
|
-
@client.bitop("xor", "dest1", "key1", "key2").
|
172
|
-
@client.bitcount("dest1").
|
173
|
-
@client.getbit("dest1", 1).
|
174
|
-
@client.getbit("dest1", 2).
|
175
|
-
@client.getbit("dest1", 3).
|
176
|
-
@client.getbit("dest1", 4).
|
177
|
-
@client.getbit("dest1", 5).
|
178
|
-
@client.getbit("dest1", 6).
|
171
|
+
expect(@client.bitop("xor", "dest1", "key1", "key2")).to eq(1)
|
172
|
+
expect(@client.bitcount("dest1")).to eq(4)
|
173
|
+
expect(@client.getbit("dest1", 1)).to eq(1)
|
174
|
+
expect(@client.getbit("dest1", 2)).to eq(1)
|
175
|
+
expect(@client.getbit("dest1", 3)).to eq(0)
|
176
|
+
expect(@client.getbit("dest1", 4)).to eq(1)
|
177
|
+
expect(@client.getbit("dest1", 5)).to eq(1)
|
178
|
+
expect(@client.getbit("dest1", 6)).to eq(0)
|
179
179
|
end
|
180
180
|
end
|
181
181
|
|
182
182
|
describe "not" do
|
183
183
|
it 'raises an argument error when not passed any keys' do
|
184
|
-
|
184
|
+
expect { @client.bitop("not", "destkey") }.to raise_error(Redis::CommandError)
|
185
185
|
end
|
186
186
|
|
187
187
|
it 'raises an argument error when not passed too many keys' do
|
188
|
-
|
188
|
+
expect { @client.bitop("not", "destkey", "key1", "key2") }.to raise_error(Redis::CommandError)
|
189
189
|
end
|
190
190
|
|
191
191
|
it "should apply bitwise negation operation" do
|
@@ -193,16 +193,16 @@ module FakeRedis
|
|
193
193
|
@client.setbit("key1", 3, 1)
|
194
194
|
@client.setbit("key1", 5, 1)
|
195
195
|
|
196
|
-
@client.bitop("not", "dest1", "key1").
|
197
|
-
@client.bitcount("dest1").
|
198
|
-
@client.getbit("dest1", 0).
|
199
|
-
@client.getbit("dest1", 1).
|
200
|
-
@client.getbit("dest1", 2).
|
201
|
-
@client.getbit("dest1", 3).
|
202
|
-
@client.getbit("dest1", 4).
|
203
|
-
@client.getbit("dest1", 5).
|
204
|
-
@client.getbit("dest1", 6).
|
205
|
-
@client.getbit("dest1", 7).
|
196
|
+
expect(@client.bitop("not", "dest1", "key1")).to eq(1)
|
197
|
+
expect(@client.bitcount("dest1")).to eq(5)
|
198
|
+
expect(@client.getbit("dest1", 0)).to eq(1)
|
199
|
+
expect(@client.getbit("dest1", 1)).to eq(0)
|
200
|
+
expect(@client.getbit("dest1", 2)).to eq(1)
|
201
|
+
expect(@client.getbit("dest1", 3)).to eq(0)
|
202
|
+
expect(@client.getbit("dest1", 4)).to eq(1)
|
203
|
+
expect(@client.getbit("dest1", 5)).to eq(0)
|
204
|
+
expect(@client.getbit("dest1", 6)).to eq(1)
|
205
|
+
expect(@client.getbit("dest1", 7)).to eq(1)
|
206
206
|
end
|
207
207
|
end
|
208
208
|
end
|
data/spec/fakeredis_spec.rb
CHANGED
@@ -18,4 +18,56 @@ describe FakeRedis do
|
|
18
18
|
expect(described_class.enabled?).to be_falsy
|
19
19
|
end
|
20
20
|
end
|
21
|
+
|
22
|
+
describe '.disabling' do
|
23
|
+
context 'FakeRedis is enabled' do
|
24
|
+
before { described_class.enable }
|
25
|
+
|
26
|
+
it 'in memory connection' do
|
27
|
+
described_class.disabling do
|
28
|
+
expect(described_class.enabled?).to be_falsy
|
29
|
+
end
|
30
|
+
|
31
|
+
expect(described_class.enabled?).to be_truthy
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
context 'FakeRedis is disabled' do
|
36
|
+
before { described_class.disable }
|
37
|
+
|
38
|
+
it 'in memory connection' do
|
39
|
+
described_class.disabling do
|
40
|
+
expect(described_class.enabled?).to be_falsy
|
41
|
+
end
|
42
|
+
|
43
|
+
expect(described_class.enabled?).to be_falsy
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
describe '.enabling' do
|
49
|
+
context 'FakeRedis is enabled' do
|
50
|
+
before { described_class.enable }
|
51
|
+
|
52
|
+
it 'in memory connection' do
|
53
|
+
described_class.enabling do
|
54
|
+
expect(described_class.enabled?).to be_truthy
|
55
|
+
end
|
56
|
+
|
57
|
+
expect(described_class.enabled?).to be_truthy
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
context 'FakeRedis is disabled' do
|
62
|
+
before { described_class.disable }
|
63
|
+
|
64
|
+
it 'in memory connection' do
|
65
|
+
described_class.enabling do
|
66
|
+
expect(described_class.enabled?).to be_truthy
|
67
|
+
end
|
68
|
+
|
69
|
+
expect(described_class.enabled?).to be_falsy
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
21
73
|
end
|
@@ -0,0 +1,164 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
module FakeRedis
|
4
|
+
describe "GeoMethods" do
|
5
|
+
before do
|
6
|
+
@client = Redis.new
|
7
|
+
end
|
8
|
+
|
9
|
+
describe "#geoadd" do
|
10
|
+
it "should raise when not enough arguments" do
|
11
|
+
expect { @client.geoadd("Sicily", []) }.to raise_error(Redis::CommandError, "ERR wrong number of arguments for 'geoadd' command")
|
12
|
+
expect { @client.geoadd("Sicily", [13.361389, 38.115556]) }.to raise_error(Redis::CommandError, "ERR wrong number of arguments for 'geoadd' command")
|
13
|
+
end
|
14
|
+
|
15
|
+
it "should add items to the set" do
|
16
|
+
added_items_count = add_sicily
|
17
|
+
expect(added_items_count).to eq(2)
|
18
|
+
end
|
19
|
+
|
20
|
+
it "should update existing items" do
|
21
|
+
@client.geoadd("Sicily", 13.361389, 38.115556, "Palermo")
|
22
|
+
added_items_count = @client.geoadd("Sicily", 13, 39, "Palermo", 15.087269, 37.502669, "Catania")
|
23
|
+
expect(added_items_count).to eq(1)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
describe "#geodist" do
|
28
|
+
before do
|
29
|
+
add_sicily
|
30
|
+
end
|
31
|
+
|
32
|
+
it "should return destination between two elements" do
|
33
|
+
distance_in_meters = @client.geodist("Sicily", "Palermo", "Catania")
|
34
|
+
expect(distance_in_meters).to eq("166412.6051")
|
35
|
+
|
36
|
+
distance_in_feet = @client.geodist("Sicily", "Palermo", "Catania", "ft")
|
37
|
+
expect(distance_in_feet).to eq("545973.1137")
|
38
|
+
end
|
39
|
+
|
40
|
+
it "should raise for unknown unit name" do
|
41
|
+
expect {
|
42
|
+
@client.geodist("Sicily", "Palermo", "Catania", "unknown")
|
43
|
+
}.to raise_error(Redis::CommandError, "ERR unsupported unit provided. please use m, km, ft, mi")
|
44
|
+
end
|
45
|
+
|
46
|
+
it "should return nil when element is missing" do
|
47
|
+
expect(@client.geodist("Sicily", "Palermo", "Rome")).to be_nil
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
describe "#geohash" do
|
52
|
+
before do
|
53
|
+
add_sicily
|
54
|
+
end
|
55
|
+
|
56
|
+
it "should raise when not enough arguments" do
|
57
|
+
expect { @client.geohash("Sicily", []) }.to raise_error(Redis::CommandError, "ERR wrong number of arguments for 'geohash' command")
|
58
|
+
end
|
59
|
+
|
60
|
+
it "should return geohashes" do
|
61
|
+
geohash = @client.geohash("Sicily", "Palermo")
|
62
|
+
expect(geohash).to eq(["sqc8b49rny"])
|
63
|
+
|
64
|
+
geohashes = @client.geohash("Sicily", ["Palermo", "Catania"])
|
65
|
+
expect(geohashes).to eq(["sqc8b49rny", "sqdtr74hyu"])
|
66
|
+
end
|
67
|
+
|
68
|
+
it "should return nils for nonexistent elements" do
|
69
|
+
geohashes = @client.geohash("Sicily", ["Palermo", "Rome"])
|
70
|
+
expect(geohashes).to eq(["sqc8b49rny", nil])
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
describe "#geopos" do
|
75
|
+
it "should return positions (longitude, latitude) for elements" do
|
76
|
+
add_sicily
|
77
|
+
position = @client.geopos("Sicily", "Catania")
|
78
|
+
expect(position).to eq([["15.087269", "37.502669"]])
|
79
|
+
|
80
|
+
positions = @client.geopos("Sicily", ["Palermo", "Catania"])
|
81
|
+
expect(positions).to eq([["13.361389", "38.115556"], ["15.087269", "37.502669"]])
|
82
|
+
end
|
83
|
+
|
84
|
+
it "should return nil for nonexistent elements" do
|
85
|
+
expect(@client.geopos("nonexistent", "nonexistent2")).to be_nil
|
86
|
+
add_sicily
|
87
|
+
|
88
|
+
position = @client.geopos("Sicily", "Rome")
|
89
|
+
expect(position).to eq([nil])
|
90
|
+
|
91
|
+
positions = @client.geopos("Sicily", ["Rome", "Catania"])
|
92
|
+
expect(positions).to eq([nil, ["15.087269", "37.502669"]])
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
describe "#georadius" do
|
97
|
+
before do
|
98
|
+
add_sicily
|
99
|
+
end
|
100
|
+
|
101
|
+
it "should return members within specified radius" do
|
102
|
+
nearest_cities = @client.georadius("Sicily", 15, 37, 100, "km")
|
103
|
+
expect(nearest_cities).to eq(["Catania"])
|
104
|
+
end
|
105
|
+
|
106
|
+
it "should sort returned members" do
|
107
|
+
nearest_cities = @client.georadius("Sicily", 15, 37, 200, "km", sort: "asc")
|
108
|
+
expect(nearest_cities).to eq(["Catania", "Palermo"])
|
109
|
+
|
110
|
+
farthest_cities = @client.georadius("Sicily", 15, 37, 200, "km", sort: "desc")
|
111
|
+
expect(farthest_cities).to eq(["Palermo", "Catania"])
|
112
|
+
end
|
113
|
+
|
114
|
+
it "should return specified count of members" do
|
115
|
+
city = @client.georadius("Sicily", 15, 37, 200, "km", sort: "asc", count: 1)
|
116
|
+
expect(city).to eq(["Catania"])
|
117
|
+
end
|
118
|
+
|
119
|
+
it "should include additional info for members" do
|
120
|
+
cities = @client.georadius("Sicily", 15, 37, 200, "km", "WITHDIST")
|
121
|
+
expect(cities).to eq([["Palermo", "190.6009"], ["Catania", "56.4883"]])
|
122
|
+
|
123
|
+
cities = @client.georadius("Sicily", 15, 37, 200, "km", "WITHCOORD")
|
124
|
+
expect(cities).to eq [["Palermo", ["13.361389", "38.115556"]], ["Catania", ["15.087269", "37.502669"]]]
|
125
|
+
|
126
|
+
cities = @client.georadius("Sicily", 15, 37, 200, "km", "WITHDIST", "WITHCOORD")
|
127
|
+
expect(cities).to eq(
|
128
|
+
[["Palermo", "190.6009", ["13.361389", "38.115556"]],
|
129
|
+
["Catania", "56.4883", ["15.087269", "37.502669"]]]
|
130
|
+
)
|
131
|
+
end
|
132
|
+
end
|
133
|
+
|
134
|
+
describe "#georadiusbymember" do
|
135
|
+
before do
|
136
|
+
add_sicily
|
137
|
+
end
|
138
|
+
|
139
|
+
it "should sort returned members" do
|
140
|
+
nearest_cities = @client.georadiusbymember("Sicily", "Catania", 200, "km", sort: "asc")
|
141
|
+
expect(nearest_cities).to eq(["Catania", "Palermo"])
|
142
|
+
|
143
|
+
farthest_cities = @client.georadiusbymember("Sicily", "Catania", 200, "km", sort: "desc")
|
144
|
+
expect(farthest_cities).to eq(["Palermo", "Catania"])
|
145
|
+
end
|
146
|
+
|
147
|
+
it "should limit number of returned members" do
|
148
|
+
city = @client.georadiusbymember("Sicily", "Catania", 100, "km", count: 1)
|
149
|
+
expect(city).to eq(["Catania"])
|
150
|
+
end
|
151
|
+
|
152
|
+
it "should include extra info if requested" do
|
153
|
+
city = @client.georadiusbymember("Sicily", "Catania", 200, "km", sort: :desc, options: :WITHDIST, count: 1)
|
154
|
+
expect(city).to eq([["Palermo", "166.4126"]])
|
155
|
+
end
|
156
|
+
end
|
157
|
+
|
158
|
+
private
|
159
|
+
|
160
|
+
def add_sicily
|
161
|
+
@client.geoadd("Sicily", 13.361389, 38.115556, "Palermo", 15.087269, 37.502669, "Catania")
|
162
|
+
end
|
163
|
+
end
|
164
|
+
end
|
data/spec/hashes_spec.rb
CHANGED
@@ -32,7 +32,7 @@ module FakeRedis
|
|
32
32
|
expect(@client.hdel("key1", "k1")).to be(1)
|
33
33
|
expect(@client.hdel("key1", "k2")).to be(1)
|
34
34
|
|
35
|
-
expect(@client.exists("key1")).to eq(
|
35
|
+
expect(@client.exists("key1")).to eq(0)
|
36
36
|
end
|
37
37
|
|
38
38
|
it "should convert key to a string for hset" do
|
@@ -108,6 +108,13 @@ module FakeRedis
|
|
108
108
|
expect(@client.hlen("key1")).to eq(2)
|
109
109
|
end
|
110
110
|
|
111
|
+
it "should get the string length of the value associated with field in a hash" do
|
112
|
+
@client.hset("key1", "i1", "val1")
|
113
|
+
expect(@client.hstrlen("key1", "i1")).to eq(4)
|
114
|
+
expect(@client.hstrlen("key1", "nonexistent")).to eq(0)
|
115
|
+
expect(@client.hstrlen("nonexistent", "field")).to eq(0)
|
116
|
+
end
|
117
|
+
|
111
118
|
it "should get the values of all the given hash fields" do
|
112
119
|
@client.hset("key1", "i1", "val1")
|
113
120
|
@client.hset("key1", "i2", "val2")
|
@@ -122,15 +129,19 @@ module FakeRedis
|
|
122
129
|
expect { @client.hmget("key1") }.to raise_error(Redis::CommandError)
|
123
130
|
end
|
124
131
|
|
132
|
+
it "should throw an argument error when the list of keys you ask for is empty" do
|
133
|
+
expect { @client.hmget("key1", []) }.to raise_error(Redis::CommandError)
|
134
|
+
end
|
135
|
+
|
125
136
|
it "should reject an empty list of values" do
|
126
137
|
expect { @client.hmset("key") }.to raise_error(Redis::CommandError)
|
127
|
-
expect(@client.exists("key")).to be false
|
138
|
+
expect(@client.exists?("key")).to be false
|
128
139
|
end
|
129
140
|
|
130
141
|
it "rejects an insert with a key but no value" do
|
131
142
|
expect { @client.hmset("key", 'foo') }.to raise_error(Redis::CommandError)
|
132
143
|
expect { @client.hmset("key", 'foo', 3, 'bar') }.to raise_error(Redis::CommandError)
|
133
|
-
expect(@client.exists("key")).to be false
|
144
|
+
expect(@client.exists?("key")).to be false
|
134
145
|
end
|
135
146
|
|
136
147
|
it "should reject the wrong number of arguments" do
|
@@ -156,12 +167,42 @@ module FakeRedis
|
|
156
167
|
end
|
157
168
|
|
158
169
|
it "should set the string value of a hash field" do
|
159
|
-
expect(@client.hset("key1", "k1", "val1")).to eq(
|
160
|
-
expect(@client.hset("key1", "k1", "val1")).to eq(
|
170
|
+
expect(@client.hset("key1", "k1", "val1")).to eq(1)
|
171
|
+
expect(@client.hset("key1", "k1", "val1")).to eq(0)
|
161
172
|
|
162
173
|
expect(@client.hget("key1", "k1")).to eq("val1")
|
163
174
|
end
|
164
175
|
|
176
|
+
it "should accept a list of key-value pair" do
|
177
|
+
@client.hset("key1", "k1", "val1", "k2", "val2")
|
178
|
+
|
179
|
+
expect(@client.hget("key1", "k1")).to eq("val1")
|
180
|
+
expect(@client.hget("key1", "k2")).to eq("val2")
|
181
|
+
end
|
182
|
+
|
183
|
+
it "should accept a hash of attributes to insert" do
|
184
|
+
@client.hset("key1", {"k1" => "val1", "k2" => "val2"})
|
185
|
+
|
186
|
+
expect(@client.hget("key1", "k1")).to eq("val1")
|
187
|
+
expect(@client.hget("key1", "k2")).to eq("val2")
|
188
|
+
end
|
189
|
+
|
190
|
+
it "should return correct value when inserting a list of key-value pair" do
|
191
|
+
@client.hset("key1", "k1", "val1", "k2", "val2")
|
192
|
+
|
193
|
+
expect(@client.hset("key1", "k1", "val1", "k2", "val2")).to eq(0)
|
194
|
+
expect(@client.hset("key1", "k2", "val2", "k3", "val3")).to eq(1)
|
195
|
+
expect(@client.hset("key1", "k4", "val4", "k5", "val5")).to eq(2)
|
196
|
+
end
|
197
|
+
|
198
|
+
it "should return correct value when inserting a hash of attributes" do
|
199
|
+
@client.hset("key1", { "k1" => "val1", "k2" => "val2" })
|
200
|
+
|
201
|
+
expect(@client.hset("key1", { "k1" => "val1", "k2" => "val2" })).to eq(0)
|
202
|
+
expect(@client.hset("key1", { "k2" => "val2", "k3" => "val3" })).to eq(1)
|
203
|
+
expect(@client.hset("key1", { "k4" => "val4", "k5" => "val5" })).to eq(2)
|
204
|
+
end
|
205
|
+
|
165
206
|
it "should set the value of a hash field, only if the field does not exist" do
|
166
207
|
@client.hset("key1", "k1", "val1")
|
167
208
|
expect(@client.hsetnx("key1", "k1", "value")).to eq(false)
|
@@ -0,0 +1,50 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
module FakeRedis
|
4
|
+
describe "HyperLogLogsMethods" do
|
5
|
+
let(:redis) { Redis.new }
|
6
|
+
|
7
|
+
it "should add item to hyperloglog" do
|
8
|
+
expect(redis.pfadd("hll", "val")).to eq(true)
|
9
|
+
expect(redis.pfcount("hll")).to eq(1)
|
10
|
+
end
|
11
|
+
|
12
|
+
it "should not add duplicated item to hyperloglog" do
|
13
|
+
redis.pfadd("hll", "val")
|
14
|
+
expect(redis.pfadd("hll", "val")).to eq(false)
|
15
|
+
expect(redis.pfcount("hll")).to eq(1)
|
16
|
+
end
|
17
|
+
|
18
|
+
it "should not add multiple items to hyperloglog" do
|
19
|
+
expect(redis.pfadd("hll", ["val1", "val2"])).to eq(true)
|
20
|
+
expect(redis.pfcount("hll")).to eq(2)
|
21
|
+
end
|
22
|
+
|
23
|
+
it "should return zero as cardinality for nonexistent key" do
|
24
|
+
expect(redis.pfcount("nonexistent")).to eq(0)
|
25
|
+
end
|
26
|
+
|
27
|
+
it "should return cardinality of union of hyperloglogs" do
|
28
|
+
redis.pfadd("hll1", ["val1", "val2"])
|
29
|
+
redis.pfadd("hll2", ["val2", "val3"])
|
30
|
+
expect(redis.pfcount("hll1", "hll2")).to eq(3)
|
31
|
+
end
|
32
|
+
|
33
|
+
it "should error if an empty list of keys is given" do
|
34
|
+
expect { redis.pfcount([]) }.to raise_error(Redis::CommandError, "ERR wrong number of arguments for 'pfcount' command")
|
35
|
+
end
|
36
|
+
|
37
|
+
it "should merge hyperloglogs" do
|
38
|
+
redis.pfadd("hll1", ["val1", "val2"])
|
39
|
+
redis.pfadd("hll2", ["val2", "val3"])
|
40
|
+
expect(redis.pfmerge("hll3", "hll1", "hll2")).to eq(true)
|
41
|
+
expect(redis.pfcount("hll3")).to eq(3)
|
42
|
+
end
|
43
|
+
|
44
|
+
it "should merge nonexistent hyperloglogs with others" do
|
45
|
+
redis.pfadd("hll1", "val")
|
46
|
+
expect(redis.pfmerge("hll3", "hll1", "nonexistent")).to eq(true)
|
47
|
+
expect(redis.pfcount("hll3")).to eq(1)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|