2012-08-30 21:50:30 -04:00
|
|
|
require "isolation/abstract_unit"
|
2014-01-01 21:52:47 -05:00
|
|
|
require "active_support/core_ext/string/strip"
|
2012-08-30 21:50:30 -04:00
|
|
|
|
|
|
|
module ApplicationTests
|
|
|
|
module RakeTests
|
|
|
|
class RakeDbsTest < ActiveSupport::TestCase
|
|
|
|
include ActiveSupport::Testing::Isolation
|
|
|
|
|
|
|
|
def setup
|
|
|
|
build_app
|
|
|
|
boot_rails
|
|
|
|
FileUtils.rm_rf("#{app_path}/config/environments")
|
|
|
|
end
|
|
|
|
|
|
|
|
def teardown
|
|
|
|
teardown_app
|
|
|
|
end
|
|
|
|
|
|
|
|
def database_url_db_name
|
2013-12-22 00:41:04 -05:00
|
|
|
File.join(app_path, "db/database_url_db.sqlite3")
|
2012-08-30 21:50:30 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def set_database_url
|
2014-01-02 16:00:30 -05:00
|
|
|
ENV['DATABASE_URL'] = File.join("sqlite3://:@localhost", database_url_db_name)
|
2013-02-20 08:44:06 -05:00
|
|
|
# ensure it's using the DATABASE_URL
|
|
|
|
FileUtils.rm_rf("#{app_path}/config/database.yml")
|
2012-08-30 21:50:30 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def expected
|
|
|
|
@expected ||= {}
|
|
|
|
end
|
|
|
|
|
|
|
|
def db_create_and_drop
|
|
|
|
Dir.chdir(app_path) do
|
|
|
|
output = `bundle exec rake db:create`
|
|
|
|
assert_equal output, ""
|
2013-11-01 05:39:06 -04:00
|
|
|
assert File.exist?(expected[:database])
|
2012-08-30 21:50:30 -04:00
|
|
|
assert_equal expected[:database],
|
|
|
|
ActiveRecord::Base.connection_config[:database]
|
|
|
|
output = `bundle exec rake db:drop`
|
|
|
|
assert_equal output, ""
|
2013-11-01 05:39:06 -04:00
|
|
|
assert !File.exist?(expected[:database])
|
2012-08-30 21:50:30 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'db:create and db:drop without database url' do
|
|
|
|
require "#{app_path}/config/environment"
|
|
|
|
expected[:database] = ActiveRecord::Base.configurations[Rails.env]['database']
|
|
|
|
db_create_and_drop
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'db:create and db:drop with database url' do
|
|
|
|
require "#{app_path}/config/environment"
|
|
|
|
set_database_url
|
|
|
|
expected[:database] = database_url_db_name
|
|
|
|
db_create_and_drop
|
|
|
|
end
|
|
|
|
|
|
|
|
def db_migrate_and_status
|
|
|
|
Dir.chdir(app_path) do
|
2012-12-06 21:06:08 -05:00
|
|
|
`rails generate model book title:string;
|
|
|
|
bundle exec rake db:migrate`
|
2012-08-30 21:50:30 -04:00
|
|
|
output = `bundle exec rake db:migrate:status`
|
2013-12-22 00:41:04 -05:00
|
|
|
assert_match(%r{database:\s+\S*#{Regexp.escape(expected[:database])}}, output)
|
2012-08-30 21:50:30 -04:00
|
|
|
assert_match(/up\s+\d{14}\s+Create books/, output)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'db:migrate and db:migrate:status without database_url' do
|
|
|
|
require "#{app_path}/config/environment"
|
|
|
|
expected[:database] = ActiveRecord::Base.configurations[Rails.env]['database']
|
|
|
|
db_migrate_and_status
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'db:migrate and db:migrate:status with database_url' do
|
|
|
|
require "#{app_path}/config/environment"
|
|
|
|
set_database_url
|
|
|
|
expected[:database] = database_url_db_name
|
|
|
|
db_migrate_and_status
|
|
|
|
end
|
|
|
|
|
|
|
|
def db_schema_dump
|
|
|
|
Dir.chdir(app_path) do
|
2012-12-06 21:06:08 -05:00
|
|
|
`rails generate model book title:string;
|
|
|
|
rake db:migrate db:schema:dump`
|
2012-08-30 21:50:30 -04:00
|
|
|
schema_dump = File.read("db/schema.rb")
|
|
|
|
assert_match(/create_table \"books\"/, schema_dump)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'db:schema:dump without database_url' do
|
|
|
|
db_schema_dump
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'db:schema:dump with database_url' do
|
|
|
|
set_database_url
|
|
|
|
db_schema_dump
|
|
|
|
end
|
|
|
|
|
|
|
|
def db_fixtures_load
|
|
|
|
Dir.chdir(app_path) do
|
2012-12-06 21:06:08 -05:00
|
|
|
`rails generate model book title:string;
|
|
|
|
bundle exec rake db:migrate db:fixtures:load`
|
2012-09-15 08:13:48 -04:00
|
|
|
assert_match(/#{expected[:database]}/,
|
|
|
|
ActiveRecord::Base.connection_config[:database])
|
2012-08-30 21:50:30 -04:00
|
|
|
require "#{app_path}/app/models/book"
|
|
|
|
assert_equal 2, Book.count
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'db:fixtures:load without database_url' do
|
|
|
|
require "#{app_path}/config/environment"
|
|
|
|
expected[:database] = ActiveRecord::Base.configurations[Rails.env]['database']
|
|
|
|
db_fixtures_load
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'db:fixtures:load with database_url' do
|
|
|
|
require "#{app_path}/config/environment"
|
|
|
|
set_database_url
|
|
|
|
expected[:database] = database_url_db_name
|
|
|
|
db_fixtures_load
|
|
|
|
end
|
|
|
|
|
|
|
|
def db_structure_dump_and_load
|
|
|
|
Dir.chdir(app_path) do
|
2012-12-06 21:06:08 -05:00
|
|
|
`rails generate model book title:string;
|
|
|
|
bundle exec rake db:migrate db:structure:dump`
|
2012-08-30 21:50:30 -04:00
|
|
|
structure_dump = File.read("db/structure.sql")
|
|
|
|
assert_match(/CREATE TABLE \"books\"/, structure_dump)
|
2013-12-25 17:25:38 -05:00
|
|
|
`bundle exec rake environment db:drop db:structure:load`
|
2012-09-15 08:13:48 -04:00
|
|
|
assert_match(/#{expected[:database]}/,
|
|
|
|
ActiveRecord::Base.connection_config[:database])
|
2012-08-30 21:50:30 -04:00
|
|
|
require "#{app_path}/app/models/book"
|
|
|
|
#if structure is not loaded correctly, exception would be raised
|
|
|
|
assert Book.count, 0
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'db:structure:dump and db:structure:load without database_url' do
|
|
|
|
require "#{app_path}/config/environment"
|
|
|
|
expected[:database] = ActiveRecord::Base.configurations[Rails.env]['database']
|
|
|
|
db_structure_dump_and_load
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'db:structure:dump and db:structure:load with database_url' do
|
|
|
|
require "#{app_path}/config/environment"
|
|
|
|
set_database_url
|
|
|
|
expected[:database] = database_url_db_name
|
|
|
|
db_structure_dump_and_load
|
|
|
|
end
|
|
|
|
|
|
|
|
def db_test_load_structure
|
|
|
|
Dir.chdir(app_path) do
|
2012-12-06 21:06:08 -05:00
|
|
|
`rails generate model book title:string;
|
|
|
|
bundle exec rake db:migrate db:structure:dump db:test:load_structure`
|
2012-08-30 21:50:30 -04:00
|
|
|
ActiveRecord::Base.configurations = Rails.application.config.database_configuration
|
2013-12-25 04:54:25 -05:00
|
|
|
ActiveRecord::Base.establish_connection :test
|
2012-08-30 21:50:30 -04:00
|
|
|
require "#{app_path}/app/models/book"
|
|
|
|
#if structure is not loaded correctly, exception would be raised
|
|
|
|
assert Book.count, 0
|
2012-09-15 08:13:48 -04:00
|
|
|
assert_match(/#{ActiveRecord::Base.configurations['test']['database']}/,
|
|
|
|
ActiveRecord::Base.connection_config[:database])
|
2012-08-30 21:50:30 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'db:test:load_structure without database_url' do
|
|
|
|
require "#{app_path}/config/environment"
|
|
|
|
db_test_load_structure
|
|
|
|
end
|
2013-12-29 06:05:04 -05:00
|
|
|
|
|
|
|
test 'db:test deprecation' do
|
|
|
|
require "#{app_path}/config/environment"
|
|
|
|
Dir.chdir(app_path) do
|
|
|
|
output = `bundle exec rake db:migrate db:test:prepare 2>&1`
|
|
|
|
assert_equal "WARNING: db:test:prepare is deprecated. The Rails test helper now maintains " \
|
|
|
|
"your test schema automatically, see the release notes for details.\n", output
|
|
|
|
end
|
|
|
|
end
|
2012-08-30 21:50:30 -04:00
|
|
|
end
|
|
|
|
end
|
Run rake tasks all at once in rake dbs test for a speed up
Before:
Finished tests in 56.245787s, 0.2133 tests/s, 0.0000 assertions/s.
12 tests, 0 assertions, 0 failures, 0 errors, 0 skips
After:
Finished tests in 42.401416s, 0.2830 tests/s, 0.0000 assertions/s.
12 tests, 0 assertions, 0 failures, 0 errors, 0 skips
2012-12-06 18:41:00 -05:00
|
|
|
end
|