Skip to content

Commit

Permalink
test: Add ActiveRecord tests
Browse files Browse the repository at this point in the history
  • Loading branch information
arielvalentin committed Nov 20, 2024
1 parent bf6394d commit eb1187e
Showing 1 changed file with 128 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

0 comments on commit eb1187e

Please sign in to comment.