Sha256: 616ca17ebf07c25e725159ac3ab01fd1738a0db02af28e560efb88902be54337

Contents?: true

Size: 1.4 KB

Versions: 2

Compression:

Stored size: 1.4 KB

Contents

module Spreeference
  describe Spreeference::Store, type: :model do
    before :each do
      @store = Spreeference::StoreInstance.new
    end

    it "sets and gets a key" do
      @store.set :test, 1
      expect(@store.exist?(:test)).to be true
      expect(@store.get(:test)).to eq 1
    end

    it "can set and get false values when cache return nil" do
      @store.set :test, false
      expect(@store.get(:test)).to be false
    end

    it "will return db value when cache is emtpy and cache the db value" do
      preference = Spreeference::Preference.where(key: 'test').first_or_initialize
      preference.value = '123'
      preference.save

      Rails.cache.clear
      expect(@store.get(:test)).to eq '123'
      expect(Rails.cache.read(:test)).to eq '123'
    end

    it "should return and cache fallback value when supplied" do
      Rails.cache.clear
      expect(@store.get(:test){ false }).to be false
      expect(Rails.cache.read(:test)).to be false
    end

    it "should return but not cache fallback value when persistence is disabled" do
      Rails.cache.clear
      allow(@store).to receive_messages(should_persist?: false)
      expect(@store.get(:test){ true }).to be true
      expect(Rails.cache.exist?(:test)).to be false
    end

    it "should return nil when key can't be found and fallback value is not supplied" do
      expect(@store.get(:random_key){ nil }).to be_nil
    end

  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
spreeference-0.1.1 spec/lib/spreeference/store_spec.rb
spreeference-0.1.0 spec/lib/spreeference/store_spec.rb