1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00
rails--rails/activestorage/test/models/variant_test.rb

200 lines
6.5 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
2017-07-11 12:53:17 -04:00
require "test_helper"
require "database/setup"
require "minitest/mock"
2017-07-11 12:53:17 -04:00
2017-07-21 16:52:09 -04:00
class ActiveStorage::VariantTest < ActiveSupport::TestCase
setup do
@was_tracking, ActiveStorage.track_variants = ActiveStorage.track_variants, false
end
teardown do
ActiveStorage.track_variants = @was_tracking
end
2019-04-20 06:05:58 -04:00
test "variations have the same key for different types of the same transformation" do
blob = create_file_blob(filename: "racecar.jpg")
variant_a = blob.variant(resize: "100x100")
variant_b = blob.variant("resize" => "100x100")
assert_equal variant_a.key, variant_b.key
end
test "resized variation of JPEG blob" do
blob = create_file_blob(filename: "racecar.jpg")
variant = blob.variant(resize: "100x100").processed
assert_match(/racecar\.jpg/, variant.url)
2017-09-28 16:43:37 -04:00
image = read_image(variant)
assert_equal 100, image.width
assert_equal 67, image.height
2017-07-21 16:51:31 -04:00
end
2017-07-11 12:53:17 -04:00
test "resized and monochrome variation of JPEG blob" do
blob = create_file_blob(filename: "racecar.jpg")
variant = blob.variant(resize: "100x100", monochrome: true).processed
assert_match(/racecar\.jpg/, variant.url)
2017-09-28 16:43:37 -04:00
image = read_image(variant)
assert_equal 100, image.width
assert_equal 67, image.height
assert_match(/Gray/, image.colorspace)
2017-07-11 12:53:17 -04:00
end
2018-05-19 22:14:57 -04:00
test "monochrome with default variant_processor" do
blob = create_file_blob(filename: "racecar.jpg")
variant = blob.variant(monochrome: true).processed
image = read_image(variant)
assert_match(/Gray/, image.colorspace)
2018-05-19 22:14:57 -04:00
end
test "disabled variation of JPEG blob" do
blob = create_file_blob(filename: "racecar.jpg")
variant = blob.variant(resize: "100x100", monochrome: false).processed
assert_match(/racecar\.jpg/, variant.url)
2018-05-19 22:14:57 -04:00
image = read_image(variant)
assert_equal 100, image.width
assert_equal 67, image.height
assert_match(/RGB/, image.colorspace)
end
test "center-weighted crop of JPEG blob using :resize_to_fill" do
blob = create_file_blob(filename: "racecar.jpg")
variant = blob.variant(resize_to_fill: [100, 100]).processed
assert_match(/racecar\.jpg/, variant.url)
image = read_image(variant)
assert_equal 100, image.width
assert_equal 100, image.height
end
test "resized variation of PSD blob" do
blob = create_file_blob(filename: "icon.psd", content_type: "image/vnd.adobe.photoshop")
variant = blob.variant(resize: "20x20").processed
assert_match(/icon\.png/, variant.url)
image = read_image(variant)
assert_equal "PNG", image.type
assert_equal 20, image.width
assert_equal 20, image.height
end
test "resized variation of ICO blob" do
blob = create_file_blob(filename: "favicon.ico", content_type: "image/vnd.microsoft.icon")
variant = blob.variant(resize: "20x20").processed
assert_match(/icon\.png/, variant.url)
image = read_image(variant)
assert_equal "PNG", image.type
assert_equal 20, image.width
assert_equal 20, image.height
end
test "resized variation of TIFF blob" do
blob = create_file_blob(filename: "racecar.tif")
variant = blob.variant(resize: "50x50").processed
assert_match(/racecar\.png/, variant.url)
image = read_image(variant)
assert_equal "PNG", image.type
assert_equal 50, image.width
assert_equal 33, image.height
end
test "resized variation of BMP blob" do
blob = create_file_blob(filename: "colors.bmp", content_type: "image/bmp")
variant = blob.variant(resize: "15x15").processed
assert_match(/colors\.png/, variant.url)
image = read_image(variant)
assert_equal "PNG", image.type
assert_equal 15, image.width
assert_equal 8, image.height
end
test "optimized variation of GIF blob" do
blob = create_file_blob(filename: "image.gif", content_type: "image/gif")
assert_nothing_raised do
blob.variant(layers: "Optimize").processed
end
end
test "PNG variation of JPEG blob with lowercase format" do
blob = create_file_blob(filename: "racecar.jpg")
variant = blob.variant(format: :png).processed
assert_equal "racecar.png", variant.filename.to_s
assert_equal "image/png", variant.content_type
assert_equal "PNG", read_image(variant).type
end
test "PNG variation of JPEG blob with uppercase format" do
blob = create_file_blob(filename: "racecar.jpg")
variant = blob.variant(format: "PNG").processed
assert_equal "racecar.png", variant.filename.to_s
assert_equal "image/png", variant.content_type
assert_equal "PNG", read_image(variant).type
end
test "variation of invariable blob" do
assert_raises ActiveStorage::InvariableError do
create_file_blob(filename: "report.pdf", content_type: "application/pdf").variant(resize: "100x100")
end
end
test "url doesn't grow in length despite long variant options" do
blob = create_file_blob(filename: "racecar.jpg")
variant = blob.variant(font: "a" * 10_000).processed
assert_operator variant.url.length, :<, 785
end
Use ImageProcessing gem for ActiveStorage variants ImageProcessing gem is a wrapper around MiniMagick and ruby-vips, and implements an interface for common image resizing and processing. This is the canonical image processing gem recommended in [Shrine], and that's where it developed from. The initial implementation was extracted from Refile, which also implements on-the-fly transformations. Some features that ImageProcessing gem adds on top of MiniMagick: * resizing macros - #resize_to_limit - #resize_to_fit - #resize_to_fill - #resize_and_pad * automatic orientation * automatic thumbnail sharpening * avoids the complex and inefficient MiniMagick::Image class * will use "magick" instead of "convert" on ImageMagick 7 However, the biggest feature of the ImageProcessing gem is that it has an alternative implementation that uses libvips. Libvips is an alternative to ImageMagick that can process images very rapidly (we've seen up 10x faster than ImageMagick). What's great is that the ImageProcessing gem provides the same interface for both implementations. The macros are named the same, and the libvips implementation does auto orientation and thumbnail sharpening as well; only the operations/options specific to ImageMagick/libvips differ. The integration provided by this PR should work for both implementations. The plan is to introduce the ImageProcessing backend in Rails 6.0 as the default backend and deprecate the MiniMagick backend, then in Rails 6.1 remove the MiniMagick backend.
2018-04-05 19:48:29 -04:00
test "thumbnail variation of JPEG blob processed with VIPS" do
process_variants_with :vips do
blob = create_file_blob(filename: "racecar.jpg")
variant = blob.variant(thumbnail_image: 100).processed
image = read_image(variant)
assert_equal 100, image.width
assert_equal 67, image.height
end
end
test "thumbnail variation of extensionless GIF blob processed with VIPS" do
process_variants_with :vips do
blob = ActiveStorage::Blob.create_and_upload!(io: file_fixture("image.gif").open, filename: "image", content_type: "image/gif")
variant = blob.variant(resize_to_fit: [100, 100]).processed
image = read_image(variant)
assert_equal "image.gif", variant.filename.to_s
assert_equal "image/gif", variant.content_type
assert_equal "GIF", image.type
assert_equal 100, image.width
assert_equal 100, image.height
end
Use ImageProcessing gem for ActiveStorage variants ImageProcessing gem is a wrapper around MiniMagick and ruby-vips, and implements an interface for common image resizing and processing. This is the canonical image processing gem recommended in [Shrine], and that's where it developed from. The initial implementation was extracted from Refile, which also implements on-the-fly transformations. Some features that ImageProcessing gem adds on top of MiniMagick: * resizing macros - #resize_to_limit - #resize_to_fit - #resize_to_fill - #resize_and_pad * automatic orientation * automatic thumbnail sharpening * avoids the complex and inefficient MiniMagick::Image class * will use "magick" instead of "convert" on ImageMagick 7 However, the biggest feature of the ImageProcessing gem is that it has an alternative implementation that uses libvips. Libvips is an alternative to ImageMagick that can process images very rapidly (we've seen up 10x faster than ImageMagick). What's great is that the ImageProcessing gem provides the same interface for both implementations. The macros are named the same, and the libvips implementation does auto orientation and thumbnail sharpening as well; only the operations/options specific to ImageMagick/libvips differ. The integration provided by this PR should work for both implementations. The plan is to introduce the ImageProcessing backend in Rails 6.0 as the default backend and deprecate the MiniMagick backend, then in Rails 6.1 remove the MiniMagick backend.
2018-04-05 19:48:29 -04:00
end
2019-09-17 00:08:22 -04:00
test "passes content_type on upload" do
blob = create_file_blob(filename: "racecar.jpg", content_type: "image/jpeg")
mock_upload = lambda do |_, _, options = {}|
assert_equal "image/jpeg", options[:content_type]
2019-09-17 00:08:22 -04:00
end
blob.service.stub(:upload, mock_upload) do
blob.variant(resize: "100x100").processed
end
end
private
def process_variants_with(processor)
previous_processor, ActiveStorage.variant_processor = ActiveStorage.variant_processor, processor
yield
rescue LoadError
skip "Variant processor #{processor.inspect} is not installed"
ensure
ActiveStorage.variant_processor = previous_processor
end
2017-07-11 12:53:17 -04:00
end