From 6bafdee99fd5ebad268b986d70a4804df9f0495b Mon Sep 17 00:00:00 2001 From: Mike Gunderloy Date: Tue, 24 Mar 2009 17:58:52 -0500 Subject: [PATCH] rewrite polymorphic_url tests --- .../test/activerecord/polymorphic_routes_test.rb | 370 ++++++++++++++++++++ .../test/controller/polymorphic_routes_test.rb | 293 ---------------- actionpack/test/fixtures/bid.rb | 2 + actionpack/test/fixtures/db_definitions/sqlite.sql | 32 ++- actionpack/test/fixtures/fax.rb | 2 + actionpack/test/fixtures/series.rb | 2 + actionpack/test/fixtures/step.rb | 2 + actionpack/test/fixtures/task.rb | 2 + actionpack/test/fixtures/tax.rb | 2 + 9 files changed, 413 insertions(+), 294 deletions(-) create mode 100644 actionpack/test/activerecord/polymorphic_routes_test.rb delete mode 100644 actionpack/test/controller/polymorphic_routes_test.rb create mode 100644 actionpack/test/fixtures/bid.rb create mode 100644 actionpack/test/fixtures/fax.rb create mode 100644 actionpack/test/fixtures/series.rb create mode 100644 actionpack/test/fixtures/step.rb create mode 100644 actionpack/test/fixtures/task.rb create mode 100644 actionpack/test/fixtures/tax.rb diff --git a/actionpack/test/activerecord/polymorphic_routes_test.rb b/actionpack/test/activerecord/polymorphic_routes_test.rb new file mode 100644 index 0000000..2f68d09 --- /dev/null +++ b/actionpack/test/activerecord/polymorphic_routes_test.rb @@ -0,0 +1,370 @@ +require 'active_record_unit' + +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 diff --git a/actionpack/test/controller/polymorphic_routes_test.rb b/actionpack/test/controller/polymorphic_routes_test.rb deleted file mode 100644 index 146d703..0000000 --- a/actionpack/test/controller/polymorphic_routes_test.rb +++ /dev/null @@ -1,293 +0,0 @@ -require 'abstract_unit' - -class Article - attr_reader :id - def save; @id = 1 end - def new_record?; @id.nil? end - def name - model = self.class.name.downcase - @id.nil? ? "new #{model}" : "#{model} ##{@id}" - end -end - -class Response < Article - def post_id; 1 end -end - -class Tag < Article - def response_id; 1 end -end - -class Tax - attr_reader :id - def save; @id = 1 end - def new_record?; @id.nil? end - def name - model = self.class.name.downcase - @id.nil? ? "new #{model}" : "#{model} ##{@id}" - end -end - -class Fax < Tax - def store_id; 1 end -end - -# TODO: test nested models -class Response::Nested < Response; end - -class PolymorphicRoutesTest < ActiveSupport::TestCase - include ActionController::PolymorphicRoutes - - def setup - @article = Article.new - @response = Response.new - @tax = Tax.new - @fax = Fax.new - end - - def test_with_record - @article.save - expects(:article_url).with(@article) - polymorphic_url(@article) - end - - def test_with_new_record - expects(:articles_url).with() - @article.expects(:new_record?).returns(true) - polymorphic_url(@article) - end - - def test_with_record_and_action - expects(:new_article_url).with() - @article.expects(:new_record?).never - polymorphic_url(@article, :action => 'new') - end - - def test_url_helper_prefixed_with_new - expects(:new_article_url).with() - new_polymorphic_url(@article) - end - - def test_url_helper_prefixed_with_edit - @article.save - expects(:edit_article_url).with(@article) - edit_polymorphic_url(@article) - end - - def test_url_helper_prefixed_with_edit_with_url_options - @article.save - expects(:edit_article_url).with(@article, :param1 => '10') - edit_polymorphic_url(@article, :param1 => '10') - end - - def test_url_helper_with_url_options - @article.save - expects(:article_url).with(@article, :param1 => '10') - polymorphic_url(@article, :param1 => '10') - end - - def test_formatted_url_helper_is_deprecated - expects(:articles_url).with(:format => :pdf) - assert_deprecated do - formatted_polymorphic_url([@article, :pdf]) - end - end - - def test_format_option - @article.save - expects(:article_url).with(@article, :format => :pdf) - polymorphic_url(@article, :format => :pdf) - end - - def test_format_option_with_url_options - @article.save - expects(:article_url).with(@article, :format => :pdf, :param1 => '10') - polymorphic_url(@article, :format => :pdf, :param1 => '10') - end - - def test_id_and_format_option - @article.save - expects(:article_url).with(:id => @article, :format => :pdf) - polymorphic_url(:id => @article, :format => :pdf) - end - - def test_with_nested - @response.save - expects(:article_response_url).with(@article, @response) - polymorphic_url([@article, @response]) - end - - def test_with_nested_unsaved - expects(:article_responses_url).with(@article) - polymorphic_url([@article, @response]) - end - - def test_new_with_array_and_namespace - expects(:new_admin_article_url).with() - polymorphic_url([:admin, @article], :action => 'new') - end - - def test_unsaved_with_array_and_namespace - expects(:admin_articles_url).with() - polymorphic_url([:admin, @article]) - end - - def test_nested_unsaved_with_array_and_namespace - @article.save - expects(:admin_article_url).with(@article) - polymorphic_url([:admin, @article]) - expects(:admin_article_responses_url).with(@article) - polymorphic_url([:admin, @article, @response]) - end - - def test_nested_with_array_and_namespace - @response.save - expects(:admin_article_response_url).with(@article, @response) - polymorphic_url([:admin, @article, @response]) - - # a ridiculously long named route tests correct ordering of namespaces and nesting: - @tag = Tag.new - @tag.save - expects(:site_admin_article_response_tag_url).with(@article, @response, @tag) - polymorphic_url([:site, :admin, @article, @response, @tag]) - end - - def test_nesting_with_array_ending_in_singleton_resource - expects(:article_response_url).with(@article) - polymorphic_url([@article, :response]) - end - - def test_nesting_with_array_containing_singleton_resource - @tag = Tag.new - @tag.save - expects(:article_response_tag_url).with(@article, @tag) - polymorphic_url([@article, :response, @tag]) - end - - def test_nesting_with_array_containing_namespace_and_singleton_resource - @tag = Tag.new - @tag.save - expects(:admin_article_response_tag_url).with(@article, @tag) - polymorphic_url([:admin, @article, :response, @tag]) - end - - def test_nesting_with_array_containing_singleton_resource_and_format - @tag = Tag.new - @tag.save - expects(:article_response_tag_url).with(@article, @tag, :format => :pdf) - polymorphic_url([@article, :response, @tag], :format => :pdf) - end - - def test_nesting_with_array_containing_singleton_resource_and_format_option - @tag = Tag.new - @tag.save - expects(:article_response_tag_url).with(@article, @tag, :format => :pdf) - polymorphic_url([@article, :response, @tag], :format => :pdf) - end - - def test_nesting_with_array_containing_nil - expects(:article_response_url).with(@article) - polymorphic_url([@article, nil, :response]) - end - - def test_with_array_containing_single_object - @article.save - expects(:article_url).with(@article) - polymorphic_url([nil, @article]) - end - - def test_with_array_containing_single_name - @article.save - expects(:articles_url) - polymorphic_url([:articles]) - end - - # TODO: Needs to be updated to correctly know about whether the object is in a hash or not - def xtest_with_hash - expects(:article_url).with(@article) - @article.save - polymorphic_url(:id => @article) - end - - def test_polymorphic_path_accepts_options - expects(:new_article_path).with() - polymorphic_path(@article, :action => :new) - end - - def test_polymorphic_path_does_not_modify_arguments - expects(:admin_article_responses_url).with(@article) - path = [:admin, @article, @response] - assert_no_difference 'path.size' do - polymorphic_url(path) - end - end - - # Tests for names where .plural.singular doesn't round-trip - def test_with_irregular_plural_record - @tax.save - expects(:taxis_url).with(@tax) - polymorphic_url(@tax) - end - - def test_with_irregular_plural_new_record - expects(:taxes_url).with() - @tax.expects(:new_record?).returns(true) - polymorphic_url(@tax) - end - - def test_with_irregular_plural_record_and_action - expects(:new_taxis_url).with() - @tax.expects(:new_record?).never - polymorphic_url(@tax, :action => 'new') - end - - def test_irregular_plural_url_helper_prefixed_with_new - expects(:new_taxis_url).with() - new_polymorphic_url(@tax) - end - - def test_irregular_plural_url_helper_prefixed_with_edit - @tax.save - expects(:edit_taxis_url).with(@tax) - edit_polymorphic_url(@tax) - end - - def test_with_nested_irregular_plurals - @fax.save - expects(:taxis_faxis_url).with(@tax, @fax) - polymorphic_url([@tax, @fax]) - end - - def test_with_nested_unsaved_irregular_plurals - expects(:taxis_faxes_url).with(@tax) - polymorphic_url([@tax, @fax]) - end - - def test_new_with_irregular_plural_array_and_namespace - expects(:new_admin_taxis_url).with() - polymorphic_url([:admin, @tax], :action => 'new') - end - - def test_unsaved_with_irregular_plural_array_and_namespace - expects(:admin_taxes_url).with() - polymorphic_url([:admin, @tax]) - end - - def test_nesting_with_irregular_plurals_and_array_ending_in_singleton_resource - expects(:taxis_faxis_url).with(@tax) - polymorphic_url([@tax, :faxis]) - end - - def test_with_array_containing_single_irregular_plural_object - @tax.save - expects(:taxis_url).with(@tax) - polymorphic_url([nil, @tax]) - end - - def test_with_array_containing_single_name_irregular_plural - @tax.save - expects(:taxes_url) - polymorphic_url([:taxes]) - end - -end diff --git a/actionpack/test/fixtures/bid.rb b/actionpack/test/fixtures/bid.rb new file mode 100644 index 0000000..a03f691 --- /dev/null +++ b/actionpack/test/fixtures/bid.rb @@ -0,0 +1,2 @@ +class Bid < ActiveRecord::Base +end \ No newline at end of file diff --git a/actionpack/test/fixtures/db_definitions/sqlite.sql b/actionpack/test/fixtures/db_definitions/sqlite.sql index 8e1947d..4c66cfe 100644 --- a/actionpack/test/fixtures/db_definitions/sqlite.sql +++ b/actionpack/test/fixtures/db_definitions/sqlite.sql @@ -46,4 +46,34 @@ CREATE TABLE 'mascots' ( 'id' INTEGER PRIMARY KEY NOT NULL, 'company_id' INTEGER NOT NULL, 'name' TEXT DEFAULT NULL -); \ No newline at end of file +); + +CREATE TABLE 'series' ( + 'id' INTEGER PRIMARY KEY NOT NULL, + 'name' TEXT DEFAULT NULL +); + +CREATE TABLE 'tasks' ( + 'id' INTEGER PRIMARY KEY NOT NULL, + 'name' TEXT DEFAULT NULL +); + +CREATE TABLE 'bids' ( + 'id' INTEGER PRIMARY KEY NOT NULL, + 'name' TEXT DEFAULT NULL +); + +CREATE TABLE 'steps' ( + 'id' INTEGER PRIMARY KEY NOT NULL, + 'name' TEXT DEFAULT NULL +); + +CREATE TABLE 'taxes' ( + 'id' INTEGER PRIMARY KEY NOT NULL, + 'name' TEXT DEFAULT NULL +); + +CREATE TABLE 'faxes' ( + 'id' INTEGER PRIMARY KEY NOT NULL, + 'name' TEXT DEFAULT NULL +); diff --git a/actionpack/test/fixtures/fax.rb b/actionpack/test/fixtures/fax.rb new file mode 100644 index 0000000..5f53e28 --- /dev/null +++ b/actionpack/test/fixtures/fax.rb @@ -0,0 +1,2 @@ +class Fax < ActiveRecord::Base +end \ No newline at end of file diff --git a/actionpack/test/fixtures/series.rb b/actionpack/test/fixtures/series.rb new file mode 100644 index 0000000..5dd7c50 --- /dev/null +++ b/actionpack/test/fixtures/series.rb @@ -0,0 +1,2 @@ +class Series < ActiveRecord::Base +end \ No newline at end of file diff --git a/actionpack/test/fixtures/step.rb b/actionpack/test/fixtures/step.rb new file mode 100644 index 0000000..04b480e --- /dev/null +++ b/actionpack/test/fixtures/step.rb @@ -0,0 +1,2 @@ +class Step < ActiveRecord::Base +end \ No newline at end of file diff --git a/actionpack/test/fixtures/task.rb b/actionpack/test/fixtures/task.rb new file mode 100644 index 0000000..691066d --- /dev/null +++ b/actionpack/test/fixtures/task.rb @@ -0,0 +1,2 @@ +class Task < ActiveRecord::Base +end \ No newline at end of file diff --git a/actionpack/test/fixtures/tax.rb b/actionpack/test/fixtures/tax.rb new file mode 100644 index 0000000..8314f6b --- /dev/null +++ b/actionpack/test/fixtures/tax.rb @@ -0,0 +1,2 @@ +class Tax < ActiveRecord::Base +end \ No newline at end of file -- 1.6.1