Sha256: e9fa3d17e68ecf2216c2eae494eb9975fba7d3dda32a118668db79ada058dd8c

Contents?: true

Size: 1.28 KB

Versions: 1

Compression:

Stored size: 1.28 KB

Contents

# frozen_string_literal: true

RSpec.describe Dry::Monitor::SQL::Logger do
  subject(:logger) { sql_logger.new(Dry::Monitor::Logger.new(log_file_path)) }

  let(:notifications) do
    Dry::Monitor::Notifications.new(:test)
  end

  let(:log_file_path) do
    SPEC_ROOT.join('test_logs/sql.log')
  end

  let(:log_file_content) { File.read(log_file_path) }

  shared_context '#subscribe' do
    let(:query) do
      'SELECT id, name FROM users'
    end

    before do
      File.open(log_file_path, 'w').close

      logger.subscribe(notifications)

      notifications.instrument(:sql, name: 'users', query: query) do
        sleep 0.0025
      end
    end

    it 'writes sql query info' do
      expect(log_file_content).to include('Loaded "users" in')
    end
  end

  context 'without colors' do
    let(:sql_logger) do
      Dry::Monitor::SQL::Logger.load_extensions(:rouge_colorizer)
      Dry::Monitor::SQL::Logger
    end

    include_context '#subscribe' do
      let(:colored_query) do
        "\e[38;5;203mSELECT\e[39m\e[38;5;230m"
      end

      it 'writes sql query in logs' do
        expect(log_file_content).to include(colored_query)
      end
    end
  end

  context 'without colors' do
    let(:sql_logger) do
      Dry::Monitor::SQL::Logger
    end

    include_context '#subscribe'
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
dry-monitor-0.3.1 spec/integration/sql_logger_spec.rb