Skip to content

Commit

Permalink
fix: apply_joins_on_records and add test test
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolasalexandre9 committed Jan 8, 2025
1 parent 3868bfb commit 4fbb147
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,10 @@ def apply_joins_on_records(initial_projection, requested_projection, records)
fk_value = record[get_foreign_key_for_projection("#{relation_name}:#{relation_projection[0]}")]
record[relation_name] = fk_value.nil? ? nil : { relation_projection[0] => fk_value }
end

# remove foreign keys
projections_to_rm.each { |field| record.delete(field) }
end

# remove foreign keys
projections_to_rm.each { |field| record.delete(field) }
end

records
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ module LazyJoin
fields: {
'id' => numeric_primary_key_build,
'author_id' => column_build(column_type: 'Number'),
'editor_id' => column_build(column_type: 'Number'),
'author' => many_to_one_build(foreign_collection: 'person', foreign_key: 'author_id'),
'editor' => many_to_one_build(foreign_collection: 'person', foreign_key: 'editor_id'),
'title' => column_build,
'price' => column_build(column_type: 'Number')
}
Expand Down Expand Up @@ -62,6 +64,28 @@ module LazyJoin
expect(result).to eq([{ 'id' => 1, 'author' => { 'id' => 2 } }, { 'id' => 2, 'author' => { 'id' => 5 } }])
end

it 'not join when projection ask for target field only with multiple relations' do
allow(@collection_book).to receive(:list).and_return(
[
{ 'id' => 1, 'author_id' => 2, 'editor_id' => 3 },
{ 'id' => 2, 'author_id' => 5, 'editor_id' => 4 }
]
)

result = @datasource_decorator.get_collection('book')
.list(caller, Filter.new, Projection.new(%w[id author:id editor:id]))

expect(@collection_book).to have_received(:list) do |_caller, _filter, projection|
expect(projection).to eq(%w[id author_id editor_id])
end
expect(result).to eq(
[
{ 'id' => 1, 'author' => { 'id' => 2 }, 'editor' => { 'id' => 3 } },
{ 'id' => 2, 'author' => { 'id' => 5 }, 'editor' => { 'id' => 4 } }
]
)
end

it 'join when projection ask for multiple fields in foreign collection' do
allow(@collection_book).to receive(:list).and_return(
[
Expand Down

0 comments on commit 4fbb147

Please sign in to comment.