-
Notifications
You must be signed in to change notification settings - Fork 21.4k
/
variant_with_record_test.rb
286 lines (237 loc) · 8.5 KB
/
variant_with_record_test.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
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
# frozen_string_literal: true
require "test_helper"
require "database/setup"
class ActiveStorage::VariantWithRecordTest < ActiveSupport::TestCase
include ActiveJob::TestHelper
setup do
@was_tracking, ActiveStorage.track_variants = ActiveStorage.track_variants, true
end
teardown do
ActiveStorage.track_variants = @was_tracking
end
test "generating a resized variation of a JPEG blob" do
blob = create_file_blob(filename: "racecar.jpg")
variant = blob.variant(resize_to_limit: [100, 100])
assert_difference -> { blob.variant_records.count }, +1 do
variant.processed
end
assert_match(/racecar\.jpg/, variant.url)
assert_equal "racecar.jpg", variant.filename.to_s
assert_equal "image/jpeg", variant.content_type
image = read_image(variant.image)
assert_equal 100, image.width
assert_equal 67, image.height
record = blob.variant_records.last
assert_equal variant.variation.digest, record.variation_digest
end
test "serving a previously-generated resized variation of a JPEG blob" do
blob = create_file_blob(filename: "racecar.jpg")
assert_difference -> { blob.variant_records.count } do
blob.variant(resize_to_limit: [100, 100]).processed
end
variant = blob.variant(resize_to_limit: [100, 100])
assert_no_difference -> { blob.variant_records.count } do
variant.processed
end
assert_match(/racecar\.jpg/, variant.url)
image = read_image(variant.image)
assert_equal 100, image.width
assert_equal 67, image.height
end
test "variant of a blob is on the same service" do
blob = create_file_blob(filename: "racecar.jpg", service_name: "local_public")
variant = blob.variant(resize_to_limit: [100, 100]).processed
assert_equal "local_public", variant.image.blob.service_name
end
test "eager loading has_one_attached record" do
user1 = User.create!(name: "Josh")
user2 = User.create!(name: "John")
blob1 = directly_upload_file_blob(filename: "racecar.jpg")
assert_difference -> { ActiveStorage::VariantRecord.count }, +1 do
blob1.representation(resize_to_limit: [100, 100]).processed
end
blob2 = directly_upload_file_blob(filename: "racecar_rotated.jpg")
assert_difference -> { ActiveStorage::VariantRecord.count }, +1 do
blob2.representation(resize_to_limit: [100, 100]).processed
end
assert_no_difference -> { ActiveStorage::VariantRecord.count } do
user1.cover_photo.attach(blob1)
user2.cover_photo.attach(blob2)
end
users = User.where(id: [user1.id, user2.id])
users.reset
assert_no_difference -> { ActiveStorage::VariantRecord.count } do
assert_queries(11) do
# 11 queries:
# users x 1
# attachment (cover photo) x 2
# blob for the cover photo x 2
# variant record x 1 per blob
# attachment x 1 per variant record
# variant record x 1 per variant record attachment
users.each do |u|
rep = u.cover_photo.representation(resize_to_limit: [100, 100])
rep.processed
rep.key
rep.url
end
end
end
users.reset
assert_no_difference -> { ActiveStorage::VariantRecord.count } do
assert_queries(6) do
# 6 queries:
# users x 1
# attachment (cover photos) x 1
# blob for the cover photo x 1
# variant record x 1
# attachment x 1
# variant record x 1
users.with_attached_cover_photo.each do |u|
rep = u.cover_photo.representation(resize_to_limit: [100, 100])
rep.processed
rep.key
rep.url
end
end
end
end
test "eager loading has_many_attached records" do
user = User.create!(name: "Josh")
blob1 = directly_upload_file_blob(filename: "racecar.jpg")
assert_difference -> { ActiveStorage::VariantRecord.count }, +1 do
blob1.representation(resize_to_limit: [100, 100]).processed
end
blob2 = directly_upload_file_blob(filename: "racecar_rotated.jpg")
assert_difference -> { ActiveStorage::VariantRecord.count }, +1 do
blob2.representation(resize_to_limit: [100, 100]).processed
end
assert_no_difference -> { ActiveStorage::VariantRecord.count } do
user.vlogs.attach(blob1)
user.vlogs.attach(blob2)
end
user.reload
assert_no_difference -> { ActiveStorage::VariantRecord.count } do
assert_queries(9) do
# 9 queries:
# attachments (vlogs) x 1
# blob x 2
# variant record x 1 per blob
# attachment x 1 per variant record
# variant record x 1 per variant record attachment
user.vlogs.each do |vlog|
rep = vlog.representation(resize_to_limit: [100, 100])
rep.processed
rep.key
rep.url
end
end
end
user.reload
assert_no_difference -> { ActiveStorage::VariantRecord.count } do
assert_queries(7) do
# 7 queries:
# attachments (vlogs) x 1
# blob x 1
# variant record x 1
# attachment -> blob x 1 per variant record (so 2)
user.vlogs.includes(blob: :variant_records).each do |vlog|
rep = vlog.representation(resize_to_limit: [100, 100])
rep.processed
rep.key
rep.url
end
end
end
user.reload
assert_no_difference -> { ActiveStorage::VariantRecord.count } do
assert_queries(5) do
# 5 queries:
# attachments (vlogs) x 1
# blobs for the vlogs x 1
# variant records for the blobs x 1
# attachments for the variant records x 1
# blobs for the attachments for the variant records x 1
user.vlogs.includes(blob: { variant_records: { image_attachment: :blob } }).each do |vlog|
rep = vlog.representation(resize_to_limit: [100, 100])
rep.processed
rep.key
rep.url
end
end
end
user.reload
assert_no_difference -> { ActiveStorage::VariantRecord.count } do
assert_queries(5) do
# 5 queries:
# attachments (vlogs) x 1
# blobs for the vlogs x 1
# variant records for the blobs x 1
# attachments for the variant records x 1
# blobs for the attachments for the variant records x 1
user.vlogs.with_all_variant_records.each do |vlog|
rep = vlog.representation(resize_to_limit: [100, 100])
rep.processed
rep.key
rep.url
end
end
end
user.reload
assert_no_difference -> { ActiveStorage::VariantRecord.count } do
assert_queries(6) do
# 6 queries:
# user x 1
# attachments (vlogs) x 1
# blobs for the vlogs x 1
# variant records for the blobs x 1
# attachments for the variant records x 1
# blobs for the attachments for the variant records x 1
User.where(id: user.id).with_attached_vlogs.each do |u|
u.vlogs.map do |vlog|
rep = vlog.representation(resize_to_limit: [100, 100])
rep.processed
rep.key
rep.url
end
end
end
end
user.reload
assert_difference -> { ActiveStorage::VariantRecord.count }, +2 do
# More queries here because we are creating a different variant.
# The second time we load this variant, we are back down to just 3 queries.
assert_queries(9, matcher: /SELECT/) do
# 9 queries:
# attachments (vlogs) initial load x 1
# blob x 1 (gets both records)
# variant record x 1 (gets both records)
# 2x get blob, attachment, variant records again, this happens when loading the new blob inside `VariantWithRecord#key`
user.vlogs.with_all_variant_records.each do |vlog|
rep = vlog.representation(resize_to_limit: [200, 200])
rep.processed
rep.key
rep.url
end
end
user.reload
assert_queries(5) do
user.vlogs.with_all_variant_records.each do |vlog|
rep = vlog.representation(resize_to_limit: [200, 200])
rep.processed
rep.key
rep.url
end
end
end
end
test "destroy deletes file from service" do
blob = create_file_blob(filename: "racecar.jpg")
variant = blob.variant(resize_to_limit: [100, 100]).processed
assert_equal 1, ActiveStorage::VariantRecord.count
assert blob.service.exist?(variant.key)
variant.destroy
assert_equal 0, ActiveStorage::VariantRecord.count
assert_enqueued_with(job: ActiveStorage::PurgeJob, args: [variant.image.blob])
end
end