Added unit tests for CacheStoreDatabase and LinkageCacheStore.

This commit is contained in:
Andrew R. McBurney 2018-05-22 14:15:36 -04:00
parent ddb7f06e9f
commit 91f7a5eb76
2 changed files with 234 additions and 0 deletions

View File

@ -0,0 +1,146 @@
require "cache_store"
describe CacheStoreDatabase do
subject { CacheStoreDatabase.new(:sample) }
describe "self.use" do
let(:type) { :test }
it "creates a new `DatabaseCache` instance and yields it" do
cache_store = double("cache_store", close_if_open!: nil)
expect(CacheStoreDatabase).to receive(:new).with(type).and_return(cache_store)
expect(CacheStoreDatabase).to receive(:call).with(cache_store)
end
end
describe "#set" do
let(:db) { double("db", :[]= => nil) }
before(:each) do
allow(File).to receive(:write)
allow(subject).to receive(:created?).and_return(true)
expect(db).to receive(:has_key?).with(:foo).and_return(false)
subject.stub(:db).and_return(db)
end
it "sets the value in the `CacheStoreDatabase`" do
expect(db).to_not have_key(:foo)
subject.set(:foo, "bar")
end
end
describe "#get" do
context "database created" do
let(:db) { double("db", :[] => "bar") }
before(:each) do
allow(subject).to receive(:created?).and_return(true)
expect(db).to receive(:has_key?).with(:foo).and_return(true)
subject.stub(:db).and_return(db)
end
it "gets value in the `CacheStoreDatabase` corresponding to the key" do
expect(db).to have_key(:foo)
expect(subject.get(:foo)).to equal("bar")
end
end
context "database not created" do
let(:db) { double("db", :[] => nil) }
before(:each) do
allow(subject).to receive(:created?).and_return(false)
subject.stub(:db).and_return(db)
end
it "does not get value in the `CacheStoreDatabase` corresponding to key" do
expect(subject.get(:foo)).to_not be("bar")
end
it "does not call `db[]` if `CacheStoreDatabase.created?` is `false`" do
expect(db).not_to receive(:[])
subject.get(:foo)
end
end
end
describe "#delete" do
context "database created" do
let(:db) { double("db", :[] => { foo: "bar" }) }
before(:each) do
allow(subject).to receive(:created?).and_return(true)
subject.stub(:db).and_return(db)
end
it "deletes value in the `CacheStoreDatabase` corresponding to the key" do
expect(db).to receive(:delete).with(:foo)
subject.delete(:foo)
end
end
context "database not created" do
let(:db) { double("db", delete: nil) }
before(:each) do
allow(subject).to receive(:created?).and_return(false)
subject.stub(:db).and_return(db)
end
it "does not call `db.delete` if `CacheStoreDatabase.created?` is `false`" do
expect(db).not_to receive(:delete)
subject.delete(:foo)
end
end
end
describe "#close_if_open!" do
context "database open" do
before(:each) do
subject.instance_variable_set(:@db, instance_double("db", close: nil))
end
it "does not raise an error when `close` is called on the database" do
expect { subject.close_if_open! }.to_not raise_error(NoMethodError)
end
end
context "database not open" do
before(:each) do
subject.instance_variable_set(:@db, nil)
end
it "does not raise an error when `close` is called on the database" do
expect { subject.close_if_open! }.to_not raise_error(NoMethodError)
end
end
end
describe "#created?" do
let(:cache_path) { "path/to/homebrew/cache/sample.db" }
before(:each) do
subject.stub(:cache_path).and_return(cache_path)
end
context "`File.exist?(cache_path)` returns `true`" do
before(:each) do
allow(File).to receive(:exist?).with(cache_path).and_return(true)
end
it "returns `true`" do
expect(subject.created?).to be(true)
end
end
context "`File.exist?(cache_path)` returns `false`" do
before(:each) do
allow(File).to receive(:exist?).with(cache_path).and_return(false)
end
it "returns `false`" do
expect(subject.created?).to be(false)
end
end
end
end

View File

@ -0,0 +1,88 @@
require "test/support/fixtures/testball"
require "linkage_cache_store"
describe LinkageCacheStore do
let(:keg_name) { "keg_name" }
let(:database) { double("database") }
subject { LinkageCacheStore.new(keg_name, database) }
describe "#keg_exists?" do
context "`keg_name` exists in cache" do
before(:each) do
expect(database).to receive(:get).with(keg_name).and_return("")
end
it "returns `true`" do
expect(subject.keg_exists?).to be(true)
end
end
context "`keg_name` does not exist in cache" do
before(:each) do
expect(database).to receive(:get).with(keg_name).and_return(nil)
end
it "returns `false`" do
expect(subject.keg_exists?).to be(false)
end
end
end
describe "#update!" do
context "a `value` is a `Hash`" do
it "sets the cache for the `keg_name`" do
expect(database).to receive(:set).with(keg_name, anything)
subject.update!(a_value: { key: ["value"] })
end
end
context "a `value` is an `Array`" do
it "sets the cache for the `keg_name`" do
expect(database).to receive(:set).with(keg_name, anything)
subject.update!(a_value: ["value"])
end
end
context "a `value` is not an `Array` or `Hash`" do
it "raises a `TypeError` if a `value` is not an `Array` or `Hash`" do
expect { subject.update!(key: 1) }.to raise_error(TypeError)
end
end
end
describe "#flush_cache!" do
it "calls `delete` on the `database` with `keg_name` as parameter" do
expect(database).to receive(:delete).with(keg_name)
subject.flush_cache!
end
end
describe "#fetch_type" do
context "`HASH_LINKAGE_TYPES.include?(type)`" do
before(:each) do
expect(database).to receive(:get).with(keg_name).and_return(nil)
end
it "returns a `Hash` of values" do
expect(subject.fetch_type(:brewed_dylibs)).to be_an_instance_of(Hash)
end
end
context "`ARRAY_LINKAGE_TYPES.include?(type)`" do
before(:each) do
expect(database).to receive(:get).with(keg_name).and_return(nil)
end
it "returns an `Array` of values" do
expect(subject.fetch_type(:system_dylibs)).to be_an_instance_of(Array)
end
end
context "`type` not in `HASH_LINKAGE_TYPES` or `ARRAY_LINKAGE_TYPES`" do
it "raises a `TypeError` if the `type` is not supported" do
expect { subject.fetch_type(:bad_type) }.to raise_error(TypeError)
end
end
end
end