Skip to content

Commit

Permalink
Add test converage
Browse files Browse the repository at this point in the history
  • Loading branch information
kvokka committed Mar 29, 2018
1 parent 2f444d6 commit 5dcdac5
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 0 deletions.
36 changes: 36 additions & 0 deletions test/pp_sql_rails_test.rb
@@ -0,0 +1,36 @@
# frozen_string_literal: true

require 'test_helper'

TeatApp::Application.initialize!

require 'active_record'
require 'sqlite3'

ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')

ActiveRecord::Base.connection.create_table(:users) do |t|
t.string :name
end

ActiveRecord::Base.logger = Logger.new(LOGGER = StringIO.new)

class User < ActiveRecord::Base; end

describe PpSql do
after { clear_logs! }

it 'load with right 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

private

def clear_logs!
LOGGER.string.clear
end
end
22 changes: 22 additions & 0 deletions test/pp_sql_test.rb
@@ -0,0 +1,22 @@
# frozen_string_literal: true

require 'test_helper'

describe PpSql do
class SubString < String
include PpSql::ToSqlBeautify
end

let(:str) { SubString.new 'SELECT COUNT(*) FROM "users"' }

after { PpSql.rewrite_to_sql_method = true }

it 'will parse provided sql' do
assert_equal str.to_sql.lines.count, 4
end

it 'throw string as is' do
PpSql.rewrite_to_sql_method = false
assert_equal str.to_sql.lines.count, 1
end
end
16 changes: 16 additions & 0 deletions test/pp_to_sql_beautify_refinement_test.rb
@@ -0,0 +1,16 @@

# frozen_string_literal: false

require 'test_helper'

using PpSql::ToSqlBeautifyRefinement

module PpSql
describe ToSqlBeautifyRefinement do
let(:str) { 'SELECT COUNT(*) FROM "users"' }

it 'will parse provided sql' do
assert_equal str.to_sql.lines.count, 4
end
end
end
18 changes: 18 additions & 0 deletions test/test_helper.rb
@@ -0,0 +1,18 @@
# frozen_string_literal: true

require 'minitest/autorun'
require 'minitest/focus'
require 'rails'
require 'pp_sql'

require 'minitest/reporters'
Minitest::Reporters.use!

ENV['RAILS_ENV'] = 'test'
ENV['DATABASE'] = 'sqlite3://localhost/:memory:'

module TeatApp
class Application < Rails::Application
config.eager_load = false
end
end

0 comments on commit 5dcdac5

Please sign in to comment.