Skip to content

Commit da32425

Browse files
authored
Merge pull request rails#54126 from larouxn/notif_assert_small_revamp
Update `NotificationAssertions`'s `assert_notifcation` to match against payload subsets and return matched notification
2 parents 1387af8 + 72eaf25 commit da32425

File tree

15 files changed

+92
-66
lines changed

15 files changed

+92
-66
lines changed

actionmailer/test/base_test.rb

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -973,14 +973,13 @@ def a_callback
973973
end
974974

975975
test "notification for deliver" do
976-
event = capture_notifications("deliver.action_mailer") do
977-
assert_notifications_count("deliver.action_mailer", 1) do
976+
assert_notifications_count("deliver.action_mailer", 1) do
977+
notification = assert_notification("deliver.action_mailer") do
978978
BaseMailer.welcome(body: "Hello there").deliver_now
979979
end
980-
end.first
981980

982-
assert_equal "deliver.action_mailer", event.name
983-
assert_not_nil event.payload[:message_id]
981+
assert_not_nil notification.payload[:message_id]
982+
end
984983
end
985984

986985
private

actionpack/test/controller/allow_browser_test.rb

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,13 +84,12 @@ class AllowBrowserTest < ActionController::TestCase
8484
end
8585

8686
test "a blocked request instruments a browser_block.action_controller event" do
87-
event, *rest = capture_notifications "browser_block.action_controller" do
87+
notification = assert_notification("browser_block.action_controller") do
8888
get_with_agent :modern, CHROME_118
8989
end
9090

91-
assert_equal request, event.payload[:request]
92-
assert_not_empty event.payload[:versions]
93-
assert_empty rest
91+
assert_equal request, notification.payload[:request]
92+
assert_not_empty notification.payload[:versions]
9493
end
9594

9695
private

actionpack/test/controller/caching_test.rb

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -255,12 +255,9 @@ def test_render_inline_before_fragment_caching
255255
end
256256

257257
def test_fragment_cache_instrumentation
258-
payload = capture_notifications("read_fragment.action_controller") do
258+
assert_notification("read_fragment.action_controller", controller: "functional_caching", action: "inline_fragment_cached") do
259259
get :inline_fragment_cached
260-
end.first.payload
261-
262-
assert_equal "functional_caching", payload[:controller]
263-
assert_equal "inline_fragment_cached", payload[:action]
260+
end
264261
end
265262

266263
def test_html_formatted_fragment_caching

actionpack/test/controller/redirect_test.rb

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -608,13 +608,11 @@ def test_url_from_fallback
608608
end
609609

610610
def test_redirect_to_instrumentation
611-
payload = capture_notifications("redirect_to.action_controller") do
611+
notification = assert_notification("redirect_to.action_controller", status: 302, location: "http://test.host/redirect/hello_world") do
612612
get :simple_redirect
613-
end.first.payload
613+
end
614614

615-
assert_equal request, payload[:request]
616-
assert_equal 302, payload[:status]
617-
assert_equal "http://test.host/redirect/hello_world", payload[:location]
615+
assert_kind_of ActionDispatch::Request, notification.payload[:request]
618616
end
619617

620618
def test_redirect_to_external_with_rescue

actionpack/test/dispatch/routing/instrumentation_test.rb

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,11 @@ class RoutingInstrumentationTest < ActionDispatch::IntegrationTest
88
get "redirect", to: redirect("/login")
99
end
1010

11-
event = capture_notifications("redirect.action_dispatch") do
12-
assert_notifications_count("redirect.action_dispatch", 1) do
13-
get "/redirect"
14-
end
15-
end.first
11+
notification = assert_notification("redirect.action_dispatch", status: 301, location: "http://www.example.com/login") do
12+
get "/redirect"
13+
end
1614

17-
assert_equal 301, event.payload[:status]
18-
assert_equal "http://www.example.com/login", event.payload[:location]
19-
assert_kind_of ActionDispatch::Request, event.payload[:request]
15+
assert_kind_of ActionDispatch::Request, notification.payload[:request]
2016
end
2117

2218
private

actionview/test/activerecord/partial_rendering_query_test.rb

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,10 @@ def setup
1010
end
1111

1212
def test_render_with_relation_collection
13-
notifications = capture_notifications("sql.active_record") do
14-
@view.render partial: "topics/topic", collection: Topic.all
13+
assert_notifications_count("sql.active_record", 1) do
14+
assert_notification("sql.active_record", sql: 'SELECT "topics".* FROM "topics"') do
15+
@view.render partial: "topics/topic", collection: Topic.all
16+
end
1517
end
16-
17-
queries = notifications.filter_map { _1.payload[:sql] unless %w[ SCHEMA TRANSACTION ].include?(_1.payload[:name]) }
18-
19-
assert_equal 1, queries.size
20-
assert_equal 'SELECT "topics".* FROM "topics"', queries[0]
2118
end
2219
end

activejob/test/cases/queuing_test.rb

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -97,12 +97,10 @@ class QueuingTest < ActiveSupport::TestCase
9797
test "perform_all_later instrumentation" do
9898
jobs = HelloJob.new("Jamie"), HelloJob.new("John")
9999

100-
payload = capture_notifications("enqueue_all.active_job") do
100+
notification = assert_notification("enqueue_all.active_job", jobs:, enqueued_count: 2) do
101101
ActiveJob.perform_all_later(jobs)
102-
end.first.payload
102+
end
103103

104-
assert payload[:adapter]
105-
assert_equal jobs, payload[:jobs]
106-
assert_equal 2, payload[:enqueued_count]
104+
assert notification.payload[:adapter]
107105
end
108106
end

activerecord/test/activejob/job_runtime_test.rb

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

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

2321
test "db_runtime tracks database runtime for job only" do
2422
ActiveRecord::RuntimeRegistry.sql_runtime = 100.0
2523

26-
event = capture_notifications("perform.active_job") { TestJob.perform_now }.first
24+
assert_notification("perform.active_job", db_runtime: 42.0) { TestJob.perform_now }
2725

28-
assert_equal 42.0, event.payload[:db_runtime]
2926
assert_equal 142.0, ActiveRecord::RuntimeRegistry.sql_runtime
3027
end
3128
end

activerecord/test/cases/associations/eager_test.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1136,14 +1136,14 @@ def test_count_with_include
11361136
end
11371137

11381138
def test_association_loading_notification
1139-
payload = capture_notifications("instantiation.active_record") do
1139+
notification = assert_notification("instantiation.active_record", class_name: Developer.name) do
11401140
Developer.all.merge!(includes: "projects", where: { "developers_projects.access_level" => 1 }, limit: 5).to_a.size
1141-
end.first.payload
1141+
end
1142+
11421143
count = Developer.all.merge!(includes: "projects", where: { "developers_projects.access_level" => 1 }, limit: 5).to_a.size
11431144

11441145
# eagerloaded row count should be greater than just developer count
1145-
assert_operator payload[:record_count], :>, count
1146-
assert_equal Developer.name, payload[:class_name]
1146+
assert_operator notification.payload[:record_count], :>, count
11471147
end
11481148

11491149
def test_base_messages

activerecord/test/cases/instrumentation_test.rb

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -128,11 +128,7 @@ def test_payload_row_count_on_cache
128128
end
129129

130130
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]
131+
assert_notification("sql.active_record", connection: ClothingItem.lease_connection) { Book.first }
136132
end
137133

138134
def test_payload_connection_with_query_cache_enabled

0 commit comments

Comments
 (0)