|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require "test_helper" |
| 4 | + |
| 5 | +module Dial |
| 6 | + class TestProsopiteIntegration < ActiveSupport::TestCase |
| 7 | + def setup |
| 8 | + ::Prosopite.instance_variable_set :@custom_logger, false |
| 9 | + ::Prosopite.instance_variable_set :@dial_logger, nil |
| 10 | + ::Prosopite.instance_variable_set :@original_logger, nil |
| 11 | + end |
| 12 | + |
| 13 | + def teardown |
| 14 | + ::Prosopite.instance_variable_set :@custom_logger, false |
| 15 | + ::Prosopite.instance_variable_set :@dial_logger, nil |
| 16 | + ::Prosopite.instance_variable_set :@original_logger, nil |
| 17 | + end |
| 18 | + |
| 19 | + def test_dial_logger_sets_custom_logger_when_none_exists |
| 20 | + test_logger = ProsopiteLogger.new |
| 21 | + |
| 22 | + ::Prosopite.dial_logger = test_logger |
| 23 | + |
| 24 | + assert_equal test_logger, ::Prosopite.instance_variable_get(:@custom_logger) |
| 25 | + end |
| 26 | + |
| 27 | + def test_dial_logger_preserves_existing_custom_logger |
| 28 | + existing_logger = Logger.new StringIO.new |
| 29 | + ::Prosopite.custom_logger = existing_logger |
| 30 | + |
| 31 | + dial_logger = ProsopiteLogger.new |
| 32 | + ::Prosopite.dial_logger = dial_logger |
| 33 | + |
| 34 | + assert_instance_of ProsopiteCompositeLogger, ::Prosopite.instance_variable_get(:@custom_logger) |
| 35 | + end |
| 36 | + |
| 37 | + def test_setting_custom_logger_after_dial_logger_creates_composite |
| 38 | + dial_logger = ProsopiteLogger.new |
| 39 | + ::Prosopite.dial_logger = dial_logger |
| 40 | + |
| 41 | + existing_logger = Logger.new StringIO.new |
| 42 | + ::Prosopite.custom_logger = existing_logger |
| 43 | + |
| 44 | + assert_instance_of ProsopiteCompositeLogger, ::Prosopite.instance_variable_get(:@custom_logger) |
| 45 | + end |
| 46 | + |
| 47 | + def test_setting_dial_logger_twice_updates_composite |
| 48 | + existing_logger = Logger.new StringIO.new |
| 49 | + ::Prosopite.custom_logger = existing_logger |
| 50 | + |
| 51 | + dial_logger1 = ProsopiteLogger.new |
| 52 | + ::Prosopite.dial_logger = dial_logger1 |
| 53 | + |
| 54 | + first_composite = ::Prosopite.instance_variable_get(:@custom_logger) |
| 55 | + assert_instance_of ProsopiteCompositeLogger, first_composite |
| 56 | + |
| 57 | + dial_logger2 = ProsopiteLogger.new |
| 58 | + ::Prosopite.dial_logger = dial_logger2 |
| 59 | + |
| 60 | + second_composite = ::Prosopite.instance_variable_get(:@custom_logger) |
| 61 | + assert_instance_of ProsopiteCompositeLogger, second_composite |
| 62 | + |
| 63 | + refute_same first_composite, second_composite |
| 64 | + end |
| 65 | + |
| 66 | + def test_composite_logger_receives_notifications |
| 67 | + existing_io = StringIO.new |
| 68 | + existing_logger = Logger.new existing_io |
| 69 | + ::Prosopite.custom_logger = existing_logger |
| 70 | + |
| 71 | + dial_logger = ProsopiteLogger.new |
| 72 | + ::Prosopite.dial_logger = dial_logger |
| 73 | + |
| 74 | + Thread.current[:prosopite_notifications] = { |
| 75 | + ["SELECT * FROM users"] => ["app/controllers/users_controller.rb:10"] |
| 76 | + } |
| 77 | + |
| 78 | + ::Prosopite.send :send_notifications |
| 79 | + |
| 80 | + assert_includes existing_io.string, "N+1 queries detected" |
| 81 | + assert_includes ProsopiteLogger.log_io.string, "N+1 queries detected" |
| 82 | + end |
| 83 | + |
| 84 | + def test_setting_same_logger_twice_does_not_create_nested_composite |
| 85 | + existing_logger = Logger.new StringIO.new |
| 86 | + ::Prosopite.custom_logger = existing_logger |
| 87 | + |
| 88 | + dial_logger = ProsopiteLogger.new |
| 89 | + ::Prosopite.dial_logger = dial_logger |
| 90 | + |
| 91 | + ::Prosopite.custom_logger = existing_logger |
| 92 | + |
| 93 | + custom_logger = ::Prosopite.instance_variable_get(:@custom_logger) |
| 94 | + assert_instance_of ProsopiteCompositeLogger, custom_logger |
| 95 | + |
| 96 | + dial_internal = custom_logger.instance_variable_get(:@dial_logger) |
| 97 | + existing_internal = custom_logger.instance_variable_get(:@existing_logger) |
| 98 | + |
| 99 | + assert_equal dial_logger, dial_internal |
| 100 | + assert_equal existing_logger, existing_internal |
| 101 | + end |
| 102 | + |
| 103 | + def test_setting_custom_logger_to_nil_removes_existing_logger |
| 104 | + existing_logger = Logger.new StringIO.new |
| 105 | + ::Prosopite.custom_logger = existing_logger |
| 106 | + |
| 107 | + dial_logger = ProsopiteLogger.new |
| 108 | + ::Prosopite.dial_logger = dial_logger |
| 109 | + |
| 110 | + ::Prosopite.custom_logger = nil |
| 111 | + |
| 112 | + assert_nil ::Prosopite.instance_variable_get(:@custom_logger) |
| 113 | + end |
| 114 | + end |
| 115 | +end |
0 commit comments