Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 20 additions & 13 deletions BrainPortal/spec/models/data_provider_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -849,13 +849,20 @@
end

describe "#cache_revision_of_last_init" do
let(:cache_root) { Pathname.new("cache") }
let(:cache_file) { (cache_root + DataProvider::DP_CACHE_ID_FILE).to_s }
let(:cache_rev) { "1234-12-12" }
before(:each) do
allow(DataProvider).to receive(:cache_rootdir).and_return("cache")
# Preserve filesystem access used by lazy loading and other RSpec hooks.
allow(File).to receive(:exist?).and_call_original
allow(File).to receive(:read).and_call_original
allow(IO).to receive(:sysopen).and_call_original
allow(IO).to receive(:open).and_call_original
allow(DataProvider).to receive(:cache_rootdir).and_return(cache_root)
allow(DataProvider).to receive(:class_variable_defined?).and_return(false)
allow(DataProvider).to receive(:this_is_a_proper_cache_dir!)
allow(File).to receive(:exist?).and_return(true)
allow(File).to receive(:read).and_return(cache_rev)
allow(File).to receive(:exist?).with(cache_file).and_return(true)
allow(File).to receive(:read).with(cache_file).and_return(cache_rev)
end
it "should check if the cache revision variable is already defined" do
DataProvider.class_variable_set("@@cache_rev", Time.now.to_s)
Expand All @@ -867,24 +874,24 @@
DataProvider.cache_revision_of_last_init
end
it "should check if the information already exists in a file" do
expect(File).to receive(:exist?).and_return(true)
expect(File).to receive(:exist?).with(cache_file).and_return(true)
DataProvider.cache_revision_of_last_init
end
it "should read information from the file if it exists" do
expect(File).to receive(:read).and_return(cache_rev)
expect(File).to receive(:read).with(cache_file).and_return(cache_rev)
DataProvider.cache_revision_of_last_init
end
context "when the information does not already exist in a file" do
let(:file_descriptor) {double("fd").as_null_object}
let(:file_handle) {double("fh").as_null_object}
before(:each) do
allow(File).to receive(:exist?).and_return(false)
allow(File).to receive(:exist?).with(cache_file).and_return(false)
allow(DataProvider).to receive_message_chain(:revision_info).and_return(double("rev_info").as_null_object)
allow(IO).to receive(:sysopen).and_return(file_descriptor)
allow(IO).to receive(:open).and_return(file_handle)
allow(IO).to receive(:sysopen).with(cache_file, anything).and_return(file_descriptor)
allow(IO).to receive(:open).with(file_descriptor).and_return(file_handle)
end
it "should open a file" do
expect(IO).to receive(:open).and_return(file_handle)
expect(IO).to receive(:open).with(file_descriptor).and_return(file_handle)
DataProvider.cache_revision_of_last_init
end
it "should attempt to write to the file" do
Expand All @@ -897,19 +904,19 @@
end
context "when the write fails" do
before(:each) do
allow(IO).to receive(:sysopen).and_raise(StandardError)
allow(IO).to receive(:sysopen).with(cache_file, anything).and_raise(StandardError)
end
it "should raise an exception if the file does not exist" do
allow(File).to receive(:exist?).and_return(false)
allow(File).to receive(:exist?).with(cache_file).and_return(false)
expect {DataProvider.cache_revision_of_last_init}.to raise_error(RuntimeError, /could not create/)
end
context "because the file was just created" do
before(:each) do
allow(File).to receive(:exist?).and_return(false, true)
allow(File).to receive(:exist?).with(cache_file).and_return(false, true)
allow(DataProvider).to receive(:sleep)
end
it "should read the file" do
expect(File).to receive(:read).and_return(cache_rev)
expect(File).to receive(:read).with(cache_file).and_return(cache_rev)
DataProvider.cache_revision_of_last_init
end
it "should raise an exception if the revision info is blank" do
Expand Down