Skip to content

Commit 8680a5b

Browse files
authored
Merge pull request rails#53822 from larouxn/notification_assertions_activerecord
Migrate applicable `activerecord` tests to use `NotificationAssertions`
2 parents 3553bd6 + 5e7b649 commit 8680a5b

File tree

3 files changed

+100
-177
lines changed

3 files changed

+100
-177
lines changed

activerecord/test/activejob/job_runtime_test.rb

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,27 +15,17 @@ def perform(*)
1515
test "job notification payload includes db_runtime" do
1616
ActiveRecord::RuntimeRegistry.sql_runtime = 0.0
1717

18-
assert_equal 42, notification_payload[:db_runtime]
18+
event = capture_notifications("perform.active_job") { TestJob.perform_now }.first
19+
20+
assert_equal 42, event.payload[:db_runtime]
1921
end
2022

2123
test "db_runtime tracks database runtime for job only" do
2224
ActiveRecord::RuntimeRegistry.sql_runtime = 100.0
2325

24-
assert_equal 42.0, notification_payload[:db_runtime]
26+
event = capture_notifications("perform.active_job") { TestJob.perform_now }.first
27+
28+
assert_equal 42.0, event.payload[:db_runtime]
2529
assert_equal 142.0, ActiveRecord::RuntimeRegistry.sql_runtime
2630
end
27-
28-
private
29-
def notification_payload
30-
payload = nil
31-
subscriber = ActiveSupport::Notifications.subscribe("perform.active_job") do |*, _payload|
32-
payload = _payload
33-
end
34-
35-
TestJob.perform_now
36-
37-
ActiveSupport::Notifications.unsubscribe(subscriber)
38-
39-
payload
40-
end
4131
end

activerecord/test/cases/associations/eager_test.rb

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1141,12 +1141,9 @@ def test_count_with_include
11411141
end
11421142

11431143
def test_association_loading_notification
1144-
notifications = messages_for("instantiation.active_record") do
1144+
payload = capture_notifications("instantiation.active_record") do
11451145
Developer.all.merge!(includes: "projects", where: { "developers_projects.access_level" => 1 }, limit: 5).to_a.size
1146-
end
1147-
1148-
message = notifications.first
1149-
payload = message.last
1146+
end.first.payload
11501147
count = Developer.all.merge!(includes: "projects", where: { "developers_projects.access_level" => 1 }, limit: 5).to_a.size
11511148

11521149
# eagerloaded row count should be greater than just developer count
@@ -1155,27 +1152,14 @@ def test_association_loading_notification
11551152
end
11561153

11571154
def test_base_messages
1158-
notifications = messages_for("instantiation.active_record") do
1155+
payload = capture_notifications("instantiation.active_record") do
11591156
Developer.all.to_a
1160-
end
1161-
message = notifications.first
1162-
payload = message.last
1157+
end.first.payload
11631158

11641159
assert_equal Developer.all.to_a.count, payload[:record_count]
11651160
assert_equal Developer.name, payload[:class_name]
11661161
end
11671162

1168-
def messages_for(name)
1169-
notifications = []
1170-
ActiveSupport::Notifications.subscribe(name) do |*args|
1171-
notifications << args
1172-
end
1173-
yield
1174-
notifications
1175-
ensure
1176-
ActiveSupport::Notifications.unsubscribe(name)
1177-
end
1178-
11791163
def test_load_with_sti_sharing_association
11801164
assert_queries_count(2) do # should not do 1 query per subclass
11811165
Comment.includes(:post).to_a

activerecord/test/cases/instrumentation_test.rb

Lines changed: 90 additions & 141 deletions
Original file line numberDiff line numberDiff line change
@@ -13,163 +13,142 @@ def setup
1313

1414
def test_payload_name_on_load
1515
Book.create(name: "test book")
16-
subscriber = ActiveSupport::Notifications.subscribe("sql.active_record") do |_, _, _, _, payload|
17-
if payload[:sql].match?("SELECT")
18-
assert_equal "Book Load", payload[:name]
19-
end
20-
end
21-
Book.first
22-
ensure
23-
ActiveSupport::Notifications.unsubscribe(subscriber) if subscriber
16+
17+
notification = capture_notifications("sql.active_record") { Book.first }
18+
.find { _1.payload[:sql].match?("SELECT") }
19+
20+
assert_equal "Book Load", notification.payload[:name]
2421
end
2522

2623
def test_payload_name_on_create
27-
subscriber = ActiveSupport::Notifications.subscribe("sql.active_record") do |_, _, _, _, payload|
28-
if payload[:sql].match?("INSERT")
29-
assert_equal "Book Create", payload[:name]
30-
end
31-
end
32-
Book.create(name: "test book")
33-
ensure
34-
ActiveSupport::Notifications.unsubscribe(subscriber) if subscriber
24+
notification = capture_notifications("sql.active_record") { Book.create(name: "test book") }
25+
.find { _1.payload[:sql].match?("INSERT") }
26+
27+
assert_equal "Book Create", notification.payload[:name]
3528
end
3629

3730
def test_payload_name_on_update
38-
subscriber = ActiveSupport::Notifications.subscribe("sql.active_record") do |_, _, _, _, payload|
39-
if payload[:sql].match?("UPDATE")
40-
assert_equal "Book Update", payload[:name]
41-
end
42-
end
4331
book = Book.create(name: "test book", format: "paperback")
44-
book.update_attribute(:format, "ebook")
45-
ensure
46-
ActiveSupport::Notifications.unsubscribe(subscriber) if subscriber
32+
33+
notification = capture_notifications("sql.active_record") { book.update_attribute(:format, "ebook") }
34+
.find { _1.payload[:sql].match?("UPDATE") }
35+
36+
assert_equal "Book Update", notification.payload[:name]
4737
end
4838

4939
def test_payload_name_on_update_all
50-
subscriber = ActiveSupport::Notifications.subscribe("sql.active_record") do |_, _, _, _, payload|
51-
if payload[:sql].match?("UPDATE")
52-
assert_equal "Book Update All", payload[:name]
53-
end
54-
end
55-
Book.update_all(format: "ebook")
56-
ensure
57-
ActiveSupport::Notifications.unsubscribe(subscriber) if subscriber
40+
notification = capture_notifications("sql.active_record") { Book.update_all(format: "ebook") }
41+
.find { _1.payload[:sql].match?("UPDATE") }
42+
43+
assert_equal "Book Update All", notification.payload[:name]
5844
end
5945

6046
def test_payload_name_on_destroy
61-
subscriber = ActiveSupport::Notifications.subscribe("sql.active_record") do |_, _, _, _, payload|
62-
if payload[:sql].match?("DELETE")
63-
assert_equal "Book Destroy", payload[:name]
64-
end
65-
end
6647
book = Book.create(name: "test book")
67-
book.destroy
68-
ensure
69-
ActiveSupport::Notifications.unsubscribe(subscriber) if subscriber
48+
49+
notification = capture_notifications("sql.active_record") { book.destroy }
50+
.find { _1.payload[:sql].match?("DELETE") }
51+
52+
assert_equal "Book Destroy", notification.payload[:name]
7053
end
7154

7255
def test_payload_name_on_delete_all
73-
subscriber = ActiveSupport::Notifications.subscribe("sql.active_record") do |_, _, _, _, payload|
74-
if payload[:sql].match?("DELETE")
75-
assert_equal "Book Delete All", payload[:name]
76-
end
77-
end
78-
Book.delete_all
79-
ensure
80-
ActiveSupport::Notifications.unsubscribe(subscriber) if subscriber
56+
notification = capture_notifications("sql.active_record") { Book.delete_all }
57+
.find { _1.payload[:sql].match?("DELETE") }
58+
59+
assert_equal "Book Delete All", notification.payload[:name]
8160
end
8261

8362
def test_payload_name_on_pluck
84-
subscriber = ActiveSupport::Notifications.subscribe("sql.active_record") do |_, _, _, _, payload|
85-
if payload[:sql].match?("SELECT")
86-
assert_equal "Book Pluck", payload[:name]
87-
end
88-
end
89-
Book.pluck(:name)
90-
ensure
91-
ActiveSupport::Notifications.unsubscribe(subscriber) if subscriber
63+
notification = capture_notifications("sql.active_record") { Book.pluck(:name) }
64+
.find { _1.payload[:sql].match?("SELECT") }
65+
66+
assert_equal "Book Pluck", notification.payload[:name]
9267
end
9368

9469
def test_payload_name_on_count
95-
subscriber = ActiveSupport::Notifications.subscribe("sql.active_record") do |_, _, _, _, payload|
96-
if payload[:sql].match?("SELECT")
97-
assert_equal "Book Count", payload[:name]
98-
end
99-
end
100-
Book.count
101-
ensure
102-
ActiveSupport::Notifications.unsubscribe(subscriber) if subscriber
70+
notification = capture_notifications("sql.active_record") { Book.count }
71+
.find { _1.payload[:sql].match?("SELECT") }
72+
73+
assert_equal "Book Count", notification.payload[:name]
10374
end
10475

10576
def test_payload_name_on_grouped_count
106-
subscriber = ActiveSupport::Notifications.subscribe("sql.active_record") do |_, _, _, _, payload|
107-
if payload[:sql].match?("SELECT")
108-
assert_equal "Book Count", payload[:name]
109-
end
110-
end
111-
Book.group(:status).count
112-
ensure
113-
ActiveSupport::Notifications.unsubscribe(subscriber) if subscriber
77+
notification = capture_notifications("sql.active_record") { Book.group(:status).count }
78+
.find { _1.payload[:sql].match?("SELECT") }
79+
80+
assert_equal "Book Count", notification.payload[:name]
11481
end
11582

11683
def test_payload_row_count_on_select_all
11784
10.times { Book.create(name: "row count book 1") }
118-
subscriber = ActiveSupport::Notifications.subscribe("sql.active_record") do |_, _, _, _, payload|
119-
if payload[:sql].match?("SELECT")
120-
assert_equal 10, payload[:row_count]
121-
end
122-
end
123-
Book.where(name: "row count book 1").to_a
124-
ensure
125-
ActiveSupport::Notifications.unsubscribe(subscriber) if subscriber
85+
86+
notification = capture_notifications("sql.active_record") { Book.where(name: "row count book 1").to_a }
87+
.find { _1.payload[:sql].match?("SELECT") }
88+
89+
assert_equal 10, notification.payload[:row_count]
12690
end
12791

12892
def test_payload_row_count_on_pluck
12993
10.times { Book.create(name: "row count book 2") }
130-
subscriber = ActiveSupport::Notifications.subscribe("sql.active_record") do |_, _, _, _, payload|
131-
if payload[:sql].match?("SELECT")
132-
assert_equal 10, payload[:row_count]
133-
end
134-
end
135-
Book.where(name: "row count book 2").pluck(:name)
136-
ensure
137-
ActiveSupport::Notifications.unsubscribe(subscriber) if subscriber
94+
95+
notification = capture_notifications("sql.active_record") { Book.where(name: "row count book 2").pluck(:name) }
96+
.find { _1.payload[:sql].match?("SELECT") }
97+
98+
assert_equal 10, notification.payload[:row_count]
13899
end
139100

140101
def test_payload_row_count_on_raw_sql
141102
10.times { Book.create(name: "row count book 3") }
142-
subscriber = ActiveSupport::Notifications.subscribe("sql.active_record") do |_, _, _, _, payload|
143-
if payload[:sql].match?("SELECT")
144-
assert_equal 10, payload[:row_count]
145-
end
146-
end
147-
ActiveRecord::Base.lease_connection.execute("SELECT * FROM books WHERE name='row count book 3';")
148-
ensure
149-
ActiveSupport::Notifications.unsubscribe(subscriber) if subscriber
103+
104+
notification = capture_notifications("sql.active_record") do
105+
ActiveRecord::Base.lease_connection.execute("SELECT * FROM books WHERE name='row count book 3';")
106+
end.find { _1.payload[:sql].match?("SELECT") }
107+
108+
assert_equal 10, notification.payload[:row_count]
150109
end
151110

152111
def test_payload_row_count_on_cache
153-
events = []
154-
callback = -> (event) do
155-
payload = event.payload
156-
events << payload if payload[:sql].include?("SELECT")
157-
end
158-
159112
Book.create!(name: "row count book")
160-
ActiveSupport::Notifications.subscribed(callback, "sql.active_record") do
113+
114+
notifications = capture_notifications("sql.active_record") do
161115
Book.cache do
162116
Book.first
163117
Book.first
164118
end
165119
end
166120

167-
assert_equal 2, events.size
168-
assert_not events[0][:cached]
169-
assert events[1][:cached]
121+
payloads = notifications.select { _1.payload[:sql].match?("SELECT") }.map(&:payload)
122+
123+
assert_equal 2, payloads.size
124+
assert_not payloads[0][:cached]
125+
assert payloads[1][:cached]
126+
assert_equal 1, payloads[0][:row_count]
127+
assert_equal 1, payloads[1][:row_count]
128+
end
129+
130+
def test_payload_connection_with_query_cache_disabled
131+
connection = ClothingItem.lease_connection
132+
133+
payload = capture_notifications("sql.active_record") { Book.first }.first.payload
134+
135+
assert_equal connection, payload[:connection]
136+
end
137+
138+
def test_payload_connection_with_query_cache_enabled
139+
connection = ClothingItem.lease_connection
140+
141+
payloads = capture_notifications("sql.active_record") do
142+
assert_notifications_count("sql.active_record", 2) do
143+
Book.cache do
144+
Book.first
145+
Book.first
146+
end
147+
end
148+
end.map(&:payload)
170149

171-
assert_equal 1, events[0][:row_count]
172-
assert_equal 1, events[1][:row_count]
150+
assert_equal connection, payloads.first[:connection]
151+
assert_equal connection, payloads.second[:connection]
173152
end
174153

175154
def test_payload_affected_rows
@@ -193,43 +172,13 @@ def test_payload_affected_rows
193172
assert_equal [4, 3, 2, 0], affected_row_values
194173
end
195174

196-
def test_payload_connection_with_query_cache_disabled
197-
connection = ClothingItem.lease_connection
198-
subscriber = ActiveSupport::Notifications.subscribe("sql.active_record") do |_, _, _, _, payload|
199-
assert_equal connection, payload[:connection]
200-
end
201-
Book.first
202-
ensure
203-
ActiveSupport::Notifications.unsubscribe(subscriber) if subscriber
204-
end
205-
206-
def test_payload_connection_with_query_cache_enabled
207-
connection = ClothingItem.lease_connection
208-
subscriber = ActiveSupport::Notifications.subscribe("sql.active_record") do |_, _, _, _, payload|
209-
assert_equal connection, payload[:connection]
210-
end
211-
Book.cache do
212-
Book.first
213-
Book.first
214-
end
215-
ensure
216-
ActiveSupport::Notifications.unsubscribe(subscriber) if subscriber
217-
end
218-
219175
def test_no_instantiation_notification_when_no_records
220176
author = Author.create!(id: 100, name: "David")
221177

222-
called = false
223-
subscriber = ActiveSupport::Notifications.subscribe("instantiation.active_record") do
224-
called = true
178+
assert_no_notifications("instantiation.active_record") do
179+
Author.where(id: 0).to_a
180+
author.books.to_a
225181
end
226-
227-
Author.where(id: 0).to_a
228-
author.books.to_a
229-
230-
assert_equal false, called
231-
ensure
232-
ActiveSupport::Notifications.unsubscribe(subscriber) if subscriber
233182
end
234183
end
235184

0 commit comments

Comments
 (0)