Skip to content

Commit

Permalink
Add more comprehensive rake task
Browse files Browse the repository at this point in the history
Previous rake task does not fix the data.
If there are draft assets in the chain of replacements, it will not work.
  • Loading branch information
lauraghiorghisor-tw committed Dec 9, 2024
1 parent e2dfc17 commit 826384f
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 35 deletions.
39 changes: 39 additions & 0 deletions lib/tasks/attachments/fix_asset_variant_replacement.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
desc "Identify and update corresponding replacement asset variant"
task :fix_asset_variant_replacement, %i[csv_path] => :environment do |_, args|
csv_path = args[:csv_path]

CSV.foreach(csv_path, headers: false) do |row|
attachment_data_id = row[0]
asset_manager_id = row[1]
variant = row[2]

attachment_data = AttachmentData.find(attachment_data_id)

unless attachment_data.replaced?
puts "Attachment Data ID: #{attachment_data.id}, Asset ID: #{asset_manager_id} - SKIPPED. No replacement found."
next
end

if attachment_data.deleted?
puts "Attachment Data ID: #{attachment_data.id}, Asset ID: #{asset_manager_id} - SKIPPED. Attachment data is deleted."
next
end

replacement_id = attachment_data.replaced_by_id
replacement_data = nil

until replacement_id.nil?
replacement_data = AttachmentData.find(replacement_id)
replacement_id = replacement_data.replaced_by_id
end

begin
replacement_id = replacement_data.assets.where(variant:).first.asset_manager_id
AssetManager::AssetUpdater.call(asset_manager_id, { "replacement_id" => replacement_id })

puts "Attachment Data ID: #{attachment_data_id}, Asset ID: #{asset_manager_id}, Variant: #{variant}, Replacement ID: #{replacement_id} - OK"
rescue StandardError => e
puts "Attachment Data ID: #{attachment_data_id}, Asset ID #{asset_manager_id} - ERROR, message: #{e.message}"
end
end
end
21 changes: 0 additions & 21 deletions lib/tasks/attachments/fix_missing_asset_replacement_links.rake

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
require "test_helper"
require "rake"

class FixMissingAssetReplacementLinksTest < ActiveSupport::TestCase
class FixAssetVariantReplacementTest < ActiveSupport::TestCase
extend Minitest::Spec::DSL

describe "fix_missing_asset_replacement_links" do
let(:task) { Rake::Task["fix_missing_asset_replacement_links"] }
describe "#fix_asset_variant_replacement" do
let(:task) { Rake::Task["fix_asset_variant_replacement"] }
let(:file) { Tempfile.new("read_assets_file") }
let(:filepath) { file.path }
let(:attachable) { create(:news_article) }
Expand All @@ -14,18 +14,22 @@ class FixMissingAssetReplacementLinksTest < ActiveSupport::TestCase
let(:replacement_data2) { create(:attachment_data, attachable:) }
let(:original_asset) { attachment_data.assets.original.first }
let(:thumbnail_asset) { attachment_data.assets.thumbnail.first }
let(:replacement_original_asset) { replacement_data.assets.original.first }
let(:replacement_thumbnail_asset) { replacement_data.assets.thumbnail.first }
let(:replacement_original_asset) { replacement_data2.assets.original.first }
let(:replacement_thumbnail_asset) { replacement_data2.assets.thumbnail.first }

teardown { task.reenable }

before do
csv_file = <<~CSV
123456
123456,1592008029c8c3e4dc76256c,original
123456,1592008029c8c3e4dc76256d,thumbnail
CSV
file.write(csv_file)
file.close

attachment_data.replaced_by = replacement_data
replacement_data.replaced_by = replacement_data2

original_asset.asset_manager_id = "1592008029c8c3e4dc76256c"
original_asset.save!
thumbnail_asset.asset_manager_id = "1592008029c8c3e4dc76256d"
Expand All @@ -42,14 +46,14 @@ class FixMissingAssetReplacementLinksTest < ActiveSupport::TestCase
replacement_data2.save!
end

test "it outputs AttachmentData ID and sends correct load" do
test "it sends the last replacement in the chain to asset-manager" do
Services.asset_manager.stubs(:asset).with(original_asset.asset_manager_id).returns("id" => "http://asset-manager/assets/#{original_asset.asset_manager_id}", "name" => "original.pdf")
Services.asset_manager.stubs(:asset).with(thumbnail_asset.asset_manager_id).returns("id" => "http://asset-manager/assets/#{thumbnail_asset.asset_manager_id}", "name" => "thumbnail.pdf.png")

expected_output = <<~OUTPUT
OK - ad: #{attachment_data.id}
Attachment Data ID: #{attachment_data.id}, Asset ID: #{original_asset.asset_manager_id}, Variant: #{original_asset.variant}, Replacement ID: #{replacement_original_asset.asset_manager_id} - OK
Attachment Data ID: #{attachment_data.id}, Asset ID: #{thumbnail_asset.asset_manager_id}, Variant: #{thumbnail_asset.variant}, Replacement ID: #{replacement_thumbnail_asset.asset_manager_id} - OK
OUTPUT

Services.asset_manager.expects(:update_asset)
.at_least_once
.with(original_asset.asset_manager_id, { "replacement_id" => replacement_original_asset.asset_manager_id })
Expand All @@ -62,24 +66,40 @@ class FixMissingAssetReplacementLinksTest < ActiveSupport::TestCase
assert_equal expected_output, output
end

test "it logs if no replacement is found" do
test "it skips and logs if no replacement is found" do
attachment_data.replaced_by_id = nil
attachment_data.save!

expected_output = <<~OUTPUT
ERROR - ad: #{attachment_data.id} does not have a replacement.
Attachment Data ID: #{attachment_data.id}, Asset ID: #{original_asset.asset_manager_id} - SKIPPED. No replacement found.
Attachment Data ID: #{attachment_data.id}, Asset ID: #{thumbnail_asset.asset_manager_id} - SKIPPED. No replacement found.
OUTPUT

output, _err = capture_io { task.invoke(filepath) }
assert_equal expected_output, output
end

test "it skips and logs if attachment data is deleted" do
AttachmentData.any_instance.stubs(:deleted?).returns(true)

expected_output = <<~OUTPUT
Attachment Data ID: #{attachment_data.id}, Asset ID: #{original_asset.asset_manager_id} - SKIPPED. Attachment data is deleted.
Attachment Data ID: #{attachment_data.id}, Asset ID: #{thumbnail_asset.asset_manager_id} - SKIPPED. Attachment data is deleted.
OUTPUT

output, _err = capture_io { task.invoke(filepath) }
assert_equal output, expected_output
assert_equal expected_output, output
end

test "it logs error if asset not found" do
error = GdsApi::HTTPClientError.new(404, "Not found")
Services.asset_manager.expects(:asset).with(original_asset.asset_manager_id).raises(error)
Services.asset_manager.stubs(:asset).with(thumbnail_asset.asset_manager_id).returns("id" => "http://asset-manager/assets/#{thumbnail_asset.asset_manager_id}", "name" => "thumbnail.pdf.png")
Services.asset_manager.stubs(:update_asset).with(thumbnail_asset.asset_manager_id, { "replacement_id" => replacement_original_asset.asset_manager_id }).raises(error)

expected_output = <<~OUTPUT
ERROR - ad: #{attachment_data.id}, message: Not found
Attachment Data ID: #{attachment_data.id}, Asset ID #{original_asset.asset_manager_id} - ERROR, message: #{error.message}
Attachment Data ID: #{attachment_data.id}, Asset ID: #{thumbnail_asset.asset_manager_id}, Variant: #{thumbnail_asset.variant}, Replacement ID: #{replacement_thumbnail_asset.asset_manager_id} - OK
OUTPUT

output, _err = capture_io { task.invoke(filepath) }
Expand All @@ -91,9 +111,11 @@ class FixMissingAssetReplacementLinksTest < ActiveSupport::TestCase
Services.asset_manager.stubs(:asset).with(original_asset.asset_manager_id).returns("id" => "http://asset-manager/assets/#{original_asset.asset_manager_id}", "name" => "original.pdf", "replacement_id" => nil)
Services.asset_manager.stubs(:asset).with(thumbnail_asset.asset_manager_id).returns("id" => "http://asset-manager/assets/#{thumbnail_asset.asset_manager_id}", "name" => "thumbnail.pdf.png")
Services.asset_manager.stubs(:update_asset).with(original_asset.asset_manager_id, { "replacement_id" => replacement_original_asset.asset_manager_id }).raises(error)
Services.asset_manager.stubs(:update_asset).with(thumbnail_asset.asset_manager_id, { "replacement_id" => replacement_thumbnail_asset.asset_manager_id })

expected_output = <<~OUTPUT
ERROR - ad: #{attachment_data.id}, message: Server error
Attachment Data ID: #{attachment_data.id}, Asset ID #{original_asset.asset_manager_id} - ERROR, message: Server error
Attachment Data ID: #{attachment_data.id}, Asset ID: #{thumbnail_asset.asset_manager_id}, Variant: #{thumbnail_asset.variant}, Replacement ID: #{replacement_thumbnail_asset.asset_manager_id} - OK
OUTPUT

output, _err = capture_io { task.invoke(filepath) }
Expand Down

0 comments on commit 826384f

Please sign in to comment.