Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix specialist_sector_cleanup task. #2270

Merged
merged 1 commit into from
Jul 14, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions lib/data_hygiene/specialist_sector_cleanup.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,23 @@ def any_taggings?
end

def any_published_taggings?
taggings.map(&:edition).any? do |edition|
taggings.map(&:edition).compact.any? do |edition|
edition.document.ever_published_editions.any?
end
end

def remove_taggings(add_note: true)
taggings.each do |tagging|
edition = tagging.edition

puts "Removing tagging to edition ##{edition.id}"
if edition
puts "Removing tagging to edition ##{edition.id}"
else
puts "Removing orphaned tagging to edition_id ##{tagging.edition_id}"
end

tagging.destroy

if add_note
if add_note && edition
puts "Adding an editorial note from the GDS user"

gds_user = User.find_by(email: "[email protected]")
Expand Down
22 changes: 22 additions & 0 deletions test/unit/data_hygiene/specialist_sector_cleanup_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,17 @@ class SpecialistSectorCleanupTest < ActiveSupport::TestCase
assert cleanup.any_published_taggings?
end

test "#any_published_taggings? handles orphaned taggings" do
cleanup = SpecialistSectorCleanup.new('oil-and-gas/offshore')

tagging = create(:specialist_sector, tag: 'oil-and-gas/offshore', edition: @draft_edition)
tagging.update_column(:edition_id, 12345)
refute cleanup.any_published_taggings?

create(:specialist_sector, tag: 'oil-and-gas/offshore', edition: @published_edition)
assert cleanup.any_published_taggings?
end

test "#remove_taggings(add_note: false) removes the taggings without adding notes" do
create(:specialist_sector, tag: 'oil-and-gas/offshore', edition: @draft_edition)
create(:specialist_sector, tag: 'oil-and-gas/offshore', edition: @published_edition)
Expand All @@ -39,6 +50,17 @@ class SpecialistSectorCleanupTest < ActiveSupport::TestCase
assert_equal 0, EditorialRemark.count
end

test "#remove_taggings handles orphaned taggings" do
tagging = create(:specialist_sector, tag: 'oil-and-gas/offshore', edition: @draft_edition)
tagging.update_column(:edition_id, 12345)
create(:specialist_sector, tag: 'oil-and-gas/offshore', edition: @published_edition)

SpecialistSectorCleanup.new('oil-and-gas/offshore').remove_taggings

assert_equal 0, SpecialistSector.count
#assert_equal 0, EditorialRemark.count
end

test "#remove_taggings(add_note: true) removes the taggings and adds notes" do
create(:specialist_sector, tag: 'oil-and-gas/offshore', edition: @draft_edition)
create(:specialist_sector, tag: 'oil-and-gas/offshore', edition: @published_edition)
Expand Down