|
| 1 | +require "test_helper" |
| 2 | + |
| 3 | +class SolidErrors::CleanerTest < ActiveSupport::TestCase |
| 4 | + setup do |
| 5 | + assert_nil SolidErrors.destroy_after |
| 6 | + end |
| 7 | + |
| 8 | + test "not destroy if destroy_after is not set" do |
| 9 | + simulate_99_old_exceptions(:resolved) |
| 10 | + previous_error = SolidErrors::Error.last |
| 11 | + previous_occurrence = SolidErrors::Occurrence.last |
| 12 | + Rails.error.report(dummy_exception) |
| 13 | + |
| 14 | + assert SolidErrors::Error.exists?(id: previous_error.id) |
| 15 | + assert SolidErrors::Occurrence.exists?(id: previous_occurrence.id) |
| 16 | + end |
| 17 | + |
| 18 | + test "destroy old occurrences every 100 insertions if destroy_after is set" do |
| 19 | + set_destroy_after |
| 20 | + simulate_99_old_exceptions(:resolved) |
| 21 | + Rails.error.report(dummy_exception) |
| 22 | + |
| 23 | + assert_equal 1, SolidErrors::Error.count |
| 24 | + assert_equal 1, SolidErrors::Occurrence.count |
| 25 | + end |
| 26 | + |
| 27 | + test "not destroy if errors are unresolved" do |
| 28 | + set_destroy_after |
| 29 | + simulate_99_old_exceptions(:unresolved) |
| 30 | + |
| 31 | + assert_difference -> { SolidErrors::Error.count }, +1 do |
| 32 | + assert_difference -> { SolidErrors::Occurrence.count }, +1 do |
| 33 | + Rails.error.report(dummy_exception) |
| 34 | + end |
| 35 | + end |
| 36 | + end |
| 37 | + |
| 38 | + private |
| 39 | + |
| 40 | + def simulate_99_old_exceptions(status) |
| 41 | + Rails.error.report(dummy_exception("argh")) |
| 42 | + SolidErrors::Error.update_all(resolved_at: Time.current) if status == :resolved |
| 43 | + SolidErrors::Occurrence.last.update!(id: 99, created_at: 1.day.ago) |
| 44 | + end |
| 45 | + |
| 46 | + def set_destroy_after |
| 47 | + SolidErrors.stubs(destroy_after: 1.day) |
| 48 | + end |
| 49 | + |
| 50 | + def dummy_exception(message = "oof") |
| 51 | + exception = StandardError.new(message) |
| 52 | + exception.set_backtrace(caller) |
| 53 | + exception |
| 54 | + end |
| 55 | +end |
0 commit comments