2011-06-06 14:17:44 -04:00
|
|
|
require "cases/helper"
|
2010-11-23 13:13:43 -05:00
|
|
|
require 'models/topic'
|
|
|
|
|
|
|
|
module ActiveRecord
|
2010-11-23 14:13:24 -05:00
|
|
|
class DupTest < ActiveRecord::TestCase
|
2010-11-23 13:13:43 -05:00
|
|
|
fixtures :topics
|
|
|
|
|
|
|
|
def test_dup
|
2010-11-23 13:58:19 -05:00
|
|
|
assert !Topic.new.freeze.dup.frozen?
|
2010-11-23 13:13:43 -05:00
|
|
|
end
|
|
|
|
|
2010-11-23 16:38:20 -05:00
|
|
|
def test_not_readonly
|
|
|
|
topic = Topic.first
|
|
|
|
|
|
|
|
duped = topic.dup
|
2010-11-23 20:28:26 -05:00
|
|
|
assert !duped.readonly?, 'should not be readonly'
|
2010-11-23 16:38:20 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_is_readonly
|
|
|
|
topic = Topic.first
|
|
|
|
topic.readonly!
|
|
|
|
|
|
|
|
duped = topic.dup
|
2010-11-23 20:28:26 -05:00
|
|
|
assert duped.readonly?, 'should be readonly'
|
2010-11-23 16:38:20 -05:00
|
|
|
end
|
|
|
|
|
2010-11-23 13:13:43 -05:00
|
|
|
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
|
|
|
|
|
2010-11-23 13:58:19 -05:00
|
|
|
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
|
2010-11-23 14:57:33 -05:00
|
|
|
dbtopic = Topic.first
|
|
|
|
topic = Topic.new
|
|
|
|
|
2012-09-12 12:35:05 -04:00
|
|
|
topic.attributes = dbtopic.attributes.except("id")
|
2010-11-23 14:57:33 -05:00
|
|
|
|
2010-11-24 03:32:02 -05:00
|
|
|
#duped has no timestamp values
|
2010-11-23 14:57:33 -05:00
|
|
|
duped = dbtopic.dup
|
2010-11-24 03:32:02 -05:00
|
|
|
|
|
|
|
#clear topic timestamp values
|
|
|
|
topic.send(:clear_timestamp_attributes)
|
|
|
|
|
2010-11-23 13:58:19 -05:00
|
|
|
assert_equal topic.changes, duped.changes
|
2010-11-23 13:13:43 -05:00
|
|
|
end
|
2010-11-23 13:59:23 -05:00
|
|
|
|
|
|
|
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
|
2010-11-23 14:10:56 -05:00
|
|
|
|
|
|
|
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
|
2010-11-24 03:32:02 -05:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2012-01-07 04:11:04 -05:00
|
|
|
def test_dup_after_initialize_callbacks
|
|
|
|
topic = Topic.new
|
|
|
|
assert Topic.after_initialize_called
|
|
|
|
Topic.after_initialize_called = false
|
|
|
|
topic.dup
|
|
|
|
assert Topic.after_initialize_called
|
|
|
|
end
|
|
|
|
|
2010-11-23 13:13:43 -05:00
|
|
|
end
|
|
|
|
end
|