Skip to content

A platform for community discussion. Free, open, simple.

DEV: Remove admin notices when destroying problem check trackers (#35976)

Ted Johansson8ca5fb706a0e
diff --git a/app/models/problem_check_tracker.rb b/app/models/problem_check_tracker.rb
index 1345f18dc1f..38da7379c49 100644
--- a/app/models/problem_check_tracker.rb
+++ b/app/models/problem_check_tracker.rb
@@ -2,11 +2,14 @@
class ProblemCheckTracker < ActiveRecord::Base
validates :identifier, presence: true, uniqueness: { scope: :target }
+ validates :target, presence: true
validates :blips, presence: true, numericality: { greater_than_or_equal_to: 0 }
scope :failing, -> { where("last_problem_at = last_run_at") }
scope :passing, -> { where("last_success_at = last_run_at") }
+ before_destroy :silence_the_alarm
+
def self.[](identifier, target = ProblemCheck::NO_TARGET)
find_or_create_by(identifier:, target:)
end
@@ -57,16 +60,8 @@ class ProblemCheckTracker < ActiveRecord::Base
private
- def resolved_target
- self.target || ProblemCheck::NO_TARGET
- end
-
- def details_with_target(details)
- details.merge(target: resolved_target)
- end
-
def update_notice_details(details)
- admin_notice.where(identifier:).update_all(details: details_with_target(details))
+ admin_notice.where(identifier:).update_all(details: details.merge(target:))
end
def sound_the_alarm?
@@ -76,7 +71,7 @@ class ProblemCheckTracker < ActiveRecord::Base
def sound_the_alarm
admin_notice.create_with(
priority: check.priority,
- details: details_with_target(self.details),
+ details: details.merge(target:),
).find_or_create_by(identifier:)
end
@@ -85,7 +80,7 @@ class ProblemCheckTracker < ActiveRecord::Base
end
def admin_notice
- AdminNotice.problem.where("details->>'target' = ?", resolved_target)
+ AdminNotice.problem.where("details->>'target' = ?", target)
end
end
diff --git a/spec/models/problem_check_tracker_spec.rb b/spec/models/problem_check_tracker_spec.rb
index f49721afbde..17a376acea5 100644
--- a/spec/models/problem_check_tracker_spec.rb
+++ b/spec/models/problem_check_tracker_spec.rb
@@ -8,6 +8,22 @@ RSpec.describe ProblemCheckTracker do
it { expect(record).to validate_uniqueness_of(:identifier).scoped_to(:target) }
it { expect(record).to validate_numericality_of(:blips).is_greater_than_or_equal_to(0) }
+
+ it { expect(record).to validate_presence_of(:target) }
+ end
+
+ describe "callbacks" do
+ describe "before_destroy (silence the alarm)" do
+ let(:tracker) do
+ ProblemCheckTracker.create!(identifier: "twitter_login", target: ProblemCheck::NO_TARGET)
+ end
+
+ before { tracker.problem! }
+
+ it "removes any associated admin notices" do
+ expect { tracker.destroy }.to change { AdminNotice.count }.by(-1)
+ end
+ end
end
describe ".[]" do
diff --git a/spec/services/problem_check_spec.rb b/spec/services/problem_check_spec.rb
index 1de255afc45..e9425c3c158 100644
--- a/spec/services/problem_check_spec.rb
+++ b/spec/services/problem_check_spec.rb
@@ -144,16 +144,24 @@ RSpec.describe ProblemCheck do
it { expect { passing_check.new.run }.to change { ProblemCheckTracker.passing.count }.by(1) }
end
- context "when targeted check has a no-target tracker" do
- before do
- ProblemCheckTracker.create!(
- identifier: "multi_target_check",
- target: ProblemCheck::NO_TARGET,
- )
+ context "when targeted check is initialized with no target" do
+ context "when a tracker exists" do
+ before do
+ ProblemCheckTracker.create!(
+ identifier: "multi_target_check",
+ target: ProblemCheck::NO_TARGET,
+ )
+ end
+
+ it "deletes the tracker" do
+ expect { multi_target_check.new.run }.to change { ProblemCheckTracker.count }.by(-1)
+ end
end
- it "deletes the tracker" do
- expect { multi_target_check.new.run }.to change { ProblemCheckTracker.count }.by(-1)
+ context "when a tracker does not exist" do
+ it "does nothing" do
+ expect { multi_target_check.new.run }.not_to change { ProblemCheckTracker.count }
+ end
end
end