diff --git a/lib/tasks/election.rake b/lib/tasks/election.rake index 590c09084aba..5138fc5d63a4 100644 --- a/lib/tasks/election.rake +++ b/lib/tasks/election.rake @@ -3,12 +3,14 @@ namespace :election do task remove_mp_letters: :environment do puts "Removing MP from MP's letters:" Person.where('letters LIKE "%MP%"').find_each do |person| - puts "updating #{person.name}" - new_letters = person.letters.gsub(/(^|\s)MP(\s|$)/, "") - if person.letters != new_letters + new_letters = person.letters.split(" ").reject { _1 == "MP" }.join(" ") + if person.letters == new_letters + puts "skipped #{person.name} - includes MP (case insensitive), but doesn't match exactly" + else + old_name = person.name person.update!(letters: new_letters) + puts "updated #{old_name} to #{person.name}" end - puts "changed to #{person.name}" end end diff --git a/test/unit/lib/tasks/election_remove_mp_letters_test.rb b/test/unit/lib/tasks/election_remove_mp_letters_test.rb new file mode 100644 index 000000000000..9e53af0eaef9 --- /dev/null +++ b/test/unit/lib/tasks/election_remove_mp_letters_test.rb @@ -0,0 +1,34 @@ +require "test_helper" +require "rake" + +class RemoveMpLettersRake < ActiveSupport::TestCase + extend Minitest::Spec::DSL + + teardown { task.reenable } + + describe "#remove_mp_letters" do + let(:task) { Rake::Task["election:remove_mp_letters"] } + + it "removes the letters MP from people names" do + member_a = create(:person, forename: "A", surname: "Member", letters: "MP") + member_b = create(:person, forename: "B", surname: "Member", letters: "CBE MP") + member_c = create(:person, forename: "C", surname: "Member", letters: "MP MEng") + member_d = create(:person, forename: "D", surname: "Member", letters: "CBE MP MPhil") + non_member_a = create(:person, forename: "A", surname: "Non-Member", letters: "CBE MPhil VC") + + out, _err = capture_io { task.invoke } + + assert_match /updated A Member MP to A Member/, out + assert_match /updated B Member CBE MP to B Member CBE/, out + assert_match /updated C Member MP MEng to C Member MEng/, out + assert_match /updated D Member CBE MP MPhil to D Member CBE MPhil/, out + assert_match /skipped A Non-Member/, out + + assert_equal "A Member", member_a.reload.name + assert_equal "B Member CBE", member_b.reload.name + assert_equal "C Member MEng", member_c.reload.name + assert_equal "D Member CBE MPhil", member_d.reload.name + assert_equal "A Non-Member CBE MPhil VC", non_member_a.reload.name + end + end +end