Skip to content

Commit

Permalink
test: add tests on lookup generator
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolasalexandre9 committed Feb 27, 2025
1 parent 0a97f47 commit 29ccae4
Show file tree
Hide file tree
Showing 8 changed files with 336 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ def self.lookup_projection(current_path, schema_stack, projection, options)
fields.merge!(add_fields(name, relation_projection, options))
end

pipeline.push({ '$addFields' => fields }) unless fields.empty?

pipeline
end

Expand All @@ -36,7 +38,7 @@ def self.add_fields(name, projection, options)
projection.filter { |field| field.include?('@@@') }
.map { |field_name| "#{name}.#{field_name.tr(":", ".")}" }
.each_with_object({}) do |curr, acc|
acc[curr] = "$#{curr.tr("@@@", ".")}"
acc[curr] = "$#{curr.gsub("@@@", ".")}"
end
end

Expand Down Expand Up @@ -67,8 +69,10 @@ def self.lookup_relation(current_path, schema_stack, name, projection, options)

return [
# Push lookup to pipeline
{ '$lookup' =>
{ 'from' => from, 'localField' => local_field, 'foreignField' => foreign_field, 'as' => as } },
{
'$lookup' => { 'from' => from, 'localField' => local_field, 'foreignField' => foreign_field,
'as' => as }
},
{ '$unwind' => { 'path' => "$#{as}", 'preserveNullAndEmptyArrays' => true } },

# Recurse to get relations of relations
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ class Address
field :city, type: String
field :zip_code, type: String

embeds_one :meta, class_name: 'Meta'
embedded_in :user
end
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ class Author
field :last_name, type: String

belongs_to :post
belongs_to :user
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class CoAuthor
include Mongoid::Document
include Mongoid::Timestamps
field :first_name, type: String
field :last_name, type: String

belongs_to :user
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class Meta
include Mongoid::Document
field :length, type: String

embedded_in :address
end
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,7 @@ class Post
has_many :comments, dependent: :destroy
has_one :author
has_and_belongs_to_many :tags
belongs_to :user

embeds_one :co_author
end
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ class User
field :name, type: String
belongs_to :item, polymorphic: true
embeds_many :addresses
embeds_one :address
end
Loading

0 comments on commit 29ccae4

Please sign in to comment.