/
query_logs_test.rb
202 lines (142 loc) · 5.46 KB
/
query_logs_test.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# frozen_string_literal: true
require "isolation/abstract_unit"
require "rack/test"
module ApplicationTests
class QueryLogsTest < ActiveSupport::TestCase
include ActiveSupport::Testing::Isolation
include Rack::Test::Methods
def setup
build_app
app_file "app/models/user.rb", <<-RUBY
class User < ActiveRecord::Base
end
RUBY
app_file "app/controllers/users_controller.rb", <<-RUBY
class UsersController < ApplicationController
def index
render inline: ActiveRecord::QueryLogs.call("")
end
def dynamic_content
Time.now.to_f.to_s
end
end
RUBY
app_file "app/jobs/user_job.rb", <<-RUBY
class UserJob < ActiveJob::Base
def perform
ActiveRecord::QueryLogs.call("")
end
def dynamic_content
Time.now.to_f.to_s
end
end
RUBY
app_file "config/routes.rb", <<-RUBY
Rails.application.routes.draw do
get "/", to: "users#index"
end
RUBY
end
def teardown
teardown_app
end
def app
@app ||= Rails.application
end
test "does not modify the query execution path by default" do
boot_app
assert_not_includes ActiveRecord.query_transformers, ActiveRecord::QueryLogs
end
test "prepends the query execution path when enabled" do
add_to_config "config.active_record.query_log_tags_enabled = true"
boot_app
assert_includes ActiveRecord.query_transformers, ActiveRecord::QueryLogs
end
test "controller and job tags are defined by default" do
add_to_config "config.active_record.query_log_tags_enabled = true"
boot_app
assert_equal ActiveRecord::QueryLogs.tags, [ :application, :controller, :action, :job ]
end
test "controller actions have tagging filters enabled by default" do
add_to_config "config.active_record.query_log_tags_enabled = true"
add_to_config "config.active_record.query_log_tags_format = :legacy"
boot_app
get "/"
comment = last_response.body.strip
assert_includes comment, "controller:users"
end
test "sqlcommenter formatting works when specified" do
add_to_config "config.active_record.query_log_tags_enabled = true"
add_to_config "config.active_record.query_log_tags_format = :sqlcommenter"
add_to_config "config.active_record.query_log_tags = [ :pid ]"
boot_app
get "/"
comment = last_response.body.strip
assert_match(/pid='\d+'/, comment)
assert_includes comment, "controller='users'"
end
test "controller actions tagging filters can be disabled" do
add_to_config "config.active_record.query_log_tags_enabled = true"
add_to_config "config.action_controller.log_query_tags_around_actions = false"
boot_app
get "/"
comment = last_response.body.strip
assert_not_includes comment, "controller:users"
end
test "controller tags are not doubled up if already configured" do
add_to_config "config.active_record.query_log_tags_enabled = true"
add_to_config "config.active_record.query_log_tags = [ :action, :job, :controller, :pid ]"
boot_app
get "/"
comment = last_response.body.strip
assert_match(/\/\*action='index',controller='users',pid='\d+'\*\//, comment)
end
test "job perform method has tagging filters enabled by default" do
add_to_config "config.active_record.query_log_tags_enabled = true"
boot_app
comment = UserJob.new.perform_now
assert_includes comment, "UserJob"
end
test "job perform method tagging filters can be disabled" do
add_to_config "config.active_record.query_log_tags_enabled = true"
add_to_config "config.active_job.log_query_tags_around_perform = false"
boot_app
comment = UserJob.new.perform_now
assert_not_includes comment, "UserJob"
end
test "job tags are not doubled up if already configured" do
add_to_config "config.active_record.query_log_tags_enabled = true"
add_to_config "config.active_record.query_log_tags = [ :action, :job, :controller, :pid ]"
boot_app
comment = UserJob.new.perform_now
assert_match(/\/\*job='UserJob',pid='\d+'\*\//, comment)
end
test "query cache is cleared between requests" do
add_to_config "config.active_record.query_log_tags_enabled = true"
add_to_config "config.active_record.cache_query_log_tags = true"
add_to_config "config.active_record.query_log_tags = [ { dynamic: ->(context) { context[:controller]&.dynamic_content } } ]"
boot_app
get "/"
first_tags = last_response.body
get "/"
second_tags = last_response.body
assert_not_equal first_tags, second_tags
end
test "query cache is cleared between job executions" do
add_to_config "config.active_record.query_log_tags_enabled = true"
add_to_config "config.active_record.cache_query_log_tags = true"
add_to_config "config.active_record.query_log_tags = [ { dynamic: ->(context) { context[:job]&.dynamic_content } } ]"
boot_app
first_tags = UserJob.new.perform_now
second_tags = UserJob.new.perform_now
assert_not_equal first_tags, second_tags
end
private
def boot_app(env = "production")
ENV["RAILS_ENV"] = env
require "#{app_path}/config/environment"
ensure
ENV.delete "RAILS_ENV"
end
end
end