@@ -13,163 +13,142 @@ def setup
13
13
14
14
def test_payload_name_on_load
15
15
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 ]
24
21
end
25
22
26
23
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 ]
35
28
end
36
29
37
30
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
43
31
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 ]
47
37
end
48
38
49
39
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 ]
58
44
end
59
45
60
46
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
66
47
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 ]
70
53
end
71
54
72
55
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 ]
81
60
end
82
61
83
62
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 ]
92
67
end
93
68
94
69
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 ]
103
74
end
104
75
105
76
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 ]
114
81
end
115
82
116
83
def test_payload_row_count_on_select_all
117
84
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 ]
126
90
end
127
91
128
92
def test_payload_row_count_on_pluck
129
93
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 ]
138
99
end
139
100
140
101
def test_payload_row_count_on_raw_sql
141
102
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 ]
150
109
end
151
110
152
111
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
-
159
112
Book . create! ( name : "row count book" )
160
- ActiveSupport ::Notifications . subscribed ( callback , "sql.active_record" ) do
113
+
114
+ notifications = capture_notifications ( "sql.active_record" ) do
161
115
Book . cache do
162
116
Book . first
163
117
Book . first
164
118
end
165
119
end
166
120
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 )
170
149
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 ]
173
152
end
174
153
175
154
def test_payload_affected_rows
@@ -193,43 +172,13 @@ def test_payload_affected_rows
193
172
assert_equal [ 4 , 3 , 2 , 0 ] , affected_row_values
194
173
end
195
174
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
-
219
175
def test_no_instantiation_notification_when_no_records
220
176
author = Author . create! ( id : 100 , name : "David" )
221
177
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
225
181
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
233
182
end
234
183
end
235
184
0 commit comments