From 74f2b7e43e2cc75cb0ba52fe685cc47518d74c9f Mon Sep 17 00:00:00 2001 From: Richard Huang Date: Sat, 31 Dec 2022 20:26:50 +0800 Subject: [PATCH] test caller_in_project in stack_trace_filter_spec --- spec/bullet/detector/n_plus_one_query_spec.rb | 32 ------------------- spec/bullet/stack_trace_filter_spec.rb | 26 +++++++++++++++ 2 files changed, 26 insertions(+), 32 deletions(-) create mode 100644 spec/bullet/stack_trace_filter_spec.rb diff --git a/spec/bullet/detector/n_plus_one_query_spec.rb b/spec/bullet/detector/n_plus_one_query_spec.rb index 10bd357c..842d46cd 100644 --- a/spec/bullet/detector/n_plus_one_query_spec.rb +++ b/spec/bullet/detector/n_plus_one_query_spec.rb @@ -127,38 +127,6 @@ module Detector end end - context '.caller_in_project' do - it 'should include only paths that are in the project' do - in_project = OpenStruct.new(absolute_path: File.join(Dir.pwd, 'abc', 'abc.rb')) - not_in_project = OpenStruct.new(absolute_path: '/def/def.rb') - - expect(NPlusOneQuery).to receive(:caller_locations).and_return([in_project, not_in_project]) - expect(NPlusOneQuery).to receive(:conditions_met?).with(@post, :association).and_return(true) - expect(NPlusOneQuery).to receive(:create_notification).with([in_project], 'Post', :association) - NPlusOneQuery.call_association(@post, :association) - end - - context 'stacktrace_includes' do - before { Bullet.stacktrace_includes = ['def', /xyz/] } - after { Bullet.stacktrace_includes = nil } - - it 'should include paths that are in the stacktrace_include list' do - in_project = OpenStruct.new(absolute_path: File.join(Dir.pwd, 'abc', 'abc.rb')) - included_gems = [OpenStruct.new(absolute_path: '/def/def.rb'), OpenStruct.new(absolute_path: 'xyz/xyz.rb')] - excluded_gem = OpenStruct.new(absolute_path: '/ghi/ghi.rb') - - expect(NPlusOneQuery).to receive(:caller_locations).and_return([in_project, *included_gems, excluded_gem]) - expect(NPlusOneQuery).to receive(:conditions_met?).with(@post, :association).and_return(true) - expect(NPlusOneQuery).to receive(:create_notification).with( - [in_project, *included_gems], - 'Post', - :association - ) - NPlusOneQuery.call_association(@post, :association) - end - end - end - context '.add_possible_objects' do it 'should add possible objects' do NPlusOneQuery.add_possible_objects([@post, @post2]) diff --git a/spec/bullet/stack_trace_filter_spec.rb b/spec/bullet/stack_trace_filter_spec.rb new file mode 100644 index 00000000..b99d7704 --- /dev/null +++ b/spec/bullet/stack_trace_filter_spec.rb @@ -0,0 +1,26 @@ +# frozen_string_literal: true + +require 'spec_helper' + +module Bullet + RSpec.describe StackTraceFilter do + let(:dummy_class) { Class.new { extend StackTraceFilter } } + let(:root_path) { Dir.pwd } + let(:bundler_path) { Bundler.bundle_path } + + describe '#caller_in_project' do + it 'gets the caller in the project' do + expect(dummy_class).to receive(:call_stacks).and_return({ + 'Post:1' => [ + File.join(root_path, 'lib/bullet.rb'), + File.join(root_path, 'vendor/uniform_notifier.rb'), + File.join(bundler_path, 'rack.rb') + ] + }) + expect(dummy_class.caller_in_project('Post:1')).to eq([ + File.join(root_path, 'lib/bullet.rb') + ]) + end + end + end +end \ No newline at end of file