Froze rails gems
[depot.git] / vendor / rails / activerecord / test / cases / associations / callbacks_test.rb
1 require "cases/helper"
2 require 'models/post'
3 require 'models/comment'
4 require 'models/author'
5 require 'models/category'
6 require 'models/project'
7 require 'models/developer'
8
9 class AssociationCallbacksTest < ActiveRecord::TestCase
10 fixtures :posts, :authors, :projects, :developers
11
12 def setup
13 @david = authors(:david)
14 @thinking = posts(:thinking)
15 @authorless = posts(:authorless)
16 assert @david.post_log.empty?
17 end
18
19 def test_adding_macro_callbacks
20 @david.posts_with_callbacks << @thinking
21 assert_equal ["before_adding#{@thinking.id}", "after_adding#{@thinking.id}"], @david.post_log
22 @david.posts_with_callbacks << @thinking
23 assert_equal ["before_adding#{@thinking.id}", "after_adding#{@thinking.id}", "before_adding#{@thinking.id}",
24 "after_adding#{@thinking.id}"], @david.post_log
25 end
26
27 def test_adding_with_proc_callbacks
28 @david.posts_with_proc_callbacks << @thinking
29 assert_equal ["before_adding#{@thinking.id}", "after_adding#{@thinking.id}"], @david.post_log
30 @david.posts_with_proc_callbacks << @thinking
31 assert_equal ["before_adding#{@thinking.id}", "after_adding#{@thinking.id}", "before_adding#{@thinking.id}",
32 "after_adding#{@thinking.id}"], @david.post_log
33 end
34
35 def test_removing_with_macro_callbacks
36 first_post, second_post = @david.posts_with_callbacks[0, 2]
37 @david.posts_with_callbacks.delete(first_post)
38 assert_equal ["before_removing#{first_post.id}", "after_removing#{first_post.id}"], @david.post_log
39 @david.posts_with_callbacks.delete(second_post)
40 assert_equal ["before_removing#{first_post.id}", "after_removing#{first_post.id}", "before_removing#{second_post.id}",
41 "after_removing#{second_post.id}"], @david.post_log
42 end
43
44 def test_removing_with_proc_callbacks
45 first_post, second_post = @david.posts_with_callbacks[0, 2]
46 @david.posts_with_proc_callbacks.delete(first_post)
47 assert_equal ["before_removing#{first_post.id}", "after_removing#{first_post.id}"], @david.post_log
48 @david.posts_with_proc_callbacks.delete(second_post)
49 assert_equal ["before_removing#{first_post.id}", "after_removing#{first_post.id}", "before_removing#{second_post.id}",
50 "after_removing#{second_post.id}"], @david.post_log
51 end
52
53 def test_multiple_callbacks
54 @david.posts_with_multiple_callbacks << @thinking
55 assert_equal ["before_adding#{@thinking.id}", "before_adding_proc#{@thinking.id}", "after_adding#{@thinking.id}",
56 "after_adding_proc#{@thinking.id}"], @david.post_log
57 @david.posts_with_multiple_callbacks << @thinking
58 assert_equal ["before_adding#{@thinking.id}", "before_adding_proc#{@thinking.id}", "after_adding#{@thinking.id}",
59 "after_adding_proc#{@thinking.id}", "before_adding#{@thinking.id}", "before_adding_proc#{@thinking.id}",
60 "after_adding#{@thinking.id}", "after_adding_proc#{@thinking.id}"], @david.post_log
61 end
62
63 def test_has_many_callbacks_with_create
64 morten = Author.create :name => "Morten"
65 post = morten.posts_with_proc_callbacks.create! :title => "Hello", :body => "How are you doing?"
66 assert_equal ["before_adding<new>", "after_adding#{post.id}"], morten.post_log
67 end
68
69 def test_has_many_callbacks_with_create!
70 morten = Author.create! :name => "Morten"
71 post = morten.posts_with_proc_callbacks.create :title => "Hello", :body => "How are you doing?"
72 assert_equal ["before_adding<new>", "after_adding#{post.id}"], morten.post_log
73 end
74
75 def test_has_many_callbacks_for_save_on_parent
76 jack = Author.new :name => "Jack"
77 post = jack.posts_with_callbacks.build :title => "Call me back!", :body => "Before you wake up and after you sleep"
78
79 callback_log = ["before_adding<new>", "after_adding#{jack.posts_with_callbacks.first.id}"]
80 assert_equal callback_log, jack.post_log
81 assert jack.save
82 assert_equal 1, jack.posts_with_callbacks.count
83 assert_equal callback_log, jack.post_log
84 end
85
86 def test_has_and_belongs_to_many_add_callback
87 david = developers(:david)
88 ar = projects(:active_record)
89 assert ar.developers_log.empty?
90 ar.developers_with_callbacks << david
91 assert_equal ["before_adding#{david.id}", "after_adding#{david.id}"], ar.developers_log
92 ar.developers_with_callbacks << david
93 assert_equal ["before_adding#{david.id}", "after_adding#{david.id}", "before_adding#{david.id}",
94 "after_adding#{david.id}"], ar.developers_log
95 end
96
97 def test_has_and_belongs_to_many_after_add_called_after_save
98 ar = projects(:active_record)
99 assert ar.developers_log.empty?
100 alice = Developer.new(:name => 'alice')
101 ar.developers_with_callbacks << alice
102 assert_equal"after_adding#{alice.id}", ar.developers_log.last
103
104 bob = ar.developers_with_callbacks.create(:name => 'bob')
105 assert_equal "after_adding#{bob.id}", ar.developers_log.last
106
107 ar.developers_with_callbacks.build(:name => 'charlie')
108 assert_equal "after_adding<new>", ar.developers_log.last
109 end
110
111
112 def test_has_and_belongs_to_many_remove_callback
113 david = developers(:david)
114 jamis = developers(:jamis)
115 activerecord = projects(:active_record)
116 assert activerecord.developers_log.empty?
117 activerecord.developers_with_callbacks.delete(david)
118 assert_equal ["before_removing#{david.id}", "after_removing#{david.id}"], activerecord.developers_log
119
120 activerecord.developers_with_callbacks.delete(jamis)
121 assert_equal ["before_removing#{david.id}", "after_removing#{david.id}", "before_removing#{jamis.id}",
122 "after_removing#{jamis.id}"], activerecord.developers_log
123 end
124
125 def test_has_and_belongs_to_many_remove_callback_on_clear
126 activerecord = projects(:active_record)
127 assert activerecord.developers_log.empty?
128 if activerecord.developers_with_callbacks.size == 0
129 activerecord.developers << developers(:david)
130 activerecord.developers << developers(:jamis)
131 activerecord.reload
132 assert activerecord.developers_with_callbacks.size == 2
133 end
134 log_array = activerecord.developers_with_callbacks.collect {|d| ["before_removing#{d.id}","after_removing#{d.id}"]}.flatten.sort
135 assert activerecord.developers_with_callbacks.clear
136 assert_equal log_array, activerecord.developers_log.sort
137 end
138
139 def test_has_many_and_belongs_to_many_callbacks_for_save_on_parent
140 project = Project.new :name => "Callbacks"
141 project.developers_with_callbacks.build :name => "Jack", :salary => 95000
142
143 callback_log = ["before_adding<new>", "after_adding<new>"]
144 assert_equal callback_log, project.developers_log
145 assert project.save
146 assert_equal 1, project.developers_with_callbacks.size
147 assert_equal callback_log, project.developers_log
148 end
149
150 def test_dont_add_if_before_callback_raises_exception
151 assert !@david.unchangable_posts.include?(@authorless)
152 begin
153 @david.unchangable_posts << @authorless
154 rescue Exception => e
155 end
156 assert @david.post_log.empty?
157 assert !@david.unchangable_posts.include?(@authorless)
158 @david.reload
159 assert !@david.unchangable_posts.include?(@authorless)
160 end
161 end