Sha256: 39f22f815578a5ab603e4e955c9465684949602c3349ede3fed777f1b609d3c8

Contents?: true

Size: 1.94 KB

Versions: 11

Compression:

Stored size: 1.94 KB

Contents

require 'test_helper'

class CachingTest < Test::Unit::TestCase
  context "Caching" do
    setup do
      @klass = Class.new do
        extend MongoMapper::Plugins
        plugin MongoMapper::Plugins::Caching
      end
      @klass.stubs(:name).returns('Post')
      @klass.any_instance.stubs(:persisted?).returns(true)
      @klass.any_instance.stubs(:[]).returns(nil)
      @klass.any_instance.stubs(:[]=).returns(nil)
    end

    context "new" do
      setup do
        @doc = @klass.new
        @doc.stubs(:persisted?).returns(false)
      end

      should "be class/new" do
        @doc.cache_key.should == 'Post/new'
      end

      should "work with suffix" do
        @doc.cache_key(:foo).
          should == 'Post/new/foo'

        @doc.cache_key(:foo, :bar).
          should == 'Post/new/foo/bar'
      end
    end

    context "not new" do
      setup do
        @object_id = BSON::ObjectId.new
        @doc = @klass.new
        @doc.stubs(:persisted).returns(true)
        @doc.stubs(:id).returns(@object_id)
      end

      context "with updated_at" do
        setup do
          time = Time.utc(2010, 6, 20, 8, 10, 7)
          @doc.stubs(:[]).with(:updated_at).returns(time)
        end

        should "be class/id-timestamp" do
          @doc.cache_key.should == "Post/#{@object_id}-20100620081007"
        end

        should "work with suffix" do
          @doc.cache_key(:foo).
            should == "Post/#{@object_id}-20100620081007/foo"

          @doc.cache_key(:foo, :bar).
            should == "Post/#{@object_id}-20100620081007/foo/bar"
        end
      end

      context "without updated_at" do
        should "be class/id" do
          @doc.cache_key.should == "Post/#{@object_id}"
        end

        should "work with suffix" do
          @doc.cache_key(:foo).
            should == "Post/#{@object_id}/foo"

          @doc.cache_key(:foo, :bar, :baz).
            should == "Post/#{@object_id}/foo/bar/baz"
        end
      end
    end
  end
end

Version data entries

11 entries across 11 versions & 3 rubygems

Version Path
mongo_mapper-0.12.0 test/functional/test_caching.rb
lookout-mongo_mapper-0.11.3 test/functional/test_caching.rb
mongo_mapper-0.11.2 test/functional/test_caching.rb
jamieorc-mongo_mapper-0.11.1.1 test/functional/test_caching.rb
mongo_mapper-0.11.1 test/functional/test_caching.rb
mongo_mapper-0.11.0 test/functional/test_caching.rb
mongo_mapper-0.10.1 test/functional/test_caching.rb
mongo_mapper-0.10.0 test/functional/test_caching.rb
mongo_mapper-0.9.2 test/functional/test_caching.rb
mongo_mapper-0.9.1 test/functional/test_caching.rb
mongo_mapper-0.9.0 test/functional/test_caching.rb