Sha256: 1b1f687b998f972801b045091756347b6f0d0a123af742b7bf3572d6f1b5d116

Contents?: true

Size: 1.82 KB

Versions: 4

Compression:

Stored size: 1.82 KB

Contents

require 'spec_helper'

describe "Caching" do
  before do
    @klass = Class.new do
      extend MongoMapper::Plugins
      plugin MongoMapper::Plugins::Caching
    end
    @klass.stub(:name).and_return('Post')
    @klass.any_instance.stub(:persisted?).and_return(true)
    @klass.any_instance.stub(:[]).and_return(nil)
    @klass.any_instance.stub(:[]=).and_return(nil)
  end

  context "new" do
    before do
      @doc = @klass.new
      @doc.stub(:persisted?).and_return(false)
    end

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

    it "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
    before do
      @object_id = BSON::ObjectId.new
      @doc = @klass.new
      @doc.stub(:persisted).and_return(true)
      @doc.stub(:id).and_return(@object_id)
    end

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

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

      it "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
      it "should be class/id" do
        @doc.cache_key.should == "Post/#{@object_id}"
      end

      it "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

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
mongo_mapper-0.13.1 spec/functional/caching_spec.rb
mongo_mapper-0.13.0 spec/functional/caching_spec.rb
mongo_mapper-0.13.0.beta2 spec/functional/caching_spec.rb
mongo_mapper-0.13.0.beta1 spec/functional/caching_spec.rb