aboutsummaryrefslogtreecommitdiffstats
path: root/railties/test/application/dbconsole_test.rb
blob: 8eb293c17988e0b1ca9d46ed42190be454bacdc7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# frozen_string_literal: true

require "isolation/abstract_unit"
require "console_helpers"

module ApplicationTests
  class DBConsoleTest < ActiveSupport::TestCase
    include ActiveSupport::Testing::Isolation
    include ConsoleHelpers

    def setup
      skip "PTY unavailable" unless available_pty?

      build_app
    end

    def teardown
      teardown_app
    end

    def test_use_value_defined_in_environment_file_in_database_yml
      app_file "config/database.yml", <<-YAML
        development:
           database: <%= Rails.application.config.database %>
           adapter: sqlite3
           pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
           timeout: 5000
      YAML

      app_file "config/environments/development.rb", <<-RUBY
        Rails.application.configure do
          config.database = "db/development.sqlite3"
        end
      RUBY

      master, slave = PTY.open
      spawn_dbconsole(slave)
      assert_output("sqlite>", master)
    ensure
      master.puts ".exit"
    end

    def test_respect_environment_option
      app_file "config/database.yml", <<-YAML
        default: &default
          adapter: sqlite3
          pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
          timeout: 5000

        development:
          <<: *default
          database: db/development.sqlite3

        production:
          <<: *default
          database: db/production.sqlite3
      YAML

      master, slave = PTY.open
      spawn_dbconsole(slave, "-e production")
      assert_output("sqlite>", master)

      master.puts "pragma database_list;"
      assert_output("production.sqlite3", master)
    ensure
      master.puts ".exit"
    end

    private
      def spawn_dbconsole(fd, options = nil)
        Process.spawn("#{app_path}/bin/rails dbconsole #{options}", in: fd, out: fd, err: fd)
      end
  end
end