diff options
Diffstat (limited to 'activestorage/test')
-rw-r--r-- | activestorage/test/analyzer/image_analyzer_test.rb | 5 | ||||
-rw-r--r-- | activestorage/test/analyzer/video_analyzer_test.rb | 5 | ||||
-rw-r--r-- | activestorage/test/models/presence_validation_test.rb | 30 | ||||
-rw-r--r-- | activestorage/test/models/variant_test.rb | 45 | ||||
-rw-r--r-- | activestorage/test/test_helper.rb | 6 |
5 files changed, 74 insertions, 17 deletions
diff --git a/activestorage/test/analyzer/image_analyzer_test.rb b/activestorage/test/analyzer/image_analyzer_test.rb index f8a38f001a..55bb5e7280 100644 --- a/activestorage/test/analyzer/image_analyzer_test.rb +++ b/activestorage/test/analyzer/image_analyzer_test.rb @@ -29,9 +29,4 @@ class ActiveStorage::Analyzer::ImageAnalyzerTest < ActiveSupport::TestCase assert_equal 792, metadata[:width] assert_equal 584, metadata[:height] end - - private - def extract_metadata_from(blob) - blob.tap(&:analyze).metadata - end end diff --git a/activestorage/test/analyzer/video_analyzer_test.rb b/activestorage/test/analyzer/video_analyzer_test.rb index 2612006551..d30f49315a 100644 --- a/activestorage/test/analyzer/video_analyzer_test.rb +++ b/activestorage/test/analyzer/video_analyzer_test.rb @@ -51,9 +51,4 @@ class ActiveStorage::Analyzer::VideoAnalyzerTest < ActiveSupport::TestCase metadata = extract_metadata_from(blob) assert_equal({ "analyzed" => true, "identified" => true }, metadata) end - - private - def extract_metadata_from(blob) - blob.tap(&:analyze).metadata - end end diff --git a/activestorage/test/models/presence_validation_test.rb b/activestorage/test/models/presence_validation_test.rb new file mode 100644 index 0000000000..aa804506dd --- /dev/null +++ b/activestorage/test/models/presence_validation_test.rb @@ -0,0 +1,30 @@ +# frozen_string_literal: true + +require "test_helper" +require "database/setup" + +class ActiveStorage::PresenceValidationTest < ActiveSupport::TestCase + class Admin < User; end + + teardown do + Admin.clear_validators! + end + + test "validates_presence_of has_one_attached" do + Admin.validates_presence_of :avatar + a = Admin.new + assert_predicate a, :invalid? + + a.avatar.attach create_blob(filename: "funky.jpg") + assert_predicate a, :valid? + end + + test "validates_presence_of has_many_attached" do + Admin.validates_presence_of :highlights + a = Admin.new + assert_predicate a, :invalid? + + a.highlights.attach create_blob(filename: "funky.jpg") + assert_predicate a, :valid? + end +end diff --git a/activestorage/test/models/variant_test.rb b/activestorage/test/models/variant_test.rb index 0f3ada25c0..e74bbc9ab4 100644 --- a/activestorage/test/models/variant_test.rb +++ b/activestorage/test/models/variant_test.rb @@ -25,13 +25,30 @@ class ActiveStorage::VariantTest < ActiveSupport::TestCase assert_match(/Gray/, image.colorspace) end - test "center-weighted crop of JPEG blob" do + test "center-weighted crop of JPEG blob using :combine_options" do + begin + ActiveStorage.variant_processor = nil + blob = create_file_blob(filename: "racecar.jpg") + variant = ActiveSupport::Deprecation.silence do + blob.variant(combine_options: { + gravity: "center", + resize: "100x100^", + crop: "100x100+0+0", + }).processed + end + assert_match(/racecar\.jpg/, variant.service_url) + + image = read_image(variant) + assert_equal 100, image.width + assert_equal 100, image.height + ensure + ActiveStorage.variant_processor = :mini_magick + end + end + + test "center-weighted crop of JPEG blob using :resize_to_fill" do blob = create_file_blob(filename: "racecar.jpg") - variant = blob.variant(combine_options: { - gravity: "center", - resize: "100x100^", - crop: "100x100+0+0", - }).processed + variant = blob.variant(resize_to_fill: [100, 100]).processed assert_match(/racecar\.jpg/, variant.service_url) image = read_image(variant) @@ -80,4 +97,20 @@ class ActiveStorage::VariantTest < ActiveSupport::TestCase variant = blob.variant(font: "a" * 10_000).processed assert_operator variant.service_url.length, :<, 525 end + + test "works for vips processor" do + begin + ActiveStorage.variant_processor = :vips + 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 + rescue LoadError + # libvips not installed + ensure + ActiveStorage.variant_processor = :mini_magick + end + end end diff --git a/activestorage/test/test_helper.rb b/activestorage/test/test_helper.rb index 028874f374..499d955a2f 100644 --- a/activestorage/test/test_helper.rb +++ b/activestorage/test/test_helper.rb @@ -7,7 +7,7 @@ require "bundler/setup" require "active_support" require "active_support/test_case" require "active_support/testing/autorun" -require "mini_magick" +require "image_processing/mini_magick" begin require "byebug" @@ -75,6 +75,10 @@ class ActiveSupport::TestCase def read_image(blob_or_variant) MiniMagick::Image.open blob_or_variant.service.send(:path_for, blob_or_variant.key) end + + def extract_metadata_from(blob) + blob.tap(&:analyze).metadata + end end require "global_id" |