Froze rails gems
[depot.git] / vendor / rails / activerecord / test / cases / method_scoping_test.rb
1 require "cases/helper"
2 require 'models/author'
3 require 'models/developer'
4 require 'models/project'
5 require 'models/comment'
6 require 'models/post'
7 require 'models/category'
8
9 class MethodScopingTest < ActiveRecord::TestCase
10 fixtures :authors, :developers, :projects, :comments, :posts, :developers_projects
11
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]
15 end
16 end
17
18 def test_scoped_find
19 Developer.with_scope(:find => { :conditions => "name = 'David'" }) do
20 assert_nothing_raised { Developer.find(1) }
21 end
22 end
23
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')
27 end
28 end
29
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'")
33 end
34 end
35
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)
39 end
40 end
41
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'])
45 end
46 end
47
48 def test_scoped_find_all
49 Developer.with_scope(:find => { :conditions => "name = 'David'" }) do
50 assert_equal [developers(:david)], Developer.find(:all)
51 end
52 end
53
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)
59 end
60 end
61
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)
67 end
68 end
69
70 def test_scoped_count
71 Developer.with_scope(:find => { :conditions => "name = 'David'" }) do
72 assert_equal 1, Developer.count
73 end
74
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%'")
78 end
79 end
80
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')
85 end
86 assert scoped_developers.include?(developers(:david))
87 assert !scoped_developers.include?(developers(:jamis))
88 assert_equal 1, scoped_developers.size
89 end
90
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')
94 end
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
99 end
100
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')
104 end
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
109 end
110
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')
114 end
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
119 end
120
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')
124 end
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
129 end
130
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')
134 end
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
139 end
140
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')
144 end
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
149 end
150
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')
154 end
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
159 end
160
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')
164 end
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
169 end
170
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')
175 end
176 end
177
178 def test_scoped_create
179 new_comment = nil
180
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"
184 end
185
186 assert Post.find(1).comments.include?(new_comment)
187 end
188
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 }
195 end
196
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 }
202 end
203 end
204
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 }
209 end
210 end
211
212 def test_ensure_that_method_scoping_is_correctly_restored
213 scoped_methods = Developer.instance_eval('current_scoped_methods')
214
215 begin
216 Developer.with_scope(:find => { :conditions => "name = 'Jamis'" }) do
217 raise "an exception"
218 end
219 rescue
220 end
221 assert_equal scoped_methods, Developer.instance_eval('current_scoped_methods')
222 end
223 end
224
225 class NestedScopingTest < ActiveRecord::TestCase
226 fixtures :authors, :developers, :projects, :comments, :posts
227
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)
233 end
234 end
235 end
236
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])
242 end
243 end
244 end
245
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)
252 end
253 Developer.with_scope(:find => { :conditions => "name = 'Maiha'" }) do
254 assert_equal(0, Developer.count)
255 end
256 end
257 end
258
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)
264 end
265 end
266 end
267
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)
273 end
274 assert_equal('Jamis', Developer.find(:first).name)
275 end
276 end
277
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)
283 end
284 end
285 end
286
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)
293 end
294 end
295
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)
301 end
302 end
303
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)
309 end
310 end
311 end
312
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
317 end
318 end
319 end
320
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)
324
325 Developer.with_exclusive_scope(:find => { :conditions => "name = 'David'" }) do
326 assert_equal('David', Developer.find(:first).name)
327
328 Developer.with_exclusive_scope(:find => { :conditions => "name = 'Maiha'" }) do
329 assert_equal(nil, Developer.find(:first))
330 end
331
332 # ensure that scoping is restored
333 assert_equal('David', Developer.find(:first).name)
334 end
335
336 # ensure that scoping is restored
337 assert_equal('Jamis', Developer.find(:first).name)
338 end
339 end
340
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'))
346 end
347 end
348 end
349
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) }
354 end
355 end
356 end
357
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'])
363 end
364 end
365 end
366
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 }
371 end
372 end
373 end
374
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) }
380 end
381 end
382 end
383 end
384
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) }
390 end
391 end
392 end
393 end
394
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 }
403 end
404 end
405 end
406
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 }
415 end
416 end
417 end
418
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')
422 begin
423 Developer.with_scope(:find => { :conditions => "name = 'Maiha'" }) do
424 raise "an exception"
425 end
426 rescue
427 end
428 assert_equal scoped_methods, Developer.instance_eval('current_scoped_methods')
429 end
430 end
431
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')
436 end
437 end
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
442 end
443
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')
448 end
449 end
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
454 end
455
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')
460 end
461 end
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
466 end
467 end
468
469 class HasManyScopingTest< ActiveRecord::TestCase
470 fixtures :comments, :posts
471
472 def setup
473 @welcome = Post.find(1)
474 end
475
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
479 end
480
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
484 end
485
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
489 end
490
491 def test_nested_scope
492 Comment.with_scope(:find => { :conditions => '1=1' }) do
493 assert_equal 'a comment...', @welcome.comments.what_are_you
494 end
495 end
496 end
497
498
499 class HasAndBelongsToManyScopingTest< ActiveRecord::TestCase
500 fixtures :posts, :categories, :categories_posts
501
502 def setup
503 @welcome = Post.find(1)
504 end
505
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
509 end
510
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
515 end
516
517 def test_nested_scope
518 Category.with_scope(:find => { :conditions => '1=1' }) do
519 assert_equal 'a comment...', @welcome.comments.what_are_you
520 end
521 end
522 end
523
524
525 =begin
526 # We disabled the scoping for has_one and belongs_to as we can't think of a proper use case
527
528
529 class BelongsToScopingTest< ActiveRecord::TestCase
530 fixtures :comments, :posts
531
532 def setup
533 @greetings = Comment.find(1)
534 end
535
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
539 end
540
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
544 end
545
546 end
547
548
549 class HasOneScopingTest< ActiveRecord::TestCase
550 fixtures :comments, :posts
551
552 def setup
553 @sti_comments = Post.find(4)
554 end
555
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
559 end
560
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
565 end
566
567 end
568
569 =end