dup_test.rb 2.3 KB
Newer Older
A
Aaron Patterson 已提交
1 2 3 4
require "cases/helper"
require 'models/topic'

module ActiveRecord
5
  class DupTest < ActiveRecord::TestCase
A
Aaron Patterson 已提交
6 7 8
    fixtures :topics

    def test_dup
A
Aaron Patterson 已提交
9
      assert !Topic.new.freeze.dup.frozen?
A
Aaron Patterson 已提交
10 11
    end

A
Aaron Patterson 已提交
12 13 14 15
    def test_not_readonly
      topic = Topic.first

      duped = topic.dup
A
Aaron Patterson 已提交
16
      assert !duped.readonly?, 'should not be readonly'
A
Aaron Patterson 已提交
17 18 19 20 21 22 23
    end

    def test_is_readonly
      topic = Topic.first
      topic.readonly!

      duped = topic.dup
A
Aaron Patterson 已提交
24
      assert duped.readonly?, 'should be readonly'
A
Aaron Patterson 已提交
25 26
    end

A
Aaron Patterson 已提交
27 28 29 30 31 32 33 34 35 36 37 38 39 40
    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

A
Aaron Patterson 已提交
41 42 43 44 45 46 47 48
    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
A
Aaron Patterson 已提交
49 50 51 52 53
      dbtopic = Topic.first
      topic = Topic.new

      topic.attributes = dbtopic.attributes

54
      #duped has no timestamp values
A
Aaron Patterson 已提交
55
      duped = dbtopic.dup
56 57 58 59

      #clear topic timestamp values
      topic.send(:clear_timestamp_attributes)

A
Aaron Patterson 已提交
60
      assert_equal topic.changes, duped.changes
A
Aaron Patterson 已提交
61
    end
62 63 64 65 66 67 68 69 70 71

    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
72 73 74 75 76 77 78 79 80 81 82

    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
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101

    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

A
Aaron Patterson 已提交
102 103
  end
end