File: hashes.rb

package info (click to toggle)
ruby-redis 5.3.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,160 kB
  • sloc: ruby: 11,445; makefile: 117; sh: 24
file content (231 lines) | stat: -rw-r--r-- 5,546 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
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# frozen_string_literal: true

module Lint
  module Hashes
    def test_hset_and_hget
      assert_equal 1, r.hset("foo", "f1", "s1")

      assert_equal "s1", r.hget("foo", "f1")
    end

    def test_variadic_hset
      assert_equal 2, r.hset("foo", "f1", "s1", "f2", "s2")

      assert_equal "s1", r.hget("foo", "f1")
      assert_equal "s2", r.hget("foo", "f2")

      assert_equal 2, r.hset("bar", { "f1" => "s1", "f2" => "s2" })

      assert_equal "s1", r.hget("bar", "f1")
      assert_equal "s2", r.hget("bar", "f2")
    end

    def test_hsetnx
      r.hset("foo", "f1", "s1")
      r.hsetnx("foo", "f1", "s2")

      assert_equal "s1", r.hget("foo", "f1")

      r.del("foo")
      r.hsetnx("foo", "f1", "s2")

      assert_equal "s2", r.hget("foo", "f1")
    end

    def test_hdel
      r.hset("foo", "f1", "s1")

      assert_equal "s1", r.hget("foo", "f1")

      assert_equal 1, r.hdel("foo", "f1")

      assert_nil r.hget("foo", "f1")
    end

    def test_splat_hdel
      r.hset("foo", "f1", "s1")
      r.hset("foo", "f2", "s2")

      assert_equal "s1", r.hget("foo", "f1")
      assert_equal "s2", r.hget("foo", "f2")

      assert_equal 2, r.hdel("foo", "f1", "f2")

      assert_nil r.hget("foo", "f1")
      assert_nil r.hget("foo", "f2")
    end

    def test_variadic_hdel
      r.hset("foo", "f1", "s1")
      r.hset("foo", "f2", "s2")

      assert_equal "s1", r.hget("foo", "f1")
      assert_equal "s2", r.hget("foo", "f2")

      assert_equal 2, r.hdel("foo", ["f1", "f2"])

      assert_nil r.hget("foo", "f1")
      assert_nil r.hget("foo", "f2")
    end

    def test_hexists
      assert_equal false, r.hexists("foo", "f1")

      r.hset("foo", "f1", "s1")

      assert r.hexists("foo", "f1")
    end

    def test_hlen
      assert_equal 0, r.hlen("foo")

      r.hset("foo", "f1", "s1")

      assert_equal 1, r.hlen("foo")

      r.hset("foo", "f2", "s2")

      assert_equal 2, r.hlen("foo")
    end

    def test_hkeys
      assert_equal [], r.hkeys("foo")

      r.hset("foo", "f1", "s1")
      r.hset("foo", "f2", "s2")

      assert_equal ["f1", "f2"], r.hkeys("foo")
    end

    def test_hrandfield
      target_version("6.2") do
        assert_nil r.hrandfield("foo")
        assert_equal [], r.hrandfield("foo", 1)

        error = assert_raises(ArgumentError) do
          r.hrandfield("foo", with_values: true)
        end
        assert_equal "count argument must be specified", error.message

        r.hset("foo", "f1", "s1")
        r.hset("foo", "f2", "s2")

        assert ["f1", "f2"].include?(r.hrandfield("foo"))
        assert_equal ["f1", "f2"], r.hrandfield("foo", 2).sort
        assert_equal 4, r.hrandfield("foo", -4).size

        r.hrandfield("foo", 2, with_values: true) do |(field, value)|
          assert ["f1", "f2"].include?(field)
          assert ["s1", "s2"].include?(value)
        end
      end
    end

    def test_hvals
      assert_equal [], r.hvals("foo")

      r.hset("foo", "f1", "s1")
      r.hset("foo", "f2", "s2")

      assert_equal ["s1", "s2"], r.hvals("foo")
    end

    def test_hgetall
      assert_equal({}, r.hgetall("foo"))

      r.hset("foo", "f1", "s1")
      r.hset("foo", "f2", "s2")

      assert_equal({ "f1" => "s1", "f2" => "s2" }, r.hgetall("foo"))
    end

    def test_hmset
      r.hmset("hash", "foo1", "bar1", "foo2", "bar2")

      assert_equal "bar1", r.hget("hash", "foo1")
      assert_equal "bar2", r.hget("hash", "foo2")
    end

    def test_hmset_with_invalid_arguments
      assert_raises(Redis::CommandError) do
        r.hmset("hash", "foo1", "bar1", "foo2", "bar2", "foo3")
      end
    end

    def test_mapped_hmset
      r.mapped_hmset("foo", f1: "s1", f2: "s2")

      assert_equal "s1", r.hget("foo", "f1")
      assert_equal "s2", r.hget("foo", "f2")
    end

    def test_hmget
      r.hset("foo", "f1", "s1")
      r.hset("foo", "f2", "s2")
      r.hset("foo", "f3", "s3")

      assert_equal ["s2", "s3"], r.hmget("foo", "f2", "f3")
    end

    def test_hmget_mapped
      r.hset("foo", "f1", "s1")
      r.hset("foo", "f2", "s2")
      r.hset("foo", "f3", "s3")

      assert_equal({ "f1" => "s1" }, r.mapped_hmget("foo", "f1"))
      assert_equal({ "f1" => "s1", "f2" => "s2" }, r.mapped_hmget("foo", "f1", "f2"))
    end

    def test_mapped_hmget_in_a_pipeline_returns_hash
      r.hset("foo", "f1", "s1")
      r.hset("foo", "f2", "s2")

      result = r.pipelined do |pipeline|
        pipeline.mapped_hmget("foo", "f1", "f2")
      end

      assert_equal({ "f1" => "s1", "f2" => "s2" }, result[0])
    end

    def test_hincrby
      r.hincrby("foo", "f1", 1)

      assert_equal "1", r.hget("foo", "f1")

      r.hincrby("foo", "f1", 2)

      assert_equal "3", r.hget("foo", "f1")

      r.hincrby("foo", "f1", -1)

      assert_equal "2", r.hget("foo", "f1")
    end

    def test_hincrbyfloat
      r.hincrbyfloat("foo", "f1", 1.23)

      assert_equal 1.23, Float(r.hget("foo", "f1"))

      r.hincrbyfloat("foo", "f1", 0.77)

      assert_equal "2", r.hget("foo", "f1")

      r.hincrbyfloat("foo", "f1", -0.1)

      assert_equal 1.9, Float(r.hget("foo", "f1"))
    end

    def test_hstrlen
      redis.hmset('foo', 'f1', 'HelloWorld', 'f2', 99, 'f3', -256)
      assert_equal 10, r.hstrlen('foo', 'f1')
      assert_equal 2, r.hstrlen('foo', 'f2')
      assert_equal 4, r.hstrlen('foo', 'f3')
    end

    def test_hscan
      redis.hmset('foo', 'f1', 'Jack', 'f2', 33)
      expected = ['0', [%w[f1 Jack], %w[f2 33]]]
      assert_equal expected, redis.hscan('foo', 0)
    end
  end
end