3 require 'models/comment'
4 require 'models/author'
5 require 'models/category'
6 require 'models/project'
7 require 'models/developer'
9 class AssociationCallbacksTest
< ActiveRecord
::TestCase
10 fixtures
:posts, :authors, :projects, :developers
13 @david = authors(:david)
14 @thinking = posts(:thinking)
15 @authorless = posts(:authorless)
16 assert
@david.post_log
.empty
?
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
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
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
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
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
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
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
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"
79 callback_log
= ["before_adding<new>", "after_adding#{jack.posts_with_callbacks.first.id}"]
80 assert_equal callback_log
, jack
.post_log
82 assert_equal
1, jack
.posts_with_callbacks
.count
83 assert_equal callback_log
, jack
.post_log
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
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
104 bob
= ar
.developers_with_callbacks
.create(:name => 'bob')
105 assert_equal
"after_adding#{bob.id}", ar
.developers_log
.last
107 ar
.developers_with_callbacks
.build(:name => 'charlie')
108 assert_equal
"after_adding<new>", ar
.developers_log
.last
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
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
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)
132 assert activerecord
.developers_with_callbacks
.size
== 2
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
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
143 callback_log
= ["before_adding<new>", "after_adding<new>"]
144 assert_equal callback_log
, project
.developers_log
146 assert_equal
1, project
.developers_with_callbacks
.size
147 assert_equal callback_log
, project
.developers_log
150 def test_dont_add_if_before_callback_raises_exception
151 assert
!@david.unchangable_posts
.include?(@authorless)
153 @david.unchangable_posts
<< @authorless
154 rescue Exception
=> e
156 assert
@david.post_log
.empty
?
157 assert
!@david.unchangable_posts
.include?(@authorless)
159 assert
!@david.unchangable_posts
.include?(@authorless)