Skip to content
This repository was archived by the owner on Jan 24, 2025. It is now read-only.

Commit a59b32d

Browse files
authored
Merge pull request #766 from alphagov/fix-organogram-filenames
Fix organogram filenames
2 parents 8459ae7 + c0bf83f commit a59b32d

3 files changed

+98
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
require_relative 'update_organogram_filenames.rb'
2+
3+
desc "Fix existing organogram filenames"
4+
task "fix_existing_organogram_filenames" => :environment do
5+
UpdateOrganogramFilenames.new.call
6+
end
+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
require 'csv'
2+
3+
class UpdateOrganogramFilenames
4+
def initialize
5+
@old_urls = []
6+
@new_urls = []
7+
end
8+
9+
def call
10+
csv_file = "./lib/tasks/old_new_urls.csv"
11+
12+
parse_csv(csv_file)
13+
msg = replace_urls
14+
puts msg
15+
end
16+
17+
def parse_csv(csv_file)
18+
puts "Parsing CSV '#{csv_file}'"
19+
CSV.foreach(csv_file) do |row|
20+
@old_urls << row.first
21+
@new_urls << row.last.strip
22+
end
23+
end
24+
25+
def replace_urls
26+
puts "Searching for urls containing '-posts-'..."
27+
28+
if @old_urls.empty?
29+
"No urls to process"
30+
else
31+
Link.all.each do |link|
32+
if link.url.include? "-posts-"
33+
index = @old_urls.index(link.url)
34+
if index != nil
35+
puts "From dataset: " + link.dataset.name
36+
puts "Replace url '" + link.url + "' with '" + @new_urls[index] + "'"
37+
link.url = @new_urls[index]
38+
link.save(validate: false)
39+
40+
if Link.where(url: @new_urls[index]).empty?
41+
puts "Url replacement failed"
42+
else
43+
puts "Url successfully replaced"
44+
end
45+
else
46+
puts "WARNING: " + link.url + " not found"
47+
end
48+
puts "==============="
49+
end
50+
end
51+
"Update complete"
52+
end
53+
end
54+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
require 'rails_helper'
2+
require_relative '../../../lib/tasks/update_organogram_filenames.rb'
3+
4+
describe UpdateOrganogramFilenames do
5+
before do
6+
@link1 = FactoryBot.create(:link, url: "https://amazonaws.com/datagovuk/dataset/resources/organogram-senior-posts-2019-06-06T11-18-26Z.csv")
7+
@link2 = FactoryBot.create(:link, url: "https://amazonaws.com/datagovuk/dataset/resources/organogram-junior-posts-2019-06-06T11-18-30Z.csv")
8+
File.write("sample_urls.csv", "https://amazonaws.com/datagovuk/dataset/resources/organogram-senior-posts-2019-06-06T11-18-26Z.csv, https://amazonaws.com/datagovuk/dataset/resources/2019-06-06T11-18-26Z-organogram-senior.csv\nhttps://amazonaws.com/datagovuk/dataset/resources/organogram-junior-posts-2019-06-06T11-18-30Z.csv, https://amazonaws.com/datagovuk/dataset/resources/2019-06-06T11-18-26Z-organogram-junior.csv")
9+
end
10+
11+
context "when parsing a CSV file" do
12+
it "should set old_urls and new_urls array if it contains data" do
13+
update_organogram_filenames = UpdateOrganogramFilenames.new
14+
update_organogram_filenames.parse_csv("sample_urls.csv")
15+
expect(update_organogram_filenames.instance_eval { @old_urls }.length).to be(2)
16+
expect(update_organogram_filenames.instance_eval { @new_urls }.length).to be(2)
17+
end
18+
19+
it "should abort and return a message if it's empty" do
20+
File.write("sample_urls.csv", "")
21+
update_organogram_filenames = UpdateOrganogramFilenames.new
22+
update_organogram_filenames.parse_csv("sample_urls.csv")
23+
expect(update_organogram_filenames.replace_urls).to eql("No urls to process")
24+
end
25+
end
26+
27+
context "when a url containing '-posts-' is found" do
28+
it "replaces the url with the correct url" do
29+
update_organogram_filenames = UpdateOrganogramFilenames.new
30+
update_organogram_filenames.parse_csv("sample_urls.csv")
31+
32+
update_organogram_filenames.replace_urls
33+
34+
expect(Link.all[0].url).to eql("https://amazonaws.com/datagovuk/dataset/resources/2019-06-06T11-18-26Z-organogram-senior.csv")
35+
expect(Link.all[1].url).to eql("https://amazonaws.com/datagovuk/dataset/resources/2019-06-06T11-18-26Z-organogram-junior.csv")
36+
end
37+
end
38+
end

0 commit comments

Comments
 (0)