aboutsummaryrefslogtreecommitdiffstats
path: root/railties/test/generators/model_generator_test.rb
blob: d6abaf7a6fe32900c291723b4759a9ea0ea47ba9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
# frozen_string_literal: true

require "generators/generators_test_helper"
require "rails/generators/rails/model/model_generator"

class ModelGeneratorTest < Rails::Generators::TestCase
  include GeneratorsTestHelper
  arguments %w(Account name:string age:integer)

  def setup
    super
    Rails::Generators::ModelHelpers.skip_warn = false
    @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
  end

  def test_help_shows_invoked_generators_options
    content = run_generator ["--help"]
    assert_match(/ActiveRecord options:/, content)
    assert_match(/TestUnit options:/, content)
  end

  def test_model_with_missing_attribute_type
    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
  end

  def test_invokes_default_orm
    run_generator
    assert_file "app/models/account.rb", /class Account < ApplicationRecord/
  end

  def test_model_with_parent_option
    run_generator ["account", "--parent", "Admin::Account"]
    assert_file "app/models/account.rb", /class Account < Admin::Account/
    assert_no_migration "db/migrate/create_accounts.rb"
  end

  def test_plural_names_are_singularized
    content = run_generator ["accounts"]
    assert_file "app/models/account.rb", /class Account < ApplicationRecord/
    assert_file "test/models/account_test.rb", /class AccountTest/
    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)
  end

  def test_unknown_inflection_rule_are_warned
    content = run_generator ["porsche"]
    assert_match("[WARNING] Rails cannot recover singular form from its plural form 'porsches'.\nPlease 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

  def test_model_with_underscored_parent_option
    run_generator ["account", "--parent", "admin/account"]
    assert_file "app/models/account.rb", /class Account < Admin::Account/
  end

  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 < ApplicationRecord/
  end

  def test_migration
    run_generator
    assert_migration "db/migrate/create_accounts.rb", /class CreateAccounts < ActiveRecord::Migration\[[0-9.]+\]/
  end

  def test_migration_with_namespace
    run_generator ["Gallery::Image"]
    assert_migration "db/migrate/create_gallery_images", /class CreateGalleryImages < ActiveRecord::Migration\[[0-9.]+\]/
    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\[[0-9.]+\]/
    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\[[0-9.]+\]/
    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_pluralization
    ActiveRecord::Base.pluralize_table_names = false
    run_generator ["Gallery::Image"]
    assert_migration "db/migrate/create_gallery_image", /class CreateGalleryImage < ActiveRecord::Migration\[[0-9.]+\]/
    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\[[0-9.]+\]/
    assert_no_migration "db/migrate/create_accounts"
  ensure
    ActiveRecord::Base.pluralize_table_names = true
  end

  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"]

    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)
      end
    end
  end

  def test_migration_with_attributes_and_with_index
    run_generator ["product", "name:string:index", "supplier_id:integer:index", "user_id:integer:uniq", "order_id:uniq"]

    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)
        assert_match(/add_index :products, :user_id, unique: true/, up)
        assert_match(/add_index :products, :order_id, unique: true/, up)
      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)

        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)
      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)

        assert_match(/add_index :products, :name/, up)
        assert_match(/add_index :products, :supplier_id/, up)
        assert_no_match(/add_index :products, :year/, up)
      end
    end
  end

  def test_add_migration_with_attributes_index_declaration_and_attribute_options
    run_generator ["product", "title:string{40}:index", "content:string{255}", "price:decimal{5,2}:index", "discount:decimal{5,2}:uniq", "supplier:references{polymorphic}"]

    assert_migration "db/migrate/create_products.rb" do |content|
      assert_method :change, content do |up|
        assert_match(/create_table :products/, up)
        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)
        assert_match(/t.references :supplier, polymorphic: true/, up)
      end
      assert_match(/add_index :products, :title/, content)
      assert_match(/add_index :products, :price/, content)
      assert_match(/add_index :products, :discount, unique: true/, content)
    end
  end

  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\[[0-9.]+\]/

    run_generator ["project"]
    assert_file "db/migrate/002_create_projects.rb", /class CreateProjects < ActiveRecord::Migration\[[0-9.]+\]/
  ensure
    ActiveRecord::Base.timestamped_migrations = true
  end

  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.]+\]/
  ensure
    Rails.application.config.paths["db/migrate"] = old_paths
  end

  def test_model_with_references_attribute_generates_belongs_to_associations
    run_generator ["product", "name:string", "supplier: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:belongs_to"]
    assert_file "app/models/product.rb", /belongs_to :supplier/
  end

  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

  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"]

    assert_migration "db/migrate/create_accounts.rb" do |m|
      assert_method :change, m do |up|
        assert_no_match(/t\.timestamps/, up)
      end
    end
  end

  def test_migration_is_skipped_with_skip_option
    run_generator
    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
  end

  def test_migration_error_is_not_shown_on_revoke
    run_generator
    error = capture(:stderr) { run_generator ["Account"], behavior: :revoke }
    assert_no_match(/Another migration is already named create_accounts/, error)
  end

  def test_migration_is_removed_on_revoke
    run_generator
    run_generator ["Account"], behavior: :revoke
    assert_no_migration "db/migrate/create_accounts.rb"
  end

  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"] }
    assert_no_match(/Another migration is already named create_accounts/, error)
    assert_no_file old_migration
    assert_migration "db/migrate/create_accounts.rb"
  end

  def test_invokes_default_test_framework
    run_generator
    assert_file "test/models/account_test.rb", /class AccountTest < ActiveSupport::TestCase/

    assert_file "test/fixtures/accounts.yml", /name: MyString/, /age: 1/
    assert_generated_fixture("test/fixtures/accounts.yml",
                             "one" => { "name" => "MyString", "age" => 1 }, "two" => { "name" => "MyString", "age" => 1 })
  end

  def test_fixtures_use_the_references_ids
    run_generator ["LineItem", "product:references", "cart:belongs_to"]

    assert_file "test/fixtures/line_items.yml", /product: one\n  cart: one/
    assert_generated_fixture("test/fixtures/line_items.yml",
                             "one" => { "product" => "one", "cart" => "one" }, "two" => { "product" => "two", "cart" => "two" })
  end

  def test_fixtures_use_the_references_ids_and_type
    run_generator ["LineItem", "product:references{polymorphic}", "cart:belongs_to"]

    assert_file "test/fixtures/line_items.yml", /product: one\n  product_type: Product\n  cart: one/
    assert_generated_fixture("test/fixtures/line_items.yml",
                             "one" => { "product" => "one", "product_type" => "Product", "cart" => "one" },
                              "two" => { "product" => "two", "product_type" => "Product", "cart" => "two" })
  end

  def test_fixtures_respect_reserved_yml_keywords
    run_generator ["LineItem", "no:integer", "Off:boolean", "ON:boolean"]

    assert_generated_fixture("test/fixtures/line_items.yml",
                             "one" => { "no" => 1, "Off" => false, "ON" => false }, "two" => { "no" => 1, "Off" => false, "ON" => false })
  end

  def test_fixture_is_skipped
    run_generator ["account", "--skip-fixture"]
    assert_no_file "test/fixtures/accounts.yml"
  end

  def test_fixture_is_skipped_if_fixture_replacement_is_given
    content = run_generator ["account", "-r", "factory_girl"]
    assert_match(/factory_girl \[not found\]/, content)
    assert_no_file "test/fixtures/accounts.yml"
  end

  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",
                             "one" => { "name" => "MyString", "age" => 1 }, "two" => { "name" => "MyString", "age" => 1 })
  ensure
    ActiveRecord::Base.pluralize_table_names = original_pluralize_table_name
  end

  def test_check_class_collision
    content = capture(:stderr) { run_generator ["object"] }
    assert_match(/The name 'Object' is either already used in your application or reserved/, content)
  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|
        assert_no_match(/index: true/, up)
      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|
        assert_no_match(/index: true/, up)
      end
    end
  end

  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

  def test_database_puts_migrations_in_configured_folder
    with_secondary_database_configuration do
      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
      end
    end
  end

  def test_database_puts_migrations_in_configured_folder_with_aliases
    with_secondary_database_configuration do
      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

  def test_polymorphic_belongs_to_generates_correct_model
    run_generator ["account", "supplier:references{polymorphic}"]

    expected_file = <<~FILE
      class Account < ApplicationRecord
        belongs_to :supplier, polymorphic: true
      end
    FILE
    assert_file "app/models/account.rb", expected_file
  end

  def test_passing_required_to_model_generator_is_deprecated
    assert_deprecated do
      run_generator ["account", "supplier:references{required}"]
    end

    assert_migration "db/migrate/create_accounts.rb" do |m|
      assert_method :change, m do |up|
        assert_match(/t\.references :supplier,.*\snull: false/, up)
      end
    end
  end

  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

  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

  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|
        assert_match(/t\.belongs_to :supplier,.*\sforeign_key: true/, up)
        assert_match(/t\.references :user,.*\sforeign_key: true/, up)
      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|
        assert_no_match(/foreign_key/, up)
      end
    end
  end

  def test_token_option_adds_has_secure_token
    run_generator ["user", "token:token", "auth_token:token"]
    expected_file = <<~FILE
      class User < ApplicationRecord
        has_secure_token
        has_secure_token :auth_token
      end
    FILE
    assert_file "app/models/user.rb", expected_file
  end

  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

  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

  def test_skip_virtual_fields_in_fixtures
    run_generator ["message", "content:rich_text", "video:attachment", "photos:attachments"]

    assert_generated_fixture("test/fixtures/messages.yml",
                             "one" => nil, "two" => nil)
  end

  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
end