rails/railties/test/commands/dbconsole_test.rb

161 lines
4.7 KiB
Ruby
Raw Normal View History

2012-04-27 07:28:53 +00:00
require 'abstract_unit'
require 'rails/commands/dbconsole'
class Rails::DBConsoleTest < ActiveSupport::TestCase
def teardown
2012-05-04 14:40:32 +00:00
%w[PGUSER PGHOST PGPORT PGPASSWORD].each{|key| ENV.delete(key)}
2012-04-27 07:28:53 +00:00
end
2012-05-04 14:40:32 +00:00
def test_config
Rails::DBConsole.const_set(:APP_PATH, "erb")
app_config({})
capture_abort { Rails::DBConsole.config }
2012-04-27 07:28:53 +00:00
assert aborted
assert_match /No database is configured for the environment '\w+'/, output
2012-05-04 14:40:32 +00:00
2012-05-07 04:38:34 +00:00
app_config(test: "with_init")
2012-05-04 14:40:32 +00:00
assert_equal Rails::DBConsole.config, "with_init"
2012-05-07 04:38:34 +00:00
app_db_file("test:\n without_init")
2012-05-04 14:40:32 +00:00
assert_equal Rails::DBConsole.config, "without_init"
2012-05-07 04:38:34 +00:00
app_db_file("test:\n <%= Rails.something_app_specific %>")
2012-05-04 14:40:32 +00:00
assert_equal Rails::DBConsole.config, "with_init"
2012-05-07 04:38:34 +00:00
app_db_file("test:\n\ninvalid")
2012-05-04 14:40:32 +00:00
assert_equal Rails::DBConsole.config, "with_init"
end
def test_env
2012-05-07 04:38:34 +00:00
assert_equal Rails::DBConsole.env, "test"
2012-05-04 14:40:32 +00:00
Rails.stubs(:respond_to?).with(:env).returns(false)
2012-05-07 04:38:34 +00:00
assert_equal Rails::DBConsole.env, "test"
2012-05-04 14:40:32 +00:00
2012-05-07 04:38:34 +00:00
ENV['RAILS_ENV'] = nil
2012-05-04 14:40:32 +00:00
ENV['RACK_ENV'] = "rack_env"
assert_equal Rails::DBConsole.env, "rack_env"
ENV['RAILS_ENV'] = "rails_env"
assert_equal Rails::DBConsole.env, "rails_env"
2012-05-07 04:38:34 +00:00
ensure
ENV['RAILS_ENV'] = "test"
2012-04-27 07:28:53 +00:00
end
def test_mysql
dbconsole.expects(:find_cmd_and_exec).with(%w[mysql mysql5], 'db')
2012-05-04 14:40:32 +00:00
start(adapter: 'mysql', database: 'db')
2012-04-27 07:28:53 +00:00
assert !aborted
end
def test_mysql_full
dbconsole.expects(:find_cmd_and_exec).with(%w[mysql mysql5], '--host=locahost', '--port=1234', '--socket=socket', '--user=user', '--default-character-set=UTF-8', '-p', 'db')
2012-05-04 14:40:32 +00:00
start(adapter: 'mysql', database: 'db', host: 'locahost', port: 1234, socket: 'socket', username: 'user', password: 'qwerty', encoding: 'UTF-8')
2012-04-27 07:28:53 +00:00
assert !aborted
end
def test_mysql_include_password
dbconsole.expects(:find_cmd_and_exec).with(%w[mysql mysql5], '--user=user', '--password=qwerty', 'db')
2012-05-04 14:40:32 +00:00
start({adapter: 'mysql', database: 'db', username: 'user', password: 'qwerty'}, ['-p'])
2012-04-27 07:28:53 +00:00
assert !aborted
end
def test_postgresql
dbconsole.expects(:find_cmd_and_exec).with('psql', 'db')
2012-05-04 14:40:32 +00:00
start(adapter: 'postgresql', database: 'db')
2012-04-27 07:28:53 +00:00
assert !aborted
end
def test_postgresql_full
dbconsole.expects(:find_cmd_and_exec).with('psql', 'db')
2012-05-04 14:40:32 +00:00
start(adapter: 'postgresql', database: 'db', username: 'user', password: 'q1w2e3', host: 'host', port: 5432)
2012-04-27 07:28:53 +00:00
assert !aborted
assert_equal 'user', ENV['PGUSER']
assert_equal 'host', ENV['PGHOST']
assert_equal '5432', ENV['PGPORT']
assert_not_equal 'q1w2e3', ENV['PGPASSWORD']
end
def test_postgresql_include_password
dbconsole.expects(:find_cmd_and_exec).with('psql', 'db')
2012-05-04 14:40:32 +00:00
start({adapter: 'postgresql', database: 'db', username: 'user', password: 'q1w2e3'}, ['-p'])
2012-04-27 07:28:53 +00:00
assert !aborted
assert_equal 'user', ENV['PGUSER']
assert_equal 'q1w2e3', ENV['PGPASSWORD']
end
def test_sqlite
dbconsole.expects(:find_cmd_and_exec).with('sqlite', 'db')
2012-05-04 14:40:32 +00:00
start(adapter: 'sqlite', database: 'db')
2012-04-27 07:28:53 +00:00
assert !aborted
end
def test_sqlite3
dbconsole.expects(:find_cmd_and_exec).with('sqlite3', 'db')
2012-05-04 14:40:32 +00:00
start(adapter: 'sqlite3', database: 'db')
2012-04-27 07:28:53 +00:00
assert !aborted
end
def test_sqlite3_mode
dbconsole.expects(:find_cmd_and_exec).with('sqlite3', '-html', 'db')
2012-05-04 14:40:32 +00:00
start({adapter: 'sqlite3', database: 'db'}, ['--mode', 'html'])
2012-04-27 07:28:53 +00:00
assert !aborted
end
def test_sqlite3_header
dbconsole.expects(:find_cmd_and_exec).with('sqlite3', '-header', 'db')
2012-05-04 14:40:32 +00:00
start({adapter: 'sqlite3', database: 'db'}, ['--header'])
2012-04-27 07:28:53 +00:00
assert !aborted
end
def test_oracle
dbconsole.expects(:find_cmd_and_exec).with('sqlplus', 'user@db')
2012-05-04 14:40:32 +00:00
start(adapter: 'oracle', database: 'db', username: 'user', password: 'secret')
2012-04-27 07:28:53 +00:00
assert !aborted
end
def test_oracle_include_password
dbconsole.expects(:find_cmd_and_exec).with('sqlplus', 'user/secret@db')
2012-05-04 14:40:32 +00:00
start({adapter: 'oracle', database: 'db', username: 'user', password: 'secret'}, ['-p'])
2012-04-27 07:28:53 +00:00
assert !aborted
end
def test_unknown_command_line_client
2012-05-04 14:40:32 +00:00
start(adapter: 'unknown', database: 'db')
2012-04-27 07:28:53 +00:00
assert aborted
assert_match /Unknown command-line client for db/, output
end
private
attr_reader :aborted, :output
def dbconsole
2012-05-04 14:40:32 +00:00
@dbconsole ||= Rails::DBConsole.new(nil)
2012-04-27 07:28:53 +00:00
end
2012-05-04 14:40:32 +00:00
def start(config = {}, argv = [])
dbconsole.stubs(config: config.stringify_keys, arguments: argv)
capture_abort { dbconsole.start }
end
2012-04-27 07:28:53 +00:00
2012-05-04 14:40:32 +00:00
def capture_abort
2012-04-27 07:28:53 +00:00
@aborted = false
2012-05-07 04:38:34 +00:00
@output = capture(:stderr) do
2012-04-27 07:28:53 +00:00
begin
2012-05-04 14:40:32 +00:00
yield
2012-04-27 07:28:53 +00:00
rescue SystemExit
@aborted = true
end
end
end
2012-05-04 14:40:32 +00:00
def app_db_file(result)
IO.stubs(:read).with("config/database.yml").returns(result)
end
def app_config(result)
Rails.application.config.stubs(:database_configuration).returns(result.stringify_keys)
2012-04-27 07:28:53 +00:00
end
end