From eb1187e409745664eed700a9a9494f426a656f3f Mon Sep 17 00:00:00 2001 From: Ariel Valentin Date: Wed, 20 Nov 2024 17:54:19 -0600 Subject: [PATCH] test: Add ActiveRecord tests --- .../active_record/patches/querying_test.rb | 128 ++++++++++++++++++ 1 file changed, 128 insertions(+) diff --git a/instrumentation/active_record/test/instrumentation/active_record/patches/querying_test.rb b/instrumentation/active_record/test/instrumentation/active_record/patches/querying_test.rb index 879af50e6..8365c7386 100644 --- a/instrumentation/active_record/test/instrumentation/active_record/patches/querying_test.rb +++ b/instrumentation/active_record/test/instrumentation/active_record/patches/querying_test.rb @@ -14,6 +14,11 @@ let(:spans) { exporter.finished_spans } before { exporter.reset } + after do + ActiveRecord::Base.subclasses.each do |model| + model.connection.truncate(model.table_name) + end + end describe 'query' do it 'traces' do @@ -28,5 +33,128 @@ _(user_find_spans.length).must_equal(2) _(account_find_span).wont_be_nil end + + describe 'find_by_sql' do + it 'creates a span' do + account = Account.create! + + Account.find_by_sql('SELECT * FROM accounts') + + account_find_span = spans.find { |s| s.name == 'Account query' } + _(account_find_span).wont_be_nil + _(account_find_span.attributes).must_be_empty + end + + describe 'given a block' do + it 'creates a span' do + account = Account.create! + + record_ids = [] + + Account.find_by_sql('SELECT * FROM accounts') do |record| + record_ids << record.id + end + + account_find_span = spans.find { |s| s.name == 'Account query' } + _(account_find_span).wont_be_nil + _(account_find_span.attributes).must_be_empty + + _(record_ids.sort).must_equal([account.id]) + end + end + end + + describe 'find_by' do + it 'creates a span' do + account = Account.create! + User.create!(account: account) + + Account.find_by(id: account.id) + + account_find_span = spans.find { |s| s.name == 'Account query' } + _(account_find_span).wont_be_nil + _(account_find_span.attributes).must_be_empty + end + + describe 'given a block' do + it 'creates a span' do + account = Account.create! + User.create!(account: account) + + record_ids = [] + + Account.find_by(id: account.id) do |record| + record_ids << record.id + end + + account_find_span = spans.find { |s| s.name == 'Account query' } + _(account_find_span).wont_be_nil + _(account_find_span.attributes).must_be_empty + _(record_ids).must_equal([account.id]) + end + end + end + end + + describe 'find' do + it 'creates a span' do + account = Account.create! + User.create!(account: account) + + Account.find(account.id) + + account_find_span = spans.find { |s| s.name == 'Account query' } + _(account_find_span).wont_be_nil + _(account_find_span.attributes).must_be_empty + end + + describe 'given a block' do + it 'creates a span' do + account = Account.create! + User.create!(account: account) + + record_ids = [] + + Account.find(account.id) do |record| + record_ids << record.id + end + + account_find_span = spans.find { |s| s.name == 'Account query' } + _(account_find_span).wont_be_nil + _(account_find_span.attributes).must_be_empty + _(record_ids).must_equal([account.id]) + end + end + end + + describe 'where' do + it 'creates a span' do + account = Account.create! + User.create!(account: account) + + Account.where(id: [account.id]) + + account_find_span = spans.find { |s| s.name == 'Account query' } + _(account_find_span).wont_be_nil + _(account_find_span.attributes).must_be_empty + end + + describe 'given a block' do + it 'creates a span' do + account = Account.create! + User.create!(account: account) + + record_ids = [] + + Account.where(id: [account.id]) do |record| + record_ids << record.id + end + + account_find_span = spans.find { |s| s.name == 'Account query' } + _(account_find_span).wont_be_nil + _(account_find_span.attributes).must_be_empty + _(record_ids).must_equal([account.id]) + end + end end end