File: server_spec.rb

package info (click to toggle)
ruby-fakeredis 0.8.0-7
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 628 kB
  • sloc: ruby: 4,868; makefile: 2
file content (114 lines) | stat: -rw-r--r-- 3,059 bytes parent folder | download
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
require 'spec_helper'

module FakeRedis
  describe "ServerMethods" do

    before(:each) do
      @client = Redis.new
    end

    it "should return the number of keys in the selected database" do
      @client.set("key1", "1")
      @client.set("key2", "2")
      @client.set("key2", "two")

      expect(@client.dbsize).to eq(2)
    end

    it "should get information and statistics about the server" do
      expect(@client.info.key?("redis_version")).to eq(true)
    end

    it "should handle non-existent methods" do
      expect { @client.idontexist }.to raise_error(Redis::CommandError, "ERR unknown command 'idontexist'")
    end

    describe "multiple databases" do
      it "should default to database 0" do
        expect(@client.inspect).to match(%r#/0>$#)
      end

      it "should select another database" do
        @client.select(1)
        expect(@client.inspect).to match(%r#/1>$#)
      end

      it "should store keys separately in each database" do
        expect(@client.select(0)).to eq("OK")
        @client.set("key1", "1")
        @client.set("key2", "2")

        @client.select(1)
        @client.set("key3", "3")
        @client.set("key4", "4")
        @client.set("key5", "5")

        @client.select(0)
        expect(@client.dbsize).to eq(2)
        expect(@client.exists("key1")).to be 1
        expect(@client.exists("key3")).to be 0

        @client.select(1)
        expect(@client.dbsize).to eq(3)
        expect(@client.exists("key4")).to be 1
        expect(@client.exists("key2")).to be 0

        @client.flushall
        expect(@client.dbsize).to eq(0)

        @client.select(0)
        expect(@client.dbsize).to eq(0)
      end

      it "should flush a database" do
        @client.select(0)
        @client.set("key1", "1")
        @client.set("key2", "2")
        expect(@client.dbsize).to eq(2)

        @client.select(1)
        @client.set("key3", "3")
        @client.set("key4", "4")
        expect(@client.dbsize).to eq(2)

        expect(@client.flushdb).to eq("OK")

        expect(@client.dbsize).to eq(0)
        @client.select(0)
        expect(@client.dbsize).to eq(2)
      end

      it "should flush all databases" do
        @client.select(0)
        @client.set("key3", "3")
        @client.set("key4", "4")
        expect(@client.dbsize).to eq(2)

        @client.select(1)
        @client.set("key3", "3")
        @client.set("key4", "4")
        expect(@client.dbsize).to eq(2)

        expect(@client.flushall).to eq("OK")

        expect(@client.dbsize).to eq(0)
        @client.select(0)
        expect(@client.dbsize).to eq(0)
      end
    end
  end

  describe 'custom options' do
    describe 'version' do
      it 'reports default Redis version when not provided' do
        client = Redis.new
        expect(client.info['redis_version']).to eq Redis::Connection::DEFAULT_REDIS_VERSION
      end

      it 'creates with and reports properly' do
        client = Redis.new(version: '3.3.0')
        expect(client.info['redis_version']).to eq '3.3.0'
      end
    end
  end
end