PageRenderTime 32ms CodeModel.GetById 8ms app.highlight 20ms RepoModel.GetById 1ms app.codeStats 1ms

/vendor/bundle/jruby/2.1/gems/redis-3.0.7/test/commands_on_value_types_test.rb

https://github.com/delowong/logstash
Ruby | 131 lines | 87 code | 40 blank | 4 comment | 2 complexity | 394d13dc31407759436067835750dc09 MD5 | raw file
  1# encoding: UTF-8
  2
  3require File.expand_path("helper", File.dirname(__FILE__))
  4require "lint/value_types"
  5
  6class TestCommandsOnValueTypes < Test::Unit::TestCase
  7
  8  include Helper::Client
  9  include Lint::ValueTypes
 10
 11  def test_del
 12    r.set "foo", "s1"
 13    r.set "bar", "s2"
 14    r.set "baz", "s3"
 15
 16    assert_equal ["bar", "baz", "foo"], r.keys("*").sort
 17
 18    assert_equal 1, r.del("foo")
 19
 20    assert_equal ["bar", "baz"], r.keys("*").sort
 21
 22    assert_equal 2, r.del("bar", "baz")
 23
 24    assert_equal [], r.keys("*").sort
 25  end
 26
 27  def test_del_with_array_argument
 28    r.set "foo", "s1"
 29    r.set "bar", "s2"
 30    r.set "baz", "s3"
 31
 32    assert_equal ["bar", "baz", "foo"], r.keys("*").sort
 33
 34    assert_equal 1, r.del(["foo"])
 35
 36    assert_equal ["bar", "baz"], r.keys("*").sort
 37
 38    assert_equal 2, r.del(["bar", "baz"])
 39
 40    assert_equal [], r.keys("*").sort
 41  end
 42
 43  def test_randomkey
 44    assert r.randomkey.to_s.empty?
 45
 46    r.set("foo", "s1")
 47
 48    assert_equal "foo", r.randomkey
 49
 50    r.set("bar", "s2")
 51
 52    4.times do
 53      assert ["foo", "bar"].include?(r.randomkey)
 54    end
 55  end
 56
 57  def test_rename
 58    r.set("foo", "s1")
 59    r.rename "foo", "bar"
 60
 61    assert_equal "s1", r.get("bar")
 62    assert_equal nil, r.get("foo")
 63  end
 64
 65  def test_renamenx
 66    r.set("foo", "s1")
 67    r.set("bar", "s2")
 68
 69    assert_equal false, r.renamenx("foo", "bar")
 70
 71    assert_equal "s1", r.get("foo")
 72    assert_equal "s2", r.get("bar")
 73  end
 74
 75  def test_dbsize
 76    assert_equal 0, r.dbsize
 77
 78    r.set("foo", "s1")
 79
 80    assert_equal 1, r.dbsize
 81  end
 82
 83  def test_flushdb
 84    r.set("foo", "s1")
 85    r.set("bar", "s2")
 86
 87    assert_equal 2, r.dbsize
 88
 89    r.flushdb
 90
 91    assert_equal 0, r.dbsize
 92  end
 93
 94  def test_flushall
 95    redis_mock(:flushall => lambda { "+FLUSHALL" }) do |redis|
 96      assert_equal "FLUSHALL", redis.flushall
 97    end
 98  end
 99
100  def test_migrate
101    redis_mock(:migrate => lambda { |*args| args }) do |redis|
102      options = { :host => "127.0.0.1", :port => 1234 }
103
104      assert_raise(RuntimeError, /host not specified/) do
105        redis.migrate("foo", options.reject { |key, _| key == :host })
106      end
107
108      assert_raise(RuntimeError, /port not specified/) do
109        redis.migrate("foo", options.reject { |key, _| key == :port })
110      end
111
112      default_db = redis.client.db.to_i
113      default_timeout = redis.client.timeout.to_i
114
115      # Test defaults
116      actual = redis.migrate("foo", options)
117      expected = ["127.0.0.1", "1234", "foo", default_db.to_s, default_timeout.to_s]
118      assert_equal expected, actual
119
120      # Test db override
121      actual = redis.migrate("foo", options.merge(:db => default_db + 1))
122      expected = ["127.0.0.1", "1234", "foo", (default_db + 1).to_s, default_timeout.to_s]
123      assert_equal expected, actual
124
125      # Test timeout override
126      actual = redis.migrate("foo", options.merge(:timeout => default_timeout + 1))
127      expected = ["127.0.0.1", "1234", "foo", default_db.to_s, (default_timeout + 1).to_s]
128      assert_equal expected, actual
129    end
130  end
131end