diff options
Diffstat (limited to 'railties/test')
-rw-r--r-- | railties/test/application/paths_test.rb | 2 | ||||
-rw-r--r-- | railties/test/application/queue_test.rb | 85 | ||||
-rw-r--r-- | railties/test/application/rake_test.rb | 18 | ||||
-rw-r--r-- | railties/test/application/routes_inspect_test.rb (renamed from railties/test/application/route_inspect_test.rb) | 6 | ||||
-rw-r--r-- | railties/test/application/routing_test.rb | 83 | ||||
-rw-r--r-- | railties/test/engine_test.rb | 10 | ||||
-rw-r--r-- | railties/test/generators/generated_attribute_test.rb | 23 | ||||
-rw-r--r-- | railties/test/generators/model_generator_test.rb | 17 | ||||
-rw-r--r-- | railties/test/generators/namespaced_generators_test.rb | 2 | ||||
-rw-r--r-- | railties/test/generators/plugin_new_generator_test.rb | 8 | ||||
-rw-r--r-- | railties/test/generators_test.rb | 1 | ||||
-rw-r--r-- | railties/test/isolation/abstract_unit.rb | 16 | ||||
-rw-r--r-- | railties/test/paths_test.rb | 9 | ||||
-rw-r--r-- | railties/test/queueing/test_queue_test.rb | 85 | ||||
-rw-r--r-- | railties/test/railties/engine_test.rb | 2 | ||||
-rw-r--r-- | railties/test/railties/generators_test.rb | 12 |
16 files changed, 208 insertions, 171 deletions
diff --git a/railties/test/application/paths_test.rb b/railties/test/application/paths_test.rb index e0893f53be..4029984ce9 100644 --- a/railties/test/application/paths_test.rb +++ b/railties/test/application/paths_test.rb @@ -50,8 +50,6 @@ module ApplicationTests assert_path @paths["config/locales"], "config/locales/en.yml" assert_path @paths["config/environment"], "config/environment.rb" assert_path @paths["config/environments"], "config/environments/development.rb" - assert_path @paths["config/routes.rb"], "config/routes.rb" - assert_path @paths["config/routes"], "config/routes" assert_equal root("app", "controllers"), @paths["app/controllers"].expanded.first end diff --git a/railties/test/application/queue_test.rb b/railties/test/application/queue_test.rb index da8bdeed52..22d6c20404 100644 --- a/railties/test/application/queue_test.rb +++ b/railties/test/application/queue_test.rb @@ -30,46 +30,68 @@ module ApplicationTests assert_kind_of Rails::Queueing::Queue, Rails.queue end - test "in development mode, an enqueued job will be processed in a separate thread" do - app("development") + class ThreadTrackingJob + def initialize + @origin = Thread.current.object_id + end + + def run + @target = Thread.current.object_id + end - job = Struct.new(:origin, :target).new(Thread.current) - def job.run - self.target = Thread.current + def ran_in_different_thread? + @origin != @target end + def ran? + @target + end + end + + test "in development mode, an enqueued job will be processed in a separate thread" do + app("development") + + job = ThreadTrackingJob.new Rails.queue.push job sleep 0.1 - assert job.target, "The job was run" - assert_not_equal job.origin, job.target + assert job.ran?, "Expected job to be run" + assert job.ran_in_different_thread?, "Expected job to run in a different thread" end test "in test mode, explicitly draining the queue will process it in a separate thread" do app("test") - job = Struct.new(:origin, :target).new(Thread.current) - def job.run - self.target = Thread.current + Rails.queue.push ThreadTrackingJob.new + job = Rails.queue.jobs.last + Rails.queue.drain + + assert job.ran?, "Expected job to be run" + assert job.ran_in_different_thread?, "Expected job to run in a different thread" + end + + class IdentifiableJob + def initialize(id) + @id = id end - Rails.queue.push job - Rails.queue.drain + def ==(other) + other.same_id?(@id) + end + + def same_id?(other_id) + other_id == @id + end - assert job.target, "The job was run" - assert_not_equal job.origin, job.target + def run + end end test "in test mode, the queue can be observed" do app("test") - job = Struct.new(:id) do - def run - end - end - jobs = (1..10).map do |id| - job.new(id) + IdentifiableJob.new(id) end jobs.each do |job| @@ -79,6 +101,29 @@ module ApplicationTests assert_equal jobs, Rails.queue.jobs end + test "in test mode, adding an unmarshallable job will raise an exception" do + app("test") + anonymous_class_instance = Struct.new(:run).new + assert_raises TypeError do + Rails.queue.push anonymous_class_instance + end + end + + test "attempting to marshal a queue will raise an exception" do + app("test") + assert_raises TypeError do + Marshal.dump Rails.queue + end + end + + test "attempting to add a reference to itself to the queue will raise an exception" do + app("test") + job = {reference: Rails.queue} + assert_raises TypeError do + Rails.queue.push job + end + end + def setup_custom_queue add_to_env_config "production", <<-RUBY require "my_queue" diff --git a/railties/test/application/rake_test.rb b/railties/test/application/rake_test.rb index e0ee349550..a450f90dbf 100644 --- a/railties/test/application/rake_test.rb +++ b/railties/test/application/rake_test.rb @@ -144,6 +144,24 @@ module ApplicationTests assert_no_match(/Errors running/, output) end + def test_db_test_clone_when_using_sql_format + add_to_config "config.active_record.schema_format = :sql" + output = Dir.chdir(app_path) do + `rails generate scaffold user username:string; + bundle exec rake db:migrate db:test:clone 2>&1 --trace` + end + assert_match(/Execute db:test:clone_structure/, output) + end + + def test_db_test_prepare_when_using_sql_format + add_to_config "config.active_record.schema_format = :sql" + output = Dir.chdir(app_path) do + `rails generate scaffold user username:string; + bundle exec rake db:migrate db:test:prepare 2>&1 --trace` + end + assert_match(/Execute db:test:load_structure/, output) + end + def test_rake_dump_structure_should_respect_db_structure_env_variable Dir.chdir(app_path) do # ensure we have a schema_migrations table to dump diff --git a/railties/test/application/route_inspect_test.rb b/railties/test/application/routes_inspect_test.rb index 3b8c874b5b..68a8afc93e 100644 --- a/railties/test/application/route_inspect_test.rb +++ b/railties/test/application/routes_inspect_test.rb @@ -1,13 +1,13 @@ require 'minitest/autorun' -require 'rails/application/route_inspector' +require 'rails/application/routes_inspector' require 'action_controller' require 'rails/engine' module ApplicationTests - class RouteInspectTest < ActiveSupport::TestCase + class RoutesInspectTest < ActiveSupport::TestCase def setup @set = ActionDispatch::Routing::RouteSet.new - @inspector = Rails::Application::RouteInspector.new + @inspector = Rails::Application::RoutesInspector.new app = ActiveSupport::OrderedOptions.new app.config = ActiveSupport::OrderedOptions.new app.config.assets = ActiveSupport::OrderedOptions.new diff --git a/railties/test/application/routing_test.rb b/railties/test/application/routing_test.rb index d1373ba202..396b1849d8 100644 --- a/railties/test/application/routing_test.rb +++ b/railties/test/application/routing_test.rb @@ -178,90 +178,7 @@ module ApplicationTests assert_equal 'WIN', last_response.body end - test "routes drawing from config/routes" do - app_file 'config/routes.rb', <<-RUBY - AppTemplate::Application.routes.draw do - draw :external - end - RUBY - - app_file 'config/routes/external.rb', <<-RUBY - get ':controller/:action' - RUBY - - controller :success, <<-RUBY - class SuccessController < ActionController::Base - def index - render :text => "success!" - end - end - RUBY - - app 'development' - get '/success/index' - assert_equal 'success!', last_response.body - end - {"development" => "baz", "production" => "bar"}.each do |mode, expected| - test "reloads routes when external configuration is changed in #{mode}" do - controller :foo, <<-RUBY - class FooController < ApplicationController - def bar - render :text => "bar" - end - - def baz - render :text => "baz" - end - end - RUBY - - app_file 'config/routes.rb', <<-RUBY - AppTemplate::Application.routes.draw do - draw :external - end - RUBY - - app_file 'config/routes/external.rb', <<-RUBY - get 'foo', :to => 'foo#bar' - RUBY - - app(mode) - - get '/foo' - assert_equal 'bar', last_response.body - - app_file 'config/routes/external.rb', <<-RUBY - get 'foo', :to => 'foo#baz' - RUBY - - sleep 0.1 - - get '/foo' - assert_equal expected, last_response.body - - app_file 'config/routes.rb', <<-RUBY - AppTemplate::Application.routes.draw do - draw :external - draw :other_external - end - RUBY - - app_file 'config/routes/other_external.rb', <<-RUBY - get 'win', :to => 'foo#baz' - RUBY - - sleep 0.1 - - get '/win' - - if mode == "development" - assert_equal expected, last_response.body - else - assert_equal 404, last_response.status - end - end - test "reloads routes when configuration is changed in #{mode}" do controller :foo, <<-RUBY class FooController < ApplicationController diff --git a/railties/test/engine_test.rb b/railties/test/engine_test.rb index 68406dce4c..addf49cdb6 100644 --- a/railties/test/engine_test.rb +++ b/railties/test/engine_test.rb @@ -11,14 +11,4 @@ class EngineTest < ActiveSupport::TestCase assert !engine.routes? end - - it "does not add more paths to routes on each call" do - engine = Class.new(Rails::Engine) - - engine.routes - length = engine.routes.draw_paths.length - - engine.routes - assert_equal length, engine.routes.draw_paths.length - end end diff --git a/railties/test/generators/generated_attribute_test.rb b/railties/test/generators/generated_attribute_test.rb index 6e3fc84781..6ab1cd58c7 100644 --- a/railties/test/generators/generated_attribute_test.rb +++ b/railties/test/generators/generated_attribute_test.rb @@ -102,22 +102,28 @@ class GeneratedAttributeTest < Rails::Generators::TestCase def test_reference_is_true %w(references belongs_to).each do |attribute_type| - assert_equal( - true, - create_generated_attribute(attribute_type).reference? - ) + assert create_generated_attribute(attribute_type).reference? end end def test_reference_is_false %w(foo bar baz).each do |attribute_type| - assert_equal( - false, - create_generated_attribute(attribute_type).reference? - ) + assert !create_generated_attribute(attribute_type).reference? end end + def test_polymorphic_reference_is_true + %w(references belongs_to).each do |attribute_type| + assert create_generated_attribute("#{attribute_type}{polymorphic}").polymorphic? + end + end + + def test_polymorphic_reference_is_false + %w(foo bar baz).each do |attribute_type| + assert !create_generated_attribute("#{attribute_type}{polymorphic}").polymorphic? + end + end + def test_blank_type_defaults_to_string_raises_exception assert_equal :string, create_generated_attribute(nil, 'title').type assert_equal :string, create_generated_attribute("", 'title').type @@ -126,5 +132,6 @@ class GeneratedAttributeTest < Rails::Generators::TestCase def test_handles_index_names_for_references assert_equal "post", create_generated_attribute('string', 'post').index_name assert_equal "post_id", create_generated_attribute('references', 'post').index_name + assert_equal ["post_id", "post_type"], create_generated_attribute('references{polymorphic}', 'post').index_name end end diff --git a/railties/test/generators/model_generator_test.rb b/railties/test/generators/model_generator_test.rb index fd3b8c8a17..ec33bd7c6b 100644 --- a/railties/test/generators/model_generator_test.rb +++ b/railties/test/generators/model_generator_test.rb @@ -166,7 +166,7 @@ class ModelGeneratorTest < Rails::Generators::TestCase 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"] + 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| @@ -174,6 +174,7 @@ class ModelGeneratorTest < Rails::Generators::TestCase 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) @@ -193,15 +194,25 @@ class ModelGeneratorTest < Rails::Generators::TestCase end def test_model_with_references_attribute_generates_belongs_to_associations - run_generator ["product", "name:string", "supplier_id:references"] + 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_id:belongs_to"] + 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/ diff --git a/railties/test/generators/namespaced_generators_test.rb b/railties/test/generators/namespaced_generators_test.rb index 2ae9dc61a7..db2b8af217 100644 --- a/railties/test/generators/namespaced_generators_test.rb +++ b/railties/test/generators/namespaced_generators_test.rb @@ -99,7 +99,7 @@ class NamespacedModelGeneratorTest < NamespacedGeneratorTestCase run_generator ["admin/account"] assert_file "app/models/test_app/admin.rb", /module TestApp/, /module Admin/ assert_file "app/models/test_app/admin.rb", /def self\.table_name_prefix/ - assert_file "app/models/test_app/admin.rb", /'admin_'/ + assert_file "app/models/test_app/admin.rb", /'test_app_admin_'/ assert_file "app/models/test_app/admin/account.rb", /module TestApp/, /class Admin::Account < ActiveRecord::Base/ end diff --git a/railties/test/generators/plugin_new_generator_test.rb b/railties/test/generators/plugin_new_generator_test.rb index 0a235b56d5..6c3e0178f2 100644 --- a/railties/test/generators/plugin_new_generator_test.rb +++ b/railties/test/generators/plugin_new_generator_test.rb @@ -264,11 +264,14 @@ class PluginNewGeneratorTest < Rails::Generators::TestCase assert_file "spec/dummy" assert_file "spec/dummy/config/application.rb" assert_no_file "test" + assert_file '.gitignore' do |contents| + assert_match(/spec\/dummy/, contents) + end end def test_ensure_that_gitignore_can_be_generated_from_a_template_for_dummy_path FileUtils.cd(Rails.root) - run_generator([destination_root, "--dummy_path", "spec/dummy" "--skip-test-unit"]) + run_generator([destination_root, "--dummy_path", "spec/dummy", "--skip-test-unit"]) assert_file ".gitignore" do |contents| assert_match(/spec\/dummy/, contents) end @@ -280,6 +283,9 @@ class PluginNewGeneratorTest < Rails::Generators::TestCase assert_file "bukkits.gemspec" do |contents| assert_no_match(/s.test_files = Dir\["test\/\*\*\/\*"\]/, contents) end + assert_file '.gitignore' do |contents| + assert_no_match(/test\dummy/, contents) + end end def test_skipping_gemspec diff --git a/railties/test/generators_test.rb b/railties/test/generators_test.rb index 417d019178..ad2f85a5ff 100644 --- a/railties/test/generators_test.rb +++ b/railties/test/generators_test.rb @@ -1,7 +1,6 @@ require 'generators/generators_test_helper' require 'rails/generators/rails/model/model_generator' require 'rails/generators/test_unit/model/model_generator' -require 'mocha' class GeneratorsTest < Rails::Generators::TestCase include GeneratorsTestHelper diff --git a/railties/test/isolation/abstract_unit.rb b/railties/test/isolation/abstract_unit.rb index 800b1c90f0..6071cd3f39 100644 --- a/railties/test/isolation/abstract_unit.rb +++ b/railties/test/isolation/abstract_unit.rb @@ -273,24 +273,18 @@ end # Create a scope and build a fixture rails app Module.new do extend TestHelpers::Paths + # Build a rails app - if File.exist?(app_template_path) - FileUtils.rm_rf(app_template_path) - end + FileUtils.rm_rf(app_template_path) FileUtils.mkdir(app_template_path) environment = File.expand_path('../../../../load_paths', __FILE__) - if File.exist?("#{environment}.rb") - require_environment = "-r #{environment}" - end + require_environment = "-r #{environment}" `#{Gem.ruby} #{require_environment} #{RAILS_FRAMEWORK_ROOT}/railties/bin/rails new #{app_template_path}` + File.open("#{app_template_path}/config/boot.rb", 'w') do |f| - if require_environment - f.puts "Dir.chdir('#{File.dirname(environment)}') do" - f.puts " require '#{environment}'" - f.puts "end" - end + f.puts "require '#{environment}'" f.puts "require 'rails/all'" end end unless defined?(RAILS_ISOLATED_ENGINE) diff --git a/railties/test/paths_test.rb b/railties/test/paths_test.rb index 5d6b6f9f72..76ff3ec3e4 100644 --- a/railties/test/paths_test.rb +++ b/railties/test/paths_test.rb @@ -29,7 +29,6 @@ class PathsTest < ActiveSupport::TestCase test "creating a root level path" do @root.add "app" assert_equal ["/foo/bar/app"], @root["app"].to_a - assert_equal ["/foo/bar/app"], @root["app"].paths end test "creating a root level path with options" do @@ -192,7 +191,6 @@ class PathsTest < ActiveSupport::TestCase @root["app"] = "/app" @root["app"].glob = "*.rb" assert_equal "*.rb", @root["app"].glob - assert_equal ["/foo/bar/app"], @root["app"].paths end test "it should be possible to override a path's default glob without assignment" do @@ -200,6 +198,13 @@ class PathsTest < ActiveSupport::TestCase assert_equal "*.rb", @root["app"].glob end + test "it should be possible to replace a path and persist the original paths glob" do + @root.add "app", :glob => "*.rb" + @root["app"] = "app2" + assert_equal ["/foo/bar/app2"], @root["app"].to_a + assert_equal "*.rb", @root["app"].glob + end + test "a path can be added to the load path" do @root["app"] = "app" @root["app"].load_path! diff --git a/railties/test/queueing/test_queue_test.rb b/railties/test/queueing/test_queue_test.rb index 78c6c617fe..2f0f507adb 100644 --- a/railties/test/queueing/test_queue_test.rb +++ b/railties/test/queueing/test_queue_test.rb @@ -2,22 +2,18 @@ require 'abstract_unit' require 'rails/queueing' class TestQueueTest < ActiveSupport::TestCase - class Job - def initialize(&block) - @block = block - end + def setup + @queue = Rails::Queueing::TestQueue.new + end + class ExceptionRaisingJob def run - @block.call if @block + raise end end - def setup - @queue = Rails::Queueing::TestQueue.new - end - def test_drain_raises - @queue.push Job.new { raise } + @queue.push ExceptionRaisingJob.new assert_raises(RuntimeError) { @queue.drain } end @@ -27,41 +23,80 @@ class TestQueueTest < ActiveSupport::TestCase assert_equal [1,2], @queue.jobs end + class EquivalentJob + def initialize + @initial_id = self.object_id + end + + def run + end + + def ==(other) + other.same_initial_id?(@initial_id) + end + + def same_initial_id?(other_id) + other_id == @initial_id + end + end + def test_contents assert @queue.empty? - job = Job.new + job = EquivalentJob.new @queue.push job refute @queue.empty? assert_equal job, @queue.pop end - def test_order - processed = [] + class ProcessingJob + def self.clear_processed + @processed = [] + end + + def self.processed + @processed + end + + def initialize(object) + @object = object + end + + def run + self.class.processed << @object + end + end - job1 = Job.new { processed << 1 } - job2 = Job.new { processed << 2 } + def test_order + ProcessingJob.clear_processed + job1 = ProcessingJob.new(1) + job2 = ProcessingJob.new(2) @queue.push job1 @queue.push job2 @queue.drain - assert_equal [1,2], processed + assert_equal [1,2], ProcessingJob.processed end - def test_drain - t = nil - ran = false + class ThreadTrackingJob + attr_reader :thread_id - job = Job.new do - ran = true - t = Thread.current + def run + @thread_id = Thread.current.object_id end - @queue.push job + def ran? + @thread_id + end + end + + def test_drain + @queue.push ThreadTrackingJob.new + job = @queue.jobs.last @queue.drain assert @queue.empty? - assert ran, "The job runs synchronously when the queue is drained" - assert_not_equal t, Thread.current + assert job.ran?, "The job runs synchronously when the queue is drained" + assert_not_equal job.thread_id, Thread.current.object_id end end diff --git a/railties/test/railties/engine_test.rb b/railties/test/railties/engine_test.rb index 52c7fae6c6..63814f7a04 100644 --- a/railties/test/railties/engine_test.rb +++ b/railties/test/railties/engine_test.rb @@ -1098,7 +1098,7 @@ YAML assert_equal "// App's bar js\n;", last_response.body.strip # ensure that railties are not added twice - railties = Rails.application.ordered_railties.map(&:class) + railties = Rails.application.send(:ordered_railties).map(&:class) assert_equal railties, railties.uniq end diff --git a/railties/test/railties/generators_test.rb b/railties/test/railties/generators_test.rb index 1938bfb6c2..c90b795d59 100644 --- a/railties/test/railties/generators_test.rb +++ b/railties/test/railties/generators_test.rb @@ -75,6 +75,18 @@ module RailtiesTests end end + def test_table_name_prefix_is_correctly_namespaced_when_engine_is_mountable + build_mountable_engine + Dir.chdir(engine_path) do + bundled_rails("g model namespaced/topic") + assert_file "app/models/foo_bar/namespaced.rb", /module FooBar\n module Namespaced/ do |content| + assert_class_method :table_name_prefix, content do |method_content| + assert_match(/'foo_bar_namespaced_'/, method_content) + end + end + end + end + def test_helpers_are_correctly_namespaced_when_engine_is_mountable build_mountable_engine Dir.chdir(engine_path) do |