Sha256: b175886092ef182c9c96d8d1d11859a114e5070e138d93e3bf273caa1ef1893d

Contents?: true

Size: 1.68 KB

Versions: 17

Compression:

Stored size: 1.68 KB

Contents

require "spec_helper"

describe Shamu::Entities::IdentityCache do
  let( :klass ) do
    Class.new( Shamu::Entities::Entity ) do
      attribute :id
      attribute :name
    end
  end

  let( :cache )  { scorpion.new( Shamu::Entities::IdentityCache, :to_i ) }
  let( :entity ) { klass.new id: 89, name: "Sparkles" }

  describe "#fetch" do
    before( :each ) do
      cache.add( entity.id, entity )
    end

    it "fetches id by Number" do
      expect( cache.fetch( entity.id ) ).to be entity
    end

    it "fetches id by String" do
      expect( cache.fetch( entity.id.to_s ) ).to be entity
    end
  end

  describe "#uncached_keys" do
    before( :each ) do
      cache.add( entity.id, entity )
    end

    it "includes keys that have not been cached" do
      expect( cache.uncached_keys( [ 90 ] ) ).to eq [ 90 ]
    end

    it "includes coerced keys that have not been cached" do
      expect( cache.uncached_keys( [ "100" ] ) ).to eq [ 100 ]
    end

    it "excludes numeric keys that have already been cached" do
      expect( cache.uncached_keys( [ 88, 89 ] ) ).to eq [ 88 ]
    end

    it "excludes string keys that have already been cached" do
      expect( cache.uncached_keys( [ "88", "89" ] ) ).to eq [ 88 ]
    end
  end

  describe "#add" do
    it "adds entity to cache" do
      cache.add( entity.id, entity )
      expect( cache.fetch( entity.id ) ).to be entity
    end

    it "returns the entity" do
      expect( cache.add( entity.id, entity ) ).to be entity
    end
  end

  describe "#invalidate" do
    it "removes entity from cache" do
      cache.add( entity.id, entity )
      cache.invalidate( entity.id )

      expect( cache.fetch( entity.id ) ).to be_blank
    end
  end
end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
shamu-0.0.24 spec/lib/shamu/entities/identity_cache_spec.rb
shamu-0.0.21 spec/lib/shamu/entities/identity_cache_spec.rb
shamu-0.0.20 spec/lib/shamu/entities/identity_cache_spec.rb
shamu-0.0.19 spec/lib/shamu/entities/identity_cache_spec.rb
shamu-0.0.18 spec/lib/shamu/entities/identity_cache_spec.rb
shamu-0.0.17 spec/lib/shamu/entities/identity_cache_spec.rb
shamu-0.0.15 spec/lib/shamu/entities/identity_cache_spec.rb
shamu-0.0.14 spec/lib/shamu/entities/identity_cache_spec.rb
shamu-0.0.13 spec/lib/shamu/entities/identity_cache_spec.rb
shamu-0.0.11 spec/lib/shamu/entities/identity_cache_spec.rb
shamu-0.0.9 spec/lib/shamu/entities/identity_cache_spec.rb
shamu-0.0.8 spec/lib/shamu/entities/identity_cache_spec.rb
shamu-0.0.7 spec/lib/shamu/entities/identity_cache_spec.rb
shamu-0.0.5 spec/lib/shamu/entities/identity_cache_spec.rb
shamu-0.0.4 spec/lib/shamu/entities/identity_cache_spec.rb
shamu-0.0.3 spec/lib/shamu/entities/identity_cache_spec.rb
shamu-0.0.2 spec/lib/shamu/entities/identity_cache_spec.rb