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 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390
|
# frozen_string_literal: true
module Lint
module Strings
def mock(*args, &block)
redis_mock(*args, &block)
end
def test_set_and_get
r.set("foo", "s1")
assert_equal "s1", r.get("foo")
end
def test_set_and_get_with_newline_characters
r.set("foo", "1\n")
assert_equal "1\n", r.get("foo")
end
def test_set_and_get_with_non_string_value
value = ["a", "b"]
r.set("foo", value)
assert_equal value.to_s, r.get("foo")
end
def test_set_and_get_with_ascii_characters
(0..255).each do |i|
str = "#{i.chr}---#{i.chr}"
r.set("foo", str)
assert_equal str, r.get("foo")
end
end
def test_set_with_ex
r.set("foo", "bar", ex: 2)
assert_in_range 0..2, r.ttl("foo")
end
def test_set_with_px
r.set("foo", "bar", px: 2000)
assert_in_range 0..2, r.ttl("foo")
end
def test_set_with_exat
target_version "6.2" do
r.set("foo", "bar", exat: Time.now.to_i + 2)
assert_in_range 0..2, r.ttl("foo")
end
end
def test_set_with_pxat
target_version "6.2" do
r.set("foo", "bar", pxat: (1000 * Time.now.to_i) + 2000)
assert_in_range 0..2, r.ttl("foo")
end
end
def test_set_with_nx
r.set("foo", "qux", nx: true)
assert !r.set("foo", "bar", nx: true)
assert_equal "qux", r.get("foo")
r.del("foo")
assert r.set("foo", "bar", nx: true)
assert_equal "bar", r.get("foo")
end
def test_set_with_xx
r.set("foo", "qux")
assert r.set("foo", "bar", xx: true)
assert_equal "bar", r.get("foo")
r.del("foo")
assert !r.set("foo", "bar", xx: true)
end
def test_set_with_keepttl
target_version "6.0.0" do
r.set("foo", "qux", ex: 2)
assert_in_range 0..2, r.ttl("foo")
r.set("foo", "bar", keepttl: true)
assert_in_range 0..2, r.ttl("foo")
end
end
def test_set_with_get
target_version "6.2" do
r.set("foo", "qux")
assert_equal "qux", r.set("foo", "bar", get: true)
assert_equal "bar", r.get("foo")
assert_nil r.set("baz", "bar", get: true)
assert_equal "bar", r.get("baz")
end
end
def test_setex
assert r.setex("foo", 1, "bar")
assert_equal "bar", r.get("foo")
assert [0, 1].include? r.ttl("foo")
end
def test_setex_with_non_string_value
value = ["b", "a", "r"]
assert r.setex("foo", 1, value)
assert_equal value.to_s, r.get("foo")
assert [0, 1].include? r.ttl("foo")
end
def test_psetex
assert r.psetex("foo", 1000, "bar")
assert_equal "bar", r.get("foo")
assert [0, 1].include? r.ttl("foo")
end
def test_psetex_with_non_string_value
value = ["b", "a", "r"]
assert r.psetex("foo", 1000, value)
assert_equal value.to_s, r.get("foo")
assert [0, 1].include? r.ttl("foo")
end
def test_getex
target_version "6.2" do
assert r.setex("foo", 1000, "bar")
assert_equal "bar", r.getex("foo", persist: true)
assert_equal(-1, r.ttl("foo"))
end
end
def test_getdel
target_version "6.2" do
assert r.set("foo", "bar")
assert_equal "bar", r.getdel("foo")
assert_nil r.get("foo")
end
end
def test_getset
r.set("foo", "bar")
assert_equal "bar", r.getset("foo", "baz")
assert_equal "baz", r.get("foo")
end
def test_getset_with_non_string_value
r.set("foo", "zap")
value = ["b", "a", "r"]
assert_equal "zap", r.getset("foo", value)
assert_equal value.to_s, r.get("foo")
end
def test_setnx
r.set("foo", "qux")
assert !r.setnx("foo", "bar")
assert_equal "qux", r.get("foo")
r.del("foo")
assert r.setnx("foo", "bar")
assert_equal "bar", r.get("foo")
end
def test_setnx_with_non_string_value
value = ["b", "a", "r"]
r.set("foo", "qux")
assert !r.setnx("foo", value)
assert_equal "qux", r.get("foo")
r.del("foo")
assert r.setnx("foo", value)
assert_equal value.to_s, r.get("foo")
end
def test_incr
assert_equal 1, r.incr("foo")
assert_equal 2, r.incr("foo")
assert_equal 3, r.incr("foo")
end
def test_incrby
assert_equal 1, r.incrby("foo", 1)
assert_equal 3, r.incrby("foo", 2)
assert_equal 6, r.incrby("foo", 3)
end
def test_incrbyfloat
assert_equal 1.23, r.incrbyfloat("foo", 1.23)
assert_equal 2, r.incrbyfloat("foo", 0.77)
assert_equal 1.9, r.incrbyfloat("foo", -0.1)
end
def test_decr
r.set("foo", 3)
assert_equal 2, r.decr("foo")
assert_equal 1, r.decr("foo")
assert_equal 0, r.decr("foo")
end
def test_decrby
r.set("foo", 6)
assert_equal 3, r.decrby("foo", 3)
assert_equal 1, r.decrby("foo", 2)
assert_equal 0, r.decrby("foo", 1)
end
def test_append
r.set "foo", "s"
r.append "foo", "1"
assert_equal "s1", r.get("foo")
end
def test_getbit
r.set("foo", "a")
assert_equal 1, r.getbit("foo", 1)
assert_equal 1, r.getbit("foo", 2)
assert_equal 0, r.getbit("foo", 3)
assert_equal 0, r.getbit("foo", 4)
assert_equal 0, r.getbit("foo", 5)
assert_equal 0, r.getbit("foo", 6)
assert_equal 1, r.getbit("foo", 7)
end
def test_setbit
r.set("foo", "a")
r.setbit("foo", 6, 1)
assert_equal "c", r.get("foo")
end
def test_bitcount
r.set("foo", "abcde")
assert_equal 10, r.bitcount("foo", 1, 3)
assert_equal 17, r.bitcount("foo", 0, -1)
end
def test_bitcount_bits_range
target_version "7.0" do
r.set("foo", "abcde")
assert_equal 10, r.bitcount("foo", 8, 31, scale: :bit)
assert_equal 17, r.bitcount("foo", 0, -1, scale: :byte)
end
end
def test_getrange
r.set("foo", "abcde")
assert_equal "bcd", r.getrange("foo", 1, 3)
assert_equal "abcde", r.getrange("foo", 0, -1)
end
def test_setrange
r.set("foo", "abcde")
r.setrange("foo", 1, "bar")
assert_equal "abare", r.get("foo")
end
def test_setrange_with_non_string_value
r.set("foo", "abcde")
value = ["b", "a", "r"]
r.setrange("foo", 2, value)
assert_equal "ab#{value}", r.get("foo")
end
def test_strlen
r.set "foo", "lorem"
assert_equal 5, r.strlen("foo")
end
def test_bitfield
mock(bitfield: ->(*_) { "*2\r\n:1\r\n:0\r\n" }) do |redis|
assert_equal [1, 0], redis.bitfield('foo', 'INCRBY', 'i5', 100, 1, 'GET', 'u4', 0)
end
end
def test_mget
r.set('{1}foo', 's1')
r.set('{1}bar', 's2')
assert_equal %w[s1 s2], r.mget('{1}foo', '{1}bar')
assert_equal ['s1', 's2', nil], r.mget('{1}foo', '{1}bar', '{1}baz')
assert_equal ['s1', 's2', nil], r.mget(['{1}foo', '{1}bar', '{1}baz'])
end
def test_mget_mapped
r.set('{1}foo', 's1')
r.set('{1}bar', 's2')
response = r.mapped_mget('{1}foo', '{1}bar')
assert_equal 's1', response['{1}foo']
assert_equal 's2', response['{1}bar']
response = r.mapped_mget('{1}foo', '{1}bar', '{1}baz')
assert_equal 's1', response['{1}foo']
assert_equal 's2', response['{1}bar']
assert_nil response['{1}baz']
end
def test_mapped_mget_in_a_pipeline_returns_hash
r.set('{1}foo', 's1')
r.set('{1}bar', 's2')
result = r.pipelined do |pipeline|
pipeline.mapped_mget('{1}foo', '{1}bar')
end
assert_equal({ '{1}foo' => 's1', '{1}bar' => 's2' }, result[0])
end
def test_mset
r.mset('{1}foo', 's1', '{1}bar', 's2')
assert_equal 's1', r.get('{1}foo')
assert_equal 's2', r.get('{1}bar')
end
def test_mset_mapped
r.mapped_mset('{1}foo' => 's1', '{1}bar' => 's2')
assert_equal 's1', r.get('{1}foo')
assert_equal 's2', r.get('{1}bar')
end
def test_msetnx
r.set('{1}foo', 's1')
assert_equal false, r.msetnx('{1}foo', 's2', '{1}bar', 's3')
assert_equal 's1', r.get('{1}foo')
assert_nil r.get('{1}bar')
r.del('{1}foo')
assert_equal true, r.msetnx('{1}foo', 's2', '{1}bar', 's3')
assert_equal 's2', r.get('{1}foo')
assert_equal 's3', r.get('{1}bar')
end
def test_msetnx_mapped
r.set('{1}foo', 's1')
assert_equal false, r.mapped_msetnx('{1}foo' => 's2', '{1}bar' => 's3')
assert_equal 's1', r.get('{1}foo')
assert_nil r.get('{1}bar')
r.del('{1}foo')
assert_equal true, r.mapped_msetnx('{1}foo' => 's2', '{1}bar' => 's3')
assert_equal 's2', r.get('{1}foo')
assert_equal 's3', r.get('{1}bar')
end
def test_bitop
r.set('foo{1}', 'a')
r.set('bar{1}', 'b')
r.bitop(:and, 'foo&bar{1}', 'foo{1}', 'bar{1}')
assert_equal "\x60", r.get('foo&bar{1}')
r.bitop(:and, 'foo&bar{1}', ['foo{1}', 'bar{1}'])
assert_equal "\x60", r.get('foo&bar{1}')
r.bitop(:or, 'foo|bar{1}', 'foo{1}', 'bar{1}')
assert_equal "\x63", r.get('foo|bar{1}')
r.bitop(:xor, 'foo^bar{1}', 'foo{1}', 'bar{1}')
assert_equal "\x03", r.get('foo^bar{1}')
r.bitop(:not, '~foo{1}', 'foo{1}')
assert_equal "\x9E".b, r.get('~foo{1}')
end
end
end
|