From 5dcdac55a4fd9166e705fe03e18d6943a4ed1f59 Mon Sep 17 00:00:00 2001 From: kvokka Date: Thu, 29 Mar 2018 05:26:55 +0100 Subject: [PATCH] Add test converage --- test/pp_sql_rails_test.rb | 36 ++++++++++++++++++++++ test/pp_sql_test.rb | 22 +++++++++++++ test/pp_to_sql_beautify_refinement_test.rb | 16 ++++++++++ test/test_helper.rb | 18 +++++++++++ 4 files changed, 92 insertions(+) create mode 100644 test/pp_sql_rails_test.rb create mode 100644 test/pp_sql_test.rb create mode 100644 test/pp_to_sql_beautify_refinement_test.rb create mode 100644 test/test_helper.rb diff --git a/test/pp_sql_rails_test.rb b/test/pp_sql_rails_test.rb new file mode 100644 index 0000000..f8a9854 --- /dev/null +++ b/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 diff --git a/test/pp_sql_test.rb b/test/pp_sql_test.rb new file mode 100644 index 0000000..4c9f656 --- /dev/null +++ b/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 diff --git a/test/pp_to_sql_beautify_refinement_test.rb b/test/pp_to_sql_beautify_refinement_test.rb new file mode 100644 index 0000000..817c27e --- /dev/null +++ b/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 diff --git a/test/test_helper.rb b/test/test_helper.rb new file mode 100644 index 0000000..845ebbe --- /dev/null +++ b/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