spec/unit/caching_spec.rb in couch_potato-1.14.0 vs spec/unit/caching_spec.rb in couch_potato-1.15.0
- old
+ new
@@ -16,44 +16,95 @@
let(:cache) do
{}
end
- 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
+ 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'
- db.load_document '1'
- end
+ db.load_document '1'
+ db.load_document '1'
+ end
- it 'gets an object from the cache the 2nd time via #load' do
- expect(couchrest_db).to receive(:get).with('1').exactly(1).times
+ it 'gets an object from the cache the 2nd time via #load' do
+ expect(couchrest_db).to receive(:get).with('1').exactly(1).times
- db.load '1'
- db.load '1'
- end
+ db.load '1'
+ db.load '1'
+ end
- it 'gets an object from the cache the 2nd time via #load!' do
- expect(couchrest_db).to receive(:get).with('1').exactly(1).times
+ it 'gets an object from the cache the 2nd time via #load!' do
+ expect(couchrest_db).to receive(:get).with('1').exactly(1).times
- db.load! '1'
- db.load! '1'
- end
+ db.load! '1'
+ db.load! '1'
+ end
- it 'returns the correct object' do
- doc = double(:doc, 'database=': nil)
- allow(couchrest_db).to receive_messages(get: doc)
+ it 'returns the correct object' do
+ doc = double(:doc, 'database=': nil)
+ allow(couchrest_db).to receive_messages(get: doc)
- db.load_document '1'
- expect(db.load_document('1')).to eql(doc)
+ db.load_document '1'
+ expect(db.load_document('1')).to eql(doc)
+ end
end
- it 'does not cache bulk loads' do
- allow(couchrest_db).to receive_messages(bulk_load: {'rows' => []})
- expect(couchrest_db).to receive(:bulk_load).with(['1']).exactly(2).times
+ context 'for multiple documents' do
+ let(:doc1) { double(:doc1, 'database=': nil, id: '1') }
+ let(:doc2) { double(:doc12, 'database=': nil, id: '2') }
- db.load_document ['1']
- db.load_document ['1']
+ it 'only loads uncached documents' 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}])
+
+
+ db.load_document(['1'])
+ db.load_document(['1', '2'])
+
+ 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 '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'])
+ db.load_document(['1', '2'])
+
+ expect(couchrest_db).to have_received(:bulk_load).with(['1', '2']).exactly(1).times
+ end
+
+ it 'returns all requested documents' 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}])
+
+
+ db.load_document(['1'])
+ result = db.load_document(['1', '2'])
+
+ expect(result).to eql([doc1, doc2])
+ end
+
+ it 'does not cache documents that do not respond to id' do
+ doc1 = {
+ 'id' => '1',
+ }
+ doc2 = {
+ 'id' => '2',
+ }
+ allow(couchrest_db).to receive(:bulk_load).with(['1', '2'])
+ .and_return('rows' => [{'doc' => doc1}, {'doc' => doc1}])
+
+ db.load_document(['1', '2'])
+ db.load_document(['1', '2'])
+
+ expect(couchrest_db).to have_received(:bulk_load).with(['1', '2']).exactly(2).times
+ end
+ end
+
+ context 'when switching the database' do
end
it 'clears the cache when destroying a document via #destroy_document' do
expect(couchrest_db).to receive(:get).with('1').exactly(2).times