Skip to content

Commit

Permalink
feat: make sure the header document id does not get overwritten by op…
Browse files Browse the repository at this point in the history
…tions
  • Loading branch information
marcosgz committed Aug 8, 2024
1 parent 4092de5 commit 44a001a
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/esse/lazy_document_header.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def self.coerce(value)
if value.is_a?(Esse::LazyDocumentHeader)
value
elsif value.is_a?(Esse::Document)
new(id: value.id, type: value.type, routing: value.routing, **value.options)
new(**value.options, id: value.id, type: value.type, routing: value.routing)
elsif value.is_a?(Hash)
resp = value.transform_keys do |key|
case key
Expand Down
25 changes: 25 additions & 0 deletions spec/esse/lazzy_document_header_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,26 @@
expect(described_class.coerce(object)).to be_a(described_class)
end
end

context 'when value is a document with id in options' do
let(:object) do
Class.new(Esse::Document) do
def options
{ id: 1 }
end

def id
2
end
end.new(nil)
end

it 'returns a LazyDocumentHeader instance with the proper id' do
instance = described_class.coerce(object)
expect(instance).to be_a(described_class)
expect(instance.id).to eq(2)
end
end
end

describe '.coerce_each' do
Expand Down Expand Up @@ -173,6 +193,11 @@
it 'flattens the array' do
expect(described_class.coerce_each([[{_id: 1}], {_id: 2}]).size).to eq(2)
end

it 'coerces a list of Esse::Document instances' do
list = [Esse::HashDocument.new(_id: 1), Class.new(Esse::HashDocument).new(_id: 2)]
expect(described_class.coerce_each(list)).to all(be_a(described_class))
end
end

describe '#to_doc' do
Expand Down

0 comments on commit 44a001a

Please sign in to comment.