@@ -105,11 +105,12 @@ class ActiveStorage::VariantWithRecordTest < ActiveSupport::TestCase
105
105
users . reset
106
106
107
107
assert_no_difference -> { ActiveStorage ::VariantRecord . count } do
108
- assert_queries_count ( 5 ) do
109
- # 5 queries:
108
+ assert_queries_count ( 6 ) do
109
+ # 6 queries:
110
110
# attachment (cover photos) x 1
111
111
# blob for the cover photo x 1
112
112
# variant record x 1
113
+ # preview_image_attachments for non-images
113
114
# attachment x 1
114
115
# variant record x 1
115
116
users . with_attached_cover_photo . each do |u |
@@ -122,6 +123,60 @@ class ActiveStorage::VariantWithRecordTest < ActiveSupport::TestCase
122
123
end
123
124
end
124
125
126
+ def find_keys_for_representation ( filename )
127
+ user = User . create! ( name : "Justin" )
128
+
129
+ 10 . times do
130
+ blob = directly_upload_file_blob ( filename : filename )
131
+ user . highlights_with_variants . attach ( blob )
132
+ end
133
+
134
+ # Force the processing
135
+ user . highlights_with_variants . each do |highlight |
136
+ highlight . representation ( :thumb ) . processed . key
137
+ end
138
+
139
+ highlights = User . with_attached_highlights_with_variants . find_by ( name : "Justin" ) . highlights_with_variants . to_a
140
+
141
+ assert_queries_count ( 0 ) do
142
+ highlights . each do |highlight |
143
+ highlight . representation ( :thumb ) . key
144
+ end
145
+ end
146
+ end
147
+
148
+ def test_with_attached_image_variant_no_n_plus_1
149
+ find_keys_for_representation "racecar.jpg"
150
+ end
151
+
152
+ def test_with_attached_video_variant_no_n_plus_1
153
+ find_keys_for_representation "video.mp4"
154
+ end
155
+
156
+ def test_no_n_plus_1_with_all_variant_records_on_attached_video
157
+ user = User . create! ( name : "Justin" )
158
+
159
+ 10 . times do
160
+ blob = directly_upload_file_blob ( filename : "video.mp4" )
161
+ user . highlights_with_variants . attach ( blob )
162
+ end
163
+
164
+ # Force the processing
165
+ user . highlights_with_variants . each do |highlight |
166
+ highlight . representation ( :thumb ) . processed . key
167
+ end
168
+
169
+ user . reload
170
+
171
+ highlights = user . highlights_with_variants . with_all_variant_records . to_a
172
+
173
+ assert_queries_count ( 0 ) do
174
+ highlights . each do |highlight |
175
+ highlight . representation ( :thumb ) . key
176
+ end
177
+ end
178
+ end
179
+
125
180
test "eager loading has_many_attached records" do
126
181
user = User . create! ( name : "Josh" )
127
182
@@ -199,11 +254,12 @@ class ActiveStorage::VariantWithRecordTest < ActiveSupport::TestCase
199
254
user . reload
200
255
201
256
assert_no_difference -> { ActiveStorage ::VariantRecord . count } do
202
- assert_queries_count ( 5 ) do
203
- # 5 queries:
257
+ assert_queries_count ( 6 ) do
258
+ # 6 queries:
204
259
# attachments (vlogs) x 1
205
260
# blobs for the vlogs x 1
206
261
# variant records for the blobs x 1
262
+ # preview_image_attachments for non-images
207
263
# attachments for the variant records x 1
208
264
# blobs for the attachments for the variant records x 1
209
265
user . vlogs . with_all_variant_records . each do |vlog |
@@ -218,12 +274,13 @@ class ActiveStorage::VariantWithRecordTest < ActiveSupport::TestCase
218
274
user . reload
219
275
220
276
assert_no_difference -> { ActiveStorage ::VariantRecord . count } do
221
- assert_queries_count ( 6 ) do
222
- # 6 queries:
277
+ assert_queries_count ( 7 ) do
278
+ # 7 queries:
223
279
# user x 1
224
280
# attachments (vlogs) x 1
225
281
# blobs for the vlogs x 1
226
282
# variant records for the blobs x 1
283
+ # preview_image_attachments for non-images
227
284
# attachments for the variant records x 1
228
285
# blobs for the attachments for the variant records x 1
229
286
User . where ( id : user . id ) . with_attached_vlogs . each do |u |
@@ -243,11 +300,12 @@ class ActiveStorage::VariantWithRecordTest < ActiveSupport::TestCase
243
300
# More queries here because we are creating a different variant.
244
301
# The second time we load this variant, we are back down to just 3 queries.
245
302
246
- assert_queries_match ( /SELECT/i , count : 9 ) do
303
+ assert_queries_match ( /SELECT/i , count : 10 ) do
247
304
# 9 queries:
248
305
# attachments (vlogs) initial load x 1
249
306
# blob x 1 (gets both records)
250
307
# variant record x 1 (gets both records)
308
+ # preview_image_attachments for non-images
251
309
# 2x get blob, attachment, variant records again, this happens when loading the new blob inside `VariantWithRecord#key`
252
310
user . vlogs . with_all_variant_records . each do |vlog |
253
311
rep = vlog . representation ( resize_to_limit : [ 200 , 200 ] )
@@ -259,7 +317,7 @@ class ActiveStorage::VariantWithRecordTest < ActiveSupport::TestCase
259
317
260
318
user . reload
261
319
262
- assert_queries_count ( 5 ) do
320
+ assert_queries_count ( 6 ) do
263
321
user . vlogs . with_all_variant_records . each do |vlog |
264
322
rep = vlog . representation ( resize_to_limit : [ 200 , 200 ] )
265
323
rep . processed
0 commit comments