From 6b4a5825284460978c5e0aa875179720d55d2ca1 Mon Sep 17 00:00:00 2001 From: "Marcos G. Zimmermann" Date: Wed, 7 Aug 2024 18:49:10 -0300 Subject: [PATCH] add new spec to ensure documents_for_lazy_attribute works with inline hash --- spec/esse/repository/lazy_document_spec.rb | 47 ++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/spec/esse/repository/lazy_document_spec.rb b/spec/esse/repository/lazy_document_spec.rb index d680b26..a9a8675 100644 --- a/spec/esse/repository/lazy_document_spec.rb +++ b/spec/esse/repository/lazy_document_spec.rb @@ -207,5 +207,52 @@ ) repo.update_documents_attribute(:city_names, '2') end + + it 'updates the documents when passing a LazyDocumentHeader' do + header = Esse::LazyDocumentHeader.coerce(id: '2') + expect(repo.index).to receive(:bulk).with( + update: [ + Esse::LazyDocumentHeader::Document.new(header, source: { city_names: 'London' }) + ] + ) + repo.update_documents_attribute(:city_names, header) + end + + it 'updates the documents when passing a single hash' do + expect(repo.index).to receive(:bulk).with( + update: [ + Esse::LazyDocumentHeader.new(id: '2', admin: true).to_doc(city_names: 'London') + ] + ) + repo.update_documents_attribute(:city_names, {id: '2', admin: true}) + end + + it 'updates the documents when passing an array of ids' do + expect(repo.index).to receive(:bulk).with( + update: [ + Esse::LazyDocumentHeader.new(id: '2').to_doc(city_names: 'London') + ] + ) + repo.update_documents_attribute(:city_names, ['2']) + end + + it 'updates the documents when passing an array of LazyDocumentHeader' do + header = Esse::LazyDocumentHeader.coerce(id: '2') + expect(repo.index).to receive(:bulk).with( + update: [ + Esse::LazyDocumentHeader::Document.new(header, source: { city_names: 'London' }) + ] + ) + repo.update_documents_attribute(:city_names, [header]) + end + + it 'updates the documents when passing an array of hashes' do + expect(repo.index).to receive(:bulk).with( + update: [ + Esse::LazyDocumentHeader.new(id: '2').to_doc(city_names: 'London') + ] + ) + repo.update_documents_attribute(:city_names, [{id: '2', admin: true}]) + end end end