2017-08-14 13:08:09 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-07-08 21:13:11 -04:00
|
|
|
require "isolation/abstract_unit"
|
2017-07-25 02:01:33 -04:00
|
|
|
require "console_helpers"
|
2017-07-08 21:13:11 -04:00
|
|
|
|
|
|
|
module ApplicationTests
|
|
|
|
class DBConsoleTest < ActiveSupport::TestCase
|
|
|
|
include ActiveSupport::Testing::Isolation
|
2017-07-25 02:01:33 -04:00
|
|
|
include ConsoleHelpers
|
2017-07-08 21:13:11 -04:00
|
|
|
|
|
|
|
def setup
|
2017-07-16 20:11:21 -04:00
|
|
|
skip "PTY unavailable" unless available_pty?
|
|
|
|
|
2017-07-08 21:13:11 -04:00
|
|
|
build_app
|
|
|
|
end
|
|
|
|
|
|
|
|
def teardown
|
|
|
|
teardown_app
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_use_value_defined_in_environment_file_in_database_yml
|
2017-09-14 04:43:56 -04:00
|
|
|
app_file "config/database.yml", <<-YAML
|
|
|
|
development:
|
|
|
|
database: <%= Rails.application.config.database %>
|
|
|
|
adapter: sqlite3
|
|
|
|
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
|
|
|
|
timeout: 5000
|
|
|
|
YAML
|
2017-07-08 21:13:11 -04:00
|
|
|
|
2017-09-14 04:43:56 -04:00
|
|
|
app_file "config/environments/development.rb", <<-RUBY
|
|
|
|
Rails.application.configure do
|
|
|
|
config.database = "db/development.sqlite3"
|
|
|
|
end
|
|
|
|
RUBY
|
2017-07-08 21:13:11 -04:00
|
|
|
|
2018-09-10 16:29:43 -04:00
|
|
|
primary, replica = PTY.open
|
|
|
|
spawn_dbconsole(replica)
|
|
|
|
assert_output("sqlite>", primary)
|
2017-07-08 21:13:11 -04:00
|
|
|
ensure
|
2018-09-10 18:06:30 -04:00
|
|
|
primary.puts ".exit"
|
2017-07-08 21:13:11 -04:00
|
|
|
end
|
|
|
|
|
2017-07-16 20:11:21 -04:00
|
|
|
def test_respect_environment_option
|
2017-09-14 04:43:56 -04:00
|
|
|
app_file "config/database.yml", <<-YAML
|
|
|
|
default: &default
|
|
|
|
adapter: sqlite3
|
|
|
|
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
|
|
|
|
timeout: 5000
|
2017-07-16 20:11:21 -04:00
|
|
|
|
2017-09-14 04:43:56 -04:00
|
|
|
development:
|
|
|
|
<<: *default
|
|
|
|
database: db/development.sqlite3
|
2017-07-16 20:11:21 -04:00
|
|
|
|
2017-09-14 04:43:56 -04:00
|
|
|
production:
|
|
|
|
<<: *default
|
|
|
|
database: db/production.sqlite3
|
|
|
|
YAML
|
2017-07-16 20:11:21 -04:00
|
|
|
|
2018-09-10 16:29:43 -04:00
|
|
|
primary, replica = PTY.open
|
|
|
|
spawn_dbconsole(replica, "-e production")
|
|
|
|
assert_output("sqlite>", primary)
|
2017-07-16 20:11:21 -04:00
|
|
|
|
2018-09-10 18:06:30 -04:00
|
|
|
primary.puts "pragma database_list;"
|
|
|
|
assert_output("production.sqlite3", primary)
|
2017-07-16 20:11:21 -04:00
|
|
|
ensure
|
2018-09-10 18:06:30 -04:00
|
|
|
primary.puts ".exit"
|
2017-07-16 20:11:21 -04:00
|
|
|
end
|
|
|
|
|
2017-07-08 21:13:11 -04:00
|
|
|
private
|
2017-07-16 20:11:21 -04:00
|
|
|
def spawn_dbconsole(fd, options = nil)
|
|
|
|
Process.spawn("#{app_path}/bin/rails dbconsole #{options}", in: fd, out: fd, err: fd)
|
2017-07-08 21:13:11 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|