2017-08-14 13:08:09 -04:00
# frozen_string_literal: true
2016-08-06 13:16:09 -04:00
require " generators/generators_test_helper "
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
2018-08-31 01:12:53 -04:00
def setup
super
Rails :: Generators :: ModelHelpers . skip_warn = false
2019-04-16 02:12:18 -04:00
@old_belongs_to_required_by_default = Rails . application . config . active_record . belongs_to_required_by_default
Rails . application . config . active_record . belongs_to_required_by_default = true
end
def teardown
Rails . application . config . active_record . belongs_to_required_by_default = @old_belongs_to_required_by_default
2018-08-31 01:12:53 -04:00
end
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
2016-06-07 15:41:12 -04:00
assert_file " app/models/account.rb " , / class Account < ApplicationRecord /
2009-06-27 07:03:07 -04:00
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
2020-07-17 10:54:04 -04:00
def test_model_with_database_option
2020-08-02 12:55:57 -04:00
with_database_configuration do
2020-07-17 10:54:04 -04:00
run_generator [ " account " , " --database " , " secondary " ]
assert_file " app/models/secondary_record.rb " , / class SecondaryRecord < ApplicationRecord /
assert_file " app/models/account.rb " , / class Account < SecondaryRecord /
assert_migration " db/secondary_migrate/create_accounts.rb " , / class CreateAccounts < ActiveRecord::Migration \ [[0-9.]+ \ ] /
end
end
def test_model_with_parent_and_database_option
2020-08-02 12:55:57 -04:00
with_database_configuration do
2020-07-17 10:54:04 -04:00
run_generator [ " account " , " --parent " , " Admin::Account " , " --database " , " secondary " ]
assert_file " app/models/account.rb " , / class Account < Admin::Account /
assert_migration " db/secondary_migrate/create_accounts.rb " , / class CreateAccounts < ActiveRecord::Migration \ [[0-9.]+ \ ] /
end
end
def test_model_with_no_migration_and_database_option
2020-08-02 12:55:57 -04:00
with_database_configuration do
2020-07-17 10:54:04 -04:00
run_generator [ " account " , " --migration " , " false " , " --database " , " secondary " ]
assert_file " app/models/account.rb " , / class Account < SecondaryRecord /
assert_no_migration " db/secondary_migrate/create_accounts.rb "
end
end
def test_model_with_no_migration_option
run_generator [ " account " , " --migration " , " false " ]
assert_file " app/models/account.rb " , / class Account < ApplicationRecord /
assert_no_migration " db/migrate/create_accounts.rb "
end
def test_model_with_parent_option_database_option_and_no_migration_option
2020-08-02 12:55:57 -04:00
with_database_configuration do
2020-07-17 10:54:04 -04:00
run_generator [ " account " , " --migration " , " false " , " --database " , " secondary " , " --migration " , " false " , " --parent " , " Admin::Account " ]
assert_file " app/models/account.rb " , / class Account < Admin::Account /
assert_no_migration " db/secondary_migrate/create_accounts.rb "
end
end
2020-08-02 12:55:57 -04:00
def test_model_with_underscored_database_option
with_database_configuration ( " admin_accounts " ) do
run_generator [ " account " , " --database " , " admin_accounts " ]
assert_file " app/models/admin_accounts_record.rb " , / class AdminAccountsRecord < ApplicationRecord /
assert_file " app/models/account.rb " , / class Account < AdminAccountsRecord /
assert_migration " db/admin_accounts_migrate/create_accounts.rb " , / class CreateAccounts < ActiveRecord::Migration \ [[0-9.]+ \ ] /
end
end
2014-02-23 02:40:41 -05:00
def test_plural_names_are_singularized
2018-08-31 01:12:53 -04:00
content = run_generator [ " accounts " ]
2016-06-07 15:41:12 -04:00
assert_file " app/models/account.rb " , / class Account < ApplicationRecord /
2014-02-23 02:40:41 -05:00
assert_file " test/models/account_test.rb " , / class AccountTest /
2014-02-27 11:34:01 -05:00
assert_match ( / \ [WARNING \ ] The model name 'accounts' was recognized as a plural, using the singular 'account' instead \ . Override with --force-plural or setup custom inflection rules for this noun before running the generator \ . / , content )
2014-02-23 02:40:41 -05:00
end
2018-08-31 01:12:53 -04:00
def test_unknown_inflection_rule_are_warned
content = run_generator [ " porsche " ]
assert_match ( " [WARNING] Rails cannot recover singular form from its plural form 'porsches'. \n Please setup custom inflection rules for this noun before running the generator in config/initializers/inflections.rb. " , content )
assert_file " app/models/porsche.rb " , / class Porsche < ApplicationRecord /
uncountable_content = run_generator [ " sheep " ]
assert_no_match ( " [WARNING] Rails cannot recover singular form from its plural form " , uncountable_content )
regular_content = run_generator [ " account " ]
assert_no_match ( " [WARNING] Rails cannot recover singular form from its plural form " , regular_content )
end
2020-07-13 15:25:03 -04:00
def test_impossible_inflection_rules_raises_an_error
content = capture ( :stderr ) { run_generator [ " BFF " ] }
message = << ~ MESSAGE
Rails cannot recover the underscored form from its camelcase form 'BFF' .
Please use an underscored name instead , either 'bff' or 'bf_f' .
Or setup custom inflection rules for this noun before running the generator in config / initializers / inflections . rb .
MESSAGE
assert_match message , content
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_' /
2016-06-07 15:41:12 -04:00
assert_file " app/models/admin/account.rb " , / class Admin::Account < ApplicationRecord /
2010-04-11 04:27:45 -04:00
end
2009-06-28 05:56:44 -04:00
def test_migration
run_generator
2015-12-05 15:13:38 -05:00
assert_migration " db/migrate/create_accounts.rb " , / class CreateAccounts < ActiveRecord::Migration \ [[0-9.]+ \ ] /
2009-06-28 05:56:44 -04:00
end
2009-08-09 06:24:40 -04:00
def test_migration_with_namespace
run_generator [ " Gallery::Image " ]
2015-12-05 15:13:38 -05:00
assert_migration " db/migrate/create_gallery_images " , / class CreateGalleryImages < ActiveRecord::Migration \ [[0-9.]+ \ ] /
2009-08-09 06:24:40 -04:00
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 "
2015-12-05 15:13:38 -05:00
assert_migration " db/migrate/create_admin_gallery_images " , / class CreateAdminGalleryImages < ActiveRecord::Migration \ [[0-9.]+ \ ] /
2009-08-09 06:24:40 -04:00
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 "
2015-12-05 15:13:38 -05:00
assert_migration " db/migrate/create_admin_gallery_image " , / class CreateAdminGalleryImage < ActiveRecord::Migration \ [[0-9.]+ \ ] /
2009-08-09 06:24:40 -04:00
assert_migration " db/migrate/create_admin_gallery_image " , / create_table :admin_gallery_image /
ensure
ActiveRecord :: Base . pluralize_table_names = true
end
2019-02-01 06:12:40 -05:00
def test_migration_with_namespaces_in_model_name_without_pluralization
2009-08-09 06:24:40 -04:00
ActiveRecord :: Base . pluralize_table_names = false
run_generator [ " Gallery::Image " ]
2015-12-05 15:13:38 -05:00
assert_migration " db/migrate/create_gallery_image " , / class CreateGalleryImage < ActiveRecord::Migration \ [[0-9.]+ \ ] /
2009-08-09 06:24:40 -04:00
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
2015-12-05 15:13:38 -05:00
assert_migration " db/migrate/create_account " , / class CreateAccount < ActiveRecord::Migration \ [[0-9.]+ \ ] /
2009-08-09 06:24:40 -04:00
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
2011-08-17 05:16:04 -04:00
def test_migration_with_attributes_and_with_index
2011-12-24 04:38:19 -05:00
run_generator [ " product " , " name:string:index " , " supplier_id:integer:index " , " user_id:integer:uniq " , " order_id:uniq " ]
2011-08-17 05:16:04 -04:00
assert_migration " db/migrate/create_products.rb " do | m |
assert_method :change , m do | up |
assert_match ( / create_table :products / , up )
assert_match ( / t \ .string :name / , up )
assert_match ( / t \ .integer :supplier_id / , up )
assert_match ( / t \ .integer :user_id / , up )
assert_match ( / t \ .string :order_id / , up )
assert_match ( / add_index :products, :name / , up )
assert_match ( / add_index :products, :supplier_id / , up )
2011-12-24 16:06:25 -05:00
assert_match ( / add_index :products, :user_id, unique: true / , up )
assert_match ( / add_index :products, :order_id, unique: true / , up )
2011-08-17 05:16:04 -04:00
end
end
end
def test_migration_with_attributes_and_with_wrong_index_declaration
run_generator [ " product " , " name:string " , " supplier_id:integer:inex " , " user_id:integer:unqu " ]
assert_migration " db/migrate/create_products.rb " do | m |
assert_method :change , m do | up |
assert_match ( / create_table :products / , up )
assert_match ( / t \ .string :name / , up )
assert_match ( / t \ .integer :supplier_id / , up )
assert_match ( / t \ .integer :user_id / , up )
2011-12-24 11:04:32 -05:00
assert_no_match ( / add_index :products, :name / , up )
assert_no_match ( / add_index :products, :supplier_id / , up )
assert_no_match ( / add_index :products, :user_id / , up )
2011-08-17 05:16:04 -04:00
end
end
end
def test_migration_with_missing_attribute_type_and_with_index
run_generator [ " product " , " name:index " , " supplier_id:integer:index " , " year:integer " ]
assert_migration " db/migrate/create_products.rb " do | m |
assert_method :change , m do | up |
assert_match ( / create_table :products / , up )
assert_match ( / t \ .string :name / , up )
assert_match ( / t \ .integer :supplier_id / , up )
2012-10-08 00:59:42 -04:00
2011-08-17 05:16:04 -04:00
assert_match ( / add_index :products, :name / , up )
assert_match ( / add_index :products, :supplier_id / , up )
2011-12-24 11:04:32 -05:00
assert_no_match ( / add_index :products, :year / , up )
2011-08-17 05:16:04 -04:00
end
end
end
def test_add_migration_with_attributes_index_declaration_and_attribute_options
2012-06-25 14:01:31 -04:00
run_generator [ " product " , " title:string{40}:index " , " content:string{255} " , " price:decimal{5,2}:index " , " discount:decimal{5,2}:uniq " , " supplier:references{polymorphic} " ]
2011-08-17 05:16:04 -04:00
assert_migration " db/migrate/create_products.rb " do | content |
assert_method :change , content do | up |
assert_match ( / create_table :products / , up )
2011-12-24 16:06:25 -05:00
assert_match ( / t.string :title, limit: 40 / , up )
assert_match ( / t.string :content, limit: 255 / , up )
assert_match ( / t.decimal :price, precision: 5, scale: 2 / , up )
2012-06-25 14:01:31 -04:00
assert_match ( / t.references :supplier, polymorphic: true / , up )
2011-08-17 05:16:04 -04:00
end
assert_match ( / add_index :products, :title / , content )
assert_match ( / add_index :products, :price / , content )
2011-12-24 16:06:25 -05:00
assert_match ( / add_index :products, :discount, unique: true / , content )
2011-08-17 05:16:04 -04:00
end
end
2012-10-08 00:59:42 -04:00
2009-07-13 16:26:58 -04:00
def test_migration_without_timestamps
ActiveRecord :: Base . timestamped_migrations = false
run_generator [ " account " ]
2016-10-28 23:05:58 -04:00
assert_file " db/migrate/001_create_accounts.rb " , / class CreateAccounts < ActiveRecord::Migration \ [[0-9.]+ \ ] /
2009-07-13 16:26:58 -04:00
run_generator [ " project " ]
2016-10-28 23:05:58 -04:00
assert_file " db/migrate/002_create_projects.rb " , / class CreateProjects < ActiveRecord::Migration \ [[0-9.]+ \ ] /
2009-07-13 16:26:58 -04:00
ensure
ActiveRecord :: Base . timestamped_migrations = true
end
2017-01-13 11:02:02 -05:00
def test_migration_with_configured_path
old_paths = Rails . application . config . paths [ " db/migrate " ]
Rails . application . config . paths . add " db/migrate " , with : " db2/migrate "
run_generator
assert_migration " db2/migrate/create_accounts.rb " , / class CreateAccounts < ActiveRecord::Migration \ [[0-9.]+ \ ] /
2017-01-17 13:06:59 -05:00
ensure
Rails . application . config . paths [ " db/migrate " ] = old_paths
2017-01-13 11:02:02 -05:00
end
2009-06-28 05:56:44 -04:00
def test_model_with_references_attribute_generates_belongs_to_associations
2012-06-25 14:01:31 -04:00
run_generator [ " product " , " name:string " , " supplier:references " ]
2009-06-28 05:56:44 -04:00
assert_file " app/models/product.rb " , / belongs_to :supplier /
end
def test_model_with_belongs_to_attribute_generates_belongs_to_associations
2012-06-25 14:01:31 -04:00
run_generator [ " product " , " name:string " , " supplier:belongs_to " ]
2009-06-28 05:56:44 -04:00
assert_file " app/models/product.rb " , / belongs_to :supplier /
end
2012-06-25 14:01:31 -04:00
def test_model_with_polymorphic_references_attribute_generates_belongs_to_associations
run_generator [ " product " , " name:string " , " supplier:references{polymorphic} " ]
assert_file " app/models/product.rb " , / belongs_to :supplier, polymorphic: true /
end
def test_model_with_polymorphic_belongs_to_attribute_generates_belongs_to_associations
run_generator [ " product " , " name:string " , " supplier:belongs_to{polymorphic} " ]
assert_file " app/models/product.rb " , / belongs_to :supplier, polymorphic: true /
end
2009-06-28 05:56:44 -04:00
def test_migration_with_timestamps
run_generator
2017-05-06 15:08:58 -04:00
assert_migration " db/migrate/create_accounts.rb " , / t \ .timestamps /
2009-06-28 05:56:44 -04:00
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 |
2017-05-06 15:08:58 -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 " ]
2017-05-06 15:08:58 -04:00
assert_match %r{ skip \ s+db/migrate/ \ d+_create_accounts \ .rb } , output
2010-12-10 16:16:34 -05:00
end
def test_migration_is_ignored_as_identical_with_skip_option
run_generator [ " Account " ]
output = run_generator [ " Account " , " --skip " ]
2017-05-06 15:08:58 -04:00
assert_match %r{ identical \ s+db/migrate/ \ d+_create_accounts \ .rb } , output
2010-12-10 16:16:34 -05:00
end
def test_migration_is_skipped_on_skip_behavior
run_generator
2012-10-14 06:03:39 -04:00
output = run_generator [ " Account " ] , behavior : :skip
2017-05-06 15:08:58 -04:00
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
2016-08-16 03:30:11 -04:00
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
2012-10-14 06:03:39 -04:00
run_generator [ " Account " ] , behavior : :revoke
2009-07-01 06:07:05 -04:00
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
2012-12-26 10:23:39 -05:00
assert_migration " db/migrate/create_accounts.rb "
2010-09-02 10:25:36 -04:00
end
2009-06-27 07:03:07 -04:00
def test_invokes_default_test_framework
run_generator
2012-10-08 00:59:42 -04:00
assert_file " test/models/account_test.rb " , / class AccountTest < ActiveSupport::TestCase /
2012-12-26 09:16:09 -05:00
2012-12-26 10:23:39 -05:00
assert_file " test/fixtures/accounts.yml " , / name: MyString / , / age: 1 /
assert_generated_fixture ( " test/fixtures/accounts.yml " ,
2016-10-28 23:05:58 -04:00
" one " = > { " name " = > " MyString " , " age " = > 1 } , " two " = > { " name " = > " MyString " , " age " = > 1 } )
2009-06-27 07:03:07 -04:00
end
2012-12-09 12:34:26 -05:00
def test_fixtures_use_the_references_ids
run_generator [ " LineItem " , " product:references " , " cart:belongs_to " ]
2012-12-26 09:16:09 -05:00
2016-02-05 22:15:28 -05:00
assert_file " test/fixtures/line_items.yml " , / product: one \ n cart: one /
2012-12-26 10:23:39 -05:00
assert_generated_fixture ( " test/fixtures/line_items.yml " ,
2016-10-28 23:05:58 -04:00
" one " = > { " product " = > " one " , " cart " = > " one " } , " two " = > { " product " = > " two " , " cart " = > " two " } )
2012-12-09 12:34:26 -05:00
end
2012-12-09 13:20:32 -05:00
def test_fixtures_use_the_references_ids_and_type
run_generator [ " LineItem " , " product:references{polymorphic} " , " cart:belongs_to " ]
2012-12-26 09:16:09 -05:00
2016-02-05 22:15:28 -05:00
assert_file " test/fixtures/line_items.yml " , / product: one \ n product_type: Product \ n cart: one /
2012-12-26 10:23:39 -05:00
assert_generated_fixture ( " test/fixtures/line_items.yml " ,
2016-10-28 23:05:58 -04:00
" one " = > { " product " = > " one " , " product_type " = > " Product " , " cart " = > " one " } ,
" two " = > { " product " = > " two " , " product_type " = > " Product " , " cart " = > " two " } )
2012-12-26 09:16:09 -05:00
end
def test_fixtures_respect_reserved_yml_keywords
2012-12-26 10:23:39 -05:00
run_generator [ " LineItem " , " no:integer " , " Off:boolean " , " ON:boolean " ]
2012-12-26 09:16:09 -05:00
2012-12-26 10:23:39 -05:00
assert_generated_fixture ( " test/fixtures/line_items.yml " ,
2016-10-28 23:05:58 -04:00
" one " = > { " no " = > 1 , " Off " = > false , " ON " = > false } , " two " = > { " no " = > 1 , " Off " = > false , " ON " = > false } )
2012-12-09 13:20:32 -05:00
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
2015-03-26 21:08:04 -04:00
def test_fixture_without_pluralization
original_pluralize_table_name = ActiveRecord :: Base . pluralize_table_names
ActiveRecord :: Base . pluralize_table_names = false
run_generator
assert_generated_fixture ( " test/fixtures/account.yml " ,
2016-10-28 23:05:58 -04:00
" one " = > { " name " = > " MyString " , " age " = > 1 } , " two " = > { " name " = > " MyString " , " age " = > 1 } )
2015-03-26 21:08:04 -04:00
ensure
ActiveRecord :: Base . pluralize_table_names = original_pluralize_table_name
end
2009-06-27 07:03:07 -04:00
def test_check_class_collision
2016-08-16 03:30:11 -04:00
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_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 |
2012-04-16 00:14:50 -04:00
assert_no_match ( / index: true / , 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 |
2012-04-16 00:14:50 -04:00
assert_no_match ( / index: true / , up )
2011-02-01 17:27:26 -05:00
end
end
end
2012-12-26 09:16:09 -05:00
2015-10-23 19:30:26 -04:00
def test_add_uuid_to_create_table_migration
run_generator [ " account " , " --primary_key_type=uuid " ]
assert_migration " db/migrate/create_accounts.rb " do | content |
assert_method :change , content do | change |
assert_match ( / create_table :accounts, id: :uuid / , change )
end
end
end
2018-09-28 13:36:06 -04:00
def test_database_puts_migrations_in_configured_folder
2020-08-02 12:55:57 -04:00
with_database_configuration do
2018-09-28 13:36:06 -04:00
run_generator [ " account " , " --database=secondary " ]
assert_migration " db/secondary_migrate/create_accounts.rb " do | content |
assert_method :change , content do | change |
assert_match ( / create_table :accounts / , change )
end
2018-09-26 17:16:54 -04:00
end
end
end
2019-03-20 01:51:00 -04:00
def test_database_puts_migrations_in_configured_folder_with_aliases
2020-08-02 12:55:57 -04:00
with_database_configuration do
2019-03-20 01:51:00 -04:00
run_generator [ " account " , " --db=secondary " ]
assert_migration " db/secondary_migrate/create_accounts.rb " do | content |
assert_method :change , content do | change |
assert_match ( / create_table :accounts / , change )
end
end
end
end
2019-04-19 20:30:22 -04:00
def test_polymorphic_belongs_to_generates_correct_model
2019-04-16 02:12:18 -04:00
run_generator [ " account " , " supplier:references{polymorphic} " ]
2014-07-04 17:16:00 -04:00
2018-02-16 19:28:30 -05:00
expected_file = << ~ FILE
class Account < ApplicationRecord
2019-04-15 10:10:38 -04:00
belongs_to :supplier , polymorphic : true
2018-02-16 19:28:30 -05:00
end
2014-07-04 17:16:00 -04:00
FILE
assert_file " app/models/account.rb " , expected_file
end
2019-04-16 02:12:18 -04:00
def test_null_false_is_added_for_references_by_default
run_generator [ " account " , " user:references " ]
assert_migration " db/migrate/create_accounts.rb " do | m |
assert_method :change , m do | up |
assert_match ( / t \ .references :user,.* \ snull: false / , up )
end
end
end
def test_null_false_is_added_for_belongs_to_by_default
run_generator [ " account " , " user:belongs_to " ]
assert_migration " db/migrate/create_accounts.rb " do | m |
assert_method :change , m do | up |
assert_match ( / t \ .belongs_to :user,.* \ snull: false / , up )
end
end
end
def test_null_false_is_not_added_when_belongs_to_required_by_default_global_config_is_false
Rails . application . config . active_record . belongs_to_required_by_default = false
run_generator [ " account " , " user:belongs_to " ]
assert_migration " db/migrate/create_accounts.rb " do | m |
assert_method :change , m do | up |
assert_match ( / t \ .belongs_to :user / , up )
end
end
end
Use the new `foreign_key` option on `references` in generators
Changes `rails g model Post user:references` from
def change
create_table :posts do |t|
t.references :user, index: true
end
add_foreign_key :posts, :users
end
to
def change
create_table :posts do |t|
t.references :user, index: true, foreign_key: true
end
end
Changes `rails g migration add_user_to_posts user:references` from
def change
add_reference :posts, :users, index: true
add_foreign_key :posts, :users
end
to
def change
add_reference :posts, :users, index: true, foreign_key: true
end
2014-12-22 15:36:20 -05:00
def test_foreign_key_is_not_added_for_non_references
run_generator [ " account " , " supplier:string " ]
assert_migration " db/migrate/create_accounts.rb " do | m |
assert_method :change , m do | up |
assert_no_match ( / foreign_key / , up )
end
end
end
2014-11-24 21:25:15 -05:00
def test_foreign_key_is_added_for_references
run_generator [ " account " , " supplier:belongs_to " , " user:references " ]
assert_migration " db/migrate/create_accounts.rb " do | m |
assert_method :change , m do | up |
Use the new `foreign_key` option on `references` in generators
Changes `rails g model Post user:references` from
def change
create_table :posts do |t|
t.references :user, index: true
end
add_foreign_key :posts, :users
end
to
def change
create_table :posts do |t|
t.references :user, index: true, foreign_key: true
end
end
Changes `rails g migration add_user_to_posts user:references` from
def change
add_reference :posts, :users, index: true
add_foreign_key :posts, :users
end
to
def change
add_reference :posts, :users, index: true, foreign_key: true
end
2014-12-22 15:36:20 -05:00
assert_match ( / t \ .belongs_to :supplier,.* \ sforeign_key: true / , up )
assert_match ( / t \ .references :user,.* \ sforeign_key: true / , up )
2014-11-24 21:25:15 -05:00
end
end
end
def test_foreign_key_is_skipped_for_polymorphic_references
run_generator [ " account " , " supplier:belongs_to{polymorphic} " ]
assert_migration " db/migrate/create_accounts.rb " do | m |
assert_method :change , m do | up |
Use the new `foreign_key` option on `references` in generators
Changes `rails g model Post user:references` from
def change
create_table :posts do |t|
t.references :user, index: true
end
add_foreign_key :posts, :users
end
to
def change
create_table :posts do |t|
t.references :user, index: true, foreign_key: true
end
end
Changes `rails g migration add_user_to_posts user:references` from
def change
add_reference :posts, :users, index: true
add_foreign_key :posts, :users
end
to
def change
add_reference :posts, :users, index: true, foreign_key: true
end
2014-12-22 15:36:20 -05:00
assert_no_match ( / foreign_key / , up )
2014-11-24 21:25:15 -05:00
end
end
end
2015-01-11 14:06:41 -05:00
def test_token_option_adds_has_secure_token
run_generator [ " user " , " token:token " , " auth_token:token " ]
2018-02-16 19:28:30 -05:00
expected_file = << ~ FILE
class User < ApplicationRecord
has_secure_token
has_secure_token :auth_token
end
2015-01-11 14:06:41 -05:00
FILE
assert_file " app/models/user.rb " , expected_file
end
2019-03-28 10:16:47 -04:00
def test_model_with_rich_text_attribute_adds_has_rich_text
run_generator [ " message " , " content:rich_text " ]
expected_file = << ~ FILE
class Message < ApplicationRecord
has_rich_text :content
end
FILE
assert_file " app/models/message.rb " , expected_file
end
2019-03-30 12:54:47 -04:00
def test_model_with_attachment_attribute_adds_has_one_attached
run_generator [ " message " , " video:attachment " ]
expected_file = << ~ FILE
class Message < ApplicationRecord
has_one_attached :video
end
FILE
assert_file " app/models/message.rb " , expected_file
end
def test_model_with_attachments_attribute_adds_has_many_attached
run_generator [ " message " , " photos:attachments " ]
expected_file = << ~ FILE
class Message < ApplicationRecord
has_many_attached :photos
end
FILE
assert_file " app/models/message.rb " , expected_file
end
2019-03-28 10:16:47 -04:00
def test_skip_virtual_fields_in_fixtures
2019-03-30 12:54:47 -04:00
run_generator [ " message " , " content:rich_text " , " video:attachment " , " photos:attachments " ]
2019-03-28 10:16:47 -04:00
assert_generated_fixture ( " test/fixtures/messages.yml " ,
" one " = > nil , " two " = > nil )
end
2012-12-26 09:16:09 -05:00
private
def assert_generated_fixture ( path , parsed_contents )
fixture_file = File . new File . expand_path ( path , destination_root )
assert_equal ( parsed_contents , YAML . load ( fixture_file ) )
end
2009-06-27 07:03:07 -04:00
end