From 6a8f1b8b402b31ae43872c9d1f37b931c85799e7 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Tue, 23 Nov 2010 11:13:24 -0800 Subject: renaming duplication test to dup test --- activerecord/test/cases/dup_test.rb | 61 +++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 activerecord/test/cases/dup_test.rb (limited to 'activerecord/test/cases/dup_test.rb') diff --git a/activerecord/test/cases/dup_test.rb b/activerecord/test/cases/dup_test.rb new file mode 100644 index 0000000000..fa528c4936 --- /dev/null +++ b/activerecord/test/cases/dup_test.rb @@ -0,0 +1,61 @@ +require "cases/helper" +require 'models/topic' + +module ActiveRecord + class DupTest < ActiveRecord::TestCase + fixtures :topics + + def test_dup + assert !Topic.new.freeze.dup.frozen? + end + + def test_dup_not_persisted + topic = Topic.first + duped = topic.dup + + assert !duped.persisted?, 'topic not persisted' + assert duped.new_record?, 'topic is new' + end + + def test_dup_has_no_id + topic = Topic.first + duped = topic.dup + assert_nil duped.id + end + + def test_dup_with_modified_attributes + topic = Topic.first + topic.author_name = 'Aaron' + duped = topic.dup + assert_equal 'Aaron', duped.author_name + end + + def test_dup_with_changes + topic = Topic.first + topic.author_name = 'Aaron' + duped = topic.dup + assert_equal topic.changes, duped.changes + end + + def test_dup_topics_are_independent + topic = Topic.first + topic.author_name = 'Aaron' + duped = topic.dup + + duped.author_name = 'meow' + + assert_not_equal topic.changes, duped.changes + end + + def test_dup_attributes_are_independent + topic = Topic.first + duped = topic.dup + + duped.author_name = 'meow' + topic.author_name = 'Aaron' + + assert_equal 'Aaron', topic.author_name + assert_equal 'meow', duped.author_name + end + end +end -- cgit v1.2.3 From 064c28d6c290cb9b6222b2348e38e713b82a89d6 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Tue, 23 Nov 2010 11:57:33 -0800 Subject: fixing dup regressions --- activerecord/test/cases/dup_test.rb | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) (limited to 'activerecord/test/cases/dup_test.rb') diff --git a/activerecord/test/cases/dup_test.rb b/activerecord/test/cases/dup_test.rb index fa528c4936..768474e4ac 100644 --- a/activerecord/test/cases/dup_test.rb +++ b/activerecord/test/cases/dup_test.rb @@ -31,9 +31,12 @@ module ActiveRecord end def test_dup_with_changes - topic = Topic.first - topic.author_name = 'Aaron' - duped = topic.dup + dbtopic = Topic.first + topic = Topic.new + + topic.attributes = dbtopic.attributes + + duped = dbtopic.dup assert_equal topic.changes, duped.changes end -- cgit v1.2.3 From d717cb29136b8e4f557e6f6ddf076ae3de8476fc Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Tue, 23 Nov 2010 13:38:20 -0800 Subject: clone and dup are working on 1.8 --- activerecord/test/cases/dup_test.rb | 15 +++++++++++++++ 1 file changed, 15 insertions(+) (limited to 'activerecord/test/cases/dup_test.rb') diff --git a/activerecord/test/cases/dup_test.rb b/activerecord/test/cases/dup_test.rb index 768474e4ac..a1fc639290 100644 --- a/activerecord/test/cases/dup_test.rb +++ b/activerecord/test/cases/dup_test.rb @@ -9,6 +9,21 @@ module ActiveRecord assert !Topic.new.freeze.dup.frozen? end + def test_not_readonly + topic = Topic.first + + duped = topic.dup + assert !topic.readonly?, 'should not be readonly' + end + + def test_is_readonly + topic = Topic.first + topic.readonly! + + duped = topic.dup + assert topic.readonly?, 'should be readonly' + end + def test_dup_not_persisted topic = Topic.first duped = topic.dup -- cgit v1.2.3 From a551d2ec62f205a755d3b760a9943f20addafe35 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Tue, 23 Nov 2010 17:28:26 -0800 Subject: fixing variable names. oops! --- activerecord/test/cases/dup_test.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'activerecord/test/cases/dup_test.rb') diff --git a/activerecord/test/cases/dup_test.rb b/activerecord/test/cases/dup_test.rb index a1fc639290..46abe7792c 100644 --- a/activerecord/test/cases/dup_test.rb +++ b/activerecord/test/cases/dup_test.rb @@ -13,7 +13,7 @@ module ActiveRecord topic = Topic.first duped = topic.dup - assert !topic.readonly?, 'should not be readonly' + assert !duped.readonly?, 'should not be readonly' end def test_is_readonly @@ -21,7 +21,7 @@ module ActiveRecord topic.readonly! duped = topic.dup - assert topic.readonly?, 'should be readonly' + assert duped.readonly?, 'should be readonly' end def test_dup_not_persisted -- cgit v1.2.3 From 47f39d26a76430ad50dae79f212d118849b2af40 Mon Sep 17 00:00:00 2001 From: Franck Verrot Date: Wed, 24 Nov 2010 16:32:02 +0800 Subject: Testing that dup is resetting the timestamps --- activerecord/test/cases/dup_test.rb | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) (limited to 'activerecord/test/cases/dup_test.rb') diff --git a/activerecord/test/cases/dup_test.rb b/activerecord/test/cases/dup_test.rb index 46abe7792c..0236f9b0a1 100644 --- a/activerecord/test/cases/dup_test.rb +++ b/activerecord/test/cases/dup_test.rb @@ -51,7 +51,12 @@ module ActiveRecord topic.attributes = dbtopic.attributes + #duped has no timestamp values duped = dbtopic.dup + + #clear topic timestamp values + topic.send(:clear_timestamp_attributes) + assert_equal topic.changes, duped.changes end @@ -75,5 +80,24 @@ module ActiveRecord assert_equal 'Aaron', topic.author_name assert_equal 'meow', duped.author_name end + + def test_dup_timestamps_are_cleared + topic = Topic.first + assert_not_nil topic.updated_at + assert_not_nil topic.created_at + + # temporary change to the topic object + topic.updated_at -= 3.days + + #dup should not preserve the timestamps if present + new_topic = topic.dup + assert_nil new_topic.updated_at + assert_nil new_topic.created_at + + new_topic.save + assert_not_nil new_topic.updated_at + assert_not_nil new_topic.created_at + end + end end -- cgit v1.2.3