rails-flog 1.5.0 → 1.6.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,7 +1,9 @@
1
- require "active_record"
2
- require "test_helper"
1
+ # frozen_string_literal: true
3
2
 
4
- 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:')
5
7
 
6
8
  ActiveRecord::Schema.define version: 0 do
7
9
  create_table :books, force: true do |t|
@@ -12,7 +14,7 @@ end
12
14
 
13
15
  class Book < ActiveRecord::Base; end
14
16
 
15
- class SqlFormattableTest < ActiveSupport::TestCase
17
+ module SqlFormattableTestHelper
16
18
  def setup
17
19
  # default configuration
18
20
  Flog.configure do |config|
@@ -29,36 +31,68 @@ class SqlFormattableTest < ActiveSupport::TestCase
29
31
  ActiveRecord::Base.logger = @old_logger
30
32
  end
31
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
+
32
66
  def test_sql_is_formatted
33
- Book.where(category: "comics").to_a
67
+ Book.where(category: 'comics').to_a
34
68
  assert_logger do |logger|
35
- assert_equal %{\tSELECT} , logger.debugs[1]
36
- assert_equal %{\t\t"books" . *}, logger.debugs[2]
37
- assert_equal %{\tFROM} , logger.debugs[3]
38
- assert_equal %{\t\t"books"} , logger.debugs[4]
39
- assert_equal %{\tWHERE} , logger.debugs[5]
40
- assert logger.debugs[6].start_with?(%{\t\t"books" . "category" = })
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" = ))
41
75
  end
42
76
  end
43
77
 
44
78
  def test_colorized_on_colorize_loggin_is_true
45
79
  ActiveSupport::LogSubscriber.colorize_logging = true
46
- Book.where(category: "comics").to_a
80
+ Book.where(category: 'comics').to_a
47
81
  assert_logger do |logger|
48
- assert match_color_seq(logger.debugs.join())
82
+ assert match_color_seq(logger.debugs.join)
49
83
  end
50
84
  end
51
85
 
52
86
  def test_not_colorized_on_colorize_loggin_is_false
53
- Book.where(category: "comics").to_a
87
+ Book.where(category: 'comics').to_a
54
88
  assert_logger do |logger|
55
- assert_nil match_color_seq(logger.debugs.join())
89
+ assert_nil match_color_seq(logger.debugs.join)
56
90
  end
57
91
  end
58
92
 
59
93
  def test_sql_is_not_formatted_when_enabled_is_false
60
94
  Flog::Status.stub(:enabled?, false) do
61
- Book.where(category: "comics").to_a
95
+ Book.where(category: 'comics').to_a
62
96
  assert_logger do |logger|
63
97
  assert_one_line_sql logger.debugs.first
64
98
  end
@@ -67,7 +101,7 @@ class SqlFormattableTest < ActiveSupport::TestCase
67
101
 
68
102
  def test_sql_is_not_formatted_when_sql_formattable_is_false
69
103
  Flog::Status.stub(:sql_formattable?, false) do
70
- Book.where(category: "comics").to_a
104
+ Book.where(category: 'comics').to_a
71
105
  assert_logger do |logger|
72
106
  assert_one_line_sql logger.debugs.first
73
107
  end
@@ -75,120 +109,111 @@ class SqlFormattableTest < ActiveSupport::TestCase
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
114
  logger.debugs.each do |log|
84
- assert_one_line_sql log if log.include?("CACHE")
115
+ assert_one_line_sql log if log.include?('CACHE')
85
116
  end
86
117
  end
87
118
  end
88
119
 
89
120
  def test_sql_is_formatted_on_cached_query_when_ignore_cached_query_configration_is_false
90
- Flog.configure do |config|
91
- config.ignore_cached_query = false
92
- end
93
- Book.cache do
94
- Book.where(category: "comics").to_a
95
- 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
96
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
97
133
  assert_logger do |logger|
98
134
  logger.debugs.each do |log|
99
- assert_equal log.include?("SELECT"), false if log.include?("CACHE")
135
+ assert_one_line_sql log if log.include?('CACHE')
100
136
  end
101
137
  end
102
138
  end
103
139
 
104
- def test_sql_is_not_formatted_when_duration_is_under_threshold
105
- Flog.configure do |config|
106
- 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
107
145
  end
108
- 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
109
151
  assert_logger do |logger|
110
152
  assert_one_line_sql logger.debugs.first
111
153
  end
112
154
  end
113
155
 
114
156
  def test_2space_indent
115
- Flog.configure do |config|
116
- config.sql_indent = " "
117
- end
118
- Book.where(category: "comics").to_a
157
+ configure(sql_indent: ' ')
158
+ Book.where(category: 'comics').to_a
119
159
  assert_logger do |logger|
120
- assert_equal %{ SELECT} , logger.debugs[1]
121
- assert_equal %{ "books" . *}, logger.debugs[2]
122
- assert_equal %{ FROM} , logger.debugs[3]
123
- assert_equal %{ "books"} , logger.debugs[4]
124
- assert_equal %{ WHERE} , logger.debugs[5]
125
- assert logger.debugs[6].start_with?(%{ "books" . "category" = })
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" = ))
126
166
  end
127
167
  end
168
+ end
169
+
170
+ class SqlFormattableInValuesTest < ActiveSupport::TestCase
171
+ include SqlFormattableTestHelper
128
172
 
129
173
  def test_default_in_values_num
130
174
  Book.where(id: (1..10).to_a).to_a
131
175
  assert_logger do |logger|
132
- assert_equal %{\tSELECT} , logger.debugs[1]
133
- assert_equal %{\t\t"books" . *} , logger.debugs[2]
134
- assert_equal %{\tFROM} , logger.debugs[3]
135
- assert_equal %{\t\t"books"} , logger.debugs[4]
136
- assert_equal %{\tWHERE} , logger.debugs[5]
137
- assert_equal %{\t\t"books" . "id" IN (}, logger.debugs[6]
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]
138
182
  (8..16).each do |l|
139
- assert_equal 1, logger.debugs[l].count(",")
183
+ assert_equal 1, logger.debugs[l].count(',')
140
184
  end
141
185
  assert logger.debugs[17].start_with?(%{\t\t)})
142
186
  end
143
187
  end
144
188
 
145
189
  def test_in_values_num_set
146
- Flog.configure do |config|
147
- config.sql_in_values_num = 5
148
- end
190
+ configure(sql_in_values_num: 5)
149
191
  Book.where(id: (1..10).to_a).to_a
150
192
  assert_logger do |logger|
151
- assert_equal %{\tSELECT} , logger.debugs[1]
152
- assert_equal %{\t\t"books" . *} , logger.debugs[2]
153
- assert_equal %{\tFROM} , logger.debugs[3]
154
- assert_equal %{\t\t"books"} , logger.debugs[4]
155
- assert_equal %{\tWHERE} , logger.debugs[5]
156
- assert_equal %{\t\t"books" . "id" IN (}, logger.debugs[6]
157
- assert_equal 4, logger.debugs[7].count(",")
158
- assert_equal 5, logger.debugs[8].count(",")
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(',')
159
201
  assert logger.debugs[9].start_with?(%{\t\t)})
160
202
  end
161
203
  end
162
204
 
163
205
  def test_oneline_in_values
164
- Flog.configure do |config|
165
- config.sql_in_values_num = Flog::ONELINE_IN_VALUES_NUM
166
- end
206
+ configure(sql_in_values_num: Flog::ONELINE_IN_VALUES_NUM)
167
207
  Book.where(id: (1..10).to_a).to_a
168
208
  assert_logger do |logger|
169
- assert_equal %{\tSELECT} , logger.debugs[1]
170
- assert_equal %{\t\t"books" . *} , logger.debugs[2]
171
- assert_equal %{\tFROM} , logger.debugs[3]
172
- assert_equal %{\t\t"books"} , logger.debugs[4]
173
- assert_equal %{\tWHERE} , logger.debugs[5]
174
- assert_equal %{\t\t"books" . "id" IN (}, logger.debugs[6]
175
- assert_equal 9, logger.debugs[7].count(",")
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(',')
176
216
  assert logger.debugs[8].start_with?(%{\t\t)})
177
217
  end
178
218
  end
179
-
180
- private
181
- def assert_logger(&block)
182
- if ActiveRecord::Base.logger.errors.present?
183
- fail ActiveRecord::Base.logger.errors.first
184
- else
185
- block.call(ActiveRecord::Base.logger)
186
- end
187
- end
188
-
189
- def assert_one_line_sql(sql)
190
- assert sql.include?("SELECT")
191
- assert sql.include?("FROM")
192
- assert sql.include?("WHERE")
193
- end
194
219
  end
@@ -1,9 +1,47 @@
1
- require "rails"
2
- 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
3
39
 
4
40
  class StatusTest < ActiveSupport::TestCase
41
+ include StatusTestHelper
42
+
5
43
  def setup
6
- @test_root = Pathname.new(File.expand_path(File.dirname(__FILE__) + "../../"))
44
+ @test_root = Pathname.new(File.expand_path(File.dirname(__FILE__) + '../../'))
7
45
  end
8
46
 
9
47
  def teardown
@@ -108,37 +146,4 @@ class StatusTest < ActiveSupport::TestCase
108
146
  assert Flog::Status.params_formattable?
109
147
  end
110
148
  end
111
-
112
- private
113
- def create_switch_file
114
- create_file(@test_root.join("tmp", Flog::Status::SWITCH_FILE_NAME))
115
- end
116
-
117
- def delete_switch_file
118
- delete_file(@test_root.join("tmp", Flog::Status::SWITCH_FILE_NAME))
119
- end
120
-
121
- def create_sql_switch_file
122
- create_file(@test_root.join("tmp", Flog::Status::SQL_SWITCH_FILE_NAME))
123
- end
124
-
125
- def delete_sql_switch_file
126
- delete_file(@test_root.join("tmp", Flog::Status::SQL_SWITCH_FILE_NAME))
127
- end
128
-
129
- def create_params_switch_file
130
- create_file(@test_root.join("tmp", Flog::Status::PARAMS_SWITCH_FILE_NAME))
131
- end
132
-
133
- def delete_params_switch_file
134
- delete_file(@test_root.join("tmp", Flog::Status::PARAMS_SWITCH_FILE_NAME))
135
- end
136
-
137
- def create_file(file_path)
138
- File.open(file_path, "w").close
139
- end
140
-
141
- def delete_file(file_path)
142
- File.delete(file_path) if File.exist?(file_path)
143
- end
144
149
  end
metadata CHANGED
@@ -1,31 +1,31 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails-flog
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.5.0
4
+ version: 1.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - pinzolo
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-12-19 00:00:00.000000000 Z
11
+ date: 2019-07-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: '1.3'
19
+ version: '0'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - "~>"
24
+ - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: '1.3'
26
+ version: '0'
27
27
  - !ruby/object:Gem::Dependency
28
- name: rake
28
+ name: coveralls
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - ">="
@@ -39,7 +39,7 @@ dependencies:
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
41
  - !ruby/object:Gem::Dependency
42
- name: sqlite3
42
+ name: minitest
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
45
  - - ">="
@@ -53,7 +53,7 @@ dependencies:
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
55
  - !ruby/object:Gem::Dependency
56
- name: minitest
56
+ name: rake
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
59
  - - ">="
@@ -67,7 +67,7 @@ dependencies:
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
69
  - !ruby/object:Gem::Dependency
70
- name: coveralls
70
+ name: rubocop
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
73
  - - ">="
@@ -81,19 +81,19 @@ dependencies:
81
81
  - !ruby/object:Gem::Version
82
82
  version: '0'
83
83
  - !ruby/object:Gem::Dependency
84
- name: rails
84
+ name: sqlite3
85
85
  requirement: !ruby/object:Gem::Requirement
86
86
  requirements:
87
- - - ">="
87
+ - - "~>"
88
88
  - !ruby/object:Gem::Version
89
- version: 4.2.0
90
- type: :runtime
89
+ version: 1.3.0
90
+ type: :development
91
91
  prerelease: false
92
92
  version_requirements: !ruby/object:Gem::Requirement
93
93
  requirements:
94
- - - ">="
94
+ - - "~>"
95
95
  - !ruby/object:Gem::Version
96
- version: 4.2.0
96
+ version: 1.3.0
97
97
  - !ruby/object:Gem::Dependency
98
98
  name: anbt-sql-formatter
99
99
  requirement: !ruby/object:Gem::Requirement
@@ -122,6 +122,20 @@ dependencies:
122
122
  - - ">="
123
123
  - !ruby/object:Gem::Version
124
124
  version: '0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: rails
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: 4.2.0
132
+ type: :runtime
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: 4.2.0
125
139
  description: This formats parameters and sql in rails log.
126
140
  email:
127
141
  - pinzolo@gmail.com
@@ -131,6 +145,7 @@ extra_rdoc_files: []
131
145
  files:
132
146
  - ".coveralls.yml"
133
147
  - ".gitignore"
148
+ - ".rubocop.yml"
134
149
  - ".travis.yml"
135
150
  - Gemfile
136
151
  - LICENSE.txt
@@ -173,8 +188,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
173
188
  - !ruby/object:Gem::Version
174
189
  version: '0'
175
190
  requirements: []
176
- rubyforge_project:
177
- rubygems_version: 2.7.6
191
+ rubygems_version: 3.0.1
178
192
  signing_key:
179
193
  specification_version: 4
180
194
  summary: Rails log formatter for parameters and sql