2 require 'models/author'
3 require 'models/developer'
4 require 'models/project'
5 require 'models/comment'
7 require 'models/category'
9 class MethodScopingTest
< ActiveRecord
::TestCase
10 fixtures
:authors, :developers, :projects, :comments, :posts, :developers_projects
12 def test_set_conditions
13 Developer
.with_scope(:find => { :conditions => 'just a test...' }) do
14 assert_equal
'just a test...', Developer
.send(:current_scoped_methods)[:find][:conditions]
19 Developer
.with_scope(:find => { :conditions => "name = 'David'" }) do
20 assert_nothing_raised
{ Developer
.find(1) }
24 def test_scoped_find_first
25 Developer
.with_scope(:find => { :conditions => "salary = 100000" }) do
26 assert_equal Developer
.find(10), Developer
.find(:first, :order => 'name')
30 def test_scoped_find_combines_conditions
31 Developer
.with_scope(:find => { :conditions => "salary = 9000" }) do
32 assert_equal
developers(:poor_jamis), Developer
.find(:first, :conditions => "name = 'Jamis'")
36 def test_scoped_find_sanitizes_conditions
37 Developer
.with_scope(:find => { :conditions => ['salary = ?', 9000] }) do
38 assert_equal
developers(:poor_jamis), Developer
.find(:first)
42 def test_scoped_find_combines_and_sanitizes_conditions
43 Developer
.with_scope(:find => { :conditions => ['salary = ?', 9000] }) do
44 assert_equal
developers(:poor_jamis), Developer
.find(:first, :conditions => ['name = ?', 'Jamis'])
48 def test_scoped_find_all
49 Developer
.with_scope(:find => { :conditions => "name = 'David'" }) do
50 assert_equal
[developers(:david)], Developer
.find(:all)
54 def test_scoped_find_select
55 Developer
.with_scope(:find => { :select => "id, name" }) do
56 developer
= Developer
.find(:first, :conditions => "name = 'David'")
57 assert_equal
"David", developer
.name
58 assert
!developer
.has_attribute
?(:salary)
62 def test_options_select_replaces_scope_select
63 Developer
.with_scope(:find => { :select => "id, name" }) do
64 developer
= Developer
.find(:first, :select => 'id, salary', :conditions => "name = 'David'")
65 assert_equal
80000, developer
.salary
66 assert
!developer
.has_attribute
?(:name)
71 Developer
.with_scope(:find => { :conditions => "name = 'David'" }) do
72 assert_equal
1, Developer
.count
75 Developer
.with_scope(:find => { :conditions => 'salary = 100000' }) do
76 assert_equal
8, Developer
.count
77 assert_equal
1, Developer
.count(:conditions => "name LIKE 'fixture_1%'")
81 def test_scoped_find_include
82 # with the include, will retrieve only developers for the given project
83 scoped_developers
= Developer
.with_scope(:find => { :include => :projects }) do
84 Developer
.find(:all, :conditions => 'projects.id = 2')
86 assert scoped_developers
.include?(developers(:david))
87 assert
!scoped_developers
.include?(developers(:jamis))
88 assert_equal
1, scoped_developers
.size
91 def test_scoped_find_joins
92 scoped_developers
= Developer
.with_scope(:find => { :joins => 'JOIN developers_projects ON id = developer_id' } ) do
93 Developer
.find(:all, :conditions => 'developers_projects.project_id = 2')
95 assert scoped_developers
.include?(developers(:david))
96 assert
!scoped_developers
.include?(developers(:jamis))
97 assert_equal
1, scoped_developers
.size
98 assert_equal
developers(:david).attributes
, scoped_developers
.first
.attributes
101 def test_scoped_find_using_new_style_joins
102 scoped_developers
= Developer
.with_scope(:find => { :joins => :projects }) do
103 Developer
.find(:all, :conditions => 'projects.id = 2')
105 assert scoped_developers
.include?(developers(:david))
106 assert
!scoped_developers
.include?(developers(:jamis))
107 assert_equal
1, scoped_developers
.size
108 assert_equal
developers(:david).attributes
, scoped_developers
.first
.attributes
111 def test_scoped_find_merges_old_style_joins
112 scoped_authors
= Author
.with_scope(:find => { :joins => 'INNER JOIN posts ON authors.id = posts.author_id ' }) do
113 Author
.find(:all, :select => 'DISTINCT authors.*', :joins => 'INNER JOIN comments ON posts.id = comments.post_id', :conditions => 'comments.id = 1')
115 assert scoped_authors
.include?(authors(:david))
116 assert
!scoped_authors
.include?(authors(:mary))
117 assert_equal
1, scoped_authors
.size
118 assert_equal
authors(:david).attributes
, scoped_authors
.first
.attributes
121 def test_scoped_find_merges_new_style_joins
122 scoped_authors
= Author
.with_scope(:find => { :joins => :posts }) do
123 Author
.find(:all, :select => 'DISTINCT authors.*', :joins => :comments, :conditions => 'comments.id = 1')
125 assert scoped_authors
.include?(authors(:david))
126 assert
!scoped_authors
.include?(authors(:mary))
127 assert_equal
1, scoped_authors
.size
128 assert_equal
authors(:david).attributes
, scoped_authors
.first
.attributes
131 def test_scoped_find_merges_new_and_old_style_joins
132 scoped_authors
= Author
.with_scope(:find => { :joins => :posts }) do
133 Author
.find(:all, :select => 'DISTINCT authors.*', :joins => 'JOIN comments ON posts.id = comments.post_id', :conditions => 'comments.id = 1')
135 assert scoped_authors
.include?(authors(:david))
136 assert
!scoped_authors
.include?(authors(:mary))
137 assert_equal
1, scoped_authors
.size
138 assert_equal
authors(:david).attributes
, scoped_authors
.first
.attributes
141 def test_scoped_find_merges_string_array_style_and_string_style_joins
142 scoped_authors
= Author
.with_scope(:find => { :joins => ["INNER JOIN posts ON posts.author_id = authors.id"]}) do
143 Author
.find(:all, :select => 'DISTINCT authors.*', :joins => 'INNER JOIN comments ON posts.id = comments.post_id', :conditions => 'comments.id = 1')
145 assert scoped_authors
.include?(authors(:david))
146 assert
!scoped_authors
.include?(authors(:mary))
147 assert_equal
1, scoped_authors
.size
148 assert_equal
authors(:david).attributes
, scoped_authors
.first
.attributes
151 def test_scoped_find_merges_string_array_style_and_hash_style_joins
152 scoped_authors
= Author
.with_scope(:find => { :joins => :posts}) do
153 Author
.find(:all, :select => 'DISTINCT authors.*', :joins => ['INNER JOIN comments ON posts.id = comments.post_id'], :conditions => 'comments.id = 1')
155 assert scoped_authors
.include?(authors(:david))
156 assert
!scoped_authors
.include?(authors(:mary))
157 assert_equal
1, scoped_authors
.size
158 assert_equal
authors(:david).attributes
, scoped_authors
.first
.attributes
161 def test_scoped_find_merges_joins_and_eliminates_duplicate_string_joins
162 scoped_authors
= Author
.with_scope(:find => { :joins => 'INNER JOIN posts ON posts.author_id = authors.id'}) do
163 Author
.find(:all, :select => 'DISTINCT authors.*', :joins => ["INNER JOIN posts ON posts.author_id = authors.id", "INNER JOIN comments ON posts.id = comments.post_id"], :conditions => 'comments.id = 1')
165 assert scoped_authors
.include?(authors(:david))
166 assert
!scoped_authors
.include?(authors(:mary))
167 assert_equal
1, scoped_authors
.size
168 assert_equal
authors(:david).attributes
, scoped_authors
.first
.attributes
171 def test_scoped_count_include
172 # with the include, will retrieve only developers for the given project
173 Developer
.with_scope(:find => { :include => :projects }) do
174 assert_equal
1, Developer
.count(:conditions => 'projects.id = 2')
178 def test_scoped_create
181 VerySpecialComment
.with_scope(:create => { :post_id => 1 }) do
182 assert_equal({ :post_id => 1 }, VerySpecialComment
.send(:current_scoped_methods)[:create])
183 new_comment
= VerySpecialComment
.create
:body => "Wonderful world"
186 assert Post
.find(1).comments
.include?(new_comment
)
189 def test_immutable_scope
190 options
= { :conditions => "name = 'David'" }
191 Developer
.with_scope(:find => options
) do
192 assert_equal
%w(David
), Developer
.find(:all).map
{ |d
| d
.name
}
193 options
[:conditions] = "name != 'David'"
194 assert_equal
%w(David
), Developer
.find(:all).map
{ |d
| d
.name
}
197 scope
= { :find => { :conditions => "name = 'David'" }}
198 Developer
.with_scope(scope
) do
199 assert_equal
%w(David
), Developer
.find(:all).map
{ |d
| d
.name
}
200 scope
[:find][:conditions] = "name != 'David'"
201 assert_equal
%w(David
), Developer
.find(:all).map
{ |d
| d
.name
}
205 def test_scoped_with_duck_typing
206 scoping
= Struct
.new(:method_scoping).new(:find => { :conditions => ["name = ?", 'David'] })
207 Developer
.with_scope(scoping
) do
208 assert_equal
%w(David
), Developer
.find(:all).map
{ |d
| d
.name
}
212 def test_ensure_that_method_scoping_is_correctly_restored
213 scoped_methods
= Developer
.instance_eval('current_scoped_methods')
216 Developer
.with_scope(:find => { :conditions => "name = 'Jamis'" }) do
221 assert_equal scoped_methods
, Developer
.instance_eval('current_scoped_methods')
225 class NestedScopingTest
< ActiveRecord
::TestCase
226 fixtures
:authors, :developers, :projects, :comments, :posts
228 def test_merge_options
229 Developer
.with_scope(:find => { :conditions => 'salary = 80000' }) do
230 Developer
.with_scope(:find => { :limit => 10 }) do
231 merged_option
= Developer
.instance_eval('current_scoped_methods')[:find]
232 assert_equal({ :conditions => 'salary = 80000', :limit => 10 }, merged_option
)
237 def test_replace_options
238 Developer
.with_scope(:find => { :conditions => "name = 'David'" }) do
239 Developer
.with_exclusive_scope(:find => { :conditions => "name = 'Jamis'" }) do
240 assert_equal({:find => { :conditions => "name = 'Jamis'" }}, Developer
.instance_eval('current_scoped_methods'))
241 assert_equal({:find => { :conditions => "name = 'Jamis'" }}, Developer
.send(:scoped_methods)[-1])
246 def test_append_conditions
247 Developer
.with_scope(:find => { :conditions => "name = 'David'" }) do
248 Developer
.with_scope(:find => { :conditions => 'salary = 80000' }) do
249 appended_condition
= Developer
.instance_eval('current_scoped_methods')[:find][:conditions]
250 assert_equal("(name = 'David') AND (salary = 80000)", appended_condition
)
251 assert_equal(1, Developer
.count
)
253 Developer
.with_scope(:find => { :conditions => "name = 'Maiha'" }) do
254 assert_equal(0, Developer
.count
)
259 def test_merge_and_append_options
260 Developer
.with_scope(:find => { :conditions => 'salary = 80000', :limit => 10 }) do
261 Developer
.with_scope(:find => { :conditions => "name = 'David'" }) do
262 merged_option
= Developer
.instance_eval('current_scoped_methods')[:find]
263 assert_equal({ :conditions => "(salary = 80000) AND (name = 'David')", :limit => 10 }, merged_option
)
268 def test_nested_scoped_find
269 Developer
.with_scope(:find => { :conditions => "name = 'Jamis'" }) do
270 Developer
.with_exclusive_scope(:find => { :conditions => "name = 'David'" }) do
271 assert_nothing_raised
{ Developer
.find(1) }
272 assert_equal('David', Developer
.find(:first).name
)
274 assert_equal('Jamis', Developer
.find(:first).name
)
278 def test_nested_scoped_find_include
279 Developer
.with_scope(:find => { :include => :projects }) do
280 Developer
.with_scope(:find => { :conditions => "projects.id = 2" }) do
281 assert_nothing_raised
{ Developer
.find(1) }
282 assert_equal('David', Developer
.find(:first).name
)
287 def test_nested_scoped_find_merged_include
288 # :include's remain unique and don't "double up" when merging
289 Developer
.with_scope(:find => { :include => :projects, :conditions => "projects.id = 2" }) do
290 Developer
.with_scope(:find => { :include => :projects }) do
291 assert_equal
1, Developer
.instance_eval('current_scoped_methods')[:find][:include].length
292 assert_equal('David', Developer
.find(:first).name
)
296 # the nested scope doesn't remove the first :include
297 Developer
.with_scope(:find => { :include => :projects, :conditions => "projects.id = 2" }) do
298 Developer
.with_scope(:find => { :include => [] }) do
299 assert_equal
1, Developer
.instance_eval('current_scoped_methods')[:find][:include].length
300 assert_equal('David', Developer
.find(:first).name
)
304 # mixing array and symbol include's will merge correctly
305 Developer
.with_scope(:find => { :include => [:projects], :conditions => "projects.id = 2" }) do
306 Developer
.with_scope(:find => { :include => :projects }) do
307 assert_equal
1, Developer
.instance_eval('current_scoped_methods')[:find][:include].length
308 assert_equal('David', Developer
.find(:first).name
)
313 def test_nested_scoped_find_replace_include
314 Developer
.with_scope(:find => { :include => :projects }) do
315 Developer
.with_exclusive_scope(:find => { :include => [] }) do
316 assert_equal
0, Developer
.instance_eval('current_scoped_methods')[:find][:include].length
321 def test_three_level_nested_exclusive_scoped_find
322 Developer
.with_scope(:find => { :conditions => "name = 'Jamis'" }) do
323 assert_equal('Jamis', Developer
.find(:first).name
)
325 Developer
.with_exclusive_scope(:find => { :conditions => "name = 'David'" }) do
326 assert_equal('David', Developer
.find(:first).name
)
328 Developer
.with_exclusive_scope(:find => { :conditions => "name = 'Maiha'" }) do
329 assert_equal(nil, Developer
.find(:first))
332 # ensure that scoping is restored
333 assert_equal('David', Developer
.find(:first).name
)
336 # ensure that scoping is restored
337 assert_equal('Jamis', Developer
.find(:first).name
)
341 def test_merged_scoped_find
342 poor_jamis
= developers(:poor_jamis)
343 Developer
.with_scope(:find => { :conditions => "salary < 100000" }) do
344 Developer
.with_scope(:find => { :offset => 1 }) do
345 assert_equal(poor_jamis
, Developer
.find(:first, :order => 'id asc'))
350 def test_merged_scoped_find_sanitizes_conditions
351 Developer
.with_scope(:find => { :conditions => ["name = ?", 'David'] }) do
352 Developer
.with_scope(:find => { :conditions => ['salary = ?', 9000] }) do
353 assert_raise(ActiveRecord
::RecordNotFound) { developers(:poor_jamis) }
358 def test_nested_scoped_find_combines_and_sanitizes_conditions
359 Developer
.with_scope(:find => { :conditions => ["name = ?", 'David'] }) do
360 Developer
.with_exclusive_scope(:find => { :conditions => ['salary = ?', 9000] }) do
361 assert_equal
developers(:poor_jamis), Developer
.find(:first)
362 assert_equal
developers(:poor_jamis), Developer
.find(:first, :conditions => ['name = ?', 'Jamis'])
367 def test_merged_scoped_find_combines_and_sanitizes_conditions
368 Developer
.with_scope(:find => { :conditions => ["name = ?", 'David'] }) do
369 Developer
.with_scope(:find => { :conditions => ['salary > ?', 9000] }) do
370 assert_equal
%w(David
), Developer
.find(:all).map
{ |d
| d
.name
}
375 def test_merged_scoped_find_on_blank_conditions
376 [nil, " ", [], {}].each
do |blank
|
377 Developer
.with_scope(:find => {:conditions => blank
}) do
378 Developer
.with_scope(:find => {:conditions => blank
}) do
379 assert_nothing_raised
{ Developer
.find(:first) }
385 def test_merged_scoped_find_on_blank_bind_conditions
386 [ [""], ["",{}] ].each
do |blank
|
387 Developer
.with_scope(:find => {:conditions => blank
}) do
388 Developer
.with_scope(:find => {:conditions => blank
}) do
389 assert_nothing_raised
{ Developer
.find(:first) }
395 def test_immutable_nested_scope
396 options1
= { :conditions => "name = 'Jamis'" }
397 options2
= { :conditions => "name = 'David'" }
398 Developer
.with_scope(:find => options1
) do
399 Developer
.with_exclusive_scope(:find => options2
) do
400 assert_equal
%w(David
), Developer
.find(:all).map
{ |d
| d
.name
}
401 options1
[:conditions] = options2
[:conditions] = nil
402 assert_equal
%w(David
), Developer
.find(:all).map
{ |d
| d
.name
}
407 def test_immutable_merged_scope
408 options1
= { :conditions => "name = 'Jamis'" }
409 options2
= { :conditions => "salary > 10000" }
410 Developer
.with_scope(:find => options1
) do
411 Developer
.with_scope(:find => options2
) do
412 assert_equal
%w(Jamis
), Developer
.find(:all).map
{ |d
| d
.name
}
413 options1
[:conditions] = options2
[:conditions] = nil
414 assert_equal
%w(Jamis
), Developer
.find(:all).map
{ |d
| d
.name
}
419 def test_ensure_that_method_scoping_is_correctly_restored
420 Developer
.with_scope(:find => { :conditions => "name = 'David'" }) do
421 scoped_methods
= Developer
.instance_eval('current_scoped_methods')
423 Developer
.with_scope(:find => { :conditions => "name = 'Maiha'" }) do
428 assert_equal scoped_methods
, Developer
.instance_eval('current_scoped_methods')
432 def test_nested_scoped_find_merges_old_style_joins
433 scoped_authors
= Author
.with_scope(:find => { :joins => 'INNER JOIN posts ON authors.id = posts.author_id' }) do
434 Author
.with_scope(:find => { :joins => 'INNER JOIN comments ON posts.id = comments.post_id' }) do
435 Author
.find(:all, :select => 'DISTINCT authors.*', :conditions => 'comments.id = 1')
438 assert scoped_authors
.include?(authors(:david))
439 assert
!scoped_authors
.include?(authors(:mary))
440 assert_equal
1, scoped_authors
.size
441 assert_equal
authors(:david).attributes
, scoped_authors
.first
.attributes
444 def test_nested_scoped_find_merges_new_style_joins
445 scoped_authors
= Author
.with_scope(:find => { :joins => :posts }) do
446 Author
.with_scope(:find => { :joins => :comments }) do
447 Author
.find(:all, :select => 'DISTINCT authors.*', :conditions => 'comments.id = 1')
450 assert scoped_authors
.include?(authors(:david))
451 assert
!scoped_authors
.include?(authors(:mary))
452 assert_equal
1, scoped_authors
.size
453 assert_equal
authors(:david).attributes
, scoped_authors
.first
.attributes
456 def test_nested_scoped_find_merges_new_and_old_style_joins
457 scoped_authors
= Author
.with_scope(:find => { :joins => :posts }) do
458 Author
.with_scope(:find => { :joins => 'INNER JOIN comments ON posts.id = comments.post_id' }) do
459 Author
.find(:all, :select => 'DISTINCT authors.*', :joins => '', :conditions => 'comments.id = 1')
462 assert scoped_authors
.include?(authors(:david))
463 assert
!scoped_authors
.include?(authors(:mary))
464 assert_equal
1, scoped_authors
.size
465 assert_equal
authors(:david).attributes
, scoped_authors
.first
.attributes
469 class HasManyScopingTest
< ActiveRecord
::TestCase
470 fixtures
:comments, :posts
473 @welcome = Post
.find(1)
476 def test_forwarding_of_static_methods
477 assert_equal
'a comment...', Comment
.what_are_you
478 assert_equal
'a comment...', @welcome.comments
.what_are_you
481 def test_forwarding_to_scoped
482 assert_equal
4, Comment
.search_by_type('Comment').size
483 assert_equal
2, @welcome.comments
.search_by_type('Comment').size
486 def test_forwarding_to_dynamic_finders
487 assert_equal
4, Comment
.find_all_by_type('Comment').size
488 assert_equal
2, @welcome.comments
.find_all_by_type('Comment').size
491 def test_nested_scope
492 Comment
.with_scope(:find => { :conditions => '1=1' }) do
493 assert_equal
'a comment...', @welcome.comments
.what_are_you
499 class HasAndBelongsToManyScopingTest
< ActiveRecord
::TestCase
500 fixtures
:posts, :categories, :categories_posts
503 @welcome = Post
.find(1)
506 def test_forwarding_of_static_methods
507 assert_equal
'a category...', Category
.what_are_you
508 assert_equal
'a category...', @welcome.categories
.what_are_you
511 def test_forwarding_to_dynamic_finders
512 assert_equal
4, Category
.find_all_by_type('SpecialCategory').size
513 assert_equal
0, @welcome.categories
.find_all_by_type('SpecialCategory').size
514 assert_equal
2, @welcome.categories
.find_all_by_type('Category').size
517 def test_nested_scope
518 Category
.with_scope(:find => { :conditions => '1=1' }) do
519 assert_equal
'a comment...', @welcome.comments
.what_are_you
526 # We disabled the scoping for has_one and belongs_to as we can't think of a proper use case
529 class BelongsToScopingTest< ActiveRecord::TestCase
530 fixtures :comments, :posts
533 @greetings = Comment.find(1)
536 def test_forwarding_of_static_method
537 assert_equal 'a post...', Post.what_are_you
538 assert_equal 'a post...', @greetings.post.what_are_you
541 def test_forwarding_to_dynamic_finders
542 assert_equal 4, Post.find_all_by_type('Post').size
543 assert_equal 1, @greetings.post.find_all_by_type('Post').size
549 class HasOneScopingTest< ActiveRecord::TestCase
550 fixtures :comments, :posts
553 @sti_comments = Post.find(4)
556 def test_forwarding_of_static_methods
557 assert_equal 'a comment...', Comment.what_are_you
558 assert_equal 'a very special comment...', @sti_comments.very_special_comment.what_are_you
561 def test_forwarding_to_dynamic_finders
562 assert_equal 1, Comment.find_all_by_type('VerySpecialComment').size
563 assert_equal 1, @sti_comments.very_special_comment.find_all_by_type('VerySpecialComment').size
564 assert_equal 0, @sti_comments.very_special_comment.find_all_by_type('Comment').size