Skip to content

Commit

Permalink
Merge pull request #25 from jfouse/master
Browse files Browse the repository at this point in the history
Fix pp_sql frozen string handling
  • Loading branch information
kvokka authored Dec 22, 2024
2 parents 44a4b15 + f791e8f commit 978673b
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
2 changes: 1 addition & 1 deletion lib/pp_sql.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def pp_sql
puts to_sql
else
extend Formatter
puts _sql_formatter.format(to_sql.to_s)
puts _sql_formatter.format(to_sql.dup)
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion pp_sql.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Gem::Specification.new do |s|
s.required_ruby_version = '>= 3.1.0'

s.add_dependency 'activerecord'
s.add_dependency 'anbt-sql-formatter', '~> 0.1.0', '~> 0.1.0'
s.add_dependency 'anbt-sql-formatter', '~> 0.1.0'

s.add_development_dependency 'minitest'
s.add_development_dependency 'minitest-focus'
Expand Down
30 changes: 28 additions & 2 deletions test/pp_sql_activerecord_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ class User < ActiveRecord::Base; end
describe PpSql do
after { clear_logs! && set_default_config! }

it 'ActiveRecord with formatted output' do
it 'ActiveRecord logs with formatted output' do
User.create
assert(LOGGER.string.lines.detect { |line| line =~ /INTO\n/ })
clear_logs!
User.first
assert_equal LOGGER.string.lines.count, 6
end

it 'ActiveRecord with default output' do
it 'ActiveRecord logs with default output' do
PpSql.add_rails_logger_formatting = false
User.create
clear_logs!
Expand All @@ -46,6 +46,21 @@ class User < ActiveRecord::Base; end
assert_equal User.all.to_sql, "SELECT\n \"users\" . *\n FROM\n \"users\""
end

it 'to_sql with default output' do
PpSql.rewrite_to_sql_method = false
User.create
assert_equal User.all.to_sql.lines.count, 1
end

it 'to_sql with default output but formatted logs' do
PpSql.rewrite_to_sql_method = false
User.create
assert_equal User.all.to_sql.lines.count, 1
clear_logs!
User.first
assert_equal LOGGER.string.lines.count, 6
end

it 'pp_sql formats a relation properly' do
User.create
out, = capture_io do
Expand All @@ -54,13 +69,24 @@ class User < ActiveRecord::Base; end
assert_equal out, "SELECT\n \"users\" . *\n FROM\n \"users\"\n"
end

it 'pp_sql formats frozen strings properly' do
PpSql.rewrite_to_sql_method = false
User.create
frozen_clause = 'id > 0'
out, = capture_io do
User.all.where(frozen_clause).pp_sql
end
assert_equal out.lines.count, 8
end

private

def clear_logs!
LOGGER.string.clear
end

def set_default_config!
PpSql.rewrite_to_sql_method = true
PpSql.add_rails_logger_formatting = true
end
end

0 comments on commit 978673b

Please sign in to comment.