PageRenderTime 26ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/railties/test/commands/dbconsole_test.rb

https://github.com/Tho85/rails
Ruby | 249 lines | 210 code | 39 blank | 0 comment | 2 complexity | 8b70d2db2f2c714ec7c871cb155a2396 MD5 | raw file
  1. require 'abstract_unit'
  2. require 'rails/commands/dbconsole'
  3. class Rails::DBConsoleTest < ActiveSupport::TestCase
  4. def setup
  5. Rails::DBConsole.const_set('APP_PATH', 'rails/all')
  6. end
  7. def teardown
  8. Rails::DBConsole.send(:remove_const, 'APP_PATH')
  9. %w[PGUSER PGHOST PGPORT PGPASSWORD DATABASE_URL].each{|key| ENV.delete(key)}
  10. end
  11. def test_config_with_db_config_only
  12. config_sample = {
  13. "test"=> {
  14. "adapter"=> "sqlite3",
  15. "host"=> "localhost",
  16. "port"=> "9000",
  17. "database"=> "foo_test",
  18. "user"=> "foo",
  19. "password"=> "bar",
  20. "pool"=> "5",
  21. "timeout"=> "3000"
  22. }
  23. }
  24. app_db_config(config_sample)
  25. assert_equal Rails::DBConsole.new.config, config_sample["test"]
  26. end
  27. def test_config_with_no_db_config
  28. app_db_config(nil)
  29. assert_raise(ActiveRecord::AdapterNotSpecified) {
  30. Rails::DBConsole.new.config
  31. }
  32. end
  33. def test_config_with_database_url_only
  34. ENV['DATABASE_URL'] = 'postgresql://foo:bar@localhost:9000/foo_test?pool=5&timeout=3000'
  35. app_db_config(nil)
  36. expected = {
  37. "adapter" => "postgresql",
  38. "host" => "localhost",
  39. "port" => 9000,
  40. "database" => "foo_test",
  41. "username" => "foo",
  42. "password" => "bar",
  43. "pool" => "5",
  44. "timeout" => "3000"
  45. }.sort
  46. assert_equal expected, Rails::DBConsole.new.config.sort
  47. end
  48. def test_config_choose_database_url_if_exists
  49. host = "database-url-host.com"
  50. ENV['DATABASE_URL'] = "postgresql://foo:bar@#{host}:9000/foo_test?pool=5&timeout=3000"
  51. sample_config = {
  52. "test" => {
  53. "adapter" => "postgresql",
  54. "host" => "not-the-#{host}",
  55. "port" => 9000,
  56. "database" => "foo_test",
  57. "username" => "foo",
  58. "password" => "bar",
  59. "pool" => "5",
  60. "timeout" => "3000"
  61. }
  62. }
  63. app_db_config(sample_config)
  64. assert_equal host, Rails::DBConsole.new.config["host"]
  65. end
  66. def test_env
  67. assert_equal Rails::DBConsole.new.environment, "test"
  68. ENV['RAILS_ENV'] = nil
  69. ENV['RACK_ENV'] = nil
  70. Rails.stubs(:respond_to?).with(:env).returns(false)
  71. assert_equal Rails::DBConsole.new.environment, "development"
  72. ENV['RACK_ENV'] = "rack_env"
  73. assert_equal Rails::DBConsole.new.environment, "rack_env"
  74. ENV['RAILS_ENV'] = "rails_env"
  75. assert_equal Rails::DBConsole.new.environment, "rails_env"
  76. ensure
  77. ENV['RAILS_ENV'] = "test"
  78. end
  79. def test_rails_env_is_development_when_argument_is_dev
  80. Rails::DBConsole.stubs(:available_environments).returns(['development', 'test'])
  81. options = Rails::DBConsole.new.send(:parse_arguments, ['dev'])
  82. assert_match('development', options[:environment])
  83. end
  84. def test_rails_env_is_dev_when_argument_is_dev_and_dev_env_is_present
  85. Rails::DBConsole.stubs(:available_environments).returns(['dev'])
  86. options = Rails::DBConsole.new.send(:parse_arguments, ['dev'])
  87. assert_match('dev', options[:environment])
  88. end
  89. def test_mysql
  90. dbconsole.expects(:find_cmd_and_exec).with(%w[mysql mysql5], 'db')
  91. start(adapter: 'mysql', database: 'db')
  92. assert !aborted
  93. end
  94. def test_mysql_full
  95. 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')
  96. start(adapter: 'mysql', database: 'db', host: 'locahost', port: 1234, socket: 'socket', username: 'user', password: 'qwerty', encoding: 'UTF-8')
  97. assert !aborted
  98. end
  99. def test_mysql_include_password
  100. dbconsole.expects(:find_cmd_and_exec).with(%w[mysql mysql5], '--user=user', '--password=qwerty', 'db')
  101. start({adapter: 'mysql', database: 'db', username: 'user', password: 'qwerty'}, ['-p'])
  102. assert !aborted
  103. end
  104. def test_postgresql
  105. dbconsole.expects(:find_cmd_and_exec).with('psql', 'db')
  106. start(adapter: 'postgresql', database: 'db')
  107. assert !aborted
  108. end
  109. def test_postgresql_full
  110. dbconsole.expects(:find_cmd_and_exec).with('psql', 'db')
  111. start(adapter: 'postgresql', database: 'db', username: 'user', password: 'q1w2e3', host: 'host', port: 5432)
  112. assert !aborted
  113. assert_equal 'user', ENV['PGUSER']
  114. assert_equal 'host', ENV['PGHOST']
  115. assert_equal '5432', ENV['PGPORT']
  116. assert_not_equal 'q1w2e3', ENV['PGPASSWORD']
  117. end
  118. def test_postgresql_include_password
  119. dbconsole.expects(:find_cmd_and_exec).with('psql', 'db')
  120. start({adapter: 'postgresql', database: 'db', username: 'user', password: 'q1w2e3'}, ['-p'])
  121. assert !aborted
  122. assert_equal 'user', ENV['PGUSER']
  123. assert_equal 'q1w2e3', ENV['PGPASSWORD']
  124. end
  125. def test_sqlite
  126. dbconsole.expects(:find_cmd_and_exec).with('sqlite', 'db')
  127. start(adapter: 'sqlite', database: 'db')
  128. assert !aborted
  129. end
  130. def test_sqlite3
  131. dbconsole.expects(:find_cmd_and_exec).with('sqlite3', Rails.root.join('db.sqlite3').to_s)
  132. start(adapter: 'sqlite3', database: 'db.sqlite3')
  133. assert !aborted
  134. end
  135. def test_sqlite3_mode
  136. dbconsole.expects(:find_cmd_and_exec).with('sqlite3', '-html', Rails.root.join('db.sqlite3').to_s)
  137. start({adapter: 'sqlite3', database: 'db.sqlite3'}, ['--mode', 'html'])
  138. assert !aborted
  139. end
  140. def test_sqlite3_header
  141. dbconsole.expects(:find_cmd_and_exec).with('sqlite3', '-header', Rails.root.join('db.sqlite3').to_s)
  142. start({adapter: 'sqlite3', database: 'db.sqlite3'}, ['--header'])
  143. end
  144. def test_sqlite3_db_absolute_path
  145. dbconsole.expects(:find_cmd_and_exec).with('sqlite3', '/tmp/db.sqlite3')
  146. start(adapter: 'sqlite3', database: '/tmp/db.sqlite3')
  147. assert !aborted
  148. end
  149. def test_sqlite3_db_without_defined_rails_root
  150. Rails.stubs(:respond_to?)
  151. Rails.expects(:respond_to?).with(:root).once.returns(false)
  152. dbconsole.expects(:find_cmd_and_exec).with('sqlite3', Rails.root.join('../config/db.sqlite3').to_s)
  153. start(adapter: 'sqlite3', database: 'config/db.sqlite3')
  154. assert !aborted
  155. end
  156. def test_oracle
  157. dbconsole.expects(:find_cmd_and_exec).with('sqlplus', 'user@db')
  158. start(adapter: 'oracle', database: 'db', username: 'user', password: 'secret')
  159. assert !aborted
  160. end
  161. def test_oracle_include_password
  162. dbconsole.expects(:find_cmd_and_exec).with('sqlplus', 'user/secret@db')
  163. start({adapter: 'oracle', database: 'db', username: 'user', password: 'secret'}, ['-p'])
  164. assert !aborted
  165. end
  166. def test_unknown_command_line_client
  167. start(adapter: 'unknown', database: 'db')
  168. assert aborted
  169. assert_match(/Unknown command-line client for db/, output)
  170. end
  171. def test_print_help_short
  172. stdout = capture(:stdout) do
  173. start({}, ['-h'])
  174. end
  175. assert aborted
  176. assert_equal '', output
  177. assert_match(/Usage:.*dbconsole/, stdout)
  178. end
  179. def test_print_help_long
  180. stdout = capture(:stdout) do
  181. start({}, ['--help'])
  182. end
  183. assert aborted
  184. assert_equal '', output
  185. assert_match(/Usage:.*dbconsole/, stdout)
  186. end
  187. attr_reader :aborted, :output
  188. private :aborted, :output
  189. private
  190. def app_db_config(results)
  191. Rails.application.config.stubs(:database_configuration).returns(results || {})
  192. end
  193. def dbconsole
  194. @dbconsole ||= Rails::DBConsole.new(nil)
  195. end
  196. def start(config = {}, argv = [])
  197. dbconsole.stubs(config: config.stringify_keys, arguments: argv)
  198. capture_abort { dbconsole.start }
  199. end
  200. def capture_abort
  201. @aborted = false
  202. @output = capture(:stderr) do
  203. begin
  204. yield
  205. rescue SystemExit
  206. @aborted = true
  207. end
  208. end
  209. end
  210. end