2016-08-06 13:16:09 -04:00
|
|
|
require "isolation/abstract_unit"
|
2010-01-04 18:13:45 -05:00
|
|
|
|
|
|
|
module ApplicationTests
|
2012-01-05 20:30:17 -05:00
|
|
|
class TestTest < ActiveSupport::TestCase
|
2010-01-04 18:13:45 -05:00
|
|
|
include ActiveSupport::Testing::Isolation
|
|
|
|
|
|
|
|
def setup
|
|
|
|
build_app
|
|
|
|
end
|
|
|
|
|
2011-06-06 08:54:05 -04:00
|
|
|
def teardown
|
|
|
|
teardown_app
|
|
|
|
end
|
|
|
|
|
2016-09-16 12:26:10 -04:00
|
|
|
test "simple successful test" do
|
2016-08-06 13:16:09 -04:00
|
|
|
app_file "test/unit/foo_test.rb", <<-RUBY
|
2010-01-04 18:13:45 -05:00
|
|
|
require 'test_helper'
|
|
|
|
|
|
|
|
class FooTest < ActiveSupport::TestCase
|
|
|
|
def test_truth
|
|
|
|
assert true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
RUBY
|
|
|
|
|
2016-08-06 13:16:09 -04:00
|
|
|
assert_successful_test_run "unit/foo_test.rb"
|
2010-01-04 18:13:45 -05:00
|
|
|
end
|
|
|
|
|
2016-09-16 12:31:39 -04:00
|
|
|
test "after_run" do
|
|
|
|
app_file "test/unit/foo_test.rb", <<-RUBY
|
|
|
|
require 'test_helper'
|
|
|
|
|
|
|
|
Minitest.after_run { puts "WORLD" }
|
|
|
|
Minitest.after_run { puts "HELLO" }
|
|
|
|
|
|
|
|
class FooTest < ActiveSupport::TestCase
|
|
|
|
def test_truth
|
|
|
|
assert true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
RUBY
|
|
|
|
|
|
|
|
result = assert_successful_test_run "unit/foo_test.rb"
|
|
|
|
assert_equal ["HELLO", "WORLD"], result.scan(/HELLO|WORLD/) # only once and in correct order
|
|
|
|
end
|
|
|
|
|
2016-09-16 12:26:10 -04:00
|
|
|
test "simple failed test" do
|
|
|
|
app_file "test/unit/foo_test.rb", <<-RUBY
|
|
|
|
require 'test_helper'
|
|
|
|
|
|
|
|
class FooTest < ActiveSupport::TestCase
|
|
|
|
def test_truth
|
|
|
|
assert false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
RUBY
|
|
|
|
|
2017-05-02 09:09:21 -04:00
|
|
|
assert_unsuccessful_run "unit/foo_test.rb", "Failure:\nFooTest#test_truth"
|
2016-09-16 12:26:10 -04:00
|
|
|
end
|
|
|
|
|
2010-01-05 12:47:10 -05:00
|
|
|
test "integration test" do
|
2016-08-06 13:16:09 -04:00
|
|
|
controller "posts", <<-RUBY
|
2010-01-05 12:47:10 -05:00
|
|
|
class PostsController < ActionController::Base
|
|
|
|
end
|
|
|
|
RUBY
|
|
|
|
|
2016-08-06 13:16:09 -04:00
|
|
|
app_file "app/views/posts/index.html.erb", <<-HTML
|
2010-01-05 12:47:10 -05:00
|
|
|
Posts#index
|
|
|
|
HTML
|
|
|
|
|
2016-08-06 13:16:09 -04:00
|
|
|
app_file "test/integration/posts_test.rb", <<-RUBY
|
2010-01-05 12:47:10 -05:00
|
|
|
require 'test_helper'
|
|
|
|
|
2010-09-24 20:15:52 -04:00
|
|
|
class PostsTest < ActionDispatch::IntegrationTest
|
2010-01-05 12:47:10 -05:00
|
|
|
def test_index
|
|
|
|
get '/posts'
|
|
|
|
assert_response :success
|
2015-05-13 06:28:33 -04:00
|
|
|
assert_includes @response.body, 'Posts#index'
|
2010-01-05 12:47:10 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
RUBY
|
|
|
|
|
2016-08-06 13:16:09 -04:00
|
|
|
assert_successful_test_run "integration/posts_test.rb"
|
2013-10-28 08:44:17 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
test "enable full backtraces on test failures" do
|
2016-08-06 13:16:09 -04:00
|
|
|
app_file "test/unit/failing_test.rb", <<-RUBY
|
2013-10-28 08:44:17 -04:00
|
|
|
require 'test_helper'
|
|
|
|
|
|
|
|
class FailingTest < ActiveSupport::TestCase
|
|
|
|
def test_failure
|
|
|
|
raise "fail"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
RUBY
|
|
|
|
|
2016-08-06 13:16:09 -04:00
|
|
|
output = run_test_file("unit/failing_test.rb", env: { "BACKTRACE" => "1" })
|
2015-03-21 08:15:56 -04:00
|
|
|
assert_match %r{test/unit/failing_test\.rb}, output
|
|
|
|
assert_match %r{test/unit/failing_test\.rb:4}, output
|
2010-01-05 12:47:10 -05:00
|
|
|
end
|
|
|
|
|
2014-07-24 08:46:25 -04:00
|
|
|
test "ruby schema migrations" do
|
2016-08-06 13:16:09 -04:00
|
|
|
output = script("generate model user name:string")
|
2013-12-29 06:05:04 -05:00
|
|
|
version = output.match(/(\d+)_create_users\.rb/)[1]
|
|
|
|
|
2016-08-06 13:16:09 -04:00
|
|
|
app_file "test/models/user_test.rb", <<-RUBY
|
2013-12-29 06:05:04 -05:00
|
|
|
require 'test_helper'
|
|
|
|
|
|
|
|
class UserTest < ActiveSupport::TestCase
|
|
|
|
test "user" do
|
|
|
|
User.create! name: "Jon"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
RUBY
|
2016-08-06 13:16:09 -04:00
|
|
|
app_file "db/schema.rb", ""
|
2013-12-29 06:05:04 -05:00
|
|
|
|
|
|
|
assert_unsuccessful_run "models/user_test.rb", "Migrations are pending"
|
|
|
|
|
2016-08-06 13:16:09 -04:00
|
|
|
app_file "db/schema.rb", <<-RUBY
|
2013-12-29 06:05:04 -05:00
|
|
|
ActiveRecord::Schema.define(version: #{version}) do
|
|
|
|
create_table :users do |t|
|
|
|
|
t.string :name
|
|
|
|
end
|
|
|
|
end
|
|
|
|
RUBY
|
|
|
|
|
2016-08-06 13:16:09 -04:00
|
|
|
app_file "config/initializers/disable_maintain_test_schema.rb", <<-RUBY
|
2013-12-29 06:05:04 -05:00
|
|
|
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"
|
|
|
|
|
2016-08-06 13:16:09 -04:00
|
|
|
result = assert_successful_test_run("models/user_test.rb")
|
2016-09-16 12:44:05 -04:00
|
|
|
assert_not_includes result, "create_table(:users)"
|
2013-12-29 06:05:04 -05:00
|
|
|
end
|
|
|
|
|
2014-07-24 08:46:25 -04:00
|
|
|
test "sql structure migrations" do
|
2016-08-06 13:16:09 -04:00
|
|
|
output = script("generate model user name:string")
|
2014-07-24 08:46:25 -04:00
|
|
|
version = output.match(/(\d+)_create_users\.rb/)[1]
|
|
|
|
|
2016-08-06 13:16:09 -04:00
|
|
|
app_file "test/models/user_test.rb", <<-RUBY
|
2014-07-24 08:46:25 -04:00
|
|
|
require 'test_helper'
|
|
|
|
|
|
|
|
class UserTest < ActiveSupport::TestCase
|
|
|
|
test "user" do
|
|
|
|
User.create! name: "Jon"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
RUBY
|
|
|
|
|
2016-08-06 13:16:09 -04:00
|
|
|
app_file "db/structure.sql", ""
|
|
|
|
app_file "config/initializers/enable_sql_schema_format.rb", <<-RUBY
|
2014-07-24 08:46:25 -04:00
|
|
|
Rails.application.config.active_record.schema_format = :sql
|
|
|
|
RUBY
|
|
|
|
|
|
|
|
assert_unsuccessful_run "models/user_test.rb", "Migrations are pending"
|
|
|
|
|
2016-08-06 13:16:09 -04:00
|
|
|
app_file "db/structure.sql", <<-SQL
|
2014-07-24 08:46:25 -04:00
|
|
|
CREATE TABLE "schema_migrations" ("version" varchar(255) NOT NULL);
|
|
|
|
CREATE UNIQUE INDEX "unique_schema_migrations" ON "schema_migrations" ("version");
|
|
|
|
CREATE TABLE "users" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "name" varchar(255));
|
|
|
|
INSERT INTO schema_migrations (version) VALUES ('#{version}');
|
|
|
|
SQL
|
|
|
|
|
2016-08-06 13:16:09 -04:00
|
|
|
app_file "config/initializers/disable_maintain_test_schema.rb", <<-RUBY
|
2014-07-24 08:46:25 -04:00
|
|
|
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"
|
|
|
|
|
2016-08-06 13:16:09 -04:00
|
|
|
assert_successful_test_run("models/user_test.rb")
|
2014-07-24 08:46:25 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
test "sql structure migrations when adding column to existing table" do
|
2016-08-06 13:16:09 -04:00
|
|
|
output_1 = script("generate model user name:string")
|
2014-07-24 08:46:25 -04:00
|
|
|
version_1 = output_1.match(/(\d+)_create_users\.rb/)[1]
|
|
|
|
|
2016-08-06 13:16:09 -04:00
|
|
|
app_file "test/models/user_test.rb", <<-RUBY
|
2014-07-24 08:46:25 -04:00
|
|
|
require 'test_helper'
|
|
|
|
class UserTest < ActiveSupport::TestCase
|
|
|
|
test "user" do
|
|
|
|
User.create! name: "Jon"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
RUBY
|
|
|
|
|
2016-08-06 13:16:09 -04:00
|
|
|
app_file "config/initializers/enable_sql_schema_format.rb", <<-RUBY
|
2014-07-24 08:46:25 -04:00
|
|
|
Rails.application.config.active_record.schema_format = :sql
|
|
|
|
RUBY
|
|
|
|
|
2016-08-06 13:16:09 -04:00
|
|
|
app_file "db/structure.sql", <<-SQL
|
2014-07-24 08:46:25 -04:00
|
|
|
CREATE TABLE "schema_migrations" ("version" varchar(255) NOT NULL);
|
|
|
|
CREATE UNIQUE INDEX "unique_schema_migrations" ON "schema_migrations" ("version");
|
|
|
|
CREATE TABLE "users" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "name" varchar(255));
|
|
|
|
INSERT INTO schema_migrations (version) VALUES ('#{version_1}');
|
|
|
|
SQL
|
|
|
|
|
2016-08-06 13:16:09 -04:00
|
|
|
assert_successful_test_run("models/user_test.rb")
|
2014-07-24 08:46:25 -04:00
|
|
|
|
2016-08-06 13:16:09 -04:00
|
|
|
output_2 = script("generate migration add_email_to_users")
|
2014-07-24 08:46:25 -04:00
|
|
|
version_2 = output_2.match(/(\d+)_add_email_to_users\.rb/)[1]
|
|
|
|
|
2016-08-06 13:16:09 -04:00
|
|
|
app_file "test/models/user_test.rb", <<-RUBY
|
2014-07-24 08:46:25 -04:00
|
|
|
require 'test_helper'
|
|
|
|
|
|
|
|
class UserTest < ActiveSupport::TestCase
|
|
|
|
test "user" do
|
|
|
|
User.create! name: "Jon", email: "jon@doe.com"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
RUBY
|
|
|
|
|
2016-08-06 13:16:09 -04:00
|
|
|
app_file "db/structure.sql", <<-SQL
|
2014-07-24 08:46:25 -04:00
|
|
|
CREATE TABLE "schema_migrations" ("version" varchar(255) NOT NULL);
|
|
|
|
CREATE UNIQUE INDEX "unique_schema_migrations" ON "schema_migrations" ("version");
|
|
|
|
CREATE TABLE "users" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "name" varchar(255), "email" varchar(255));
|
|
|
|
INSERT INTO schema_migrations (version) VALUES ('#{version_1}');
|
|
|
|
INSERT INTO schema_migrations (version) VALUES ('#{version_2}');
|
|
|
|
SQL
|
|
|
|
|
2016-08-06 13:16:09 -04:00
|
|
|
assert_successful_test_run("models/user_test.rb")
|
2014-07-24 08:46:25 -04:00
|
|
|
end
|
|
|
|
|
2014-11-24 06:04:01 -05:00
|
|
|
# TODO: would be nice if we could detect the schema change automatically.
|
|
|
|
# For now, the user has to synchronize the schema manually.
|
|
|
|
# This test-case serves as a reminder for this use-case.
|
|
|
|
test "manually synchronize test schema after rollback" do
|
2016-08-06 13:16:09 -04:00
|
|
|
output = script("generate model user name:string")
|
2014-11-24 06:04:01 -05:00
|
|
|
version = output.match(/(\d+)_create_users\.rb/)[1]
|
|
|
|
|
2016-08-06 13:16:09 -04:00
|
|
|
app_file "test/models/user_test.rb", <<-RUBY
|
2014-11-24 06:04:01 -05:00
|
|
|
require 'test_helper'
|
|
|
|
|
|
|
|
class UserTest < ActiveSupport::TestCase
|
|
|
|
test "user" do
|
|
|
|
assert_equal ["id", "name"], User.columns_hash.keys
|
|
|
|
end
|
|
|
|
end
|
|
|
|
RUBY
|
2016-08-06 13:16:09 -04:00
|
|
|
app_file "db/schema.rb", <<-RUBY
|
2014-11-24 06:04:01 -05:00
|
|
|
ActiveRecord::Schema.define(version: #{version}) do
|
|
|
|
create_table :users do |t|
|
|
|
|
t.string :name
|
|
|
|
end
|
|
|
|
end
|
|
|
|
RUBY
|
|
|
|
|
|
|
|
assert_successful_test_run "models/user_test.rb"
|
|
|
|
|
|
|
|
# Simulate `db:rollback` + edit of the migration file + `db:migrate`
|
2016-08-06 13:16:09 -04:00
|
|
|
app_file "db/schema.rb", <<-RUBY
|
2014-11-24 06:04:01 -05:00
|
|
|
ActiveRecord::Schema.define(version: #{version}) do
|
|
|
|
create_table :users do |t|
|
|
|
|
t.string :name
|
|
|
|
t.integer :age
|
|
|
|
end
|
|
|
|
end
|
|
|
|
RUBY
|
|
|
|
|
|
|
|
assert_successful_test_run "models/user_test.rb"
|
|
|
|
|
2016-01-23 14:25:02 -05:00
|
|
|
Dir.chdir(app_path) { `bin/rails db:test:prepare` }
|
2014-11-24 06:04:01 -05:00
|
|
|
|
|
|
|
assert_unsuccessful_run "models/user_test.rb", <<-ASSERTION
|
|
|
|
Expected: ["id", "name"]
|
|
|
|
Actual: ["id", "name", "age"]
|
|
|
|
ASSERTION
|
|
|
|
end
|
|
|
|
|
|
|
|
test "hooks for plugins" do
|
2016-08-06 13:16:09 -04:00
|
|
|
output = script("generate model user name:string")
|
2014-11-24 06:04:01 -05:00
|
|
|
version = output.match(/(\d+)_create_users\.rb/)[1]
|
|
|
|
|
2016-08-06 13:16:09 -04:00
|
|
|
app_file "lib/tasks/hooks.rake", <<-RUBY
|
2014-11-24 06:04:01 -05:00
|
|
|
task :before_hook do
|
|
|
|
has_user_table = ActiveRecord::Base.connection.table_exists?('users')
|
|
|
|
puts "before: " + has_user_table.to_s
|
|
|
|
end
|
|
|
|
|
|
|
|
task :after_hook do
|
|
|
|
has_user_table = ActiveRecord::Base.connection.table_exists?('users')
|
|
|
|
puts "after: " + has_user_table.to_s
|
|
|
|
end
|
|
|
|
|
|
|
|
Rake::Task["db:test:prepare"].enhance [:before_hook] do
|
|
|
|
Rake::Task[:after_hook].invoke
|
|
|
|
end
|
|
|
|
RUBY
|
2016-08-06 13:16:09 -04:00
|
|
|
app_file "test/models/user_test.rb", <<-RUBY
|
2014-11-24 06:04:01 -05:00
|
|
|
require 'test_helper'
|
|
|
|
class UserTest < ActiveSupport::TestCase
|
|
|
|
test "user" do
|
|
|
|
User.create! name: "Jon"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
RUBY
|
|
|
|
|
|
|
|
# Simulate `db:migrate`
|
2016-08-06 13:16:09 -04:00
|
|
|
app_file "db/schema.rb", <<-RUBY
|
2014-11-24 06:04:01 -05:00
|
|
|
ActiveRecord::Schema.define(version: #{version}) do
|
|
|
|
create_table :users do |t|
|
|
|
|
t.string :name
|
|
|
|
end
|
|
|
|
end
|
|
|
|
RUBY
|
|
|
|
|
|
|
|
output = assert_successful_test_run "models/user_test.rb"
|
|
|
|
assert_includes output, "before: false\nafter: true"
|
|
|
|
|
|
|
|
# running tests again won't trigger a schema update
|
|
|
|
output = assert_successful_test_run "models/user_test.rb"
|
|
|
|
assert_not_includes output, "before:"
|
|
|
|
assert_not_includes output, "after:"
|
|
|
|
end
|
|
|
|
|
2010-01-04 18:13:45 -05:00
|
|
|
private
|
2013-12-29 06:05:04 -05:00
|
|
|
def assert_unsuccessful_run(name, message)
|
|
|
|
result = run_test_file(name)
|
|
|
|
assert_not_equal 0, $?.to_i
|
2016-09-16 12:44:05 -04:00
|
|
|
assert_includes result, message
|
2013-12-29 06:05:04 -05:00
|
|
|
result
|
|
|
|
end
|
|
|
|
|
2013-10-28 08:44:17 -04:00
|
|
|
def assert_successful_test_run(name)
|
|
|
|
result = run_test_file(name)
|
2010-01-04 18:13:45 -05:00
|
|
|
assert_equal 0, $?.to_i, result
|
2013-12-29 06:05:04 -05:00
|
|
|
result
|
2010-01-04 18:13:45 -05:00
|
|
|
end
|
|
|
|
|
2013-10-28 08:44:17 -04:00
|
|
|
def run_test_file(name, options = {})
|
2015-03-05 09:19:09 -05:00
|
|
|
Dir.chdir(app_path) { `bin/rails test "#{app_path}/test/#{name}" 2>&1` }
|
2013-10-28 08:44:17 -04:00
|
|
|
end
|
2010-01-04 18:13:45 -05:00
|
|
|
end
|
|
|
|
end
|