From bd79a4eb3b20c3e46fd7eaf79162df7429766514 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Thu, 19 May 2005 16:39:50 +0000 Subject: Fixed that clone would break when an aggregate had the same name as one of its attributes #1307 [bitsweat] git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@1309 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- activerecord/test/base_test.rb | 34 ++++++++++++++++++++++++++++++--- activerecord/test/fixtures/developer.rb | 6 ++++++ 2 files changed, 37 insertions(+), 3 deletions(-) (limited to 'activerecord/test') diff --git a/activerecord/test/base_test.rb b/activerecord/test/base_test.rb index 9185d05c8a..043fc6ea6e 100755 --- a/activerecord/test/base_test.rb +++ b/activerecord/test/base_test.rb @@ -2,6 +2,7 @@ require 'abstract_unit' require 'fixtures/topic' require 'fixtures/reply' require 'fixtures/company' +require 'fixtures/developer' require 'fixtures/project' require 'fixtures/default' require 'fixtures/auto_id' @@ -33,7 +34,7 @@ end class Booleantest < ActiveRecord::Base; end class BasicsTest < Test::Unit::TestCase - fixtures :topics, :companies, :projects, :computers + fixtures :topics, :companies, :developers, :projects, :computers def test_set_attributes topic = Topic.find(1) @@ -568,7 +569,8 @@ class BasicsTest < Test::Unit::TestCase def test_clone topic = Topic.find(1) - cloned_topic = topic.clone + cloned_topic = nil + assert_nothing_raised { cloned_topic = topic.clone } assert_equal topic.title, cloned_topic.title assert cloned_topic.new_record? @@ -588,7 +590,33 @@ class BasicsTest < Test::Unit::TestCase assert !cloned_topic.new_record? assert cloned_topic.id != topic.id end - + + def test_clone_with_aggregate_of_same_name_as_attribute + dev = DeveloperWithAggregate.find(1) + assert_kind_of DeveloperSalary, dev.salary + + clone = nil + assert_nothing_raised { clone = dev.clone } + assert_kind_of DeveloperSalary, clone.salary + assert_equal dev.salary.amount, clone.salary.amount + assert clone.new_record? + + # test if the attributes have been cloned + original_amount = clone.salary.amount + dev.salary.amount = 1 + assert_equal original_amount, clone.salary.amount + + assert clone.save + assert !clone.new_record? + assert clone.id != dev.id + end + + def test_clone_preserves_subtype + clone = nil + assert_nothing_raised { clone = Company.find(3).clone } + assert_kind_of Client, clone + end + def test_bignum company = Company.find(1) company.rating = 2147483647 diff --git a/activerecord/test/fixtures/developer.rb b/activerecord/test/fixtures/developer.rb index 78f8f54db7..6d01490844 100644 --- a/activerecord/test/fixtures/developer.rb +++ b/activerecord/test/fixtures/developer.rb @@ -4,3 +4,9 @@ class Developer < ActiveRecord::Base validates_inclusion_of :salary, :in => 50000..200000 validates_length_of :name, :within => 3..20 end + +DeveloperSalary = Struct.new(:amount) +class DeveloperWithAggregate < ActiveRecord::Base + self.table_name = 'developers' + composed_of :salary, :class_name => 'DeveloperSalary', :mapping => [%w(salary amount)] +end -- cgit v1.2.3