Skip to content
Permalink
Browse files

AO3-5245 Update Open Doors rake task to use AO3 ids to find works (#3551

)

* AO3-5245 Update Open Doors rake task to use AO3 ids to find works

* AO3-5245 Test that Rake task processes CSV files correctly

* AO3-5245 Remove Before and After and add a file fixture
  • Loading branch information...
ariana-paris authored and redsummernight committed Apr 24, 2019
1 parent 617840a commit c4858341cb40349199940e0abcad8d95830a53f4
@@ -1,19 +1,44 @@
namespace :opendoors do
desc "Map import urls based on spreadsheet data"
task(:import_url_mapping => :environment) do
puts "Where is the Open Doors CSV located?"
loc = gets.chomp
CSV.foreach(loc, headers: true) do |row|

class UrlUpdater
def update_work(row)
begin
work = Work.where(imported_from_url: row["URL Imported From"]).first
if work
work = Work.find(row["AO3 id"])
if work&.imported_from_url.blank? || work&.imported_from_url == row["URL Imported From"]
work.imported_from_url = row["Original URL"]
puts work.imported_from_url
work.save!
"#{work.id}\twas updated: its import url is now #{work.imported_from_url}"
else
"#{work.id}\twas not changed: its import url is #{work.imported_from_url}"
end
rescue
puts "Could not update work from #{row["Original URL"]}"
rescue StandardError => e
"#{row["AO3 id"]}\twas not changed: #{e}"
end
end
end
end

desc "Map import urls based on spreadsheet data - required fields: 'AO3 id', 'URL Imported From', 'Original URL'"
task :import_url_mapping, [:csv] => :environment do |_t, args|
loc = if args[:csv].nil?
puts "Where is the Open Doors CSV located?"
STDIN.gets.chomp
else
args[:csv]
end

begin
f = File.open("opendoors_result.txt", "w")
url_updater = UrlUpdater.new

CSV.foreach(loc, headers: true) do |row|
result = url_updater.update_work(row)
puts result
f.write(result)
end
rescue TypeError => e # No or invalid CSV file
puts "Error parsing CSV file #{loc}: #{e.message}"
ensure
f.close
end
end
end
@@ -0,0 +1,70 @@
require 'spec_helper'
require 'rake'

describe 'rake opendoors:import_url_mapping' do
id1 = 1235
id2 = 1236
temp_url = "http://temp/1"
original_url = "http://another/1"
original_url2 = "http://another/2"

let!(:work_with_temp_url) { create(:work, id: id1, imported_from_url: temp_url) }
let!(:work_with_no_url) { create(:work, id: id2, imported_from_url: nil) }

context "Open Doors rake task" do
it "takes a path to a CSV and updates works" do
path = file_fixture("opendoors_import_url_mapping.csv")
subject.invoke(path)
expect(Work.find(work_with_temp_url.id).imported_from_url).to eq(original_url)
expect(Work.find(work_with_no_url.id).imported_from_url).to eq(original_url2)
end
it "fails if the CSV path is invalid" do
expect { subject.invoke("not a path") }.to raise_error Errno::ENOENT
end
end

context "UrlUpdater" do
let(:url_updater) { UrlUpdater.new }
let(:work_with_other_url) { create(:work, imported_from_url: "http://another/1") }

it "returns an error if the work is not found" do
row = {
"URL Imported From" => "http://temp/1",
"Original URL" => "http://another/2",
"AO3 id" => 7777773
}
result = url_updater.update_work(row)
expect(result).to eq("7777773\twas not changed: Couldn't find Work with 'id'=7777773")
end

it "returns a message if the work already has another imported URL" do
row = {
"URL Imported From" => "http://temp/1",
"Original URL" => "http://another/2",
"AO3 id" => work_with_other_url.id
}
result = url_updater.update_work(row)
expect(result).to match("\\d+\twas not changed: its import url is http://another/1")
end

it "updates the work if it has no imported URL" do
row = {
"URL Imported From" => "http://temp/1",
"Original URL" => "http://another/2",
"AO3 id" => work_with_no_url.id
}
result = url_updater.update_work(row)
expect(result).to match("\\d+\twas updated: its import url is now http://another/2")
end

it "updates the work if it has the temp site imported URL" do
row = {
"URL Imported From" => "http://temp/1",
"Original URL" => "http://another/2",
"AO3 id" => work_with_temp_url.id
}
result = url_updater.update_work(row)
expect(result).to match("\\d+\twas updated: its import url is now http://another/2")
end
end
end
@@ -89,6 +89,8 @@
# Set default formatter to print out the description of each test as it runs
config.color = true
config.formatter = :documentation

config.file_fixture_path = "spec/support/fixtures"
end

def clean_the_database
@@ -0,0 +1,3 @@
AO3 id,URL Imported From,Original URL
1235,http://temp/1,http://another/1
1236,non-existent URL,http://another/2

0 comments on commit c485834

Please sign in to comment.
You can’t perform that action at this time.