Skip to content

Commit

Permalink
test caller_in_project in stack_trace_filter_spec
Browse files Browse the repository at this point in the history
  • Loading branch information
flyerhzm committed Dec 31, 2022
1 parent ef33b38 commit 74f2b7e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 32 deletions.
32 changes: 0 additions & 32 deletions spec/bullet/detector/n_plus_one_query_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down
26 changes: 26 additions & 0 deletions spec/bullet/stack_trace_filter_spec.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 74f2b7e

Please sign in to comment.