2017-07-09 13:41:28 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2011-06-06 14:17:44 -04:00
|
|
|
require "cases/helper"
|
2016-08-06 12:26:20 -04:00
|
|
|
require "models/reply"
|
|
|
|
require "models/topic"
|
2018-01-20 04:55:31 -05:00
|
|
|
require "models/movie"
|
2010-11-23 13:13:43 -05:00
|
|
|
|
|
|
|
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
|
2018-01-25 18:14:09 -05:00
|
|
|
assert_not_predicate 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
|
2018-05-12 22:26:10 -04:00
|
|
|
assert_not 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
|
2016-08-06 12:26:20 -04: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
|
|
|
|
|
2018-05-12 22:26:10 -04:00
|
|
|
assert_not duped.persisted?, "topic not persisted"
|
2016-08-06 12:26:20 -04:00
|
|
|
assert duped.new_record?, "topic is new"
|
2010-11-23 13:13:43 -05:00
|
|
|
end
|
|
|
|
|
2014-05-02 14:54:07 -04:00
|
|
|
def test_dup_not_destroyed
|
|
|
|
topic = Topic.first
|
|
|
|
topic.destroy
|
|
|
|
|
|
|
|
duped = topic.dup
|
2018-01-25 18:14:09 -05:00
|
|
|
assert_not_predicate duped, :destroyed?
|
2014-05-02 14:54:07 -04:00
|
|
|
end
|
|
|
|
|
2010-11-23 13:13:43 -05:00
|
|
|
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
|
2016-08-06 12:26:20 -04:00
|
|
|
topic.author_name = "Aaron"
|
2010-11-23 13:58:19 -05:00
|
|
|
duped = topic.dup
|
2016-08-06 12:26:20 -04:00
|
|
|
assert_equal "Aaron", duped.author_name
|
2010-11-23 13:58:19 -05:00
|
|
|
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
|
|
|
|
2017-12-14 03:30:54 -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
|
|
|
|
2017-12-14 03:30:54 -05:00
|
|
|
# clear topic timestamp values
|
2010-11-24 03:32:02 -05:00
|
|
|
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
|
2016-08-06 12:26:20 -04:00
|
|
|
topic.author_name = "Aaron"
|
2010-11-23 13:59:23 -05:00
|
|
|
duped = topic.dup
|
|
|
|
|
2016-08-06 12:26:20 -04:00
|
|
|
duped.author_name = "meow"
|
2010-11-23 13:59:23 -05:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2016-08-06 12:26:20 -04:00
|
|
|
duped.author_name = "meow"
|
|
|
|
topic.author_name = "Aaron"
|
2010-11-23 14:10:56 -05:00
|
|
|
|
2016-08-06 12:26:20 -04:00
|
|
|
assert_equal "Aaron", topic.author_name
|
|
|
|
assert_equal "meow", duped.author_name
|
2010-11-23 14:10:56 -05:00
|
|
|
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
|
|
|
|
|
2017-12-14 03:30:54 -05:00
|
|
|
# dup should not preserve the timestamps if present
|
2010-11-24 03:32:02 -05:00
|
|
|
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
|
|
|
|
|
2012-08-16 17:19:47 -04:00
|
|
|
def test_dup_validity_is_independent
|
2013-01-08 21:07:37 -05:00
|
|
|
repair_validations(Topic) do
|
|
|
|
Topic.validates_presence_of :title
|
2013-03-18 14:34:39 -04:00
|
|
|
topic = Topic.new("title" => "Literature")
|
2013-01-08 21:07:37 -05:00
|
|
|
topic.valid?
|
|
|
|
|
|
|
|
duped = topic.dup
|
|
|
|
duped.title = nil
|
2018-01-25 18:14:09 -05:00
|
|
|
assert_predicate duped, :invalid?
|
2013-01-08 21:07:37 -05:00
|
|
|
|
|
|
|
topic.title = nil
|
2016-08-06 12:26:20 -04:00
|
|
|
duped.title = "Mathematics"
|
2018-01-25 18:14:09 -05:00
|
|
|
assert_predicate topic, :invalid?
|
|
|
|
assert_predicate duped, :valid?
|
2013-01-08 21:07:37 -05:00
|
|
|
end
|
2012-08-16 17:19:47 -04:00
|
|
|
end
|
2013-02-25 10:30:49 -05:00
|
|
|
|
|
|
|
def test_dup_with_default_scope
|
|
|
|
prev_default_scopes = Topic.default_scopes
|
2016-08-06 13:37:57 -04:00
|
|
|
Topic.default_scopes = [proc { Topic.where(approved: true) }]
|
|
|
|
topic = Topic.new(approved: false)
|
2018-05-12 22:26:10 -04:00
|
|
|
assert_not topic.dup.approved?, "should not be overridden by default scopes"
|
2013-02-25 10:30:49 -05:00
|
|
|
ensure
|
|
|
|
Topic.default_scopes = prev_default_scopes
|
|
|
|
end
|
2014-07-02 09:00:26 -04:00
|
|
|
|
|
|
|
def test_dup_without_primary_key
|
2017-02-16 03:34:24 -05:00
|
|
|
skip if current_adapter?(:OracleAdapter)
|
|
|
|
|
2014-07-02 09:00:26 -04:00
|
|
|
klass = Class.new(ActiveRecord::Base) do
|
2016-08-06 12:26:20 -04:00
|
|
|
self.table_name = "parrots_pirates"
|
2014-07-02 09:00:26 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
record = klass.create!
|
|
|
|
|
|
|
|
assert_nothing_raised do
|
|
|
|
record.dup
|
|
|
|
end
|
|
|
|
end
|
2018-01-20 04:55:31 -05:00
|
|
|
|
|
|
|
def test_dup_record_not_persisted_after_rollback_transaction
|
|
|
|
movie = Movie.new(name: "test")
|
|
|
|
|
|
|
|
assert_raises(ActiveRecord::RecordInvalid) do
|
|
|
|
Movie.transaction do
|
|
|
|
movie.save!
|
|
|
|
duped = movie.dup
|
|
|
|
duped.name = nil
|
|
|
|
duped.save!
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-04-17 18:21:34 -04:00
|
|
|
assert_not movie.persisted?
|
2018-01-20 04:55:31 -05:00
|
|
|
end
|
2010-11-23 13:13:43 -05:00
|
|
|
end
|
|
|
|
end
|