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
|
2014-04-02 08:36:16 -04:00
|
|
|
"db/database_url_db.sqlite3"
|
2012-08-30 21:50:30 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def set_database_url
|
2014-04-02 08:36:16 -04:00
|
|
|
ENV['DATABASE_URL'] = "sqlite3:#{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
|
|
|
|
|
2014-03-20 07:27:22 -04:00
|
|
|
def db_create_and_drop(expected_database)
|
2012-08-30 21:50:30 -04:00
|
|
|
Dir.chdir(app_path) do
|
2015-06-30 05:19:01 -04:00
|
|
|
output = `bin/rake db:create`
|
2014-03-20 07:19:33 -04:00
|
|
|
assert_empty output
|
2014-03-20 07:27:22 -04:00
|
|
|
assert File.exist?(expected_database)
|
|
|
|
assert_equal expected_database, ActiveRecord::Base.connection_config[:database]
|
2015-06-30 05:19:01 -04:00
|
|
|
output = `bin/rake db:drop`
|
2014-03-20 07:19:33 -04:00
|
|
|
assert_empty output
|
2014-03-20 07:27:22 -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"
|
2014-03-20 07:27:22 -04:00
|
|
|
db_create_and_drop ActiveRecord::Base.configurations[Rails.env]['database']
|
2014-03-20 07:19:33 -04:00
|
|
|
end
|
2012-08-30 21:50:30 -04:00
|
|
|
|
|
|
|
test 'db:create and db:drop with database url' do
|
|
|
|
require "#{app_path}/config/environment"
|
|
|
|
set_database_url
|
2014-03-20 07:27:22 -04:00
|
|
|
db_create_and_drop database_url_db_name
|
2012-08-30 21:50:30 -04:00
|
|
|
end
|
|
|
|
|
2015-04-26 23:17:05 -04:00
|
|
|
def with_database_existing
|
|
|
|
Dir.chdir(app_path) do
|
|
|
|
set_database_url
|
|
|
|
`bin/rake db:create`
|
|
|
|
yield
|
|
|
|
`bin/rake db:drop`
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'db:create failure because database exists' do
|
|
|
|
with_database_existing do
|
|
|
|
output = `bin/rake db:create 2>&1`
|
|
|
|
assert_match /already exists/, output
|
|
|
|
assert_equal 0, $?.exitstatus
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'db:drop failure because database does not exist' do
|
|
|
|
Dir.chdir(app_path) do
|
|
|
|
output = `bin/rake db:drop 2>&1`
|
2015-08-07 16:05:36 -04:00
|
|
|
assert_match /does not exist/, output
|
2015-04-26 23:17:05 -04:00
|
|
|
assert_equal 0, $?.exitstatus
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-03-20 07:27:22 -04:00
|
|
|
def db_migrate_and_status(expected_database)
|
2012-08-30 21:50:30 -04:00
|
|
|
Dir.chdir(app_path) do
|
2015-06-30 05:19:01 -04:00
|
|
|
`bin/rails generate model book title:string;
|
|
|
|
bin/rake db:migrate`
|
|
|
|
output = `bin/rake db:migrate:status`
|
2014-03-20 07:27:22 -04: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"
|
2014-03-20 07:27:22 -04:00
|
|
|
db_migrate_and_status ActiveRecord::Base.configurations[Rails.env]['database']
|
2012-08-30 21:50:30 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
test 'db:migrate and db:migrate:status with database_url' do
|
|
|
|
require "#{app_path}/config/environment"
|
|
|
|
set_database_url
|
2014-03-20 07:27:22 -04:00
|
|
|
db_migrate_and_status database_url_db_name
|
2012-08-30 21:50:30 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def db_schema_dump
|
|
|
|
Dir.chdir(app_path) do
|
2015-06-30 05:19:01 -04:00
|
|
|
`bin/rails generate model book title:string;
|
|
|
|
bin/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
|
|
|
|
|
2014-03-20 07:27:22 -04:00
|
|
|
def db_fixtures_load(expected_database)
|
2012-08-30 21:50:30 -04:00
|
|
|
Dir.chdir(app_path) do
|
2015-06-30 05:19:01 -04:00
|
|
|
`bin/rails generate model book title:string;
|
|
|
|
bin/rake db:migrate db:fixtures:load`
|
2014-03-20 07:27:22 -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"
|
2014-03-20 07:27:22 -04:00
|
|
|
db_fixtures_load ActiveRecord::Base.configurations[Rails.env]['database']
|
2012-08-30 21:50:30 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
test 'db:fixtures:load with database_url' do
|
|
|
|
require "#{app_path}/config/environment"
|
|
|
|
set_database_url
|
2014-03-20 07:27:22 -04:00
|
|
|
db_fixtures_load database_url_db_name
|
2012-08-30 21:50:30 -04:00
|
|
|
end
|
|
|
|
|
2014-10-11 22:08:50 -04:00
|
|
|
test 'db:fixtures:load with namespaced fixture' do
|
|
|
|
require "#{app_path}/config/environment"
|
|
|
|
Dir.chdir(app_path) do
|
2015-06-30 05:19:01 -04:00
|
|
|
`bin/rails generate model admin::book title:string;
|
|
|
|
bin/rake db:migrate db:fixtures:load`
|
2014-10-11 22:08:50 -04:00
|
|
|
require "#{app_path}/app/models/admin/book"
|
|
|
|
assert_equal 2, Admin::Book.count
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-03-20 07:27:22 -04:00
|
|
|
def db_structure_dump_and_load(expected_database)
|
2012-08-30 21:50:30 -04:00
|
|
|
Dir.chdir(app_path) do
|
2015-06-30 05:19:01 -04:00
|
|
|
`bin/rails generate model book title:string;
|
|
|
|
bin/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)
|
2015-06-30 05:19:01 -04:00
|
|
|
`bin/rake environment db:drop db:structure:load`
|
2014-03-20 07:27:22 -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
|
2014-03-14 19:57:36 -04:00
|
|
|
assert_equal 0, Book.count
|
2012-08-30 21:50:30 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'db:structure:dump and db:structure:load without database_url' do
|
|
|
|
require "#{app_path}/config/environment"
|
2014-03-20 07:27:22 -04:00
|
|
|
db_structure_dump_and_load ActiveRecord::Base.configurations[Rails.env]['database']
|
2012-08-30 21:50:30 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
test 'db:structure:dump and db:structure:load with database_url' do
|
|
|
|
require "#{app_path}/config/environment"
|
|
|
|
set_database_url
|
2014-03-20 07:27:22 -04:00
|
|
|
db_structure_dump_and_load database_url_db_name
|
2012-08-30 21:50:30 -04:00
|
|
|
end
|
|
|
|
|
2014-03-20 13:40:51 -04:00
|
|
|
test 'db:structure:dump does not dump schema information when no migrations are used' do
|
|
|
|
Dir.chdir(app_path) do
|
|
|
|
# create table without migrations
|
2015-06-30 05:19:01 -04:00
|
|
|
`bin/rails runner 'ActiveRecord::Base.connection.create_table(:posts) {|t| t.string :title }'`
|
2014-03-20 13:40:51 -04:00
|
|
|
|
2015-06-30 05:19:01 -04:00
|
|
|
stderr_output = capture(:stderr) { `bin/rake db:structure:dump` }
|
2014-03-20 13:40:51 -04:00
|
|
|
assert_empty stderr_output
|
|
|
|
structure_dump = File.read("db/structure.sql")
|
|
|
|
assert_match(/CREATE TABLE \"posts\"/, structure_dump)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-12-16 03:58:34 -05:00
|
|
|
test 'db:schema:load and db:structure:load do not purge the existing database' do
|
|
|
|
Dir.chdir(app_path) do
|
|
|
|
`bin/rails runner 'ActiveRecord::Base.connection.create_table(:posts) {|t| t.string :title }'`
|
|
|
|
|
|
|
|
app_file 'db/schema.rb', <<-RUBY
|
|
|
|
ActiveRecord::Schema.define(version: 20140423102712) do
|
|
|
|
create_table(:comments) {}
|
|
|
|
end
|
|
|
|
RUBY
|
|
|
|
|
|
|
|
list_tables = lambda { `bin/rails runner 'p ActiveRecord::Base.connection.tables'`.strip }
|
|
|
|
|
|
|
|
assert_equal '["posts"]', list_tables[]
|
|
|
|
`bin/rake db:schema:load`
|
|
|
|
assert_equal '["posts", "comments", "schema_migrations"]', list_tables[]
|
|
|
|
|
|
|
|
app_file 'db/structure.sql', <<-SQL
|
|
|
|
CREATE TABLE "users" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "name" varchar(255));
|
|
|
|
SQL
|
|
|
|
|
|
|
|
`bin/rake db:structure:load`
|
|
|
|
assert_equal '["posts", "comments", "schema_migrations", "users"]', list_tables[]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-04-27 04:45:22 -04:00
|
|
|
test "db:schema:load with inflections" do
|
|
|
|
Dir.chdir(app_path) do
|
|
|
|
app_file 'config/initializers/inflection.rb', <<-RUBY
|
|
|
|
ActiveSupport::Inflector.inflections do |inflect|
|
|
|
|
inflect.irregular 'goose', 'geese'
|
|
|
|
end
|
|
|
|
RUBY
|
|
|
|
app_file 'config/initializers/primary_key_table_name.rb', <<-RUBY
|
|
|
|
ActiveRecord::Base.primary_key_prefix_type = :table_name
|
|
|
|
RUBY
|
|
|
|
app_file 'db/schema.rb', <<-RUBY
|
|
|
|
ActiveRecord::Schema.define(version: 20140423102712) do
|
|
|
|
create_table("goose".pluralize) do |t|
|
|
|
|
t.string :name
|
|
|
|
end
|
|
|
|
end
|
|
|
|
RUBY
|
|
|
|
|
|
|
|
`bin/rake db:schema:load`
|
|
|
|
|
|
|
|
tables = `bin/rails runner 'p ActiveRecord::Base.connection.tables'`.strip
|
|
|
|
assert_match(/"geese"/, tables)
|
|
|
|
|
|
|
|
columns = `bin/rails runner 'p ActiveRecord::Base.connection.columns("geese").map(&:name)'`.strip
|
|
|
|
assert_equal columns, '["gooseid", "name"]'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-08-30 21:50:30 -04:00
|
|
|
def db_test_load_structure
|
|
|
|
Dir.chdir(app_path) do
|
2015-06-30 05:19:01 -04:00
|
|
|
`bin/rails generate model book title:string;
|
|
|
|
bin/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
|
2014-03-14 19:57:36 -04:00
|
|
|
assert_equal 0, Book.count
|
2014-03-20 07:27:22 -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
|
|
|
|
2014-09-03 11:31:38 -04:00
|
|
|
test 'db:setup loads schema and seeds database' do
|
|
|
|
begin
|
2014-09-03 15:59:38 -04:00
|
|
|
@old_rails_env = ENV["RAILS_ENV"]
|
2015-03-20 11:14:11 -04:00
|
|
|
@old_rack_env = ENV["RACK_ENV"]
|
2014-09-03 11:31:38 -04:00
|
|
|
ENV.delete "RAILS_ENV"
|
2015-03-20 11:14:11 -04:00
|
|
|
ENV.delete "RACK_ENV"
|
2014-09-03 11:31:38 -04:00
|
|
|
|
|
|
|
app_file 'db/schema.rb', <<-RUBY
|
|
|
|
ActiveRecord::Schema.define(version: "1") do
|
|
|
|
create_table :users do |t|
|
|
|
|
t.string :name
|
|
|
|
end
|
|
|
|
end
|
|
|
|
RUBY
|
|
|
|
|
|
|
|
app_file 'db/seeds.rb', <<-RUBY
|
|
|
|
puts ActiveRecord::Base.connection_config[:database]
|
|
|
|
RUBY
|
|
|
|
|
|
|
|
Dir.chdir(app_path) do
|
2015-06-30 05:19:01 -04:00
|
|
|
database_path = `bin/rake db:setup`
|
2014-09-03 11:31:38 -04:00
|
|
|
assert_equal "development.sqlite3", File.basename(database_path.strip)
|
|
|
|
end
|
|
|
|
ensure
|
2014-09-03 15:59:38 -04:00
|
|
|
ENV["RAILS_ENV"] = @old_rails_env
|
2015-03-20 11:14:11 -04:00
|
|
|
ENV["RACK_ENV"] = @old_rack_env
|
2014-09-03 11:31:38 -04:00
|
|
|
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
|