<commit_before>require_relative "test_helper"

class LogSubscriberTest < Minitest::Test
  def test_create
    output = capture_logs do
      Product.create!(name: "Product A")
    end
    assert_match "Product Store", output
  end

  def test_update
    product = Product.create!(name: "Product A")
    output = capture_logs do
      product.update!(name: "Product B")
    end
    assert_match "Product Store", output
  end

  def test_destroy
    product = Product.create!(name: "Product A")
    output = capture_logs do
      product.destroy
    end
    assert_match "Product Remove", output
  end

  def test_bulk
    output = capture_logs do
      Searchkick.callbacks(:bulk) do
        Product.create!(name: "Product A")
      end
    end
    assert_match "Bulk", output
    refute_match "Product Store", output
  end

  def test_reindex
    Product.create!(name: "Product A")
    output = capture_logs do
      Product.reindex
    end
    assert_match "Product Import", output
    assert_match '"count":1', output
  end

  def capture_logs
    previous_logger = ActiveSupport::LogSubscriber.logger
    io = StringIO.new
    begin
      ActiveSupport::LogSubscriber.logger = ActiveSupport::Logger.new(io)
      yield
      io.rewind
      output = io.read
      previous_logger.debug(output) if previous_logger
      puts output if ENV["LOG_SUBSCRIBER"]
      output
    ensure
      ActiveSupport::LogSubscriber.logger = previous_logger
    end
  end
end
<commit_msg>Added log subscriber test for relation reindex [skip ci]<commit_after>require_relative "test_helper"

class LogSubscriberTest < Minitest::Test
  def test_create
    output = capture_logs do
      Product.create!(name: "Product A")
    end
    assert_match "Product Store", output
  end

  def test_update
    product = Product.create!(name: "Product A")
    output = capture_logs do
      product.update!(name: "Product B")
    end
    assert_match "Product Store", output
  end

  def test_destroy
    product = Product.create!(name: "Product A")
    output = capture_logs do
      product.destroy
    end
    assert_match "Product Remove", output
  end

  def test_bulk
    output = capture_logs do
      Searchkick.callbacks(:bulk) do
        Product.create!(name: "Product A")
      end
    end
    assert_match "Bulk", output
    refute_match "Product Store", output
  end

  def test_reindex
    create_products
    output = capture_logs do
      Product.reindex
    end
    assert_match "Product Import", output
    assert_match '"count":3', output
  end

  def test_reindex_relation
    products = create_products
    output = capture_logs do
      Product.where.not(id: products.last.id).reindex
    end
    assert_match "Product Import", output
    assert_match '"count":2', output
  end

  def create_products
    Searchkick.callbacks(false) do
      3.times.map do
        Product.create!(name: "Product A")
      end
    end
  end

  def capture_logs
    previous_logger = ActiveSupport::LogSubscriber.logger
    io = StringIO.new
    begin
      ActiveSupport::LogSubscriber.logger = ActiveSupport::Logger.new(io)
      yield
      io.rewind
      output = io.read
      previous_logger.debug(output) if previous_logger
      puts output if ENV["LOG_SUBSCRIBER"]
      output
    ensure
      ActiveSupport::LogSubscriber.logger = previous_logger
    end
  end
end

{'fraction_non_alphanumeric': 0.07696897374701671, 'fraction_numerical': 0.0011933174224343676, 'mean_word_length': 2.9400705052878964, 'pattern_counts': {'":': 3, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '93809', 'n_tokens_mistral': 997, 'n_tokens_neox': 982, 'n_words': 272}