Test removing attachments via #attach

This commit is contained in:
George Claghorn 2018-07-16 08:59:23 -04:00
parent faa9a29fbb
commit 1d13de4e39
2 changed files with 59 additions and 12 deletions

@ -195,21 +195,47 @@ class ActiveStorage::ManyAttachedTest < ActiveSupport::TestCase
end
end
test "successfully updating an existing record to remove dependent attachments" do
test "removing dependent attachments from an existing record" do
[ create_blob(filename: "funky.jpg"), create_blob(filename: "town.jpg") ].tap do |blobs|
@user.highlights.attach blobs
perform_enqueued_jobs do
@user.update! highlights: []
assert_enqueued_with job: ActiveStorage::PurgeJob, args: [ blobs.first ] do
assert_enqueued_with job: ActiveStorage::PurgeJob, args: [ blobs.second ] do
@user.highlights.attach []
end
end
assert_not @user.highlights.attached?
assert_not ActiveStorage::Blob.service.exist?(blobs.first.key)
assert_not ActiveStorage::Blob.service.exist?(blobs.second.key)
end
end
test "successfully updating an existing record to remove independent attachments" do
test "removing independent attachments from an existing record" do
[ create_blob(filename: "funky.jpg"), create_blob(filename: "town.jpg") ].tap do |blobs|
@user.vlogs.attach blobs
assert_no_enqueued_jobs only: ActiveStorage::PurgeJob do
@user.vlogs.attach []
end
assert_not @user.vlogs.attached?
end
end
test "updating an existing record to remove dependent attachments" do
[ create_blob(filename: "funky.jpg"), create_blob(filename: "town.jpg") ].tap do |blobs|
@user.highlights.attach blobs
assert_enqueued_with job: ActiveStorage::PurgeJob, args: [ blobs.first ] do
assert_enqueued_with job: ActiveStorage::PurgeJob, args: [ blobs.second ] do
@user.update! highlights: []
end
end
assert_not @user.highlights.attached?
end
end
test "updating an existing record to remove independent attachments" do
[ create_blob(filename: "funky.mp4"), create_blob(filename: "town.mp4") ].tap do |blobs|
@user.vlogs.attach blobs
@ -218,8 +244,6 @@ class ActiveStorage::ManyAttachedTest < ActiveSupport::TestCase
end
assert_not @user.vlogs.attached?
assert ActiveStorage::Blob.service.exist?(blobs.first.key)
assert ActiveStorage::Blob.service.exist?(blobs.second.key)
end
end

@ -195,11 +195,35 @@ class ActiveStorage::OneAttachedTest < ActiveSupport::TestCase
end
end
test "successfully updating an existing record to remove a dependent attachment" do
test "removing a dependent attachment from an existing record" do
create_blob(filename: "funky.jpg").tap do |blob|
@user.avatar.attach blob
perform_enqueued_jobs do
assert_enqueued_with job: ActiveStorage::PurgeJob, args: [ blob ] do
@user.avatar.attach nil
end
assert_not @user.avatar.attached?
end
end
test "removing an independent attachment from an existing record" do
create_blob(filename: "funky.jpg").tap do |blob|
@user.cover_photo.attach blob
assert_no_enqueued_jobs only: ActiveStorage::PurgeJob do
@user.cover_photo.attach nil
end
assert_not @user.cover_photo.attached?
end
end
test "updating an existing record to remove a dependent attachment" do
create_blob(filename: "funky.jpg").tap do |blob|
@user.avatar.attach blob
assert_enqueued_with job: ActiveStorage::PurgeJob, args: [ blob ] do
@user.update! avatar: nil
end
@ -207,7 +231,7 @@ class ActiveStorage::OneAttachedTest < ActiveSupport::TestCase
end
end
test "successfully updating an existing record to remove an independent attachment" do
test "updating an existing record to remove an independent attachment" do
create_blob(filename: "funky.jpg").tap do |blob|
@user.cover_photo.attach blob
@ -216,7 +240,6 @@ class ActiveStorage::OneAttachedTest < ActiveSupport::TestCase
end
assert_not @user.cover_photo.attached?
assert ActiveStorage::Blob.service.exist?(blob.key)
end
end