diff options
author | Jon Leighton <j@jonathanleighton.com> | 2013-12-29 11:05:04 +0000 |
---|---|---|
committer | Jon Leighton <j@jonathanleighton.com> | 2014-01-02 13:49:00 +0000 |
commit | ff7ab3bc78abc3e8439a57ef7d755c5aa5b069f4 (patch) | |
tree | 5ad6aed8ee454c320967d767229debde268da2e2 /railties/test/application | |
parent | a1d0c0fa3d8ca97edc8f2a1d6ba96af19221dbad (diff) | |
download | rails-ff7ab3bc78abc3e8439a57ef7d755c5aa5b069f4.tar.gz rails-ff7ab3bc78abc3e8439a57ef7d755c5aa5b069f4.tar.bz2 rails-ff7ab3bc78abc3e8439a57ef7d755c5aa5b069f4.zip |
Automatically maintain test database schema
* Move check from generated helper to test_help.rb, so that all
applications can benefit
* Rather than just raising when the test schema has pending migrations,
try to load in the schema and only raise if there are pending
migrations afterwards
* Opt out of the check by setting
config.active_record.maintain_test_schema = false
* Deprecate db:test:* tasks. The test helper is now fully responsible
for maintaining the test schema, so we don't need rake tasks for this.
This is also a speed improvement since we're no longer reloading the
test database on every call to "rake test".
Diffstat (limited to 'railties/test/application')
-rw-r--r-- | railties/test/application/rake/dbs_test.rb | 9 | ||||
-rw-r--r-- | railties/test/application/rake_test.rb | 4 | ||||
-rw-r--r-- | railties/test/application/test_test.rb | 47 |
3 files changed, 57 insertions, 3 deletions
diff --git a/railties/test/application/rake/dbs_test.rb b/railties/test/application/rake/dbs_test.rb index 3dce315d3a..66d3137df0 100644 --- a/railties/test/application/rake/dbs_test.rb +++ b/railties/test/application/rake/dbs_test.rb @@ -174,6 +174,15 @@ module ApplicationTests require "#{app_path}/config/environment" db_test_load_structure end + + 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 end end end diff --git a/railties/test/application/rake_test.rb b/railties/test/application/rake_test.rb index 33d3c4a19e..317e73245c 100644 --- a/railties/test/application/rake_test.rb +++ b/railties/test/application/rake_test.rb @@ -187,7 +187,7 @@ module ApplicationTests def test_scaffold_tests_pass_by_default output = Dir.chdir(app_path) do `rails generate scaffold user username:string password:string; - bundle exec rake db:migrate db:test:clone test` + bundle exec rake db:migrate test` end assert_match(/7 runs, 13 assertions, 0 failures, 0 errors/, output) @@ -197,7 +197,7 @@ module ApplicationTests def test_scaffold_with_references_columns_tests_pass_by_default output = Dir.chdir(app_path) do `rails generate scaffold LineItems product:references cart:belongs_to; - bundle exec rake db:migrate db:test:clone test` + bundle exec rake db:migrate test` end assert_match(/7 runs, 13 assertions, 0 failures, 0 errors/, output) diff --git a/railties/test/application/test_test.rb b/railties/test/application/test_test.rb index 6f2f328588..a223180169 100644 --- a/railties/test/application/test_test.rb +++ b/railties/test/application/test_test.rb @@ -67,10 +67,55 @@ module ApplicationTests assert_match %r{/app/test/unit/failing_test\.rb}, output end + test "migrations" do + output = script('generate model user name:string') + version = output.match(/(\d+)_create_users\.rb/)[1] + + app_file 'test/models/user_test.rb', <<-RUBY + require 'test_helper' + + class UserTest < ActiveSupport::TestCase + test "user" do + User.create! name: "Jon" + end + end + RUBY + app_file 'db/schema.rb', '' + + assert_unsuccessful_run "models/user_test.rb", "Migrations are pending" + + app_file 'db/schema.rb', <<-RUBY + ActiveRecord::Schema.define(version: #{version}) do + create_table :users do |t| + t.string :name + end + end + RUBY + + app_file 'config/initializers/disable_maintain_test_schema.rb', <<-RUBY + Rails.application.config.active_record.maintain_test_schema = false + RUBY + + assert_unsuccessful_run "models/user_test.rb", "Could not find table 'users'" + + File.delete "#{app_path}/config/initializers/disable_maintain_test_schema.rb" + + result = assert_successful_test_run('models/user_test.rb') + assert !result.include?("create_table(:users)") + end + private + def assert_unsuccessful_run(name, message) + result = run_test_file(name) + assert_not_equal 0, $?.to_i + assert result.include?(message) + result + end + def assert_successful_test_run(name) result = run_test_file(name) assert_equal 0, $?.to_i, result + result end def run_test_file(name, options = {}) @@ -83,7 +128,7 @@ module ApplicationTests env["RUBYLIB"] = $:.join(':') Dir.chdir(app_path) do - `#{env_string(env)} #{Gem.ruby} #{args.join(' ')}` + `#{env_string(env)} #{Gem.ruby} #{args.join(' ')} 2>&1` end end |