-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathpolymorphic_includes_spec.rb
More file actions
172 lines (142 loc) · 5.76 KB
/
polymorphic_includes_spec.rb
File metadata and controls
172 lines (142 loc) · 5.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
require 'spec_helper'
describe Mongoid::Includes::Criteria do
describe '#includes' do
Given(:inclusions) { criteria.inclusions }
context 'multiple inclusions through polymorphic associations' do
Given(:pink_floyd) { Band.create!(name: 'Pink Floyd', musician_ids: [nil, '']) }
Given(:jethro) { Band.create!(name: 'Jethro Tull') }
Given {
Artist.create!(name: 'David Gilmour', associated_act: pink_floyd)
wywh = Album.create!(name: 'Wish You Were Here', release: Date.new(1975), owner: pink_floyd)
Album.create!(name: 'The Dark Side of the Moon', release: Date.new(1973), owner: pink_floyd)
Artist.create!(name: 'Ian Anderson', associated_act: jethro)
standup = Album.create!(name: 'Stand Up', release: Date.new(1969), owner: jethro)
Album.create!(name: 'Aqualung', release: Date.new(1971), owner: jethro)
Song.create!(name: 'Shine On', album: wywh)
Song.create!(name: 'We Used to Know', album: standup)
}
Given(:criteria) {
Artist
.includes(:musicians, from: :associated_act, from_class: Band)
.includes(:associated_act, with: ->(bands) {
bands
.includes(:albums, with: ->(albums) { albums.gt(release: 1970) })
.includes(:songs, from: :albums, with: ->(songs) { songs })
})
}
describe ':with inclusions should not be overriden', skip: ENV["CI"] do
When(:artists) { expect_query(4) { criteria.entries } } # There are no musicians, so no query should be made.
Given(:albums) { artists.map(&:associated_act).flat_map(&:albums) }
Then { artists.size == 2 }
And {
expect_no_queries { albums.size == 3 } # gt(release: 1970)
}
And {
expect_no_queries { albums.flat_map(&:songs).size == 1 } # Only "Shine On"
}
end
describe 'should not replace an includes with an specified modifier with a generic one' do
Given(:inclusions) { new_criteria.inclusions.to_a }
When(:new_criteria) { criteria.includes(:musicians, from: :associated_act, from_class: Band) }
Then { inclusions.size == 2 }
And { inclusions.first.nested? }
And { inclusions.last.polymorphic? && inclusions.last.modifier }
end
it 'should fail if a polymorphic association is not disambiguated' do
expect {
criteria.includes(:musicians, from: :associated_act)
}.to raise_error(Mongoid::Includes::Errors::InvalidPolymorphicIncludes)
end
end
context 'eager loading polymorphic belongs_to associations with multiple concrete types' do
before(:context) do
class Main
include Mongoid::Document
belongs_to :related, polymorphic: true, optional: true
end
class Related
include Mongoid::Document
has_one :parent, as: :related
end
class Two < Related; end
class Three < Related; end
end
after(:context) do
%i[Main Related Two Three].each do |const|
Object.send(:remove_const, const) if Object.const_defined?(const, false)
end
end
it 'loads the related documents for each concrete type without raising' do
Main.create!(related: Two.create!)
Main.create!(related: Three.create!)
loaded = nil
expect { loaded = Main.includes(:related).entries }.not_to raise_error
expect(loaded.map { |doc| doc.related.class }).to match_array([Two, Three])
expect { Main.last.related.id }.not_to raise_error
end
end
context 'polymorphic eager loading in a fresh Ruby process' do
let(:project_root) { File.expand_path('../../..', __dir__) }
it 'does not error when includes is evaluated from the CLI' do
require 'open3'
database_name = "mongoid_includes_spec_#{SecureRandom.hex(6)}"
base_script = <<~RUBY
require 'bundler/setup'
require 'mongoid'
require 'mongoid_includes'
Mongoid.load_configuration(
clients: {
default: {
database: '#{database_name}',
hosts: %w[localhost:27017]
}
}
)
class Main
include Mongoid::Document
belongs_to :related, polymorphic: true, optional: true
end
class Related
include Mongoid::Document
has_one :parent, as: :related
end
class Two < Related; end
class Three < Related; end
RUBY
init_script = base_script + <<~RUBY
client = Mongoid::Clients.default
begin
client.database.drop
rescue Mongo::Error::OperationFailure
end
Main.destroy_all
Related.destroy_all
Main.create!(related: Two.create!)
Main.create!(related: Three.create!)
RUBY
bad_script = base_script + <<~RUBY
Main.includes(:related).entries
Main.last.related.id
Mongoid::Clients.default.database.drop
RUBY
bundle_gemfile = ENV.fetch(
'BUNDLE_GEMFILE',
File.join(project_root, 'Gemfile')
)
run_script = lambda do |script|
Open3.capture2e(
{ 'BUNDLE_GEMFILE' => bundle_gemfile },
RbConfig.ruby,
'-',
chdir: project_root,
stdin_data: script
)
end
init_out, init_status = run_script.call(init_script)
expect(init_status).to be_success, "failed to prepare polymorphic data: #{init_out}"
bad_out, bad_status = run_script.call(bad_script)
expect(bad_status).to be_success, "expected CLI reproduction to succeed, got #{bad_status.exitstatus}: #{bad_out}"
end
end
end
end