2 require 'models/developer'
3 require 'models/project'
4 require 'models/company'
7 require 'models/computer'
8 require 'models/customer'
10 require 'models/categorization'
11 require 'models/category'
13 require 'models/author'
14 require 'models/comment'
16 require 'models/tagging'
17 require 'models/person'
18 require 'models/reader'
19 require 'models/parrot'
20 require 'models/pirate'
21 require 'models/treasure'
22 require 'models/price_estimate'
24 require 'models/member'
25 require 'models/membership'
26 require 'models/sponsor'
28 class AssociationsTest
< ActiveRecord
::TestCase
29 fixtures
:accounts, :companies, :developers, :projects, :developers_projects,
30 :computers, :people, :readers
32 def test_include_with_order_works
33 assert_nothing_raised
{Account
.find(:first, :order => 'id', :include => :firm)}
34 assert_nothing_raised
{Account
.find(:first, :order => :id, :include => :firm)}
37 def test_bad_collection_keys
38 assert_raise(ArgumentError
, 'ActiveRecord should have barked on bad collection keys') do
39 Class
.new(ActiveRecord
::Base).has_many(:wheels, :name => 'wheels')
43 def test_should_construct_new_finder_sql_after_create
44 person
= Person
.new
:first_name => 'clark'
45 assert_equal
[], person
.readers
.find(:all)
47 reader
= Reader
.create
! :person => person
, :post => Post
.new(:title => "foo", :body => "bar")
48 assert person
.readers
.find(reader
.id
)
52 firm
= Firm
.new("name" => "A New Firm, Inc")
54 firm
.clients
.each
{|c
|} # forcing to load all clients
55 assert firm
.clients
.empty
?, "New firm shouldn't have client objects"
56 assert_equal
0, firm
.clients
.size
, "New firm should have 0 clients"
58 client
= Client
.new("name" => "TheClient.com", "firm_id" => firm
.id
)
61 assert firm
.clients
.empty
?, "New firm should have cached no client objects"
62 assert_equal
0, firm
.clients
.size
, "New firm should have cached 0 clients count"
64 assert
!firm
.clients(true).empty
?, "New firm should have reloaded client objects"
65 assert_equal
1, firm
.clients(true).size
, "New firm should have reloaded clients count"
68 def test_storing_in_pstore
70 store_filename
= File
.join(Dir
.tmpdir
, "ar-pstore-association-test")
71 File
.delete(store_filename
) if File
.exist
?(store_filename
)
73 apple
= Firm
.create("name" => "Apple")
74 natural
= Client
.new("name" => "Natural Company")
75 apple
.clients
<< natural
77 db
= PStore
.new(store_filename
)
82 db
= PStore
.new(store_filename
)
84 assert_equal
"Natural Company", db
["apple"].clients
.first
.name
89 class AssociationProxyTest
< ActiveRecord
::TestCase
90 fixtures
:authors, :posts, :categorizations, :categories, :developers, :projects, :developers_projects
92 def test_proxy_accessors
93 welcome
= posts(:welcome)
94 assert_equal welcome
, welcome
.author
.proxy_owner
95 assert_equal welcome
.class.reflect_on_association(:author), welcome
.author
.proxy_reflection
96 welcome
.author
.class # force load target
97 assert_equal welcome
.author
, welcome
.author
.proxy_target
99 david
= authors(:david)
100 assert_equal david
, david
.posts
.proxy_owner
101 assert_equal david
.class.reflect_on_association(:posts), david
.posts
.proxy_reflection
102 david
.posts
.class # force load target
103 assert_equal david
.posts
, david
.posts
.proxy_target
105 assert_equal david
, david
.posts_with_extension
.testing_proxy_owner
106 assert_equal david
.class.reflect_on_association(:posts_with_extension), david
.posts_with_extension
.testing_proxy_reflection
107 david
.posts_with_extension
.class # force load target
108 assert_equal david
.posts_with_extension
, david
.posts_with_extension
.testing_proxy_target
111 def test_push_does_not_load_target
112 david
= authors(:david)
114 david
.posts
<< (post
= Post
.new(:title => "New on Edge", :body => "More cool stuff!"))
115 assert
!david
.posts
.loaded
?
116 assert david
.posts
.include?(post
)
119 def test_push_has_many_through_does_not_load_target
120 david
= authors(:david)
122 david
.categories
<< categories(:technology)
123 assert
!david
.categories
.loaded
?
124 assert david
.categories
.include?(categories(:technology))
127 def test_push_followed_by_save_does_not_load_target
128 david
= authors(:david)
130 david
.posts
<< (post
= Post
.new(:title => "New on Edge", :body => "More cool stuff!"))
131 assert
!david
.posts
.loaded
?
133 assert
!david
.posts
.loaded
?
134 assert david
.posts
.include?(post
)
137 def test_push_does_not_lose_additions_to_new_record
138 josh
= Author
.new(:name => "Josh")
139 josh
.posts
<< Post
.new(:title => "New on Edge", :body => "More cool stuff!")
140 assert josh
.posts
.loaded
?
141 assert_equal
1, josh
.posts
.size
144 def test_save_on_parent_does_not_load_target
145 david
= developers(:david)
147 assert
!david
.projects
.loaded
?
148 david
.update_attribute(:created_at, Time
.now
)
149 assert
!david
.projects
.loaded
?
152 def test_inspect_does_not_reload_a_not_yet_loaded_target
153 andreas
= Developer
.new
:name => 'Andreas', :log => 'new developer added'
154 assert
!andreas
.audit_logs
.loaded
?
155 assert_match(/message: "new developer added"/, andreas
.audit_logs
.inspect
)
158 def test_save_on_parent_saves_children
159 developer
= Developer
.create
:name => "Bryan", :salary => 50_000
160 assert_equal
1, developer
.reload
.audit_logs
.size
163 def test_create_via_association_with_block
164 post
= authors(:david).posts
.create(:title => "New on Edge") {|p
| p
.body
= "More cool stuff!"}
165 assert_equal post
.title
, "New on Edge"
166 assert_equal post
.body
, "More cool stuff!"
169 def test_create_with_bang_via_association_with_block
170 post
= authors(:david).posts
.create
!(:title => "New on Edge") {|p
| p
.body
= "More cool stuff!"}
171 assert_equal post
.title
, "New on Edge"
172 assert_equal post
.body
, "More cool stuff!"
175 def test_failed_reload_returns_nil
176 p
= setup_dangling_association
177 assert_nil p
.author
.reload
180 def test_failed_reset_returns_nil
181 p
= setup_dangling_association
182 assert_nil p
.author
.reset
185 def test_reload_returns_assocition
186 david
= developers(:david)
187 assert_nothing_raised
do
188 assert_equal david
.projects
, david
.projects
.reload
.reload
192 def setup_dangling_association
193 josh
= Author
.create(:name => "Josh")
194 p
= Post
.create(:title => "New on Edge", :body => "More cool stuff!", :author => josh
)
200 class OverridingAssociationsTest
< ActiveRecord
::TestCase
201 class Person
< ActiveRecord
::Base; end
202 class DifferentPerson
< ActiveRecord
::Base; end
204 class PeopleList
< ActiveRecord
::Base
205 has_and_belongs_to_many
:has_and_belongs_to_many, :before_add => :enlist
206 has_many
:has_many, :before_add => :enlist
207 belongs_to
:belongs_to
211 class DifferentPeopleList
< PeopleList
212 # Different association with the same name, callbacks should be omitted here.
213 has_and_belongs_to_many
:has_and_belongs_to_many, :class_name => 'DifferentPerson'
214 has_many
:has_many, :class_name => 'DifferentPerson'
215 belongs_to
:belongs_to, :class_name => 'DifferentPerson'
216 has_one
:has_one, :class_name => 'DifferentPerson'
219 def test_habtm_association_redefinition_callbacks_should_differ_and_not_inherited
220 # redeclared association on AR descendant should not inherit callbacks from superclass
221 callbacks
= PeopleList
.read_inheritable_attribute(:before_add_for_has_and_belongs_to_many)
222 assert_equal([:enlist], callbacks
)
223 callbacks
= DifferentPeopleList
.read_inheritable_attribute(:before_add_for_has_and_belongs_to_many)
224 assert_equal([], callbacks
)
227 def test_has_many_association_redefinition_callbacks_should_differ_and_not_inherited
228 # redeclared association on AR descendant should not inherit callbacks from superclass
229 callbacks
= PeopleList
.read_inheritable_attribute(:before_add_for_has_many)
230 assert_equal([:enlist], callbacks
)
231 callbacks
= DifferentPeopleList
.read_inheritable_attribute(:before_add_for_has_many)
232 assert_equal([], callbacks
)
235 def test_habtm_association_redefinition_reflections_should_differ_and_not_inherited
237 PeopleList
.reflect_on_association(:has_and_belongs_to_many),
238 DifferentPeopleList
.reflect_on_association(:has_and_belongs_to_many)
242 def test_has_many_association_redefinition_reflections_should_differ_and_not_inherited
244 PeopleList
.reflect_on_association(:has_many),
245 DifferentPeopleList
.reflect_on_association(:has_many)
249 def test_belongs_to_association_redefinition_reflections_should_differ_and_not_inherited
251 PeopleList
.reflect_on_association(:belongs_to),
252 DifferentPeopleList
.reflect_on_association(:belongs_to)
256 def test_has_one_association_redefinition_reflections_should_differ_and_not_inherited
258 PeopleList
.reflect_on_association(:has_one),
259 DifferentPeopleList
.reflect_on_association(:has_one)