test/unit/rails_test.rb in memcached-1.6.1 vs test/unit/rails_test.rb in memcached-1.7.0
- old
+ new
@@ -1,15 +1,16 @@
require File.expand_path("#{File.dirname(__FILE__)}/../test_helper")
require 'active_support/duration'
+require 'logger'
class RailsTest < Test::Unit::TestCase
def setup
@servers = ['127.0.0.1:43042', '127.0.0.1:43043', "#{UNIX_SOCKET_NAME}0"]
@duration = ActiveSupport::Duration.new(2592000, [[:months, 1]])
@namespace = 'rails_test'
- @cache = Memcached::Rails.new(:servers => @servers, :namespace => @namespace)
+ @cache = Memcached::Rails.new(:servers => @servers, :namespace => @namespace, :logger => Logger.new(File.open("/dev/null", "w")))
@value = OpenStruct.new(:a => 1, :b => 2, :c => GenericClass)
@marshalled_value = Marshal.dump(@value)
end
def test_get
@@ -194,10 +195,28 @@
# should use correct ttl
@cache.expects(:set).with(key, @value, 123, nil)
@cache.write key, @value, :expires_in => 123
end
+ def test_write_with_unless_exist
+ @cache.write key, @value, :unless_exist => true
+ result = @cache.get key
+ assert_equal @value, result
+
+ rand_value = "rand-value-#{rand}"
+ @cache.write key, rand_value, :unless_exist => true
+
+ result = @cache.get key
+ assert_equal @value, result
+ end
+
+ def test_write_with_fixnum_and_raw
+ @cache.write key, 1, :raw => true
+ result = @cache.read key, :raw => true
+ assert_equal "1", result
+ end
+
def test_add_with_duration
@cache.add key, @value, @duration
result = @cache.get key
assert_equal @value, result
end
@@ -215,10 +234,23 @@
# works with options
@cache.expects(:write).with("y", 1, :foo => :bar)
@cache.fetch("y", :foo => :bar){ 1 }
end
+ def test_fetch_when_no_block_given
+ rand_key = "key-#{rand}"
+
+ # gets nil the first time
+ assert_equal nil, @cache.fetch(rand_key)
+
+ # sets value to @value
+ assert_equal @value, @cache.fetch(rand_key) { @value }
+
+ # gets @value
+ assert_equal @value, @cache.fetch(rand_key)
+ end
+
def test_read_multi
# empty
assert_equal({}, @cache.read_multi)
assert_equal({}, @cache.read_multi("a", "b"))
assert_raise TypeError do
@@ -246,9 +278,45 @@
def test_clear
@cache.write("x", 1)
assert_equal 1, @cache.read("x")
assert_equal nil, @cache.clear
assert_equal nil, @cache.read("x")
+ end
+
+ def test_increment
+ rand_key = "rand-key-#{rand}"
+ assert_equal nil, @cache.increment(rand_key)
+
+ start = 10
+ @cache.write rand_key, start.to_s, { :raw => true }
+
+ assert_equal start, @cache.read(rand_key, { :raw => true }).to_i
+ assert_equal start+1, @cache.increment(rand_key)
+
+ assert_equal start+1+5, @cache.increment(rand_key, 5)
+ end
+
+ def test_decrement
+ rand_key = "rand-key-#{rand}"
+ assert_equal nil, @cache.decrement(rand_key)
+
+ start = 10
+ @cache.write rand_key, start.to_s, { :raw => true }
+
+ assert_equal start, @cache.read(rand_key, { :raw => true }).to_i
+ assert_equal start-1, @cache.decrement(rand_key)
+
+ assert_equal start-1-5, @cache.decrement(rand_key, 5)
+ end
+
+ def test_read_with_nil_options
+ @cache.write key, @value
+ assert_equal @value, @cache.read(key, nil)
+ end
+
+ def test_write_with_nil_options
+ @cache.write key, @value, nil
+ assert_equal @value, @cache.read(key)
end
private
def key