Skip to content

Commit

Permalink
Updated tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ellohez committed Jan 20, 2025
1 parent 372cf10 commit cfc870b
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 378 deletions.
106 changes: 53 additions & 53 deletions lib/tasks/world_taxon_update.rake
Original file line number Diff line number Diff line change
Expand Up @@ -8,59 +8,6 @@
WORLD_ROOT_CONTENT_ID = "91b8ef20-74e7-4552-880c-50e6d73c2ff9".freeze

namespace :worldwide do
class CountryNameAdder
def add_country_names(log_file_path)
log_file = nil
if log_file_path
log_file = File.open(log_file_path, "w")
log_rake_progress(log_file, "Updating each worldwide taxon to include country name in their title")
end

total_taxon_updates = 0

# Build a taxonomy tree with the grandparent
# (common ancestor e.g. /world/all - Help and services around the world) as the root
taxonomy = Taxonomy::ExpandedTaxonomy.new(WORLD_ROOT_CONTENT_ID).build.child_expansion
log_rake_progress(log_file, "Taxonomy has size #{taxonomy.tree.size}")

taxonomy.tree.each do |linked_item|
# As this is a tree, we reach all grandchildren without another loop (not a nested array)
# example grandchild url /world/passports-and-emergency-travel-documents-cape-verde

next if skip_tree_item?(log_file, linked_item)

message, new_title = create_new_taxon_title(linked_item.internal_name)
log_rake_progress(log_file, message)

# Fetch the taxon and update accordingly
if new_title
new_taxon = Taxonomy::BuildTaxon.call(content_id: linked_item.content_id)
new_taxon.title = new_title

# Save the taxon with the new title
Taxonomy::UpdateTaxon.call(taxon: new_taxon)
message = "Updated taxon #{linked_item.title} to #{new_title}"
end
log_rake_progress(log_file, message)
total_taxon_updates += 1
rescue Taxonomy::UpdateTaxon::InvalidTaxonError => e
log_rake_error(log_file, "An error occurred while processing taxon #{linked_item.internal_name}: #{e.message}")
end

# Need to publish all the drafts we have created above (if latest edition is published)
# - Draft editions are updated straight away.
puts("Publishing all updated taxons")
Taxonomy::BulkPublishTaxon.call(WORLD_ROOT_CONTENT_ID)
log_rake_progress(log_file, "Total number of taxons updated - #{total_taxon_updates}")
rescue GdsApi::HTTPConflict, GdsApi::HTTPGatewayTimeout, GdsApi::TimedOutException => e
log_rake_error(log_file, "An error occurred while publishing taxons: #{e.full_message}")
rescue StandardError => e
log_rake_error(log_file, "An error occurred while publishing taxons: #{e.full_message}")
ensure
log_file&.close
end
end

desc "Update worldwide taxon titles (external name) to include the name of the country they relate to"

task :add_country_name_to_title, %i[log_file_path] => :environment do |_, args|
Expand Down Expand Up @@ -118,6 +65,59 @@ end

private

class CountryNameAdder
def add_country_names(log_file_path)
log_file = nil
if log_file_path
log_file = File.open(log_file_path, "w")
log_rake_progress(log_file, "Updating each worldwide taxon to include country name in their title")
end

total_taxon_updates = 0

# Build a taxonomy tree with the grandparent
# (common ancestor e.g. /world/all - Help and services around the world) as the root
taxonomy = Taxonomy::ExpandedTaxonomy.new(WORLD_ROOT_CONTENT_ID).build.child_expansion
log_rake_progress(log_file, "Taxonomy has size #{taxonomy.tree.size}")

taxonomy.tree.each do |linked_item|
# As this is a tree, we reach all grandchildren without another loop (not a nested array)
# example grandchild url /world/passports-and-emergency-travel-documents-cape-verde

next if skip_tree_item?(log_file, linked_item)

message, new_title = create_new_taxon_title(linked_item.internal_name)
log_rake_progress(log_file, message)

# Fetch the taxon and update accordingly
if new_title
new_taxon = Taxonomy::BuildTaxon.call(content_id: linked_item.content_id)
new_taxon.title = new_title

# Save the taxon with the new title
Taxonomy::UpdateTaxon.call(taxon: new_taxon)
message = "Updated taxon #{linked_item.title} to #{new_title}"
end
log_rake_progress(log_file, message)
total_taxon_updates += 1
rescue Taxonomy::UpdateTaxon::InvalidTaxonError => e
log_rake_error(log_file, "An error occurred while processing taxon #{linked_item.internal_name}: #{e.message}")
end

# Need to publish all the drafts we have created above (if latest edition is published)
# - Draft editions are updated straight away.
puts("Publishing all updated taxons")
Taxonomy::BulkPublishTaxon.call(WORLD_ROOT_CONTENT_ID)
log_rake_progress(log_file, "Total number of taxons updated - #{total_taxon_updates}")
rescue GdsApi::HTTPConflict, GdsApi::HTTPGatewayTimeout, GdsApi::TimedOutException => e
log_rake_error(log_file, "An error occurred while publishing taxons: #{e.full_message}")
rescue StandardError => e
log_rake_error(log_file, "An error occurred while publishing taxons: #{e.full_message}")
ensure
log_file&.close
end
end

def log_rake_progress(log_file, message)
log_file&.puts(message)
puts(message)
Expand Down
10 changes: 0 additions & 10 deletions spec/factories/linked_content_item.rb

This file was deleted.

97 changes: 32 additions & 65 deletions spec/lib/tasks/country_name_adder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,7 @@

RSpec.describe CountryNameAdder do
# TODO: Do we need to set ALL of these properties??
# linked_item 1 - 4 are only used in test for skip_tree_item
let(:linked_item_root) do
Taxonomy::LinkedContentItem.new(
base_path: "/test/",
content_id: WORLD_ROOT_CONTENT_ID,
title: "UK things in other countries",
internal_name: "UK things in other countries",
)
end
# First 3 linked_items are used in tests for skip_tree_item
let(:linked_item_living_in) do
Taxonomy::LinkedContentItem.new(
base_path: "/test/2",
Expand All @@ -35,7 +27,14 @@
internal_name: "Tax, benefits, pensions and working abroad (Betelgeuse)",
)
end

let(:linked_item_travelling_to) do
Taxonomy::LinkedContentItem.new(
base_path: "/root/1",

Check failure on line 32 in spec/lib/tasks/country_name_adder_spec.rb

View workflow job for this annotation

GitHub Actions / Lint Ruby / Run RuboCop

Layout/FirstArgumentIndentation: Indent the first argument one step more than the start of the previous line.
content_id: "id10",
title: "Some title",
internal_name: "Travelling to Porpoise Spit",
)

Check failure on line 36 in spec/lib/tasks/country_name_adder_spec.rb

View workflow job for this annotation

GitHub Actions / Lint Ruby / Run RuboCop

Layout/ClosingParenthesisIndentation: Indent `)` to column 2 (not 4)
end
let(:root_item) do
Taxonomy::LinkedContentItem.new(
base_path: "/root",
Expand All @@ -44,7 +43,7 @@
internal_name: "Stuff around the world",
)
end
let(:parent_item) do
let(:parent_item_uk_help) do
Taxonomy::LinkedContentItem.new(
base_path: "/root/taxon_a",
content_id: "id2",
Expand Down Expand Up @@ -77,8 +76,8 @@
)
end
let(:multi_level_taxons) do
root_item << parent_item
parent_item << child_item_coming_to << child_item_trade_invest << child_item_birth_death
root_item << parent_item_uk_help
parent_item_uk_help << child_item_coming_to << child_item_trade_invest << child_item_birth_death

root_item
end
Expand Down Expand Up @@ -108,7 +107,7 @@ def allow_item_double(item)
allow_item_double(multi_level_taxons)
allow(Taxonomy::BuildTaxon).to receive(:call).and_raise(Taxonomy::UpdateTaxon::InvalidTaxonError.new("Invalid taxon"))

expect { CountryNameAdder.new.add_country_names("tmp/rake_log") }.to output(/An error occurred while processing taxon/).to_stderr
expect { described_class.new.add_country_names("tmp/rake_log") }.to output(/An error occurred while processing taxon/).to_stderr
end

it "logs an error to stderr if BulkPublishTaxon fails" do
Expand All @@ -117,14 +116,14 @@ def allow_item_double(item)
allow(Taxonomy::UpdateTaxon).to receive(:call)
allow(Taxonomy::BulkPublishTaxon).to receive(:call).and_raise(GdsApi::HTTPConflict.new("Conflict error"))

expect { CountryNameAdder.new.add_country_names("tmp/rake_log") }.to output(/An error occurred while publishing taxons/).to_stderr
expect { described_class.new.add_country_names("tmp/rake_log") }.to output(/An error occurred while publishing taxons/).to_stderr
end

it "logs an error to stderr if an unexpected error is raised" do
allow_item_double(multi_level_taxons)
allow(Taxonomy::ExpandedTaxonomy).to receive(:new).and_raise(StandardError.new("An unexpected error occurred"))

expect { CountryNameAdder.new.add_country_names("tmp/rake_log") }.to output(/An unexpected error occurred/).to_stderr
expect { described_class.new.add_country_names("tmp/rake_log") }.to output(/An unexpected error occurred/).to_stderr
end

it "updates the taxon title correctly" do
Expand All @@ -135,7 +134,7 @@ def allow_item_double(item)
allow(Taxonomy::BulkPublishTaxon).to receive(:call)
allow(Taxonomy::UpdateTaxon).to receive(:call)

CountryNameAdder.new.add_country_names("tmp/rake_log")
described_class.new.add_country_names("tmp/rake_log")

# Tests all three title change possibilities
expect(Taxonomy::UpdateTaxon).to have_received(:call).with(taxon: having_attributes(content_id: child_taxon_coming_to.content_id, title: "Coming to the UK from Porpoise Spit"))
Expand All @@ -144,84 +143,52 @@ def allow_item_double(item)
end

it "skips the root taxon which has the WORLD_ROOT_CONTENT_ID" do
allow_item_double(multi_level_taxons)
# TODO: Replace call to LinkedContentItem constructor with a factory? - only need content_id?
# TODO: This doesn't work: item = FactoryBot.build(:linked_content_item, content_id: WORLD_ROOT_CONTENT_ID)
item = Taxonomy::LinkedContentItem.new(
base_path: "/root",
content_id: WORLD_ROOT_CONTENT_ID,
title: "Some title",
internal_name: "Some name",
)
allow_item_double(item)
allow_item_double(root_item)
expect(Taxonomy::BuildTaxon).not_to receive(:call)
expect(Taxonomy::UpdateTaxon).not_to receive(:call)
# TODO: Do we also need to test that create_new_taxon_title is not called?
expect(described_class).not_to receive(:create_new_taxon_title).with(root_item.internal_name)

expect { CountryNameAdder.new.add_country_names("tmp/rake_log") }.to output(/Skipping world root taxon/).to_stdout
expect { described_class.new.add_country_names("tmp/rake_log") }.to output(/Skipping world root taxon/).to_stdout
end

it "skips taxons with internal names which start with 'UK help and services '" do
allow_item_double(multi_level_taxons)
item = Taxonomy::LinkedContentItem.new(
base_path: "/root/1",
content_id: "id10",
title: "Some title",
internal_name: "UK help and services in Porpoise Spit",
)
allow_item_double(item)
allow_item_double(parent_item_uk_help)
expect(Taxonomy::BuildTaxon).not_to receive(:call)
expect(Taxonomy::UpdateTaxon).not_to receive(:call)
expect(described_class).not_to receive(:create_new_taxon_title).with(parent_item_uk_help.internal_name)

expect { CountryNameAdder.new.add_country_names("tmp/rake_log") }.to output(/Skipping UK help and services in Porpoise Spit/).to_stdout
expect { described_class.new.add_country_names("tmp/rake_log") }.to output(/Skipping UK help and services in Porpoise Spit/).to_stdout
end

it "skips taxons with internal names which start with 'Living in '" do
allow_item_double(multi_level_taxons)
item = Taxonomy::LinkedContentItem.new(
base_path: "/root/1",
content_id: "id10",
title: "Some title",
internal_name: "Living in Porpoise Spit",
)
allow_item_double(item)
allow_item_double(linked_item_living_in)
expect(Taxonomy::BuildTaxon).not_to receive(:call)
expect(Taxonomy::UpdateTaxon).not_to receive(:call)
expect(described_class).not_to receive(:create_new_taxon_title).with(linked_item_living_in.internal_name)

expect { CountryNameAdder.new.add_country_names("tmp/rake_log") }.to output(/Skipping Living in Porpoise Spit/).to_stdout
expect { described_class.new.add_country_names("tmp/rake_log") }.to output(/Skipping Living in Moe's Tavern/).to_stdout
end

it "skips taxons with internal names which start with 'Travelling to '" do
allow_item_double(multi_level_taxons)
item = Taxonomy::LinkedContentItem.new(
base_path: "/root/1",
content_id: "id10",
title: "Some title",
internal_name: "Travelling to Porpoise Spit",
)
allow_item_double(item)
allow_item_double(linked_item_travelling_to)
expect(Taxonomy::BuildTaxon).not_to receive(:call)
expect(Taxonomy::UpdateTaxon).not_to receive(:call)
expect(described_class).not_to receive(:create_new_taxon_title).with(linked_item_travelling_to.internal_name)

expect { CountryNameAdder.new.add_country_names("tmp/rake_log") }.to output(/Skipping Travelling to Porpoise Spit/).to_stdout
expect { described_class.new.add_country_names("tmp/rake_log") }.to output(/Skipping Travelling to Porpoise Spit/).to_stdout
end

it "skips taxons with internal names that include '(GENERIC)'" do
allow_item_double(multi_level_taxons)
item = Taxonomy::LinkedContentItem.new(
base_path: "/root/1",
content_id: "id10",
title: "Some title",
internal_name: "name (GENERIC)",
)
allow_item_double(item)
allow_item_double(linked_item_generic)
expect(Taxonomy::BuildTaxon).not_to receive(:call)
expect(Taxonomy::UpdateTaxon).not_to receive(:call)
expect(described_class).not_to receive(:create_new_taxon_title).with(linked_item_generic.internal_name)

expect { CountryNameAdder.new.add_country_names("tmp/rake_log") }.to output(/Skipping name \(GENERIC\)/).to_stdout
expect { described_class.new.add_country_names("tmp/rake_log") }.to output(/Skipping Doing a thing \(GENERIC\)/).to_stdout
end
end

describe ""
# # TODO: describe "worldwide:remove_country_name_from_title" do
# # TODO: test it "reverts the taxon title correctly" do
# # Set up the already processed taxons we will need to revert (e.g. titles including the COUNTRY_NAME) in a describe before block if necessary
Expand Down
Loading

0 comments on commit cfc870b

Please sign in to comment.