-
Notifications
You must be signed in to change notification settings - Fork 0
/
joins_spec.rb
54 lines (45 loc) · 1.37 KB
/
joins_spec.rb
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
RSpec.describe "joins" do
let!(:company) { FactoryBot.create(:company) }
let!(:pricing_settings) do
FactoryBot.create_list(:pricing_setting, 2, company: company)
end
it 'load a company producing duplicates' do
r = ORM::Company
.joins(:pricing_settings)
.where(code: company.code)
expect(r).to include(
have_attributes(
code: company.code
),
have_attributes(
code: company.code
)
)
end
it 'pricing_settings are brought through `inner join`' do
sql = ORM::Company
.joins(:pricing_settings)
.where(code: company.code)
.to_sql
expect(sql).to include(
"FROM `companies` INNER JOIN `pricing_settings` ON `pricing_settings`.`company_code` = `companies`.`code`"
)
end
it 'pricing_settings are NOT eager loaded' do
r = ORM::Company
.joins(:pricing_settings)
.where(code: company.code)
.first
sql = r.pricing_settings.to_sql
expect(sql).to eq("SELECT `pricing_settings`.* FROM `pricing_settings` WHERE `pricing_settings`.`company_code` = '#{company.code}'")
end
context 'when a company does not have pricing_settings' do
let!(:company2) { FactoryBot.create(:company) }
it 'the company is NOT loaded' do
r = ORM::Company
.joins(:pricing_settings)
.where(code: company2.code)
expect(r).to be_empty
end
end
end