require File.expand_path(File.dirname(__FILE__) + '/spec_helper') describe Itrigga::Cache::Filecache do before do Itrigga::Cache::Filecache.reset_instance @file = mock("File") @file.stub!(:read).and_return("content") @file.stub!(:write) File.stub!(:open).and_yield(@file) File.stub!(:delete) File.stub!(:exists?).and_return(false) @instance = Itrigga::Cache::Filecache.instance @opts = {} end it "should include Singleton" do Itrigga::Cache::Filecache.should include Singleton end describe "setup!" do describe "when given no options" do it "should assign default cache_dir" do Itrigga::Cache::Filecache.setup! Itrigga::Cache::Filecache.instance.cache_dir.should == "#{RAILS_ROOT}/tmp/cache" end it "should assign default timeout" do Itrigga::Cache::Filecache.setup! Itrigga::Cache::Filecache.instance.timeout.should == 3600 end end it "should assign the given cache_dir" do Itrigga::Cache::Filecache.setup! :cache_dir => "monkeys" Itrigga::Cache::Filecache.instance.cache_dir.should == "monkeys" end it "should assign the given timeout" do Itrigga::Cache::Filecache.setup! :timeout => 42 Itrigga::Cache::Filecache.instance.timeout.should == 42 end it "should return the instance" do Itrigga::Cache::Filecache.setup!().should be_a_kind_of Itrigga::Cache::Filecache end end describe "instance methods" do before do Itrigga::Cache::Filecache.setup! # need to set the defaults on the instance @file_path = "#{RAILS_ROOT}/tmp/cache/key" end describe "get" do describe "when expired" do before do @instance.stub!(:expired?).and_return(true) end it "should call delete" do @instance.should_receive(:delete).with("key", @opts) @instance.get("key", @opts) end it "should return nil" do @instance.get("key", @opts).should == nil end end describe "when not expired" do before do @instance.stub!(:expired?).and_return(false) end it "should read the file when the file exists" do @instance.stub!(:is_in_cache?).and_return(true) File.should_receive(:open).with(@file_path,'r').and_yield(@file) @file.should_receive(:read).and_return("content") @instance.get("key", @opts).should == "content" end it "should not try to read the file if not exist" do @instance.stub!(:is_in_cache?).and_return(false) File.should_not_receive(:open).with(@file_path,'r') @instance.get("key", @opts).should == nil end end end describe "set" do it "should write the content to file" do File.should_receive(:open).with(@file_path,'w').and_yield(@file) @file.should_receive(:write).with("content") @instance.set("key","content",@opts) end end describe "delete" do describe "when the file exists" do before do @instance.stub!(:is_in_cache?).and_return(true) end it "should call File.delete" do File.should_receive(:delete).with(@file_path) @instance.delete("key") end end describe "when the file does not exists" do before do @instance.stub!(:is_in_cache?).and_return(false) end it "should call File.delete" do File.should_not_receive(:delete) @instance.delete("key") end end end describe "expired?" do describe "when file is not in cache" do it "should return true" do @instance.should_receive(:is_in_cache?).with("key",@opts).and_return(false) @instance.expired?("key", @opts).should == true end end describe "when file is in cache" do before do @instance.stub!(:is_in_cache?).and_return(true) Time.stub!(:now).and_return(42) File.stub!(:mtime).and_return(20) end it "should return false if not expired" do @instance.expired?("key",@opts).should == false end it "should return true if expired" do @instance.expired?("key",{:timeout => 5}).should == true end end end end end