| 
									
										
										
										
											2019-04-19 15:38:03 +09:00
										 |  |  | # frozen_string_literal: true | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-22 14:15:36 -04:00
										 |  |  | require "cache_store" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-18 15:11:11 -08:00
										 |  |  | RSpec.describe CacheStoreDatabase do | 
					
						
							| 
									
										
										
										
											2021-01-31 13:14:23 -05:00
										 |  |  |   subject(:sample_db) { described_class.new(:sample) } | 
					
						
							| 
									
										
										
										
											2018-05-22 14:15:36 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |   describe "self.use" do | 
					
						
							|  |  |  |     let(:type) { :test } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-22 15:31:58 -04:00
										 |  |  |     it "creates a new `DatabaseCache` instance" do | 
					
						
							| 
									
										
										
										
											2023-01-22 17:03:50 -08:00
										 |  |  |       cache_store = instance_double(described_class, "cache_store", write_if_dirty!: nil) | 
					
						
							| 
									
										
										
										
											2018-09-20 09:07:56 +01:00
										 |  |  |       expect(described_class).to receive(:new).with(type).and_return(cache_store) | 
					
						
							| 
									
										
										
										
											2020-09-11 12:56:15 +01:00
										 |  |  |       expect(cache_store).to receive(:write_if_dirty!) | 
					
						
							| 
									
										
										
										
											2023-03-08 23:14:46 +00:00
										 |  |  |       described_class.use(type) do |_db| | 
					
						
							| 
									
										
										
										
											2023-01-26 21:18:24 -08:00
										 |  |  |         # do nothing | 
					
						
							| 
									
										
										
										
											2023-03-08 23:14:46 +00:00
										 |  |  |       end | 
					
						
							| 
									
										
										
										
											2018-05-22 14:15:36 -04:00
										 |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   describe "#set" do | 
					
						
							| 
									
										
										
										
											2023-01-22 17:03:50 -08:00
										 |  |  |     let(:db) { instance_double(Hash, "db", :[]= => nil) } | 
					
						
							| 
									
										
										
										
											2018-05-22 14:15:36 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-28 19:16:56 +00:00
										 |  |  |     it "sets the value in the `CacheStoreDatabase`" do | 
					
						
							| 
									
										
										
										
											2018-05-22 14:15:36 -04:00
										 |  |  |       allow(File).to receive(:write) | 
					
						
							| 
									
										
										
										
											2023-12-14 02:52:30 +00:00
										 |  |  |       allow(sample_db).to receive_messages(created?: true, db: db) | 
					
						
							| 
									
										
										
										
											2018-05-22 14:15:36 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-28 19:16:56 +00:00
										 |  |  |       expect(db).to receive(:has_key?).with(:foo).and_return(false) | 
					
						
							| 
									
										
										
										
											2018-09-20 09:07:56 +01:00
										 |  |  |       expect(db).not_to have_key(:foo) | 
					
						
							| 
									
										
										
										
											2021-01-31 13:14:23 -05:00
										 |  |  |       sample_db.set(:foo, "bar") | 
					
						
							| 
									
										
										
										
											2018-05-22 14:15:36 -04:00
										 |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   describe "#get" do | 
					
						
							| 
									
										
										
										
											2021-02-19 23:15:33 +00:00
										 |  |  |     context "with a database created" do | 
					
						
							| 
									
										
										
										
											2023-01-22 17:03:50 -08:00
										 |  |  |       let(:db) { instance_double(Hash, "db", :[] => "bar") } | 
					
						
							| 
									
										
										
										
											2018-05-22 14:15:36 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-28 19:16:56 +00:00
										 |  |  |       it "gets value in the `CacheStoreDatabase` corresponding to the key" do | 
					
						
							| 
									
										
										
										
											2018-05-22 14:15:36 -04:00
										 |  |  |         expect(db).to receive(:has_key?).with(:foo).and_return(true) | 
					
						
							| 
									
										
										
										
											2023-12-14 02:52:30 +00:00
										 |  |  |         allow(sample_db).to receive_messages(created?: true, db: db) | 
					
						
							| 
									
										
										
										
											2018-05-22 14:15:36 -04:00
										 |  |  |         expect(db).to have_key(:foo) | 
					
						
							| 
									
										
										
										
											2021-01-31 13:14:23 -05:00
										 |  |  |         expect(sample_db.get(:foo)).to eq("bar") | 
					
						
							| 
									
										
										
										
											2018-05-22 14:15:36 -04:00
										 |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-19 23:15:33 +00:00
										 |  |  |     context "without a database created" do | 
					
						
							| 
									
										
										
										
											2023-01-22 17:03:50 -08:00
										 |  |  |       let(:db) { instance_double(Hash, "db", :[] => nil) } | 
					
						
							| 
									
										
										
										
											2018-05-22 14:15:36 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-20 09:07:56 +01:00
										 |  |  |       before do | 
					
						
							| 
									
										
										
										
											2023-12-14 02:52:30 +00:00
										 |  |  |         allow(sample_db).to receive_messages(created?: false, db: db) | 
					
						
							| 
									
										
										
										
											2018-05-22 14:15:36 -04:00
										 |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it "does not get value in the `CacheStoreDatabase` corresponding to key" do | 
					
						
							| 
									
										
										
										
											2021-01-31 13:14:23 -05:00
										 |  |  |         expect(sample_db.get(:foo)).not_to be("bar") | 
					
						
							| 
									
										
										
										
											2018-05-22 14:15:36 -04:00
										 |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it "does not call `db[]` if `CacheStoreDatabase.created?` is `false`" do | 
					
						
							|  |  |  |         expect(db).not_to receive(:[]) | 
					
						
							| 
									
										
										
										
											2021-01-31 13:14:23 -05:00
										 |  |  |         sample_db.get(:foo) | 
					
						
							| 
									
										
										
										
											2018-05-22 14:15:36 -04:00
										 |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   describe "#delete" do | 
					
						
							| 
									
										
										
										
											2021-02-19 23:15:33 +00:00
										 |  |  |     context "with a database created" do | 
					
						
							| 
									
										
										
										
											2023-01-22 17:03:50 -08:00
										 |  |  |       let(:db) { instance_double(Hash, "db", :[] => { foo: "bar" }) } | 
					
						
							| 
									
										
										
										
											2018-05-22 14:15:36 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-20 09:07:56 +01:00
										 |  |  |       before do | 
					
						
							| 
									
										
										
										
											2023-12-14 02:52:30 +00:00
										 |  |  |         allow(sample_db).to receive_messages(created?: true, db: db) | 
					
						
							| 
									
										
										
										
											2018-05-22 14:15:36 -04:00
										 |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it "deletes value in the `CacheStoreDatabase` corresponding to the key" do | 
					
						
							|  |  |  |         expect(db).to receive(:delete).with(:foo) | 
					
						
							| 
									
										
										
										
											2021-01-31 13:14:23 -05:00
										 |  |  |         sample_db.delete(:foo) | 
					
						
							| 
									
										
										
										
											2018-05-22 14:15:36 -04:00
										 |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-19 23:15:33 +00:00
										 |  |  |     context "without a database created" do | 
					
						
							| 
									
										
										
										
											2023-01-22 17:03:50 -08:00
										 |  |  |       let(:db) { instance_double(Hash, "db", delete: nil) } | 
					
						
							| 
									
										
										
										
											2018-05-22 14:15:36 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-20 09:07:56 +01:00
										 |  |  |       before do | 
					
						
							| 
									
										
										
										
											2023-12-14 02:52:30 +00:00
										 |  |  |         allow(sample_db).to receive_messages(created?: false, db: db) | 
					
						
							| 
									
										
										
										
											2018-05-22 14:15:36 -04:00
										 |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it "does not call `db.delete` if `CacheStoreDatabase.created?` is `false`" do | 
					
						
							|  |  |  |         expect(db).not_to receive(:delete) | 
					
						
							| 
									
										
										
										
											2021-01-31 13:14:23 -05:00
										 |  |  |         sample_db.delete(:foo) | 
					
						
							| 
									
										
										
										
											2018-05-22 14:15:36 -04:00
										 |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-11 12:56:15 +01:00
										 |  |  |   describe "#write_if_dirty!" do | 
					
						
							| 
									
										
										
										
											2021-02-19 23:15:33 +00:00
										 |  |  |     context "with an open database" do | 
					
						
							| 
									
										
										
										
											2018-05-22 14:15:36 -04:00
										 |  |  |       it "does not raise an error when `close` is called on the database" do | 
					
						
							| 
									
										
										
										
											2021-01-31 13:14:23 -05:00
										 |  |  |         expect { sample_db.write_if_dirty! }.not_to raise_error(NoMethodError) | 
					
						
							| 
									
										
										
										
											2018-05-22 14:15:36 -04:00
										 |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-19 23:15:33 +00:00
										 |  |  |     context "without an open database" do | 
					
						
							| 
									
										
										
										
											2018-09-20 09:07:56 +01:00
										 |  |  |       before do | 
					
						
							| 
									
										
										
										
											2021-01-31 13:14:23 -05:00
										 |  |  |         sample_db.instance_variable_set(:@db, nil) | 
					
						
							| 
									
										
										
										
											2018-05-22 14:15:36 -04:00
										 |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it "does not raise an error when `close` is called on the database" do | 
					
						
							| 
									
										
										
										
											2021-01-31 13:14:23 -05:00
										 |  |  |         expect { sample_db.write_if_dirty! }.not_to raise_error(NoMethodError) | 
					
						
							| 
									
										
										
										
											2018-05-22 14:15:36 -04:00
										 |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   describe "#created?" do | 
					
						
							| 
									
										
										
										
											2018-09-25 20:19:30 +01:00
										 |  |  |     let(:cache_path) { Pathname("path/to/homebrew/cache/sample.json") } | 
					
						
							| 
									
										
										
										
											2018-05-22 14:15:36 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-20 09:07:56 +01:00
										 |  |  |     before do | 
					
						
							| 
									
										
										
										
											2021-01-31 13:14:23 -05:00
										 |  |  |       allow(sample_db).to receive(:cache_path).and_return(cache_path) | 
					
						
							| 
									
										
										
										
											2018-05-22 14:15:36 -04:00
										 |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-19 23:15:33 +00:00
										 |  |  |     context "when `cache_path.exist?` returns `true`" do | 
					
						
							| 
									
										
										
										
											2018-09-20 09:07:56 +01:00
										 |  |  |       before do | 
					
						
							| 
									
										
										
										
											2018-09-20 13:40:38 +01:00
										 |  |  |         allow(cache_path).to receive(:exist?).and_return(true) | 
					
						
							| 
									
										
										
										
											2018-05-22 14:15:36 -04:00
										 |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it "returns `true`" do | 
					
						
							| 
									
										
										
										
											2021-01-31 13:14:23 -05:00
										 |  |  |         expect(sample_db.created?).to be(true) | 
					
						
							| 
									
										
										
										
											2018-05-22 14:15:36 -04:00
										 |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-19 23:15:33 +00:00
										 |  |  |     context "when `cache_path.exist?` returns `false`" do | 
					
						
							| 
									
										
										
										
											2018-09-20 09:07:56 +01:00
										 |  |  |       before do | 
					
						
							| 
									
										
										
										
											2018-09-20 13:40:38 +01:00
										 |  |  |         allow(cache_path).to receive(:exist?).and_return(false) | 
					
						
							| 
									
										
										
										
											2018-05-22 14:15:36 -04:00
										 |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it "returns `false`" do | 
					
						
							| 
									
										
										
										
											2021-01-31 13:14:23 -05:00
										 |  |  |         expect(sample_db.created?).to be(false) | 
					
						
							| 
									
										
										
										
											2018-05-22 14:15:36 -04:00
										 |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | end |