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.
@@ -1,8 +1,9 @@
1
- # coding: utf-8
2
- require "active_record"
3
- require "test_helper"
1
+ # frozen_string_literal: true
4
2
 
5
- ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:")
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
- class SqlFormattableTest < ActiveSupport::TestCase
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: "comics").to_a
67
+ Book.where(category: 'comics').to_a
35
68
  assert_logger do |logger|
36
- logs = logger.debugs.map { |log| log.gsub("\t", " ") }
37
- assert_equal %{ SELECT} , logs[1]
38
- assert_equal %{ "books" . *} , logs[2]
39
- assert_equal %{ FROM} , logs[3]
40
- assert_equal %{ "books"} , logs[4]
41
- assert_equal %{ WHERE} , logs[5]
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: "comics").to_a
80
+ Book.where(category: 'comics').to_a
49
81
  assert_logger do |logger|
50
- assert /\e\[(\d+;)*\d+m/.match(logger.debugs.join())
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: "comics").to_a
87
+ Book.where(category: 'comics').to_a
56
88
  assert_logger do |logger|
57
- assert_nil /\e\[(\d+;)*\d+m/.match(logger.debugs.join())
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.stubs(:enabled?).returns(false)
63
- Book.where(category: "comics").to_a
64
- assert_logger do |logger|
65
- assert_one_line_sql logger.debugs.first
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.stubs(:sql_formattable?).returns(false)
71
- Book.where(category: "comics").to_a
72
- assert_logger do |logger|
73
- assert_one_line_sql logger.debugs.first
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
- Book.cache do
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
- logs = logger.debugs.map { |log| log.gsub("\t", " ") }
84
- logs.each do |log|
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
- Flog.configure do |config|
92
- config.ignore_cached_query = false
93
- end
94
- Book.cache do
95
- Book.where(category: "comics").to_a
96
- Book.where(category: "comics").to_a
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
- logs = logger.debugs.map { |log| log.gsub("\t", " ") }
100
- logs.each do |log|
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 test_sql_is_not_formatted_when_duration_is_under_threshold
107
- Flog.configure do |config|
108
- config.query_duration_threshold = 100.0
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
- Book.where(category: "comics").to_a
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
- private
117
- def assert_logger(&block)
118
- if ActiveRecord::Base.logger.errors.present?
119
- fail ActiveRecord::Base.logger.errors.first
120
- else
121
- block.call(ActiveRecord::Base.logger)
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
- def assert_one_line_sql(sql)
126
- assert sql.include?("SELECT")
127
- assert sql.include?("FROM")
128
- assert sql.include?("WHERE")
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
@@ -1,10 +1,47 @@
1
- # coding: utf-8
2
- require "rails"
3
- require "test_helper"
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.expects(:root).at_most(2).returns(@test_root)
23
- create_switch_file
24
- assert_equal false, Flog::Status.enabled?
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.expects(:root).returns(nil) # For raise NoMethodError
29
- create_switch_file
30
- assert Flog::Status.enabled?
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.expects(:root).at_most(2).returns(@test_root)
35
- delete_switch_file
36
- delete_sql_switch_file
37
- assert Flog::Status.sql_formattable?
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.expects(:root).at_most(2).returns(@test_root)
42
- delete_switch_file
43
- create_sql_switch_file
44
- assert_equal false, Flog::Status.sql_formattable?
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.expects(:root).at_most(2).returns(@test_root)
49
- create_switch_file
50
- delete_sql_switch_file
51
- assert_equal false, Flog::Status.sql_formattable?
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.expects(:root).at_most(2).returns(nil) # For raise NoMethodError
56
- create_sql_switch_file
57
- assert Flog::Status.sql_formattable?
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.expects(:root).at_most(2).returns(@test_root)
62
- create_switch_file
63
- create_sql_switch_file
64
- assert_equal false, Flog::Status.sql_formattable?
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.expects(:root).at_most(2).returns(@test_root)
69
- delete_switch_file
70
- delete_params_switch_file
71
- assert Flog::Status.params_formattable?
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.expects(:root).at_most(2).returns(@test_root)
76
- delete_switch_file
77
- create_params_switch_file
78
- assert_equal false, Flog::Status.params_formattable?
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.expects(:root).at_most(2).returns(@test_root)
83
- create_switch_file
84
- delete_params_switch_file
85
- assert_equal false, Flog::Status.params_formattable?
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.expects(:root).at_most(2).returns(@test_root)
90
- create_switch_file
91
- create_params_switch_file
92
- assert_equal false, Flog::Status.params_formattable?
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.expects(:root).at_most(2).returns(nil) # For raise NoMethodError
97
- create_params_switch_file
98
- assert Flog::Status.params_formattable?
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