aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/test/activerecord/polymorphic_routes_test.rb
diff options
context:
space:
mode:
authorMike Gunderloy <MikeG1@larkfarm.com>2009-04-21 14:32:52 +0100
committerPratik Naik <pratiknaik@gmail.com>2009-04-21 14:32:52 +0100
commit398b07e655b6f643f31fb47b3cf7bc2dba009dc7 (patch)
treed101489056ea639cfce19cb935b0dd166cb718b5 /actionpack/test/activerecord/polymorphic_routes_test.rb
parent2e6d1bf43e162026307141806e04b4d62e40a2f8 (diff)
downloadrails-398b07e655b6f643f31fb47b3cf7bc2dba009dc7.tar.gz
rails-398b07e655b6f643f31fb47b3cf7bc2dba009dc7.tar.bz2
rails-398b07e655b6f643f31fb47b3cf7bc2dba009dc7.zip
Remove excess mocking from polymorphic_url tests [#2330 state:resolved]
Signed-off-by: Pratik Naik <pratiknaik@gmail.com>
Diffstat (limited to 'actionpack/test/activerecord/polymorphic_routes_test.rb')
-rw-r--r--actionpack/test/activerecord/polymorphic_routes_test.rb394
1 files changed, 394 insertions, 0 deletions
diff --git a/actionpack/test/activerecord/polymorphic_routes_test.rb b/actionpack/test/activerecord/polymorphic_routes_test.rb
new file mode 100644
index 0000000000..35139fbb7f
--- /dev/null
+++ b/actionpack/test/activerecord/polymorphic_routes_test.rb
@@ -0,0 +1,394 @@
+require 'active_record_unit'
+
+class Task < ActiveRecord::Base
+ set_table_name 'projects'
+end
+
+class Step < ActiveRecord::Base
+ set_table_name 'projects'
+end
+
+class Bid < ActiveRecord::Base
+ set_table_name 'projects'
+end
+
+class Tax < ActiveRecord::Base
+ set_table_name 'projects'
+end
+
+class Fax < ActiveRecord::Base
+ set_table_name 'projects'
+end
+
+class Series < ActiveRecord::Base
+ set_table_name 'projects'
+end
+
+class PolymorphicRoutesTest < ActionController::TestCase
+ include ActionController::UrlWriter
+ self.default_url_options[:host] = 'example.com'
+
+ def setup
+ @project = Project.new
+ @task = Task.new
+ @step = Step.new
+ @bid = Bid.new
+ @tax = Tax.new
+ @fax = Fax.new
+ @series = Series.new
+ end
+
+ def test_with_record
+ with_test_routes do
+ @project.save
+ assert_equal "http://example.com/projects/#{@project.id}", polymorphic_url(@project)
+ end
+ end
+
+ def test_with_new_record
+ with_test_routes do
+ assert_equal "http://example.com/projects", polymorphic_url(@project)
+ end
+ end
+
+ def test_with_record_and_action
+ with_test_routes do
+ assert_equal "http://example.com/projects/new", polymorphic_url(@project, :action => 'new')
+ end
+ end
+
+ def test_url_helper_prefixed_with_new
+ with_test_routes do
+ assert_equal "http://example.com/projects/new", new_polymorphic_url(@project)
+ end
+ end
+
+ def test_url_helper_prefixed_with_edit
+ with_test_routes do
+ @project.save
+ assert_equal "http://example.com/projects/#{@project.id}/edit", edit_polymorphic_url(@project)
+ end
+ end
+
+ def test_url_helper_prefixed_with_edit_with_url_options
+ with_test_routes do
+ @project.save
+ assert_equal "http://example.com/projects/#{@project.id}/edit?param1=10", edit_polymorphic_url(@project, :param1 => '10')
+ end
+ end
+
+ def test_url_helper_with_url_options
+ with_test_routes do
+ @project.save
+ assert_equal "http://example.com/projects/#{@project.id}?param1=10", polymorphic_url(@project, :param1 => '10')
+ end
+ end
+
+ def test_formatted_url_helper_is_deprecated
+ with_test_routes do
+ assert_deprecated do
+ formatted_polymorphic_url([@project, :pdf])
+ end
+ end
+ end
+
+ def test_format_option
+ with_test_routes do
+ @project.save
+ assert_equal "http://example.com/projects/#{@project.id}.pdf", polymorphic_url(@project, :format => :pdf)
+ end
+ end
+
+ def test_format_option_with_url_options
+ with_test_routes do
+ @project.save
+ assert_equal "http://example.com/projects/#{@project.id}.pdf?param1=10", polymorphic_url(@project, :format => :pdf, :param1 => '10')
+ end
+ end
+
+ def test_id_and_format_option
+ with_test_routes do
+ @project.save
+ assert_equal "http://example.com/projects/#{@project.id}.pdf", polymorphic_url(:id => @project, :format => :pdf)
+ end
+ end
+
+ def test_with_nested
+ with_test_routes do
+ @project.save
+ @task.save
+ assert_equal "http://example.com/projects/#{@project.id}/tasks/#{@task.id}", polymorphic_url([@project, @task])
+ end
+ end
+
+ def test_with_nested_unsaved
+ with_test_routes do
+ @project.save
+ assert_equal "http://example.com/projects/#{@project.id}/tasks", polymorphic_url([@project, @task])
+ end
+ end
+
+ def test_new_with_array_and_namespace
+ with_admin_test_routes do
+ assert_equal "http://example.com/admin/projects/new", polymorphic_url([:admin, @project], :action => 'new')
+ end
+ end
+
+ def test_unsaved_with_array_and_namespace
+ with_admin_test_routes do
+ assert_equal "http://example.com/admin/projects", polymorphic_url([:admin, @project])
+ end
+ end
+
+ def test_nested_unsaved_with_array_and_namespace
+ with_admin_test_routes do
+ @project.save
+ assert_equal "http://example.com/admin/projects/#{@project.id}/tasks", polymorphic_url([:admin, @project, @task])
+ end
+ end
+
+ def test_nested_with_array_and_namespace
+ with_admin_test_routes do
+ @project.save
+ @task.save
+ assert_equal "http://example.com/admin/projects/#{@project.id}/tasks/#{@task.id}", polymorphic_url([:admin, @project, @task])
+ end
+ end
+
+ def test_ordering_of_nesting_and_namespace
+ with_admin_and_site_test_routes do
+ @project.save
+ @task.save
+ @step.save
+ assert_equal "http://example.com/admin/projects/#{@project.id}/site/tasks/#{@task.id}/steps/#{@step.id}", polymorphic_url([:admin, @project, :site, @task, @step])
+ end
+ end
+
+ def test_nesting_with_array_ending_in_singleton_resource
+ with_test_routes do
+ @project.save
+ assert_equal "http://example.com/projects/#{@project.id}/bid", polymorphic_url([@project, :bid])
+ end
+ end
+
+ def test_nesting_with_array_containing_singleton_resource
+ with_test_routes do
+ @project.save
+ @task.save
+ assert_equal "http://example.com/projects/#{@project.id}/bid/tasks/#{@task.id}", polymorphic_url([@project, :bid, @task])
+ end
+ end
+
+ def test_nesting_with_array_containing_singleton_resource_and_format
+ with_test_routes do
+ @project.save
+ @task.save
+ assert_equal "http://example.com/projects/#{@project.id}/bid/tasks/#{@task.id}.pdf", polymorphic_url([@project, :bid, @task], :format => :pdf)
+ end
+ end
+
+ def test_nesting_with_array_containing_namespace_and_singleton_resource
+ with_admin_test_routes do
+ @project.save
+ @task.save
+ assert_equal "http://example.com/admin/projects/#{@project.id}/bid/tasks/#{@task.id}", polymorphic_url([:admin, @project, :bid, @task])
+ end
+ end
+
+ def test_nesting_with_array_containing_nil
+ with_test_routes do
+ @project.save
+ assert_equal "http://example.com/projects/#{@project.id}/bid", polymorphic_url([@project, nil, :bid])
+ end
+ end
+
+ def test_with_array_containing_single_object
+ with_test_routes do
+ @project.save
+ assert_equal "http://example.com/projects/#{@project.id}", polymorphic_url([nil, @project])
+ end
+ end
+
+ def test_with_array_containing_single_name
+ with_test_routes do
+ @project.save
+ assert_equal "http://example.com/projects", polymorphic_url([:projects])
+ end
+ end
+
+ def test_with_hash
+ with_test_routes do
+ @project.save
+ assert_equal "http://example.com/projects/#{@project.id}", polymorphic_url(:id => @project)
+ end
+ end
+
+ def test_polymorphic_path_accepts_options
+ with_test_routes do
+ assert_equal "/projects/new", polymorphic_path(@project, :action => 'new')
+ end
+ end
+
+ def test_polymorphic_path_does_not_modify_arguments
+ with_admin_test_routes do
+ @project.save
+ @task.save
+ object_array = [:admin, @project, @task]
+ assert_no_difference 'object_array.size' do
+ polymorphic_url(object_array)
+ end
+ end
+ end
+
+ # Tests for names where .plural.singular doesn't round-trip
+ def test_with_irregular_plural_record
+ with_test_routes do
+ @tax.save
+ assert_equal "http://example.com/taxes/#{@tax.id}", polymorphic_url(@tax)
+ end
+ end
+
+ def test_with_irregular_plural_new_record
+ with_test_routes do
+ assert_equal "http://example.com/taxes", polymorphic_url(@tax)
+ end
+ end
+
+ def test_with_irregular_plural_record_and_action
+ with_test_routes do
+ assert_equal "http://example.com/taxes/new", polymorphic_url(@tax, :action => 'new')
+ end
+ end
+
+ def test_irregular_plural_url_helper_prefixed_with_new
+ with_test_routes do
+ assert_equal "http://example.com/taxes/new", new_polymorphic_url(@tax)
+ end
+ end
+
+ def test_irregular_plural_url_helper_prefixed_with_edit
+ with_test_routes do
+ @tax.save
+ assert_equal "http://example.com/taxes/#{@tax.id}/edit", edit_polymorphic_url(@tax)
+ end
+ end
+
+ def test_with_nested_irregular_plurals
+ with_test_routes do
+ @tax.save
+ @fax.save
+ assert_equal "http://example.com/taxes/#{@tax.id}/faxes/#{@fax.id}", polymorphic_url([@tax, @fax])
+ end
+ end
+
+ def test_with_nested_unsaved_irregular_plurals
+ with_test_routes do
+ @tax.save
+ assert_equal "http://example.com/taxes/#{@tax.id}/faxes", polymorphic_url([@tax, @fax])
+ end
+ end
+
+ def test_new_with_irregular_plural_array_and_namespace
+ with_admin_test_routes do
+ assert_equal "http://example.com/admin/taxes/new", polymorphic_url([:admin, @tax], :action => 'new')
+ end
+ end
+
+ def test_unsaved_with_irregular_plural_array_and_namespace
+ with_admin_test_routes do
+ assert_equal "http://example.com/admin/taxes", polymorphic_url([:admin, @tax])
+ end
+ end
+
+ def test_nesting_with_irregular_plurals_and_array_ending_in_singleton_resource
+ with_test_routes do
+ @tax.save
+ assert_equal "http://example.com/taxes/#{@tax.id}/bid", polymorphic_url([@tax, :bid])
+ end
+ end
+
+ def test_with_array_containing_single_irregular_plural_object
+ with_test_routes do
+ @tax.save
+ assert_equal "http://example.com/taxes/#{@tax.id}", polymorphic_url([nil, @tax])
+ end
+ end
+
+ def test_with_array_containing_single_name_irregular_plural
+ with_test_routes do
+ @tax.save
+ assert_equal "http://example.com/taxes", polymorphic_url([:taxes])
+ end
+ end
+
+ # Tests for uncountable names
+ def test_uncountable_resource
+ with_test_routes do
+ @series.save
+ assert_equal "http://example.com/series/#{@series.id}", polymorphic_url(@series)
+ end
+ end
+
+ def with_test_routes(options = {})
+ with_routing do |set|
+ set.draw do |map|
+ map.resources :projects do |projects|
+ projects.resources :tasks
+ projects.resource :bid do |bid|
+ bid.resources :tasks
+ end
+ end
+ map.resources :taxes do |taxes|
+ taxes.resources :faxes
+ taxes.resource :bid
+ end
+ map.resources :series
+ end
+
+ ActionController::Routing::Routes.install_helpers(self.class)
+ yield
+ end
+ end
+
+ def with_admin_test_routes(options = {})
+ with_routing do |set|
+ set.draw do |map|
+ map.namespace :admin do |admin|
+ admin.resources :projects do |projects|
+ projects.resources :tasks
+ projects.resource :bid do |bid|
+ bid.resources :tasks
+ end
+ end
+ admin.resources :taxes do |taxes|
+ taxes.resources :faxes
+ end
+ admin.resources :series
+ end
+ end
+
+ ActionController::Routing::Routes.install_helpers(self.class)
+ yield
+ end
+ end
+
+ def with_admin_and_site_test_routes(options = {})
+ with_routing do |set|
+ set.draw do |map|
+ map.namespace :admin do |admin|
+ admin.resources :projects do |projects|
+ projects.namespace :site do |site|
+ site.resources :tasks do |tasks|
+ tasks.resources :steps
+ end
+ end
+ end
+ end
+ end
+
+ ActionController::Routing::Routes.install_helpers(self.class)
+ yield
+ end
+ end
+
+end \ No newline at end of file