2018-05-29 19:57:56 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require "test_helper"
|
|
|
|
|
|
|
|
class ActiveStorage::ReflectionTest < ActiveSupport::TestCase
|
2018-07-07 21:04:47 +00:00
|
|
|
test "reflecting on a singular attachment" do
|
2018-05-29 19:57:56 +00:00
|
|
|
reflection = User.reflect_on_attachment(:avatar)
|
2018-07-07 21:04:47 +00:00
|
|
|
assert_equal User, reflection.active_record
|
2018-05-29 19:57:56 +00:00
|
|
|
assert_equal :avatar, reflection.name
|
|
|
|
assert_equal :has_one_attached, reflection.macro
|
2018-07-07 21:04:47 +00:00
|
|
|
assert_equal :purge_later, reflection.options[:dependent]
|
2019-01-14 19:56:35 +00:00
|
|
|
|
|
|
|
reflection = User.reflect_on_attachment(:cover_photo)
|
|
|
|
assert_equal :local, reflection.options[:service_name]
|
2020-05-03 19:03:46 +00:00
|
|
|
|
|
|
|
reflection = User.reflect_on_attachment(:avatar_with_variants)
|
2023-02-23 03:26:49 +00:00
|
|
|
assert_instance_of Hash, reflection.named_variants
|
2018-05-29 19:57:56 +00:00
|
|
|
end
|
|
|
|
|
2018-07-07 21:04:47 +00:00
|
|
|
test "reflection on a singular attachment with the same name as an attachment on another model" do
|
|
|
|
reflection = Group.reflect_on_attachment(:avatar)
|
|
|
|
assert_equal Group, reflection.active_record
|
|
|
|
end
|
2018-05-29 19:57:56 +00:00
|
|
|
|
2018-07-07 21:04:47 +00:00
|
|
|
test "reflecting on a collection attachment" do
|
|
|
|
reflection = User.reflect_on_attachment(:highlights)
|
|
|
|
assert_equal User, reflection.active_record
|
2018-05-29 19:57:56 +00:00
|
|
|
assert_equal :highlights, reflection.name
|
|
|
|
assert_equal :has_many_attached, reflection.macro
|
2018-07-07 21:04:47 +00:00
|
|
|
assert_equal :purge_later, reflection.options[:dependent]
|
2019-01-14 19:56:35 +00:00
|
|
|
|
|
|
|
reflection = User.reflect_on_attachment(:vlogs)
|
|
|
|
assert_equal :local, reflection.options[:service_name]
|
2020-05-03 19:03:46 +00:00
|
|
|
|
|
|
|
reflection = User.reflect_on_attachment(:highlights_with_variants)
|
2023-02-23 03:26:49 +00:00
|
|
|
assert_instance_of Hash, reflection.named_variants
|
2018-07-07 21:04:47 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
test "reflecting on all attachments" do
|
|
|
|
reflections = User.reflect_on_all_attachments.sort_by(&:name)
|
|
|
|
assert_equal [ User ], reflections.collect(&:active_record).uniq
|
2024-03-19 13:23:36 +00:00
|
|
|
assert_equal %i[ avatar avatar_with_conditional_preprocessed avatar_with_preprocessed avatar_with_variants cover_photo highlights highlights_with_conditional_preprocessed highlights_with_preprocessed highlights_with_variants intro_video name_pronunciation_audio resume resume_with_preprocessing vlogs ], reflections.collect(&:name)
|
|
|
|
assert_equal %i[ has_one_attached has_one_attached has_one_attached has_one_attached has_one_attached has_many_attached has_many_attached has_many_attached has_many_attached has_one_attached has_one_attached has_one_attached has_one_attached has_many_attached ], reflections.collect(&:macro)
|
|
|
|
assert_equal [ :purge_later, :purge_later, :purge_later, :purge_later, false, :purge_later, :purge_later, :purge_later, :purge_later, :purge_later, :purge_later, :purge_later, :purge_later, false ], reflections.collect { |reflection| reflection.options[:dependent] }
|
2018-05-29 19:57:56 +00:00
|
|
|
end
|
|
|
|
end
|