rails-flog 1.3.2 → 1.6.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.gitignore +2 -0
- data/.rubocop.yml +25 -0
- data/.travis.yml +25 -6
- data/Gemfile +2 -0
- data/README.md +13 -2
- data/Rakefile +7 -5
- data/gemfiles/rails_4_2_x.gemfile +8 -0
- data/gemfiles/rails_5_0_x.gemfile +8 -0
- data/gemfiles/rails_5_1_x.gemfile +8 -0
- data/gemfiles/rails_5_2_x.gemfile +8 -0
- data/gemfiles/rails_6_0_x.gemfile +8 -0
- data/lib/flog.rb +6 -6
- data/lib/flog/configuration.rb +33 -9
- data/lib/flog/params_formattable.rb +55 -45
- data/lib/flog/payload_value_shuntable.rb +19 -7
- data/lib/flog/sql_formattable.rb +41 -33
- data/lib/flog/status.rb +40 -24
- data/lib/flog/version.rb +3 -2
- data/rails-flog.gemspec +21 -19
- data/test/test_helper.rb +39 -26
- data/test/unit/params_formattable_test.rb +89 -50
- data/test/unit/payload_value_shuntable_test.rb +19 -18
- data/test/unit/sql_formattable_test.rb +144 -55
- data/test/unit/status_test.rb +96 -82
- metadata +57 -41
- data/gemfiles/rails_3_2_x.gemfile +0 -7
- data/gemfiles/rails_4_0_x.gemfile +0 -7
- data/gemfiles/rails_4_1_x.gemfile +0 -8
@@ -1,8 +1,9 @@
|
|
1
|
-
#
|
2
|
-
require "active_record"
|
3
|
-
require "test_helper"
|
1
|
+
# frozen_string_literal: true
|
4
2
|
|
5
|
-
|
3
|
+
require 'active_record'
|
4
|
+
require 'test_helper'
|
5
|
+
|
6
|
+
ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')
|
6
7
|
|
7
8
|
ActiveRecord::Schema.define version: 0 do
|
8
9
|
create_table :books, force: true do |t|
|
@@ -13,7 +14,7 @@ end
|
|
13
14
|
|
14
15
|
class Book < ActiveRecord::Base; end
|
15
16
|
|
16
|
-
|
17
|
+
module SqlFormattableTestHelper
|
17
18
|
def setup
|
18
19
|
# default configuration
|
19
20
|
Flog.configure do |config|
|
@@ -30,101 +31,189 @@ class SqlFormattableTest < ActiveSupport::TestCase
|
|
30
31
|
ActiveRecord::Base.logger = @old_logger
|
31
32
|
end
|
32
33
|
|
34
|
+
def configure(pairs)
|
35
|
+
Flog.configure do |config|
|
36
|
+
pairs.each do |key, value|
|
37
|
+
meth = "#{key}="
|
38
|
+
config.send(meth, value) if config.respond_to?(meth)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
def prepare_for_query_cache
|
44
|
+
Book.cache do
|
45
|
+
Book.where(category: 'comics').to_a
|
46
|
+
Book.where(category: 'comics').to_a
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
def assert_logger(&block)
|
51
|
+
raise ActiveRecord::Base.logger.errors.first if ActiveRecord::Base.logger.errors.present?
|
52
|
+
|
53
|
+
block.call(ActiveRecord::Base.logger)
|
54
|
+
end
|
55
|
+
|
56
|
+
def assert_one_line_sql(sql)
|
57
|
+
assert sql.include?('SELECT')
|
58
|
+
assert sql.include?('FROM')
|
59
|
+
assert sql.include?('WHERE')
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
class SqlFormattableTest < ActiveSupport::TestCase
|
64
|
+
include SqlFormattableTestHelper
|
65
|
+
|
33
66
|
def test_sql_is_formatted
|
34
|
-
Book.where(category:
|
67
|
+
Book.where(category: 'comics').to_a
|
35
68
|
assert_logger do |logger|
|
36
|
-
|
37
|
-
assert_equal %
|
38
|
-
assert_equal %
|
39
|
-
assert_equal %
|
40
|
-
assert_equal %
|
41
|
-
|
42
|
-
assert_equal %{ "books" . "category" = 'comics'}, logs[6]
|
69
|
+
assert_equal %(\tSELECT) , logger.debugs[1]
|
70
|
+
assert_equal %(\t\t"books" . *), logger.debugs[2]
|
71
|
+
assert_equal %(\tFROM) , logger.debugs[3]
|
72
|
+
assert_equal %(\t\t"books") , logger.debugs[4]
|
73
|
+
assert_equal %(\tWHERE) , logger.debugs[5]
|
74
|
+
assert logger.debugs[6].start_with?(%(\t\t"books"."category" = ))
|
43
75
|
end
|
44
76
|
end
|
45
77
|
|
46
78
|
def test_colorized_on_colorize_loggin_is_true
|
47
79
|
ActiveSupport::LogSubscriber.colorize_logging = true
|
48
|
-
Book.where(category:
|
80
|
+
Book.where(category: 'comics').to_a
|
49
81
|
assert_logger do |logger|
|
50
|
-
assert
|
82
|
+
assert match_color_seq(logger.debugs.join)
|
51
83
|
end
|
52
84
|
end
|
53
85
|
|
54
86
|
def test_not_colorized_on_colorize_loggin_is_false
|
55
|
-
Book.where(category:
|
87
|
+
Book.where(category: 'comics').to_a
|
56
88
|
assert_logger do |logger|
|
57
|
-
assert_nil
|
89
|
+
assert_nil match_color_seq(logger.debugs.join)
|
58
90
|
end
|
59
91
|
end
|
60
92
|
|
61
93
|
def test_sql_is_not_formatted_when_enabled_is_false
|
62
|
-
Flog::Status.
|
63
|
-
|
64
|
-
|
65
|
-
|
94
|
+
Flog::Status.stub(:enabled?, false) do
|
95
|
+
Book.where(category: 'comics').to_a
|
96
|
+
assert_logger do |logger|
|
97
|
+
assert_one_line_sql logger.debugs.first
|
98
|
+
end
|
66
99
|
end
|
67
100
|
end
|
68
101
|
|
69
102
|
def test_sql_is_not_formatted_when_sql_formattable_is_false
|
70
|
-
Flog::Status.
|
71
|
-
|
72
|
-
|
73
|
-
|
103
|
+
Flog::Status.stub(:sql_formattable?, false) do
|
104
|
+
Book.where(category: 'comics').to_a
|
105
|
+
assert_logger do |logger|
|
106
|
+
assert_one_line_sql logger.debugs.first
|
107
|
+
end
|
74
108
|
end
|
75
109
|
end
|
76
110
|
|
77
111
|
def test_sql_is_not_formatted_on_cached_query
|
78
|
-
|
79
|
-
Book.where(category: "comics").to_a
|
80
|
-
Book.where(category: "comics").to_a
|
81
|
-
end
|
112
|
+
prepare_for_query_cache
|
82
113
|
assert_logger do |logger|
|
83
|
-
|
84
|
-
|
85
|
-
assert_one_line_sql log if log.include?("CACHE")
|
114
|
+
logger.debugs.each do |log|
|
115
|
+
assert_one_line_sql log if log.include?('CACHE')
|
86
116
|
end
|
87
117
|
end
|
88
118
|
end
|
89
119
|
|
90
120
|
def test_sql_is_formatted_on_cached_query_when_ignore_cached_query_configration_is_false
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
121
|
+
configure(ignore_cached_query: false)
|
122
|
+
prepare_for_query_cache
|
123
|
+
assert_logger do |logger|
|
124
|
+
logger.debugs.each do |log|
|
125
|
+
assert_equal log.include?('SELECT'), false if log.include?('CACHE')
|
126
|
+
end
|
97
127
|
end
|
128
|
+
end
|
129
|
+
|
130
|
+
def test_sql_is_not_formatted_on_cached_query_when_ignore_query_configuration_is_true
|
131
|
+
configure(ignore_cached_query: false, ignore_query: true)
|
132
|
+
prepare_for_query_cache
|
98
133
|
assert_logger do |logger|
|
99
|
-
|
100
|
-
|
101
|
-
assert_equal log.include?("SELECT"), false if log.include?("CACHE")
|
134
|
+
logger.debugs.each do |log|
|
135
|
+
assert_one_line_sql log if log.include?('CACHE')
|
102
136
|
end
|
103
137
|
end
|
104
138
|
end
|
105
139
|
|
106
|
-
def
|
107
|
-
|
108
|
-
|
140
|
+
def test_sql_is_not_formatted_when_ignore_query_configuration_is_true
|
141
|
+
configure(ignore_query: true)
|
142
|
+
Book.where(category: 'comics').to_a
|
143
|
+
assert_logger do |logger|
|
144
|
+
assert_one_line_sql logger.debugs.first
|
109
145
|
end
|
110
|
-
|
146
|
+
end
|
147
|
+
|
148
|
+
def test_sql_is_not_formatted_when_duration_is_under_threshold
|
149
|
+
configure(query_duration_threshold: 100.0)
|
150
|
+
Book.where(category: 'comics').to_a
|
111
151
|
assert_logger do |logger|
|
112
152
|
assert_one_line_sql logger.debugs.first
|
113
153
|
end
|
114
154
|
end
|
115
155
|
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
156
|
+
def test_2space_indent
|
157
|
+
configure(sql_indent: ' ')
|
158
|
+
Book.where(category: 'comics').to_a
|
159
|
+
assert_logger do |logger|
|
160
|
+
assert_equal %( SELECT) , logger.debugs[1]
|
161
|
+
assert_equal %( "books" . *), logger.debugs[2]
|
162
|
+
assert_equal %( FROM) , logger.debugs[3]
|
163
|
+
assert_equal %( "books") , logger.debugs[4]
|
164
|
+
assert_equal %( WHERE) , logger.debugs[5]
|
165
|
+
assert logger.debugs[6].start_with?(%( "books"."category" = ))
|
122
166
|
end
|
123
167
|
end
|
168
|
+
end
|
124
169
|
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
170
|
+
class SqlFormattableInValuesTest < ActiveSupport::TestCase
|
171
|
+
include SqlFormattableTestHelper
|
172
|
+
|
173
|
+
def test_default_in_values_num
|
174
|
+
Book.where(id: (1..10).to_a).to_a
|
175
|
+
assert_logger do |logger|
|
176
|
+
assert_equal %(\tSELECT) , logger.debugs[1]
|
177
|
+
assert_equal %(\t\t"books" . *) , logger.debugs[2]
|
178
|
+
assert_equal %(\tFROM) , logger.debugs[3]
|
179
|
+
assert_equal %(\t\t"books") , logger.debugs[4]
|
180
|
+
assert_equal %(\tWHERE) , logger.debugs[5]
|
181
|
+
assert_equal %{\t\t"books"."id" IN (}, logger.debugs[6]
|
182
|
+
(8..16).each do |l|
|
183
|
+
assert_equal 1, logger.debugs[l].count(',')
|
184
|
+
end
|
185
|
+
assert logger.debugs[17].start_with?(%{\t\t)})
|
186
|
+
end
|
187
|
+
end
|
188
|
+
|
189
|
+
def test_in_values_num_set
|
190
|
+
configure(sql_in_values_num: 5)
|
191
|
+
Book.where(id: (1..10).to_a).to_a
|
192
|
+
assert_logger do |logger|
|
193
|
+
assert_equal %(\tSELECT) , logger.debugs[1]
|
194
|
+
assert_equal %(\t\t"books" . *) , logger.debugs[2]
|
195
|
+
assert_equal %(\tFROM) , logger.debugs[3]
|
196
|
+
assert_equal %(\t\t"books") , logger.debugs[4]
|
197
|
+
assert_equal %(\tWHERE) , logger.debugs[5]
|
198
|
+
assert_equal %{\t\t"books"."id" IN (}, logger.debugs[6]
|
199
|
+
assert_equal 4, logger.debugs[7].count(',')
|
200
|
+
assert_equal 5, logger.debugs[8].count(',')
|
201
|
+
assert logger.debugs[9].start_with?(%{\t\t)})
|
202
|
+
end
|
203
|
+
end
|
204
|
+
|
205
|
+
def test_oneline_in_values
|
206
|
+
configure(sql_in_values_num: Flog::ONELINE_IN_VALUES_NUM)
|
207
|
+
Book.where(id: (1..10).to_a).to_a
|
208
|
+
assert_logger do |logger|
|
209
|
+
assert_equal %(\tSELECT) , logger.debugs[1]
|
210
|
+
assert_equal %(\t\t"books" . *) , logger.debugs[2]
|
211
|
+
assert_equal %(\tFROM) , logger.debugs[3]
|
212
|
+
assert_equal %(\t\t"books") , logger.debugs[4]
|
213
|
+
assert_equal %(\tWHERE) , logger.debugs[5]
|
214
|
+
assert_equal %{\t\t"books"."id" IN (}, logger.debugs[6]
|
215
|
+
assert_equal 9, logger.debugs[7].count(',')
|
216
|
+
assert logger.debugs[8].start_with?(%{\t\t)})
|
217
|
+
end
|
129
218
|
end
|
130
219
|
end
|
data/test/unit/status_test.rb
CHANGED
@@ -1,10 +1,47 @@
|
|
1
|
-
#
|
2
|
-
|
3
|
-
require
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'rails'
|
4
|
+
require 'test_helper'
|
5
|
+
|
6
|
+
module StatusTestHelper
|
7
|
+
def create_switch_file
|
8
|
+
create_file(@test_root.join('tmp', Flog::Status::SWITCH_FILE_NAME))
|
9
|
+
end
|
10
|
+
|
11
|
+
def delete_switch_file
|
12
|
+
delete_file(@test_root.join('tmp', Flog::Status::SWITCH_FILE_NAME))
|
13
|
+
end
|
14
|
+
|
15
|
+
def create_sql_switch_file
|
16
|
+
create_file(@test_root.join('tmp', Flog::Status::SQL_SWITCH_FILE_NAME))
|
17
|
+
end
|
18
|
+
|
19
|
+
def delete_sql_switch_file
|
20
|
+
delete_file(@test_root.join('tmp', Flog::Status::SQL_SWITCH_FILE_NAME))
|
21
|
+
end
|
22
|
+
|
23
|
+
def create_params_switch_file
|
24
|
+
create_file(@test_root.join('tmp', Flog::Status::PARAMS_SWITCH_FILE_NAME))
|
25
|
+
end
|
26
|
+
|
27
|
+
def delete_params_switch_file
|
28
|
+
delete_file(@test_root.join('tmp', Flog::Status::PARAMS_SWITCH_FILE_NAME))
|
29
|
+
end
|
30
|
+
|
31
|
+
def create_file(file_path)
|
32
|
+
File.open(file_path, 'w').close
|
33
|
+
end
|
34
|
+
|
35
|
+
def delete_file(file_path)
|
36
|
+
File.delete(file_path) if File.exist?(file_path)
|
37
|
+
end
|
38
|
+
end
|
4
39
|
|
5
40
|
class StatusTest < ActiveSupport::TestCase
|
41
|
+
include StatusTestHelper
|
42
|
+
|
6
43
|
def setup
|
7
|
-
@test_root = Pathname.new(File.expand_path(File.dirname(__FILE__) +
|
44
|
+
@test_root = Pathname.new(File.expand_path(File.dirname(__FILE__) + '../../'))
|
8
45
|
end
|
9
46
|
|
10
47
|
def teardown
|
@@ -19,117 +56,94 @@ class StatusTest < ActiveSupport::TestCase
|
|
19
56
|
end
|
20
57
|
|
21
58
|
def test_enabled_is_false_when_switch_file_exists
|
22
|
-
Rails.
|
23
|
-
|
24
|
-
|
59
|
+
Rails.stub(:root, @test_root) do
|
60
|
+
create_switch_file
|
61
|
+
assert_equal false, Flog::Status.enabled?
|
62
|
+
end
|
25
63
|
end
|
26
64
|
|
27
65
|
def test_enabled_is_true_when_error_is_raised_in_process
|
28
|
-
Rails.
|
29
|
-
|
30
|
-
|
66
|
+
Rails.stub(:root, nil) do # For raise NoMethodError
|
67
|
+
create_switch_file
|
68
|
+
assert Flog::Status.enabled?
|
69
|
+
end
|
31
70
|
end
|
32
71
|
|
33
72
|
def test_sql_formattable_is_true_when_enable_and_sql_switch_file_does_not_exist
|
34
|
-
Rails.
|
35
|
-
|
36
|
-
|
37
|
-
|
73
|
+
Rails.stub(:root, @test_root) do
|
74
|
+
delete_switch_file
|
75
|
+
delete_sql_switch_file
|
76
|
+
assert Flog::Status.sql_formattable?
|
77
|
+
end
|
38
78
|
end
|
39
79
|
|
40
80
|
def test_sql_formattable_is_false_when_enable_and_sql_switch_file_exists
|
41
|
-
Rails.
|
42
|
-
|
43
|
-
|
44
|
-
|
81
|
+
Rails.stub(:root, @test_root) do
|
82
|
+
delete_switch_file
|
83
|
+
create_sql_switch_file
|
84
|
+
assert_equal false, Flog::Status.sql_formattable?
|
85
|
+
end
|
45
86
|
end
|
46
87
|
|
47
88
|
def test_sql_formattable_is_false_when_disable_and_sql_switch_file_not_exist
|
48
|
-
Rails.
|
49
|
-
|
50
|
-
|
51
|
-
|
89
|
+
Rails.stub(:root, @test_root) do
|
90
|
+
create_switch_file
|
91
|
+
delete_sql_switch_file
|
92
|
+
assert_equal false, Flog::Status.sql_formattable?
|
93
|
+
end
|
52
94
|
end
|
53
95
|
|
54
96
|
def test_sql_formattable_is_true_when_error_is_raised_in_process
|
55
|
-
Rails.
|
56
|
-
|
57
|
-
|
97
|
+
Rails.stub(:root, nil) do # For raise NoMethodError
|
98
|
+
create_sql_switch_file
|
99
|
+
assert Flog::Status.sql_formattable?
|
100
|
+
end
|
58
101
|
end
|
59
102
|
|
60
103
|
def test_sql_formattable_is_false_when_disable_and_sql_switch_file_exists
|
61
|
-
Rails.
|
62
|
-
|
63
|
-
|
64
|
-
|
104
|
+
Rails.stub(:root, @test_root) do
|
105
|
+
create_switch_file
|
106
|
+
create_sql_switch_file
|
107
|
+
assert_equal false, Flog::Status.sql_formattable?
|
108
|
+
end
|
65
109
|
end
|
66
110
|
|
67
111
|
def test_params_formattable_is_true_when_enable_and_params_switch_file_does_not_exist
|
68
|
-
Rails.
|
69
|
-
|
70
|
-
|
71
|
-
|
112
|
+
Rails.stub(:root, @test_root) do
|
113
|
+
delete_switch_file
|
114
|
+
delete_params_switch_file
|
115
|
+
assert Flog::Status.params_formattable?
|
116
|
+
end
|
72
117
|
end
|
73
118
|
|
74
119
|
def test_params_formattable_is_false_when_enable_and_params_switch_file_exists
|
75
|
-
Rails.
|
76
|
-
|
77
|
-
|
78
|
-
|
120
|
+
Rails.stub(:root, @test_root) do
|
121
|
+
delete_switch_file
|
122
|
+
create_params_switch_file
|
123
|
+
assert_equal false, Flog::Status.params_formattable?
|
124
|
+
end
|
79
125
|
end
|
80
126
|
|
81
127
|
def test_params_formattable_is_false_when_disable_and_params_switch_file_not_exist
|
82
|
-
Rails.
|
83
|
-
|
84
|
-
|
85
|
-
|
128
|
+
Rails.stub(:root, @test_root) do
|
129
|
+
create_switch_file
|
130
|
+
delete_params_switch_file
|
131
|
+
assert_equal false, Flog::Status.params_formattable?
|
132
|
+
end
|
86
133
|
end
|
87
134
|
|
88
135
|
def test_params_formattable_is_false_when_disable_and_params_switch_file_exists
|
89
|
-
Rails.
|
90
|
-
|
91
|
-
|
92
|
-
|
136
|
+
Rails.stub(:root, @test_root) do
|
137
|
+
create_switch_file
|
138
|
+
create_params_switch_file
|
139
|
+
assert_equal false, Flog::Status.params_formattable?
|
140
|
+
end
|
93
141
|
end
|
94
142
|
|
95
143
|
def test_params_formattable_is_true_when_error_is_raised_in_process
|
96
|
-
Rails.
|
97
|
-
|
98
|
-
|
99
|
-
end
|
100
|
-
|
101
|
-
private
|
102
|
-
def create_switch_file
|
103
|
-
create_file(@test_root.join("tmp", Flog::Status::SWITCH_FILE_NAME))
|
104
|
-
end
|
105
|
-
|
106
|
-
def delete_switch_file
|
107
|
-
delete_file(@test_root.join("tmp", Flog::Status::SWITCH_FILE_NAME))
|
108
|
-
end
|
109
|
-
|
110
|
-
def create_sql_switch_file
|
111
|
-
create_file(@test_root.join("tmp", Flog::Status::SQL_SWITCH_FILE_NAME))
|
112
|
-
end
|
113
|
-
|
114
|
-
def delete_sql_switch_file
|
115
|
-
delete_file(@test_root.join("tmp", Flog::Status::SQL_SWITCH_FILE_NAME))
|
116
|
-
end
|
117
|
-
|
118
|
-
def create_params_switch_file
|
119
|
-
create_file(@test_root.join("tmp", Flog::Status::PARAMS_SWITCH_FILE_NAME))
|
120
|
-
end
|
121
|
-
|
122
|
-
def delete_params_switch_file
|
123
|
-
delete_file(@test_root.join("tmp", Flog::Status::PARAMS_SWITCH_FILE_NAME))
|
124
|
-
end
|
125
|
-
|
126
|
-
def create_file(file_path)
|
127
|
-
File.open(file_path, "w").close
|
128
|
-
end
|
129
|
-
|
130
|
-
def delete_file(file_path)
|
131
|
-
if File.exist?(file_path)
|
132
|
-
File.delete(file_path)
|
144
|
+
Rails.stub(:root, nil) do # For raise NoMethodError
|
145
|
+
create_params_switch_file
|
146
|
+
assert Flog::Status.params_formattable?
|
133
147
|
end
|
134
148
|
end
|
135
149
|
end
|