@@ -11,13 +11,14 @@ class QueryLogsTest < ActiveRecord::TestCase
11
11
}
12
12
13
13
def setup
14
- # QueryLogs context is automatically reset in Rails app via an executor hooks set in railtie
14
+ # ActiveSupport::ExecutionContext context is automatically reset in Rails app via an executor hooks set in railtie
15
15
# But not in Active Record's own test suite.
16
- ActiveRecord :: QueryLogs . clear_context
16
+ ActiveSupport :: ExecutionContext . clear
17
17
18
18
# Enable the query tags logging
19
19
@original_transformers = ActiveRecord . query_transformers
20
20
@original_prepend = ActiveRecord ::QueryLogs . prepend_comment
21
+ @original_tags = ActiveRecord ::QueryLogs . tags
21
22
ActiveRecord . query_transformers += [ ActiveRecord ::QueryLogs ]
22
23
ActiveRecord ::QueryLogs . prepend_comment = false
23
24
ActiveRecord ::QueryLogs . cache_query_log_tags = false
@@ -27,10 +28,13 @@ def setup
27
28
def teardown
28
29
ActiveRecord . query_transformers = @original_transformers
29
30
ActiveRecord ::QueryLogs . prepend_comment = @original_prepend
30
- ActiveRecord ::QueryLogs . tags = [ ]
31
- # QueryLogs context is automatically reset in Rails app via an executor hooks set in railtie
31
+ ActiveRecord ::QueryLogs . tags = @original_tags
32
+ ActiveRecord ::QueryLogs . prepend_comment = false
33
+ ActiveRecord ::QueryLogs . cache_query_log_tags = false
34
+ ActiveRecord ::QueryLogs . cached_comment = nil
35
+ # ActiveSupport::ExecutionContext context is automatically reset in Rails app via an executor hooks set in railtie
32
36
# But not in Active Record's own test suite.
33
- ActiveRecord :: QueryLogs . clear_context
37
+ ActiveSupport :: ExecutionContext . clear
34
38
end
35
39
36
40
def test_escaping_good_comment
@@ -57,8 +61,6 @@ def test_add_comments_to_beginning_of_query
57
61
assert_sql ( %r{/\* application:active_record\* / select id from posts$} ) do
58
62
ActiveRecord ::Base . connection . execute "select id from posts"
59
63
end
60
- ensure
61
- ActiveRecord ::QueryLogs . prepend_comment = nil
62
64
end
63
65
64
66
def test_exists_is_commented
@@ -112,41 +114,24 @@ def test_retrieves_comment_from_cache_when_enabled_and_set
112
114
ActiveRecord ::QueryLogs . stub ( :cached_comment , "/*cached_comment*/" ) do
113
115
assert_equal "/*cached_comment*/" , ActiveRecord ::QueryLogs . call ( "" )
114
116
end
115
- ensure
116
- ActiveRecord ::QueryLogs . cached_comment = nil
117
- ActiveRecord ::QueryLogs . cache_query_log_tags = false
118
117
end
119
118
120
119
def test_resets_cache_on_context_update
121
120
ActiveRecord ::QueryLogs . cache_query_log_tags = true
122
- ActiveRecord :: QueryLogs . set_context ( temporary : "value" )
121
+ ActiveSupport :: ExecutionContext [ :temporary ] = "value"
123
122
ActiveRecord ::QueryLogs . tags = [ temporary_tag : -> ( context ) { context [ :temporary ] } ]
124
123
125
124
assert_equal "/*temporary_tag:value*/" , ActiveRecord ::QueryLogs . call ( "" )
126
125
127
- ActiveRecord :: QueryLogs . set_context ( temporary : "new_value" )
126
+ ActiveSupport :: ExecutionContext [ :temporary ] = "new_value"
128
127
129
128
assert_nil ActiveRecord ::QueryLogs . cached_comment
130
129
assert_equal "/*temporary_tag:new_value*/" , ActiveRecord ::QueryLogs . call ( "" )
131
- ensure
132
- ActiveRecord ::QueryLogs . cached_comment = nil
133
- ActiveRecord ::QueryLogs . cache_query_log_tags = false
134
- end
135
-
136
- def test_ensure_context_has_symbol_keys
137
- ActiveRecord ::QueryLogs . tags = [ new_key : -> ( context ) { context [ :symbol_key ] } ]
138
- ActiveRecord ::QueryLogs . set_context ( "symbol_key" => "symbolized" )
139
-
140
- assert_sql ( %r{/\* new_key:symbolized} ) do
141
- Dashboard . first
142
- end
143
- ensure
144
- ActiveRecord ::QueryLogs . set_context ( application_name : nil )
145
130
end
146
131
147
132
def test_default_tag_behavior
148
133
ActiveRecord ::QueryLogs . tags = [ :application , :foo ]
149
- ActiveRecord :: QueryLogs . set_context ( foo : "bar" ) do
134
+ ActiveSupport :: ExecutionContext . set ( foo : "bar" ) do
150
135
assert_sql ( %r{/\* application:active_record,foo:bar\* /} ) do
151
136
Dashboard . first
152
137
end
@@ -157,39 +142,29 @@ def test_default_tag_behavior
157
142
end
158
143
159
144
def test_empty_comments_are_not_added
160
- original_tags = ActiveRecord ::QueryLogs . tags
161
145
ActiveRecord ::QueryLogs . tags = [ empty : -> { nil } ]
162
146
assert_sql ( %r{select id from posts$} ) do
163
147
ActiveRecord ::Base . connection . execute "select id from posts"
164
148
end
165
- ensure
166
- ActiveRecord ::QueryLogs . tags = original_tags
167
149
end
168
150
169
151
def test_custom_basic_tags
170
- original_tags = ActiveRecord ::QueryLogs . tags
171
152
ActiveRecord ::QueryLogs . tags = [ :application , { custom_string : "test content" } ]
172
153
173
154
assert_sql ( %r{/\* application:active_record,custom_string:test content\* /$} ) do
174
155
Dashboard . first
175
156
end
176
- ensure
177
- ActiveRecord ::QueryLogs . tags = original_tags
178
157
end
179
158
180
159
def test_custom_proc_tags
181
- original_tags = ActiveRecord ::QueryLogs . tags
182
160
ActiveRecord ::QueryLogs . tags = [ :application , { custom_proc : -> { "test content" } } ]
183
161
184
162
assert_sql ( %r{/\* application:active_record,custom_proc:test content\* /$} ) do
185
163
Dashboard . first
186
164
end
187
- ensure
188
- ActiveRecord ::QueryLogs . tags = original_tags
189
165
end
190
166
191
167
def test_multiple_custom_tags
192
- original_tags = ActiveRecord ::QueryLogs . tags
193
168
ActiveRecord ::QueryLogs . tags = [
194
169
:application ,
195
170
{ custom_proc : -> { "test content" } , another_proc : -> { "more test content" } } ,
@@ -198,34 +173,14 @@ def test_multiple_custom_tags
198
173
assert_sql ( %r{/\* application:active_record,custom_proc:test content,another_proc:more test content\* /$} ) do
199
174
Dashboard . first
200
175
end
201
- ensure
202
- ActiveRecord ::QueryLogs . tags = original_tags
203
176
end
204
177
205
178
def test_custom_proc_context_tags
206
- original_tags = ActiveRecord ::QueryLogs . tags
207
- ActiveRecord ::QueryLogs . set_context ( foo : "bar" )
179
+ ActiveSupport ::ExecutionContext [ :foo ] = "bar"
208
180
ActiveRecord ::QueryLogs . tags = [ :application , { custom_context_proc : -> ( context ) { context [ :foo ] } } ]
209
181
210
182
assert_sql ( %r{/\* application:active_record,custom_context_proc:bar\* /$} ) do
211
183
Dashboard . first
212
184
end
213
- ensure
214
- ActiveRecord ::QueryLogs . set_context ( foo : nil )
215
- ActiveRecord ::QueryLogs . tags = original_tags
216
- end
217
-
218
- def test_set_context_restore_state
219
- original_tags = ActiveRecord ::QueryLogs . tags
220
- ActiveRecord ::QueryLogs . tags = [ foo : -> ( context ) { context [ :foo ] } ]
221
- ActiveRecord ::QueryLogs . set_context ( foo : "bar" ) do
222
- assert_sql ( %r{/\* foo:bar\* /$} ) { Dashboard . first }
223
- ActiveRecord ::QueryLogs . set_context ( foo : "plop" ) do
224
- assert_sql ( %r{/\* foo:plop\* /$} ) { Dashboard . first }
225
- end
226
- assert_sql ( %r{/\* foo:bar\* /$} ) { Dashboard . first }
227
- end
228
- ensure
229
- ActiveRecord ::QueryLogs . tags = original_tags
230
185
end
231
186
end
0 commit comments