2009-06-27 07:03:07 -04:00
|
|
|
require 'generators/generators_test_helper'
|
2010-03-23 08:40:19 -04:00
|
|
|
require 'rails/generators/rails/model/model_generator'
|
2009-06-27 07:03:07 -04:00
|
|
|
|
2010-01-18 18:07:11 -05:00
|
|
|
class ModelGeneratorTest < Rails::Generators::TestCase
|
|
|
|
include GeneratorsTestHelper
|
2010-01-03 10:34:32 -05:00
|
|
|
arguments %w(Account name:string age:integer)
|
2009-06-27 07:03:07 -04:00
|
|
|
|
2009-06-27 15:26:53 -04:00
|
|
|
def test_help_shows_invoked_generators_options
|
|
|
|
content = run_generator ["--help"]
|
2011-05-18 07:37:57 -04:00
|
|
|
assert_match(/ActiveRecord options:/, content)
|
|
|
|
assert_match(/TestUnit options:/, content)
|
2009-06-27 15:26:53 -04:00
|
|
|
end
|
|
|
|
|
2010-09-02 06:45:22 -04:00
|
|
|
def test_model_with_missing_attribute_type
|
2011-06-17 14:10:53 -04:00
|
|
|
run_generator ["post", "title", "body:text", "author"]
|
|
|
|
|
|
|
|
assert_migration "db/migrate/create_posts.rb" do |m|
|
|
|
|
assert_method :change, m do |up|
|
|
|
|
assert_match(/t\.string :title/, up)
|
|
|
|
assert_match(/t\.text :body/, up)
|
|
|
|
assert_match(/t\.string :author/, up)
|
|
|
|
end
|
|
|
|
end
|
2010-09-02 06:45:22 -04:00
|
|
|
end
|
|
|
|
|
2009-06-27 07:03:07 -04:00
|
|
|
def test_invokes_default_orm
|
|
|
|
run_generator
|
|
|
|
assert_file "app/models/account.rb", /class Account < ActiveRecord::Base/
|
|
|
|
end
|
|
|
|
|
2009-06-28 05:56:44 -04:00
|
|
|
def test_model_with_parent_option
|
2009-06-27 09:41:29 -04:00
|
|
|
run_generator ["account", "--parent", "Admin::Account"]
|
|
|
|
assert_file "app/models/account.rb", /class Account < Admin::Account/
|
2009-06-28 13:46:34 -04:00
|
|
|
assert_no_migration "db/migrate/create_accounts.rb"
|
2009-06-27 09:41:29 -04:00
|
|
|
end
|
|
|
|
|
2009-06-28 05:56:44 -04:00
|
|
|
def test_model_with_underscored_parent_option
|
2009-06-27 10:01:11 -04:00
|
|
|
run_generator ["account", "--parent", "admin/account"]
|
|
|
|
assert_file "app/models/account.rb", /class Account < Admin::Account/
|
|
|
|
end
|
|
|
|
|
2010-04-11 04:27:45 -04:00
|
|
|
def test_model_with_namespace
|
|
|
|
run_generator ["admin/account"]
|
|
|
|
assert_file "app/models/admin.rb", /module Admin/
|
|
|
|
assert_file "app/models/admin.rb", /def self\.table_name_prefix/
|
|
|
|
assert_file "app/models/admin.rb", /'admin_'/
|
|
|
|
assert_file "app/models/admin/account.rb", /class Admin::Account < ActiveRecord::Base/
|
|
|
|
end
|
|
|
|
|
2009-06-28 05:56:44 -04:00
|
|
|
def test_migration
|
|
|
|
run_generator
|
|
|
|
assert_migration "db/migrate/create_accounts.rb", /class CreateAccounts < ActiveRecord::Migration/
|
|
|
|
end
|
|
|
|
|
2009-08-09 06:24:40 -04:00
|
|
|
def test_migration_with_namespace
|
|
|
|
run_generator ["Gallery::Image"]
|
|
|
|
assert_migration "db/migrate/create_gallery_images", /class CreateGalleryImages < ActiveRecord::Migration/
|
|
|
|
assert_no_migration "db/migrate/create_images"
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_migration_with_nested_namespace
|
|
|
|
run_generator ["Admin::Gallery::Image"]
|
|
|
|
assert_no_migration "db/migrate/create_images"
|
|
|
|
assert_no_migration "db/migrate/create_gallery_images"
|
|
|
|
assert_migration "db/migrate/create_admin_gallery_images", /class CreateAdminGalleryImages < ActiveRecord::Migration/
|
|
|
|
assert_migration "db/migrate/create_admin_gallery_images", /create_table :admin_gallery_images/
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_migration_with_nested_namespace_without_pluralization
|
|
|
|
ActiveRecord::Base.pluralize_table_names = false
|
|
|
|
run_generator ["Admin::Gallery::Image"]
|
|
|
|
assert_no_migration "db/migrate/create_images"
|
|
|
|
assert_no_migration "db/migrate/create_gallery_images"
|
|
|
|
assert_no_migration "db/migrate/create_admin_gallery_images"
|
|
|
|
assert_migration "db/migrate/create_admin_gallery_image", /class CreateAdminGalleryImage < ActiveRecord::Migration/
|
|
|
|
assert_migration "db/migrate/create_admin_gallery_image", /create_table :admin_gallery_image/
|
|
|
|
ensure
|
|
|
|
ActiveRecord::Base.pluralize_table_names = true
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_migration_with_namespaces_in_model_name_without_plurization
|
|
|
|
ActiveRecord::Base.pluralize_table_names = false
|
|
|
|
run_generator ["Gallery::Image"]
|
|
|
|
assert_migration "db/migrate/create_gallery_image", /class CreateGalleryImage < ActiveRecord::Migration/
|
|
|
|
assert_no_migration "db/migrate/create_gallery_images"
|
|
|
|
ensure
|
|
|
|
ActiveRecord::Base.pluralize_table_names = true
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_migration_without_pluralization
|
|
|
|
ActiveRecord::Base.pluralize_table_names = false
|
|
|
|
run_generator
|
|
|
|
assert_migration "db/migrate/create_account", /class CreateAccount < ActiveRecord::Migration/
|
|
|
|
assert_no_migration "db/migrate/create_accounts"
|
|
|
|
ensure
|
|
|
|
ActiveRecord::Base.pluralize_table_names = true
|
|
|
|
end
|
|
|
|
|
2009-06-28 05:56:44 -04:00
|
|
|
def test_migration_is_skipped
|
|
|
|
run_generator ["account", "--no-migration"]
|
|
|
|
assert_no_migration "db/migrate/create_accounts.rb"
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_migration_with_attributes
|
|
|
|
run_generator ["product", "name:string", "supplier_id:integer"]
|
2009-06-28 13:46:34 -04:00
|
|
|
|
|
|
|
assert_migration "db/migrate/create_products.rb" do |m|
|
2010-12-31 13:15:42 -05:00
|
|
|
assert_method :change, m do |up|
|
2011-05-18 07:37:57 -04:00
|
|
|
assert_match(/create_table :products/, up)
|
|
|
|
assert_match(/t\.string :name/, up)
|
|
|
|
assert_match(/t\.integer :supplier_id/, up)
|
2009-06-28 13:46:34 -04:00
|
|
|
end
|
|
|
|
end
|
2009-06-28 05:56:44 -04:00
|
|
|
end
|
|
|
|
|
2009-07-13 16:26:58 -04:00
|
|
|
def test_migration_without_timestamps
|
|
|
|
ActiveRecord::Base.timestamped_migrations = false
|
|
|
|
run_generator ["account"]
|
|
|
|
assert_file "db/migrate/001_create_accounts.rb", /class CreateAccounts < ActiveRecord::Migration/
|
|
|
|
|
|
|
|
run_generator ["project"]
|
|
|
|
assert_file "db/migrate/002_create_projects.rb", /class CreateProjects < ActiveRecord::Migration/
|
|
|
|
ensure
|
|
|
|
ActiveRecord::Base.timestamped_migrations = true
|
|
|
|
end
|
|
|
|
|
2009-06-28 05:56:44 -04:00
|
|
|
def test_model_with_references_attribute_generates_belongs_to_associations
|
|
|
|
run_generator ["product", "name:string", "supplier_id:references"]
|
|
|
|
assert_file "app/models/product.rb", /belongs_to :supplier/
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_model_with_belongs_to_attribute_generates_belongs_to_associations
|
|
|
|
run_generator ["product", "name:string", "supplier_id:belongs_to"]
|
|
|
|
assert_file "app/models/product.rb", /belongs_to :supplier/
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_migration_with_timestamps
|
|
|
|
run_generator
|
|
|
|
assert_migration "db/migrate/create_accounts.rb", /t.timestamps/
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_migration_timestamps_are_skipped
|
|
|
|
run_generator ["account", "--no-timestamps"]
|
2009-06-28 13:46:34 -04:00
|
|
|
|
|
|
|
assert_migration "db/migrate/create_accounts.rb" do |m|
|
2010-12-31 13:15:42 -05:00
|
|
|
assert_method :change, m do |up|
|
2011-05-18 07:37:57 -04:00
|
|
|
assert_no_match(/t.timestamps/, up)
|
2009-06-28 13:46:34 -04:00
|
|
|
end
|
|
|
|
end
|
2009-06-28 05:56:44 -04:00
|
|
|
end
|
|
|
|
|
2010-12-10 16:16:34 -05:00
|
|
|
def test_migration_is_skipped_with_skip_option
|
2009-07-01 06:07:05 -04:00
|
|
|
run_generator
|
2010-12-10 16:16:34 -05:00
|
|
|
output = run_generator ["Account", "--skip"]
|
|
|
|
assert_match %r{skip\s+db/migrate/\d+_create_accounts.rb}, output
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_migration_is_ignored_as_identical_with_skip_option
|
|
|
|
run_generator ["Account"]
|
|
|
|
output = run_generator ["Account", "--skip"]
|
|
|
|
assert_match %r{identical\s+db/migrate/\d+_create_accounts.rb}, output
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_migration_is_skipped_on_skip_behavior
|
|
|
|
run_generator
|
|
|
|
output = run_generator ["Account"], :behavior => :skip
|
|
|
|
assert_match %r{skip\s+db/migrate/\d+_create_accounts.rb}, output
|
2009-07-01 06:07:05 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_migration_error_is_not_shown_on_revoke
|
|
|
|
run_generator
|
|
|
|
error = capture(:stderr){ run_generator ["Account"], :behavior => :revoke }
|
2011-05-18 07:37:57 -04:00
|
|
|
assert_no_match(/Another migration is already named create_accounts/, error)
|
2009-07-01 06:07:05 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_migration_is_removed_on_revoke
|
|
|
|
run_generator
|
|
|
|
run_generator ["Account"], :behavior => :revoke
|
|
|
|
assert_no_migration "db/migrate/create_accounts.rb"
|
|
|
|
end
|
|
|
|
|
2010-09-02 10:25:36 -04:00
|
|
|
def test_existing_migration_is_removed_on_force
|
|
|
|
run_generator
|
|
|
|
old_migration = Dir["#{destination_root}/db/migrate/*_create_accounts.rb"].first
|
|
|
|
error = capture(:stderr) { run_generator ["Account", "--force"] }
|
2011-05-18 07:37:57 -04:00
|
|
|
assert_no_match(/Another migration is already named create_accounts/, error)
|
2010-09-02 10:25:36 -04:00
|
|
|
assert_no_file old_migration
|
|
|
|
assert_migration 'db/migrate/create_accounts.rb'
|
|
|
|
end
|
|
|
|
|
2009-06-27 07:03:07 -04:00
|
|
|
def test_invokes_default_test_framework
|
|
|
|
run_generator
|
|
|
|
assert_file "test/unit/account_test.rb", /class AccountTest < ActiveSupport::TestCase/
|
|
|
|
assert_file "test/fixtures/accounts.yml", /name: MyString/, /age: 1/
|
|
|
|
end
|
|
|
|
|
2009-06-28 05:56:44 -04:00
|
|
|
def test_fixture_is_skipped
|
2009-06-27 07:03:07 -04:00
|
|
|
run_generator ["account", "--skip-fixture"]
|
|
|
|
assert_no_file "test/fixtures/accounts.yml"
|
|
|
|
end
|
|
|
|
|
2009-06-28 05:56:44 -04:00
|
|
|
def test_fixture_is_skipped_if_fixture_replacement_is_given
|
2009-07-03 08:55:37 -04:00
|
|
|
content = run_generator ["account", "-r", "factory_girl"]
|
2011-05-18 07:37:57 -04:00
|
|
|
assert_match(/factory_girl \[not found\]/, content)
|
2009-06-27 08:03:35 -04:00
|
|
|
assert_no_file "test/fixtures/accounts.yml"
|
|
|
|
end
|
|
|
|
|
2009-06-27 07:03:07 -04:00
|
|
|
def test_check_class_collision
|
|
|
|
content = capture(:stderr){ run_generator ["object"] }
|
2011-05-18 07:37:57 -04:00
|
|
|
assert_match(/The name 'Object' is either already used in your application or reserved/, content)
|
2009-06-27 07:03:07 -04:00
|
|
|
end
|
2011-02-01 17:27:26 -05:00
|
|
|
|
|
|
|
def test_index_is_added_for_belongs_to_association
|
|
|
|
run_generator ["account", "supplier:belongs_to"]
|
|
|
|
|
|
|
|
assert_migration "db/migrate/create_accounts.rb" do |m|
|
|
|
|
assert_method :change, m do |up|
|
2011-05-18 07:37:57 -04:00
|
|
|
assert_match(/add_index/, up)
|
2011-02-01 17:27:26 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_index_is_added_for_references_association
|
|
|
|
run_generator ["account", "supplier:references"]
|
|
|
|
|
|
|
|
assert_migration "db/migrate/create_accounts.rb" do |m|
|
|
|
|
assert_method :change, m do |up|
|
2011-05-18 07:37:57 -04:00
|
|
|
assert_match(/add_index/, up)
|
2011-02-01 17:27:26 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_index_is_skipped_for_belongs_to_association
|
|
|
|
run_generator ["account", "supplier:belongs_to", "--no-indexes"]
|
|
|
|
|
|
|
|
assert_migration "db/migrate/create_accounts.rb" do |m|
|
|
|
|
assert_method :change, m do |up|
|
2011-05-18 07:37:57 -04:00
|
|
|
assert_no_match(/add_index/, up)
|
2011-02-01 17:27:26 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_index_is_skipped_for_references_association
|
|
|
|
run_generator ["account", "supplier:references", "--no-indexes"]
|
|
|
|
|
|
|
|
assert_migration "db/migrate/create_accounts.rb" do |m|
|
|
|
|
assert_method :change, m do |up|
|
2011-05-18 07:37:57 -04:00
|
|
|
assert_no_match(/add_index/, up)
|
2011-02-01 17:27:26 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2009-06-27 07:03:07 -04:00
|
|
|
end
|