Skip to content

Commit a2331b9

Browse files
olepbrarielvalentin
andcommitted
test: add tests for ActiveRecord's find, find_by and find_by_sql
Co-authored-by: Ariel Valentin <[email protected]>
1 parent e17cf0f commit a2331b9

File tree

1 file changed

+79
-0
lines changed
  • instrumentation/active_record/test/instrumentation/active_record/patches

1 file changed

+79
-0
lines changed

instrumentation/active_record/test/instrumentation/active_record/patches/querying_test.rb

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@
1414
let(:spans) { exporter.finished_spans }
1515

1616
before { exporter.reset }
17+
after do
18+
ActiveRecord::Base.subclasses.each do |model|
19+
model.connection.truncate(model.table_name)
20+
end
21+
end
1722

1823
describe 'query' do
1924
it 'traces' do
@@ -28,5 +33,79 @@
2833
_(user_find_spans.length).must_equal(2)
2934
_(account_find_span).wont_be_nil
3035
end
36+
37+
describe 'find_by_sql' do
38+
it 'creates a span' do
39+
account = Account.create!
40+
41+
Account.find_by_sql('SELECT * FROM accounts')
42+
43+
account_find_span = spans.find { |s| s.name == 'Account query' }
44+
_(account_find_span).wont_be_nil
45+
_(account_find_span.attributes).must_be_empty
46+
end
47+
48+
describe 'given a block' do
49+
it 'creates a span' do
50+
account = Account.create!
51+
52+
record_ids = []
53+
54+
Account.find_by_sql('SELECT * FROM accounts') do |record|
55+
record_ids << record.id
56+
end
57+
58+
account_find_span = spans.find { |s| s.name == 'Account query' }
59+
_(account_find_span).wont_be_nil
60+
_(account_find_span.attributes).must_be_empty
61+
62+
_(record_ids).must_equal([account.id])
63+
end
64+
end
65+
end
66+
67+
describe 'find_by' do
68+
it 'creates a span' do
69+
account = Account.create!
70+
User.create!(account: account)
71+
72+
Account.find_by(id: account.id)
73+
74+
account_find_span = spans.find { |s| s.name == 'Account query' }
75+
_(account_find_span).wont_be_nil
76+
_(account_find_span.attributes).must_be_empty
77+
end
78+
end
79+
80+
describe 'find' do
81+
it 'creates a span' do
82+
account = Account.create!
83+
User.create!(account: account)
84+
85+
Account.find(account.id)
86+
87+
account_find_span = spans.find { |s| s.name == 'Account query' }
88+
_(account_find_span).wont_be_nil
89+
_(account_find_span.attributes).must_be_empty
90+
end
91+
92+
describe 'given a block' do
93+
it 'creates a span' do
94+
account = Account.create!
95+
User.create!(account: account)
96+
97+
record_ids = []
98+
99+
Account.find(account.id) do |record|
100+
record_ids << record.id
101+
end
102+
103+
account_find_span = spans.find { |s| s.name == 'Account query' }
104+
_(account_find_span).wont_be_nil
105+
_(account_find_span.attributes).must_be_empty
106+
_(record_ids).must_equal([account.id])
107+
end
108+
end
109+
end
31110
end
32111
end

0 commit comments

Comments
 (0)