diff options
Diffstat (limited to 'activestorage/test/models')
-rw-r--r-- | activestorage/test/models/attached_test.rb | 40 | ||||
-rw-r--r-- | activestorage/test/models/blob_test.rb | 6 | ||||
-rw-r--r-- | activestorage/test/models/reflection_test.rb | 29 |
3 files changed, 53 insertions, 22 deletions
diff --git a/activestorage/test/models/attached_test.rb b/activestorage/test/models/attached_test.rb index 14395e12df..b10d2bebe3 100644 --- a/activestorage/test/models/attached_test.rb +++ b/activestorage/test/models/attached_test.rb @@ -4,8 +4,6 @@ require "test_helper" require "database/setup" class ActiveStorage::AttachmentsTest < ActiveSupport::TestCase - include ActiveJob::TestHelper - setup do @user = User.create!(name: "Josh") end @@ -19,16 +17,18 @@ class ActiveStorage::AttachmentsTest < ActiveSupport::TestCase # inherited only assert_equal "funky.jpg", @user.avatar.filename.to_s - User.class_eval do - def avatar - super.filename.to_s.reverse + begin + User.class_eval do + def avatar + super.filename.to_s.reverse + end end - end - # override with super - assert_equal "funky.jpg".reverse, @user.avatar - - User.send(:remove_method, :avatar) + # override with super + assert_equal "funky.jpg".reverse, @user.avatar + ensure + User.send(:remove_method, :avatar) + end end test "overriding has_many_attached methods works" do @@ -39,16 +39,18 @@ class ActiveStorage::AttachmentsTest < ActiveSupport::TestCase assert_equal "funky.jpg", @user.highlights.first.filename.to_s assert_equal "wonky.jpg", @user.highlights.second.filename.to_s - User.class_eval do - def highlights - super.reverse + begin + User.class_eval do + def highlights + super.reverse + end end - end - - # override with super - assert_equal "wonky.jpg", @user.highlights.first.filename.to_s - assert_equal "funky.jpg", @user.highlights.second.filename.to_s - User.send(:remove_method, :highlights) + # override with super + assert_equal "wonky.jpg", @user.highlights.first.filename.to_s + assert_equal "funky.jpg", @user.highlights.second.filename.to_s + ensure + User.send(:remove_method, :highlights) + end end end diff --git a/activestorage/test/models/blob_test.rb b/activestorage/test/models/blob_test.rb index 88ce0f868a..40b30acd3e 100644 --- a/activestorage/test/models/blob_test.rb +++ b/activestorage/test/models/blob_test.rb @@ -151,16 +151,16 @@ class ActiveStorage::BlobTest < ActiveSupport::TestCase test "urls allow for custom options" do blob = create_blob(filename: "original.txt") - options = [ + arguments = [ blob.key, - expires_in: blob.service.url_expires_in, + expires_in: ActiveStorage.service_urls_expire_in, disposition: :inline, content_type: blob.content_type, filename: blob.filename, thumb_size: "300x300", thumb_mode: "crop" ] - assert_called_with(blob.service, :url, options) do + assert_called_with(blob.service, :url, arguments) do blob.service_url(thumb_size: "300x300", thumb_mode: "crop") end end diff --git a/activestorage/test/models/reflection_test.rb b/activestorage/test/models/reflection_test.rb new file mode 100644 index 0000000000..da866ca996 --- /dev/null +++ b/activestorage/test/models/reflection_test.rb @@ -0,0 +1,29 @@ +# frozen_string_literal: true + +require "test_helper" + +class ActiveStorage::ReflectionTest < ActiveSupport::TestCase + test "allows reflecting for all attachment" do + expected_classes = + User.reflect_on_all_attachments.all? do |reflection| + reflection.is_a?(ActiveStorage::Reflection::HasOneAttachedReflection) || + reflection.is_a?(ActiveStorage::Reflection::HasManyAttachedReflection) + end + + assert expected_classes + end + + test "allows reflecting on a singular has_one_attached attachment" do + reflection = User.reflect_on_attachment(:avatar) + + assert_equal :avatar, reflection.name + assert_equal :has_one_attached, reflection.macro + end + + test "allows reflecting on a singular has_many_attached attachment" do + reflection = User.reflect_on_attachment(:highlights) + + assert_equal :highlights, reflection.name + assert_equal :has_many_attached, reflection.macro + end +end |