diff options
Diffstat (limited to 'railties/test')
8 files changed, 111 insertions, 7 deletions
diff --git a/railties/test/application/rake/dbs_test.rb b/railties/test/application/rake/dbs_test.rb index 75b9316593..258066a7e6 100644 --- a/railties/test/application/rake/dbs_test.rb +++ b/railties/test/application/rake/dbs_test.rb @@ -553,6 +553,22 @@ module ApplicationTests end end end + + test "db:prepare setup the database" do + Dir.chdir(app_path) do + rails "generate", "model", "book", "title:string" + output = rails("db:prepare") + assert_match(/CreateBooks: migrated/, output) + + output = rails("db:drop") + assert_match(/Dropped database/, output) + + rails "generate", "model", "recipe", "title:string" + output = rails("db:prepare") + assert_match(/CreateBooks: migrated/, output) + assert_match(/CreateRecipes: migrated/, output) + end + end end end end diff --git a/railties/test/application/rake/multi_dbs_test.rb b/railties/test/application/rake/multi_dbs_test.rb index d676e7486e..147b8f94e1 100644 --- a/railties/test/application/rake/multi_dbs_test.rb +++ b/railties/test/application/rake/multi_dbs_test.rb @@ -137,6 +137,21 @@ module ApplicationTests end end + def db_prepare + Dir.chdir(app_path) do + generate_models_for_animals + output = rails("db:prepare") + + ActiveRecord::Base.configurations.configs_for(env_name: Rails.env).each do |db_config| + if db_config.spec_name == "primary" + assert_match(/CreateBooks: migrated/, output) + else + assert_match(/CreateDogs: migrated/, output) + end + end + end + end + def write_models_for_animals # make a directory for the animals migration FileUtils.mkdir_p("#{app_path}/db/animals_migrate") @@ -226,6 +241,11 @@ module ApplicationTests require "#{app_path}/config/environment" db_migrate_and_schema_cache_dump_and_schema_cache_clear end + + test "db:prepare works on all databases" do + require "#{app_path}/config/environment" + db_prepare + end end end end diff --git a/railties/test/application/zeitwerk_integration_test.rb b/railties/test/application/zeitwerk_integration_test.rb index 5d2e34433a..dc6db429a9 100644 --- a/railties/test/application/zeitwerk_integration_test.rb +++ b/railties/test/application/zeitwerk_integration_test.rb @@ -181,8 +181,8 @@ class ZeitwerkIntegrationTest < ActiveSupport::TestCase boot("production") assert $zeitwerk_integration_test_user - assert !$zeitwerk_integration_test_lib - assert !$zeitwerk_integration_test_extras + assert_not $zeitwerk_integration_test_lib + assert_not $zeitwerk_integration_test_extras assert WebhookHacks assert WebsocketHacks diff --git a/railties/test/generators/generated_attribute_test.rb b/railties/test/generators/generated_attribute_test.rb index 7a1a2ee96b..bf60d6bc22 100644 --- a/railties/test/generators/generated_attribute_test.rb +++ b/railties/test/generators/generated_attribute_test.rb @@ -42,6 +42,12 @@ class GeneratedAttributeTest < Rails::Generators::TestCase assert_field_type :rich_text, :rich_text_area end + def test_field_type_returns_file_field + %w(attachment attachments).each do |attribute_type| + assert_field_type attribute_type, :file_field + end + end + def test_field_type_with_unknown_type_returns_text_field %w(foo bar baz).each do |attribute_type| assert_field_type attribute_type, :text_field @@ -88,7 +94,7 @@ class GeneratedAttributeTest < Rails::Generators::TestCase end def test_default_value_is_nil - %w(references belongs_to rich_text).each do |attribute_type| + %w(references belongs_to rich_text attachment attachments).each do |attribute_type| assert_field_default_value attribute_type, nil end end diff --git a/railties/test/generators/migration_generator_test.rb b/railties/test/generators/migration_generator_test.rb index 8a66956290..540bed551b 100644 --- a/railties/test/generators/migration_generator_test.rb +++ b/railties/test/generators/migration_generator_test.rb @@ -368,32 +368,38 @@ class MigrationGeneratorTest < Rails::Generators::TestCase def test_add_migration_ignores_virtual_attributes migration = "add_rich_text_content_to_messages" - run_generator [migration, "content:rich_text"] + run_generator [migration, "content:rich_text", "video:attachment", "photos:attachments"] assert_migration "db/migrate/#{migration}.rb" do |content| assert_method :change, content do |change| assert_no_match(/add_column :messages, :content, :rich_text/, change) + assert_no_match(/add_column :messages, :video, :attachment/, change) + assert_no_match(/add_column :messages, :photos, :attachments/, change) end end end def test_create_table_migration_ignores_virtual_attributes - run_generator ["create_messages", "content:rich_text"] + run_generator ["create_messages", "content:rich_text", "video:attachment", "photos:attachments"] assert_migration "db/migrate/create_messages.rb" do |content| assert_method :change, content do |change| assert_match(/create_table :messages/, change) assert_no_match(/ t\.rich_text :content/, change) + assert_no_match(/ t\.attachment :video/, change) + assert_no_match(/ t\.attachments :photos/, change) end end end def test_remove_migration_with_virtual_attributes migration = "remove_content_from_messages" - run_generator [migration, "content:rich_text"] + run_generator [migration, "content:rich_text", "video:attachment", "photos:attachments"] assert_migration "db/migrate/#{migration}.rb" do |content| assert_method :change, content do |change| assert_no_match(/remove_column :messages, :content, :rich_text/, change) + assert_no_match(/remove_column :messages, :video, :attachment/, change) + assert_no_match(/remove_column :messages, :photos, :attachments/, change) end end end diff --git a/railties/test/generators/model_generator_test.rb b/railties/test/generators/model_generator_test.rb index 0eb8e9d270..c97cd17ec6 100644 --- a/railties/test/generators/model_generator_test.rb +++ b/railties/test/generators/model_generator_test.rb @@ -509,8 +509,28 @@ class ModelGeneratorTest < Rails::Generators::TestCase 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"] + run_generator ["message", "content:rich_text", "video:attachment", "photos:attachments"] assert_generated_fixture("test/fixtures/messages.yml", "one" => nil, "two" => nil) diff --git a/railties/test/generators/scaffold_controller_generator_test.rb b/railties/test/generators/scaffold_controller_generator_test.rb index fd5aa817b4..1348744b0b 100644 --- a/railties/test/generators/scaffold_controller_generator_test.rb +++ b/railties/test/generators/scaffold_controller_generator_test.rb @@ -80,6 +80,15 @@ class ScaffoldControllerGeneratorTest < Rails::Generators::TestCase end end + def test_controller_permit_attachment_attributes + run_generator ["Message", "video:attachment", "photos:attachments"] + + assert_file "app/controllers/messages_controller.rb" do |content| + assert_match(/def message_params/, content) + assert_match(/params\.require\(:message\)\.permit\(:video, photos: \[\]\)/, content) + end + end + def test_helper_are_invoked_with_a_pluralized_name run_generator assert_file "app/helpers/users_helper.rb", /module UsersHelper/ @@ -276,4 +285,13 @@ class ScaffoldControllerGeneratorTest < Rails::Generators::TestCase assert_no_match(/assert_redirected_to/, content) end end + + def test_api_only_generates_params_for_attachments + run_generator ["Message", "video:attachment", "photos:attachments", "--api"] + + assert_file "app/controllers/messages_controller.rb" do |content| + assert_match(/def message_params/, content) + assert_match(/params\.require\(:message\)\.permit\(:video, photos: \[\]\)/, content) + end + end end diff --git a/railties/test/generators/scaffold_generator_test.rb b/railties/test/generators/scaffold_generator_test.rb index 715ad938f4..bfa52a1beb 100644 --- a/railties/test/generators/scaffold_generator_test.rb +++ b/railties/test/generators/scaffold_generator_test.rb @@ -471,6 +471,24 @@ class ScaffoldGeneratorTest < Rails::Generators::TestCase end end + def test_scaffold_generator_attachments + run_generator ["message", "video:attachment", "photos:attachments", "images:attachments"] + + assert_file "app/models/message.rb", /has_one_attached :video/ + assert_file "app/models/message.rb", /has_many_attached :photos/ + + assert_file "app/controllers/messages_controller.rb" do |content| + assert_instance_method :message_params, content do |m| + assert_match(/permit\(:video, photos: \[\], images: \[\]\)/, m) + end + end + + assert_file "app/views/messages/_form.html.erb" do |content| + assert_match(/^\W{4}<%= form\.file_field :video %>/, content) + assert_match(/^\W{4}<%= form\.file_field :photos, multiple: true %>/, content) + end + end + def test_scaffold_generator_database with_secondary_database_configuration do run_generator ["posts", "--database=secondary"] |