@@ -4,9 +4,9 @@ module ForestLiana
4
4
let ( :pageSize ) { 10 }
5
5
let ( :pageNumber ) { 1 }
6
6
let ( :sort ) { 'id' }
7
- let ( :fields ) { }
8
- let ( :filters ) { }
9
- let ( :scopes ) { { } }
7
+ let ( :fields ) { }
8
+ let ( :filters ) { }
9
+ let ( :scopes ) { { } }
10
10
let ( :rendering_id ) { 13 }
11
11
let ( :user ) { { 'id' => '1' , 'rendering_id' => rendering_id } }
12
12
@@ -22,7 +22,21 @@ def init_scopes
22
22
allow ( ForestLiana ::ScopeManager ) . to receive ( :fetch_scopes ) . and_return ( scopes )
23
23
end
24
24
25
+ def clean_database
26
+ [
27
+ Driver ,
28
+ Island ,
29
+ Location ,
30
+ Manufacturer ,
31
+ Product ,
32
+ Tree ,
33
+ User ,
34
+ ] . each ( &:destroy_all )
35
+ end
36
+
25
37
before ( :each ) do
38
+ clean_database
39
+
26
40
users = [ 'Michel' , 'Robert' , 'Vince' , 'Sandro' , 'Olesya' , 'Romain' , 'Valentin' , 'Jason' , 'Arnaud' , 'Jeff' , 'Steve' , 'Marc' , 'Xavier' , 'Paul' , 'Mickael' , 'Mike' , 'Maxime' , 'Gertrude' , 'Monique' , 'Mia' , 'Rachid' , 'Edouard' , 'Sacha' , 'Caro' , 'Amand' , 'Nathan' , 'Noémie' , 'Robin' , 'Gaelle' , 'Isabelle' ]
27
41
. map { |name | User . create ( name : name ) }
28
42
@@ -50,15 +64,94 @@ def init_scopes
50
64
{ :coordinates => '32154' , :island => islands [ 4 ] }
51
65
] . map { |location | Location . create ( coordinates : location [ :coordinates ] , island : location [ :island ] ) }
52
66
67
+ manufacturers = [ 'Orange' , 'Pear' ] . map { |name | Manufacturer . create! ( name : 'name' ) }
68
+
69
+ drivers = [ 'Baby driver' , 'Taxi driver' ] . map { |firstname | Driver . create! ( firstname : firstname ) }
70
+
71
+ products = [
72
+ { name : 'Valencia' , uri : 'https://valencia.com' , manufacturer : manufacturers [ 0 ] , driver : drivers [ 0 ] } ,
73
+ { name : 'Blood' , uri : 'https://blood.com' , manufacturer : manufacturers [ 0 ] , driver : drivers [ 1 ] } ,
74
+ { name : 'Conference' , uri : 'https://conference.com' , manufacturer : manufacturers [ 1 ] , driver : drivers [ 0 ] } ,
75
+ { name : 'Concorde' , uri : 'https://concorde.com' , manufacturer : manufacturers [ 1 ] , driver : drivers [ 1 ] }
76
+ ] . map { |attributes | Product . create! ( attributes ) }
77
+
53
78
reference = Reference . create ( )
54
79
init_scopes
55
80
end
56
81
57
- after ( :each ) do
58
- User . destroy_all
59
- Island . destroy_all
60
- Location . destroy_all
61
- Tree . destroy_all
82
+ describe 'records eager loading' do
83
+ let ( :resource ) { Product }
84
+ let ( :fields ) { { resource . name => 'id,name,manufacturer' , 'manufacturer' => 'name' } }
85
+
86
+ shared_context 'resource current_database' do
87
+ before do
88
+ connection = resource . connection
89
+
90
+ def connection . current_database
91
+ 'db/test.sqlite3'
92
+ end
93
+ end
94
+
95
+ after do
96
+ resource . connection . singleton_class . remove_method ( :current_database )
97
+ end
98
+ end
99
+
100
+ shared_examples 'left outer join' do
101
+ it 'should perform a left outer join with the association' do
102
+ expect ( getter . perform . to_sql ) . to match ( /LEFT OUTER JOIN "manufacturers"/ )
103
+ end
104
+ end
105
+
106
+ shared_examples 'records' do
107
+ it 'should get only the expected records' do
108
+ getter . perform
109
+
110
+ records = getter . records
111
+
112
+ count = getter . count
113
+
114
+ expect ( records . count ) . to eq 4
115
+ expect ( count ) . to eq 4
116
+ expect ( records . map ( &:name ) ) . to match_array ( %w[ Valencia Blood Conference Concorde ] )
117
+ end
118
+ end
119
+
120
+ context 'when the connections do not support current_database' do
121
+ include_examples 'left outer join'
122
+ include_examples 'records'
123
+ end
124
+
125
+ context 'when the included association uses a different database connection' do
126
+ let ( :fields ) { { resource . name => 'id,name,driver' , 'driver' => 'firstname' } }
127
+
128
+ before do
129
+ association_connection = resource . reflect_on_association ( :driver ) . klass . connection
130
+
131
+ def association_connection . current_database
132
+ 'db/different_test.sqlite3'
133
+ end
134
+ end
135
+
136
+ after do
137
+ resource . reflect_on_association ( :driver ) . klass . connection . singleton_class . remove_method ( :current_database )
138
+ end
139
+
140
+ include_context 'resource current_database'
141
+
142
+ include_examples 'records'
143
+
144
+ it 'does not perform a left outer join with the association' do
145
+ expect ( getter . perform . to_sql ) . not_to match ( /LEFT OUTER JOIN "drivers"/ )
146
+ end
147
+ end
148
+
149
+ context 'when the included association uses the same database connection' do
150
+ include_context 'resource current_database'
151
+
152
+ include_examples 'left outer join'
153
+ include_examples 'records'
154
+ end
62
155
end
63
156
64
157
describe 'when there are more records than the page size' do
0 commit comments