spec/unit/caching_spec.rb in couch_potato-1.15.0 vs spec/unit/caching_spec.rb in couch_potato-1.16.0

- old
+ new

@@ -16,10 +16,14 @@ let(:cache) do {} end + after(:each) do + ActiveSupport::Notifications.unsubscribe(@subscriber) if @subscriber + end + context 'for a single document' do it 'gets an object from the cache the 2nd time via #load_documemt' do expect(couchrest_db).to receive(:get).with('1').exactly(1).times db.load_document '1' @@ -45,10 +49,33 @@ allow(couchrest_db).to receive_messages(get: doc) db.load_document '1' expect(db.load_document('1')).to eql(doc) end + + it 'instruments the load call' do + doc = double("doc").as_null_object + allow(couchrest_db).to receive(:get).and_return(doc) + events = [] + @subscriber = ActiveSupport::Notifications.subscribe( + 'couch_potato.load.cached' + ) do |event| + events << event + end + + db.load("1") + db.load("1") + + expect(events.size).to eq(1) + expect(events.first.payload).to eq( + { + id: "1", + doc: doc + } + ) + + end end context 'for multiple documents' do let(:doc1) { double(:doc1, 'database=': nil, id: '1') } let(:doc2) { double(:doc12, 'database=': nil, id: '2') } @@ -63,10 +90,35 @@ expect(couchrest_db).to have_received(:bulk_load).with(['1']).exactly(1).times expect(couchrest_db).to have_received(:bulk_load).with(['2']).exactly(1).times end + it 'instruments the load call' do + allow(couchrest_db).to receive(:bulk_load).with(['1']) + .and_return('rows' => [{'doc' => doc1}]) + allow(couchrest_db).to receive(:bulk_load).with(['2']) + .and_return('rows' => [{'doc' => doc2}]) + events = [] + @subscriber = ActiveSupport::Notifications.subscribe( + 'couch_potato.load.cached' + ) do |event| + events << event + end + + + db.load_document(['1']) + db.load_document(['1', '2']) + + expect(events.size).to eq(1) + expect(events.first.payload).to eq( + { + ids: ["1"], + docs: [doc1] + } + ) + end + it 'loads nothing if all documents are cached' do allow(couchrest_db).to receive(:bulk_load).with(['1', '2']) .and_return('rows' => [{'doc' => doc1}, {'doc' => doc2}]) db.load_document(['1', '2']) @@ -92,10 +144,10 @@ } doc2 = { 'id' => '2', } allow(couchrest_db).to receive(:bulk_load).with(['1', '2']) - .and_return('rows' => [{'doc' => doc1}, {'doc' => doc1}]) + .and_return('rows' => [{'doc' => doc1}, {'doc' => doc2}]) db.load_document(['1', '2']) db.load_document(['1', '2']) expect(couchrest_db).to have_received(:bulk_load).with(['1', '2']).exactly(2).times