File: commands_on_strings_test.rb

package info (click to toggle)
ruby-redis 3.2.2-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 736 kB
  • ctags: 1,128
  • sloc: ruby: 8,149; makefile: 5
file content (101 lines) | stat: -rw-r--r-- 2,470 bytes parent folder | download | duplicates (3)
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
# encoding: UTF-8

require File.expand_path("helper", File.dirname(__FILE__))
require "lint/strings"

class TestCommandsOnStrings < Test::Unit::TestCase

  include Helper::Client
  include Lint::Strings

  def test_mget
    r.set("foo", "s1")
    r.set("bar", "s2")

    assert_equal ["s1", "s2"]     , r.mget("foo", "bar")
    assert_equal ["s1", "s2", nil], r.mget("foo", "bar", "baz")
  end

  def test_mget_mapped
    r.set("foo", "s1")
    r.set("bar", "s2")

    response = r.mapped_mget("foo", "bar")

    assert_equal "s1", response["foo"]
    assert_equal "s2", response["bar"]

    response = r.mapped_mget("foo", "bar", "baz")

    assert_equal "s1", response["foo"]
    assert_equal "s2", response["bar"]
    assert_equal nil , response["baz"]
  end

  def test_mapped_mget_in_a_pipeline_returns_hash
    r.set("foo", "s1")
    r.set("bar", "s2")

    result = r.pipelined do
      r.mapped_mget("foo", "bar")
    end

    assert_equal result[0], { "foo" => "s1", "bar" => "s2" }
  end

  def test_mset
    r.mset(:foo, "s1", :bar, "s2")

    assert_equal "s1", r.get("foo")
    assert_equal "s2", r.get("bar")
  end

  def test_mset_mapped
    r.mapped_mset(:foo => "s1", :bar => "s2")

    assert_equal "s1", r.get("foo")
    assert_equal "s2", r.get("bar")
  end

  def test_msetnx
    r.set("foo", "s1")
    assert_equal false, r.msetnx(:foo, "s2", :bar, "s3")
    assert_equal "s1", r.get("foo")
    assert_equal nil, r.get("bar")

    r.del("foo")
    assert_equal true, r.msetnx(:foo, "s2", :bar, "s3")
    assert_equal "s2", r.get("foo")
    assert_equal "s3", r.get("bar")
  end

  def test_msetnx_mapped
    r.set("foo", "s1")
    assert_equal false, r.mapped_msetnx(:foo => "s2", :bar => "s3")
    assert_equal "s1", r.get("foo")
    assert_equal nil, r.get("bar")

    r.del("foo")
    assert_equal true, r.mapped_msetnx(:foo => "s2", :bar => "s3")
    assert_equal "s2", r.get("foo")
    assert_equal "s3", r.get("bar")
  end

  def test_bitop
    try_encoding("UTF-8") do
      target_version "2.5.10" do
        r.set("foo", "a")
        r.set("bar", "b")

        r.bitop(:and, "foo&bar", "foo", "bar")
        assert_equal "\x60", r.get("foo&bar")
        r.bitop(:or, "foo|bar", "foo", "bar")
        assert_equal "\x63", r.get("foo|bar")
        r.bitop(:xor, "foo^bar", "foo", "bar")
        assert_equal "\x03", r.get("foo^bar")
        r.bitop(:not, "~foo", "foo")
        assert_equal "\x9E", r.get("~foo")
      end
    end
  end
end