Skip to content

Commit

Permalink
Test removing attachments via #attach
Browse files Browse the repository at this point in the history
  • Loading branch information
georgeclaghorn committed Jul 16, 2018
1 parent faa9a29 commit 1d13de4
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 12 deletions.
40 changes: 32 additions & 8 deletions activestorage/test/models/attached/many_test.rb
Expand Up @@ -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

Expand All @@ -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

Expand Down
31 changes: 27 additions & 4 deletions activestorage/test/models/attached/one_test.rb
Expand Up @@ -195,19 +195,43 @@ 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

assert_not @user.avatar.attached?
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

Expand All @@ -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

Expand Down

0 comments on commit 1d13de4

Please sign in to comment.