aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test/cases/associations/identity_map_test.rb
blob: 9b8635774c1ec53591653d0f80d226972ab7c7d2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
require "cases/helper"
require 'models/author'
require 'models/post'

if ActiveRecord::IdentityMap.enabled?
class InverseHasManyIdentityMapTest < ActiveRecord::TestCase
  fixtures :authors, :posts

  def test_parent_instance_should_be_shared_with_every_child_on_find
    m = Author.first
    is = m.posts
    is.each do |i|
      assert_equal m.name, i.author.name, "Name of man should be the same before changes to parent instance"
      m.name = 'Bongo'
      assert_equal m.name, i.author.name, "Name of man should be the same after changes to parent instance"
      i.author.name = 'Mungo'
      assert_equal m.name, i.author.name, "Name of man should be the same after changes to child-owned instance"
    end
  end

  def test_parent_instance_should_be_shared_with_eager_loaded_children
    m = Author.find(:first, :include => :posts)
    is = m.posts
    is.each do |i|
      assert_equal m.name, i.author.name, "Name of man should be the same before changes to parent instance"
      m.name = 'Bongo'
      assert_equal m.name, i.author.name, "Name of man should be the same after changes to parent instance"
      i.author.name = 'Mungo'
      assert_equal m.name, i.author.name, "Name of man should be the same after changes to child-owned instance"
    end

    m = Author.find(:first, :include => :posts, :order => 'posts.id')
    is = m.posts
    is.each do |i|
      assert_equal m.name, i.author.name, "Name of man should be the same before changes to parent instance"
      m.name = 'Bongo'
      assert_equal m.name, i.author.name, "Name of man should be the same after changes to parent instance"
      i.author.name = 'Mungo'
      assert_equal m.name, i.author.name, "Name of man should be the same after changes to child-owned instance"
    end
  end

  def test_parent_instance_should_be_shared_with_newly_built_child
    m = Author.first
    i = m.posts.build(:title => 'Industrial Revolution Re-enactment', :body => 'Lorem ipsum')
    assert_not_nil i.author
    assert_equal m.name, i.author.name, "Name of man should be the same before changes to parent instance"
    m.name = 'Bongo'
    assert_equal m.name, i.author.name, "Name of man should be the same after changes to parent instance"
    i.author.name = 'Mungo'
    assert_equal m.name, i.author.name, "Name of man should be the same after changes to just-built-child-owned instance"
  end

  def test_parent_instance_should_be_shared_with_newly_block_style_built_child
    m = Author.first
    i = m.posts.build {|ii| ii.title = 'Industrial Revolution Re-enactment'; ii.body = 'Lorem ipsum'}
    assert_not_nil i.title, "Child attributes supplied to build via blocks should be populated"
    assert_not_nil i.author
    assert_equal m.name, i.author.name, "Name of man should be the same before changes to parent instance"
    m.name = 'Bongo'
    assert_equal m.name, i.author.name, "Name of man should be the same after changes to parent instance"
    i.author.name = 'Mungo'
    assert_equal m.name, i.author.name, "Name of man should be the same after changes to just-built-child-owned instance"
  end

  def test_parent_instance_should_be_shared_with_newly_created_child
    m = Author.first
    i = m.posts.create(:title => 'Industrial Revolution Re-enactment', :body => 'Lorem ipsum')
    assert_not_nil i.author
    assert_equal m.name, i.author.name, "Name of man should be the same before changes to parent instance"
    m.name = 'Bongo'
    assert_equal m.name, i.author.name, "Name of man should be the same after changes to parent instance"
    i.author.name = 'Mungo'
    assert_equal m.name, i.author.name, "Name of man should be the same after changes to newly-created-child-owned instance"
  end

  def test_parent_instance_should_be_shared_with_newly_created_via_bang_method_child
    m = Author.first
    i = m.posts.create!(:title => 'Industrial Revolution Re-enactment', :body => 'Lorem ipsum')
    assert_not_nil i.author
    assert_equal m.name, i.author.name, "Name of man should be the same before changes to parent instance"
    m.name = 'Bongo'
    assert_equal m.name, i.author.name, "Name of man should be the same after changes to parent instance"
    i.author.name = 'Mungo'
    assert_equal m.name, i.author.name, "Name of man should be the same after changes to newly-created-child-owned instance"
  end

  def test_parent_instance_should_be_shared_with_newly_block_style_created_child
    m = Author.first
    i = m.posts.create {|ii| ii.title = 'Industrial Revolution Re-enactment'; ii.body = 'Lorem ipsum'}
    assert_not_nil i.title, "Child attributes supplied to create via blocks should be populated"
    assert_not_nil i.author
    assert_equal m.name, i.author.name, "Name of man should be the same before changes to parent instance"
    m.name = 'Bongo'
    assert_equal m.name, i.author.name, "Name of man should be the same after changes to parent instance"
    i.author.name = 'Mungo'
    assert_equal m.name, i.author.name, "Name of man should be the same after changes to newly-created-child-owned instance"
  end

  def test_parent_instance_should_be_shared_with_poked_in_child
    m = Author.first
    i = Post.create(:title => 'Industrial Revolution Re-enactment', :body => 'Lorem ipsum')
    m.posts << i
    assert_not_nil i.author
    assert_equal m.name, i.author.name, "Name of man should be the same before changes to parent instance"
    m.name = 'Bongo'
    assert_equal m.name, i.author.name, "Name of man should be the same after changes to parent instance"
    i.author.name = 'Mungo'
    assert_equal m.name, i.author.name, "Name of man should be the same after changes to newly-created-child-owned instance"
  end

  def test_parent_instance_should_be_shared_with_replaced_via_accessor_children
    m = Author.first
    i = Post.new(:title => 'Industrial Revolution Re-enactment', :body => 'Lorem ipsum')
    m.posts = [i]
    assert_same m, i.author
    assert_not_nil i.author
    assert_equal m.name, i.author.name, "Name of man should be the same before changes to parent instance"
    m.name = 'Bongo'
    assert_equal m.name, i.author.name, "Name of man should be the same after changes to parent instance"
    i.author.name = 'Mungo'
    assert_equal m.name, i.author.name, "Name of man should be the same after changes to replaced-child-owned instance"
  end

  def test_parent_instance_should_be_shared_with_replaced_via_method_children
    m = Author.first
    i = Post.new(:title => 'Industrial Revolution Re-enactment', :body => 'Lorem ipsum')
    m.posts = [i]
    assert_not_nil i.author
    assert_equal m.name, i.author.name, "Name of man should be the same before changes to parent instance"
    m.name = 'Bongo'
    assert_equal m.name, i.author.name, "Name of man should be the same after changes to parent instance"
    i.author.name = 'Mungo'
    assert_equal m.name, i.author.name, "Name of man should be the same after changes to replaced-child-owned instance"
  end
end
end