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.
@@ -12,7 +12,7 @@ module FakeRedis
12
12
  @client.set("key2", "2")
13
13
  @client.del("key1", "key2")
14
14
 
15
- @client.get("key1").should be == nil
15
+ expect(@client.get("key1")).to eq(nil)
16
16
  end
17
17
 
18
18
  it "should delete multiple keys" do
@@ -20,43 +20,69 @@ module FakeRedis
20
20
  @client.set("key2", "2")
21
21
  @client.del(["key1", "key2"])
22
22
 
23
- @client.get("key1").should be == nil
24
- @client.get("key2").should be == nil
23
+ expect(@client.get("key1")).to eq(nil)
24
+ expect(@client.get("key2")).to eq(nil)
25
25
  end
26
26
 
27
27
  it "should error deleting no keys" do
28
- lambda { @client.del }.should raise_error(Redis::CommandError, "ERR wrong number of arguments for 'del' command")
29
- lambda { @client.del [] }.should raise_error(Redis::CommandError, "ERR wrong number of arguments for 'del' command")
28
+ expect { @client.del }.to raise_error(Redis::CommandError, "ERR wrong number of arguments for 'del' command")
29
+ expect { @client.del [] }.to raise_error(Redis::CommandError, "ERR wrong number of arguments for 'del' command")
30
+ end
31
+
32
+ it "should return true when setnx keys that don't exist" do
33
+ expect(@client.setnx("key1", "1")).to eq(true)
34
+ end
35
+
36
+ it "should return false when setnx keys exist" do
37
+ @client.set("key1", "1")
38
+ expect(@client.setnx("key1", "1")).to eq(false)
30
39
  end
31
40
 
32
41
  it "should return true when setting expires on keys that exist" do
33
42
  @client.set("key1", "1")
34
- @client.expire("key1", 1).should == true
43
+ expect(@client.expire("key1", 1)).to eq(true)
44
+ end
45
+
46
+ it "should return true when setting pexpires on keys that exist" do
47
+ @client.set("key1", "1")
48
+ expect(@client.pexpire("key1", 1)).to eq(true)
35
49
  end
36
50
 
37
51
  it "should return false when attempting to set expires on a key that does not exist" do
38
- @client.expire("key1", 1).should == false
52
+ expect(@client.expire("key1", 1)).to eq(false)
53
+ end
54
+
55
+ it "should return false when attempting to set pexpires on a key that does not exist" do
56
+ expect(@client.pexpire("key1", 1)).to eq(false)
39
57
  end
40
58
 
41
59
  it "should determine if a key exists" do
42
60
  @client.set("key1", "1")
43
61
 
44
- @client.exists("key1").should be == true
45
- @client.exists("key2").should be == false
62
+ expect(@client.exists("key1")).to eq(true)
63
+ expect(@client.exists("key2")).to eq(false)
46
64
  end
47
65
 
48
66
  it "should set a key's time to live in seconds" do
49
67
  @client.set("key1", "1")
50
68
  @client.expire("key1", 1)
51
69
 
52
- @client.ttl("key1").should be == 1
70
+ expect(@client.ttl("key1")).to eq(1)
71
+ end
72
+
73
+ it "should set a key's time to live in miliseconds" do
74
+ allow(Time).to receive(:now).and_return(1000)
75
+ @client.set("key1", "1")
76
+ @client.pexpire("key1", 2200)
77
+ expect(@client.pttl("key1")).to be_within(0.1).of(2200)
78
+ allow(Time).to receive(:now).and_call_original
53
79
  end
54
80
 
55
81
  it "should set the expiration for a key as a UNIX timestamp" do
56
82
  @client.set("key1", "1")
57
83
  @client.expireat("key1", Time.now.to_i + 2)
58
84
 
59
- @client.ttl("key1").should be == 2
85
+ expect(@client.ttl("key1")).to eq(2)
60
86
  end
61
87
 
62
88
  it "should not have an expiration after re-set" do
@@ -64,21 +90,21 @@ module FakeRedis
64
90
  @client.expireat("key1", Time.now.to_i + 2)
65
91
  @client.set("key1", "1")
66
92
 
67
- @client.ttl("key1").should be == -1
93
+ expect(@client.ttl("key1")).to eq(-1)
68
94
  end
69
95
 
70
96
  it "should not have a ttl if expired (and thus key does not exist)" do
71
97
  @client.set("key1", "1")
72
98
  @client.expireat("key1", Time.now.to_i)
73
99
 
74
- @client.ttl("key1").should be == -2
100
+ expect(@client.ttl("key1")).to eq(-2)
75
101
  end
76
102
 
77
103
  it "should not find a key if expired" do
78
104
  @client.set("key1", "1")
79
105
  @client.expireat("key1", Time.now.to_i)
80
106
 
81
- @client.get("key1").should be_nil
107
+ expect(@client.get("key1")).to be_nil
82
108
  end
83
109
 
84
110
  it "should not find multiple keys if expired" do
@@ -86,7 +112,7 @@ module FakeRedis
86
112
  @client.set("key2", "2")
87
113
  @client.expireat("key1", Time.now.to_i)
88
114
 
89
- @client.mget("key1", "key2").should be == [nil, "2"]
115
+ expect(@client.mget("key1", "key2")).to eq([nil, "2"])
90
116
  end
91
117
 
92
118
  it "should only find keys that aren't expired" do
@@ -94,14 +120,14 @@ module FakeRedis
94
120
  @client.set("key2", "2")
95
121
  @client.expireat("key1", Time.now.to_i)
96
122
 
97
- @client.keys.should be == ["key2"]
123
+ expect(@client.keys).to eq(["key2"])
98
124
  end
99
125
 
100
126
  it "should not exist if expired" do
101
127
  @client.set("key1", "1")
102
128
  @client.expireat("key1", Time.now.to_i)
103
129
 
104
- @client.exists("key1").should be false
130
+ expect(@client.exists("key1")).to be false
105
131
  end
106
132
 
107
133
  it "should find all keys matching the given pattern" do
@@ -113,32 +139,32 @@ module FakeRedis
113
139
 
114
140
  @client.mset("database", 1, "above", 2, "suitability", 3, "able", 4)
115
141
 
116
- @client.keys("key:*").should =~ ["key:a", "key:b", "key:c"]
117
- @client.keys("ab*").should =~ ["above", "able"]
142
+ expect(@client.keys("key:*")).to match_array(["key:a", "key:b", "key:c"])
143
+ expect(@client.keys("ab*")).to match_array(["above", "able"])
118
144
  end
119
145
 
120
146
  it "should remove the expiration from a key" do
121
147
  @client.set("key1", "1")
122
148
  @client.expireat("key1", Time.now.to_i + 1)
123
- @client.persist("key1").should be == true
124
- @client.persist("key1").should be == false
149
+ expect(@client.persist("key1")).to eq(true)
150
+ expect(@client.persist("key1")).to eq(false)
125
151
 
126
- @client.ttl("key1").should be == -1
152
+ expect(@client.ttl("key1")).to eq(-1)
127
153
  end
128
154
 
129
155
  it "should return a random key from the keyspace" do
130
156
  @client.set("key1", "1")
131
157
  @client.set("key2", "2")
132
158
 
133
- ["key1", "key2"].include?(@client.randomkey).should be == true
159
+ expect(["key1", "key2"].include?(@client.randomkey)).to eq(true)
134
160
  end
135
161
 
136
162
  it "should rename a key" do
137
163
  @client.set("key1", "2")
138
164
  @client.rename("key1", "key2")
139
165
 
140
- @client.get("key1").should be == nil
141
- @client.get("key2").should be == "2"
166
+ expect(@client.get("key1")).to eq(nil)
167
+ expect(@client.get("key2")).to eq("2")
142
168
  end
143
169
 
144
170
  it "should rename a key, only if new key does not exist" do
@@ -148,100 +174,100 @@ module FakeRedis
148
174
  @client.renamenx("key1", "key2")
149
175
  @client.renamenx("key3", "key4")
150
176
 
151
- @client.get("key1").should be == "1"
152
- @client.get("key2").should be == "2"
153
- @client.get("key3").should be == nil
154
- @client.get("key4").should be == "3"
177
+ expect(@client.get("key1")).to eq("1")
178
+ expect(@client.get("key2")).to eq("2")
179
+ expect(@client.get("key3")).to eq(nil)
180
+ expect(@client.get("key4")).to eq("3")
155
181
  end
156
182
 
157
183
  it "should determine the type stored at key" do
158
184
  # Non-existing key
159
- @client.type("key0").should be == "none"
185
+ expect(@client.type("key0")).to eq("none")
160
186
 
161
187
  # String
162
188
  @client.set("key1", "1")
163
- @client.type("key1").should be == "string"
189
+ expect(@client.type("key1")).to eq("string")
164
190
 
165
191
  # List
166
192
  @client.lpush("key2", "1")
167
- @client.type("key2").should be == "list"
193
+ expect(@client.type("key2")).to eq("list")
168
194
 
169
195
  # Set
170
196
  @client.sadd("key3", "1")
171
- @client.type("key3").should be == "set"
197
+ expect(@client.type("key3")).to eq("set")
172
198
 
173
199
  # Sorted Set
174
200
  @client.zadd("key4", 1.0, "1")
175
- @client.type("key4").should be == "zset"
201
+ expect(@client.type("key4")).to eq("zset")
176
202
 
177
203
  # Hash
178
204
  @client.hset("key5", "a", "1")
179
- @client.type("key5").should be == "hash"
205
+ expect(@client.type("key5")).to eq("hash")
180
206
  end
181
207
 
182
208
  it "should convert the value into a string before storing" do
183
209
  @client.set("key1", 1)
184
- @client.get("key1").should be == "1"
210
+ expect(@client.get("key1")).to eq("1")
185
211
 
186
212
  @client.setex("key2", 30, 1)
187
- @client.get("key2").should be == "1"
213
+ expect(@client.get("key2")).to eq("1")
188
214
 
189
215
  @client.getset("key3", 1)
190
- @client.get("key3").should be == "1"
216
+ expect(@client.get("key3")).to eq("1")
191
217
  end
192
218
 
193
219
  it "should return 'OK' for the setex command" do
194
- @client.setex("key4", 30, 1).should be == "OK"
220
+ expect(@client.setex("key4", 30, 1)).to eq("OK")
195
221
  end
196
222
 
197
223
  it "should convert the key into a string before storing" do
198
224
  @client.set(123, "foo")
199
- @client.keys.should include("123")
200
- @client.get("123").should be == "foo"
225
+ expect(@client.keys).to include("123")
226
+ expect(@client.get("123")).to eq("foo")
201
227
 
202
228
  @client.setex(456, 30, "foo")
203
- @client.keys.should include("456")
204
- @client.get("456").should be == "foo"
229
+ expect(@client.keys).to include("456")
230
+ expect(@client.get("456")).to eq("foo")
205
231
 
206
232
  @client.getset(789, "foo")
207
- @client.keys.should include("789")
208
- @client.get("789").should be == "foo"
233
+ expect(@client.keys).to include("789")
234
+ expect(@client.get("789")).to eq("foo")
209
235
  end
210
236
 
211
237
  it "should only operate against keys containing string values" do
212
238
  @client.sadd("key1", "one")
213
- lambda { @client.get("key1") }.should raise_error(Redis::CommandError, "WRONGTYPE Operation against a key holding the wrong kind of value")
214
- lambda { @client.getset("key1", 1) }.should raise_error(Redis::CommandError, "WRONGTYPE Operation against a key holding the wrong kind of value")
239
+ expect { @client.get("key1") }.to raise_error(Redis::CommandError, "WRONGTYPE Operation against a key holding the wrong kind of value")
240
+ expect { @client.getset("key1", 1) }.to raise_error(Redis::CommandError, "WRONGTYPE Operation against a key holding the wrong kind of value")
215
241
 
216
242
  @client.hset("key2", "one", "two")
217
- lambda { @client.get("key2") }.should raise_error(Redis::CommandError, "WRONGTYPE Operation against a key holding the wrong kind of value")
218
- lambda { @client.getset("key2", 1) }.should raise_error(Redis::CommandError, "WRONGTYPE Operation against a key holding the wrong kind of value")
243
+ expect { @client.get("key2") }.to raise_error(Redis::CommandError, "WRONGTYPE Operation against a key holding the wrong kind of value")
244
+ expect { @client.getset("key2", 1) }.to raise_error(Redis::CommandError, "WRONGTYPE Operation against a key holding the wrong kind of value")
219
245
  end
220
246
 
221
247
  it "should move a key from one database to another successfully" do
222
248
  @client.select(0)
223
249
  @client.set("key1", "1")
224
250
 
225
- @client.move("key1", 1).should be == true
251
+ expect(@client.move("key1", 1)).to eq(true)
226
252
 
227
253
  @client.select(0)
228
- @client.get("key1").should be_nil
254
+ expect(@client.get("key1")).to be_nil
229
255
 
230
256
  @client.select(1)
231
- @client.get("key1").should be == "1"
257
+ expect(@client.get("key1")).to eq("1")
232
258
  end
233
259
 
234
260
  it "should fail to move a key that does not exist in the source database" do
235
261
  @client.select(0)
236
- @client.get("key1").should be_nil
262
+ expect(@client.get("key1")).to be_nil
237
263
 
238
- @client.move("key1", 1).should be == false
264
+ expect(@client.move("key1", 1)).to eq(false)
239
265
 
240
266
  @client.select(0)
241
- @client.get("key1").should be_nil
267
+ expect(@client.get("key1")).to be_nil
242
268
 
243
269
  @client.select(1)
244
- @client.get("key1").should be_nil
270
+ expect(@client.get("key1")).to be_nil
245
271
  end
246
272
 
247
273
  it "should fail to move a key that exists in the destination database" do
@@ -252,23 +278,23 @@ module FakeRedis
252
278
  @client.set("key1", "2")
253
279
 
254
280
  @client.select(0)
255
- @client.move("key1", 1).should be == false
281
+ expect(@client.move("key1", 1)).to eq(false)
256
282
 
257
283
  @client.select(0)
258
- @client.get("key1").should be == "1"
284
+ expect(@client.get("key1")).to eq("1")
259
285
 
260
286
  @client.select(1)
261
- @client.get("key1").should be == "2"
287
+ expect(@client.get("key1")).to eq("2")
262
288
  end
263
289
 
264
290
  it "should fail to move a key to the same database" do
265
291
  @client.select(0)
266
292
  @client.set("key1", "1")
267
293
 
268
- lambda { @client.move("key1", 0) }.should raise_error(Redis::CommandError, "ERR source and destination objects are the same")
294
+ expect { @client.move("key1", 0) }.to raise_error(Redis::CommandError, "ERR source and destination objects are the same")
269
295
 
270
296
  @client.select(0)
271
- @client.get("key1").should be == "1"
297
+ expect(@client.get("key1")).to eq("1")
272
298
  end
273
299
 
274
300
  it "should scan all keys in the database" do
@@ -284,8 +310,8 @@ module FakeRedis
284
310
  break if cursor == "0"
285
311
  }
286
312
 
287
- all_keys.uniq.size.should == 100
288
- all_keys[0].should =~ /key\d+/
313
+ expect(all_keys.uniq.size).to eq(100)
314
+ expect(all_keys[0]).to match(/key\d+/)
289
315
  end
290
316
 
291
317
  it "should match keys to a pattern when scanning" do
@@ -304,7 +330,7 @@ module FakeRedis
304
330
  break if cursor == "0"
305
331
  }
306
332
 
307
- all_keys.uniq.size.should == 50
333
+ expect(all_keys.uniq.size).to eq(50)
308
334
  end
309
335
 
310
336
  it "should specify doing more work when scanning" do
@@ -314,23 +340,23 @@ module FakeRedis
314
340
 
315
341
  cursor, all_keys = @client.scan(cursor, :count => 100)
316
342
 
317
- cursor.should == "0"
318
- all_keys.uniq.size.should == 100
343
+ expect(cursor).to eq("0")
344
+ expect(all_keys.uniq.size).to eq(100)
319
345
  end
320
346
 
321
347
  context "with extended options" do
322
348
  it "uses ex option to set the expire time, in seconds" do
323
349
  ttl = 7
324
350
 
325
- @client.set("key1", "1", { :ex => ttl }).should == "OK"
326
- @client.ttl("key1").should == ttl
351
+ expect(@client.set("key1", "1", { :ex => ttl })).to eq("OK")
352
+ expect(@client.ttl("key1")).to eq(ttl)
327
353
  end
328
354
 
329
355
  it "uses px option to set the expire time, in miliseconds" do
330
356
  ttl = 7000
331
357
 
332
- @client.set("key1", "1", { :px => ttl }).should == "OK"
333
- @client.ttl("key1").should == (ttl / 1000)
358
+ expect(@client.set("key1", "1", { :px => ttl })).to eq("OK")
359
+ expect(@client.ttl("key1")).to eq(ttl / 1000)
334
360
  end
335
361
 
336
362
  # Note that the redis-rb implementation will always give PX last.
@@ -340,32 +366,123 @@ module FakeRedis
340
366
  ttl_ex = 10
341
367
 
342
368
  @client.set("key1", "1", { :px => ttl_px, :ex => ttl_ex })
343
- @client.ttl("key1").should == (ttl_px / 1000)
369
+ expect(@client.ttl("key1")).to eq(ttl_px / 1000)
344
370
 
345
371
  @client.set("key1", "1", { :ex => ttl_ex, :px => ttl_px })
346
- @client.ttl("key1").should == (ttl_px / 1000)
372
+ expect(@client.ttl("key1")).to eq(ttl_px / 1000)
347
373
  end
348
374
 
349
375
  it "uses nx option to only set the key if it does not already exist" do
350
- @client.set("key1", "1", { :nx => true }).should == true
351
- @client.set("key1", "2", { :nx => true }).should == false
376
+ expect(@client.set("key1", "1", { :nx => true })).to eq(true)
377
+ expect(@client.set("key1", "2", { :nx => true })).to eq(false)
352
378
 
353
- @client.get("key1").should == "1"
379
+ expect(@client.get("key1")).to eq("1")
354
380
  end
355
381
 
356
382
  it "uses xx option to only set the key if it already exists" do
357
- @client.set("key2", "1", { :xx => true }).should == false
383
+ expect(@client.set("key2", "1", { :xx => true })).to eq(false)
358
384
  @client.set("key2", "2")
359
- @client.set("key2", "1", { :xx => true }).should == true
385
+ expect(@client.set("key2", "1", { :xx => true })).to eq(true)
360
386
 
361
- @client.get("key2").should == "1"
387
+ expect(@client.get("key2")).to eq("1")
362
388
  end
363
389
 
364
390
  it "does not set the key if both xx and nx option are specified" do
365
- @client.set("key2", "1", { :nx => true, :xx => true }).should == false
366
- @client.get("key2").should be_nil
391
+ expect(@client.set("key2", "1", { :nx => true, :xx => true })).to eq(false)
392
+ expect(@client.get("key2")).to be_nil
367
393
  end
368
394
  end
395
+
396
+ describe "#dump" do
397
+ it "returns nil for unknown key" do
398
+ expect(@client.exists("key1")).to be false
399
+ expect(@client.dump("key1")).to be nil
400
+ end
401
+
402
+ it "dumps a single known key successfully" do
403
+ @client.set("key1", "zomgwtf")
404
+
405
+ value = @client.dump("key1")
406
+ expect(value).not_to eq nil
407
+ expect(value).to be_a_kind_of(String)
408
+ end
409
+
410
+ it "errors with more than one argument" do
411
+ expect { @client.dump("key1", "key2") }.to raise_error(ArgumentError)
412
+ end
413
+ end
414
+
415
+ describe "#restore" do
416
+ it "errors with a missing payload" do
417
+ expect do
418
+ @client.restore("key1", 0, nil)
419
+ end.to raise_error(Redis::CommandError, "ERR DUMP payload version or checksum are wrong")
420
+ end
421
+
422
+ it "errors with an invalid payload" do
423
+ expect do
424
+ @client.restore("key1", 0, "zomgwtf not valid")
425
+ end.to raise_error(Redis::CommandError, "ERR DUMP payload version or checksum are wrong")
426
+ end
427
+
428
+ describe "with a dumped value" do
429
+ before do
430
+ @client.set("key1", "original value")
431
+ @dumped_value = @client.dump("key1")
432
+
433
+ @client.del("key1")
434
+ expect(@client.exists("key1")).to be false
435
+ end
436
+
437
+ it "restores to a new key successfully" do
438
+ response = @client.restore("key1", 0, @dumped_value)
439
+ expect(response).to eq "OK"
440
+ end
441
+
442
+ it "errors trying to restore to an existing key" do
443
+ @client.set("key1", "something else")
444
+
445
+ expect do
446
+ @client.restore("key1", 0, @dumped_value)
447
+ end.to raise_error(Redis::CommandError, "ERR Target key name is busy.")
448
+ end
449
+
450
+ it "restores successfully with a given expire time" do
451
+ @client.restore("key2", 2000, @dumped_value)
452
+
453
+ expect(@client.ttl("key2")).to eq 2
454
+ end
455
+
456
+ it "restores a list successfully" do
457
+ @client.lpush("key1", "val1")
458
+ @client.lpush("key1", "val2")
459
+
460
+ expect(@client.type("key1")).to eq "list"
461
+
462
+ dumped_value = @client.dump("key1")
463
+
464
+ response = @client.restore("key2", 0, dumped_value)
465
+ expect(response).to eq "OK"
466
+
467
+ expect(@client.type("key2")).to eq "list"
468
+ end
469
+
470
+ it "restores a set successfully" do
471
+ @client.sadd("key1", "val1")
472
+ @client.sadd("key1", "val2")
473
+
474
+ expect(@client.type("key1")).to eq "set"
475
+
476
+ dumped_value = @client.dump("key1")
477
+
478
+ response = @client.restore("key2", 0, dumped_value)
479
+ expect(response).to eq "OK"
480
+
481
+ expect(@client.type("key2")).to eq "set"
482
+ end
483
+ end
484
+ end
485
+
369
486
  end
370
487
  end
371
488