2017-09-28 16:43:37 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require "test_helper"
|
|
|
|
require "database/setup"
|
|
|
|
|
|
|
|
class ActiveStorage::PreviewTest < ActiveSupport::TestCase
|
|
|
|
test "previewing a PDF" do
|
|
|
|
blob = create_file_blob(filename: "report.pdf", content_type: "application/pdf")
|
|
|
|
preview = blob.preview(resize: "640x280").processed
|
2017-10-12 11:47:21 -04:00
|
|
|
|
2018-01-25 18:14:09 -05:00
|
|
|
assert_predicate preview.image, :attached?
|
2017-09-28 16:43:37 -04:00
|
|
|
assert_equal "report.png", preview.image.filename.to_s
|
|
|
|
assert_equal "image/png", preview.image.content_type
|
|
|
|
|
|
|
|
image = read_image(preview.image)
|
|
|
|
assert_equal 612, image.width
|
|
|
|
assert_equal 792, image.height
|
|
|
|
end
|
|
|
|
|
|
|
|
test "previewing an MP4 video" do
|
|
|
|
blob = create_file_blob(filename: "video.mp4", content_type: "video/mp4")
|
|
|
|
preview = blob.preview(resize: "640x280").processed
|
2017-10-12 11:47:21 -04:00
|
|
|
|
2018-01-25 18:14:09 -05:00
|
|
|
assert_predicate preview.image, :attached?
|
2018-05-22 15:03:31 -04:00
|
|
|
assert_equal "video.jpg", preview.image.filename.to_s
|
|
|
|
assert_equal "image/jpeg", preview.image.content_type
|
2017-09-28 16:43:37 -04:00
|
|
|
|
|
|
|
image = read_image(preview.image)
|
|
|
|
assert_equal 640, image.width
|
|
|
|
assert_equal 480, image.height
|
|
|
|
end
|
|
|
|
|
|
|
|
test "previewing an unpreviewable blob" do
|
|
|
|
blob = create_file_blob
|
|
|
|
|
2018-01-10 12:12:04 -05:00
|
|
|
assert_raises ActiveStorage::UnpreviewableError do
|
2017-09-28 16:43:37 -04:00
|
|
|
blob.preview resize: "640x280"
|
|
|
|
end
|
|
|
|
end
|
2019-12-05 09:21:25 -05:00
|
|
|
|
|
|
|
test "previewing on the writer DB" do
|
|
|
|
blob = create_file_blob(filename: "report.pdf", content_type: "application/pdf")
|
|
|
|
|
|
|
|
# Simulate a selector middleware switching to a read-only replica.
|
|
|
|
ActiveRecord::Base.connection_handler.while_preventing_writes do
|
|
|
|
blob.preview(resize: "640x280").processed
|
|
|
|
end
|
|
|
|
|
|
|
|
assert blob.reload.preview_image.attached?
|
|
|
|
end
|
2017-09-28 16:43:37 -04:00
|
|
|
end
|