|  | 
|  | 1 | +# Licensed to Elasticsearch B.V. under one or more contributor | 
|  | 2 | +# license agreements. See the NOTICE file distributed with | 
|  | 3 | +# this work for additional information regarding copyright | 
|  | 4 | +# ownership. Elasticsearch B.V. licenses this file to you under | 
|  | 5 | +# the Apache License, Version 2.0 (the "License"); you may | 
|  | 6 | +# not use this file except in compliance with the License. | 
|  | 7 | +# You may obtain a copy of the License at | 
|  | 8 | +# | 
|  | 9 | +#   http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 10 | +# | 
|  | 11 | +# Unless required by applicable law or agreed to in writing, | 
|  | 12 | +# software distributed under the License is distributed on an | 
|  | 13 | +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | 
|  | 14 | +# KIND, either express or implied.  See the License for the | 
|  | 15 | +# specific language governing permissions and limitations | 
|  | 16 | +# under the License. | 
|  | 17 | + | 
|  | 18 | +require 'spec_helper' | 
|  | 19 | +require 'elasticsearch/rails/instrumentation/log_subscriber' | 
|  | 20 | + | 
|  | 21 | +describe Elasticsearch::Rails::Instrumentation::LogSubscriber do | 
|  | 22 | +  subject(:instance) { described_class.new } | 
|  | 23 | + | 
|  | 24 | +  let(:logger) { instance_double(Logger) } | 
|  | 25 | + | 
|  | 26 | +  before do | 
|  | 27 | +    allow(instance).to receive(:logger) { logger } | 
|  | 28 | +  end | 
|  | 29 | + | 
|  | 30 | +  describe "#search" do | 
|  | 31 | +    subject { instance.search(event) } | 
|  | 32 | + | 
|  | 33 | +    let(:event) { double("search.elasticsearch", duration: 1.2345, payload: { name: "execute", search: { query: { match_all: {}}}}) } | 
|  | 34 | + | 
|  | 35 | +    it "logs the event" do | 
|  | 36 | +      expect(instance).to receive(:color).with(" execute (1.2ms)", described_class::GREEN, { bold: true }).and_call_original | 
|  | 37 | +      expect(logger).to receive(:debug?) { true } | 
|  | 38 | +      expect(logger).to receive(:debug).with("  \e[1m\e[32m execute (1.2ms)\e[0m \e[2m{query: {match_all: {}}}\e[0m") | 
|  | 39 | +      subject | 
|  | 40 | +    end | 
|  | 41 | + | 
|  | 42 | +    context "when Rails version is older" do | 
|  | 43 | +      let(:rails_version) { "7.0.0" } | 
|  | 44 | + | 
|  | 45 | +      before do | 
|  | 46 | +        allow(::Rails).to receive(:gem_version) { Gem::Version.new(rails_version) } | 
|  | 47 | +      end | 
|  | 48 | + | 
|  | 49 | +      it "logs the event" do | 
|  | 50 | +        expect(instance).to receive(:color).with(" execute (1.2ms)", described_class::GREEN, true).and_call_original | 
|  | 51 | +        expect(logger).to receive(:debug?) { true } | 
|  | 52 | +        expect(logger).to receive(:debug).with("  \e[1m\e[32m execute (1.2ms)\e[0m \e[2m{query: {match_all: {}}}\e[0m") | 
|  | 53 | +        subject | 
|  | 54 | +      end | 
|  | 55 | +    end | 
|  | 56 | +  end | 
|  | 57 | +end | 
0 commit comments