aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test/cases/method_scoping_test.rb
blob: a3b496a0e687c8e0216b9ec279d9e9e5dc5c64f1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
require "cases/helper"
require 'models/post'
require 'models/author'
require 'models/developer'
require 'models/project'
require 'models/comment'
require 'models/category'

class MethodScopingTest < ActiveRecord::TestCase
  fixtures :authors, :developers, :projects, :comments, :posts, :developers_projects

  def test_set_conditions
    Developer.send(:with_scope, :find => { :conditions => 'just a test...' }) do
      assert_equal '(just a test...)', Developer.scoped.arel.send(:where_clauses).join(' AND ')
    end
  end

  def test_scoped_find
    Developer.send(:with_scope, :find => { :conditions => "name = 'David'" }) do
      assert_nothing_raised { Developer.find(1) }
    end
  end

  def test_scoped_find_first
    Developer.send(:with_scope, :find => { :conditions => "salary = 100000" }) do
      assert_equal Developer.find(10), Developer.find(:first, :order => 'name')
    end
  end

  def test_scoped_find_last
    highest_salary = Developer.find(:first, :order => "salary DESC")

    Developer.send(:with_scope, :find => { :order => "salary" }) do
      assert_equal highest_salary, Developer.last
    end
  end

  def test_scoped_find_last_preserves_scope
    lowest_salary = Developer.find(:first, :order => "salary ASC")
    highest_salary = Developer.find(:first, :order => "salary DESC")

    Developer.send(:with_scope, :find => { :order => "salary" }) do
      assert_equal highest_salary, Developer.last
      assert_equal lowest_salary, Developer.first
    end
  end

  def test_scoped_find_combines_conditions
    Developer.send(:with_scope, :find => { :conditions => "salary = 9000" }) do
      assert_equal developers(:poor_jamis), Developer.find(:first, :conditions => "name = 'Jamis'")
    end
  end

  def test_scoped_find_sanitizes_conditions
    Developer.send(:with_scope, :find => { :conditions => ['salary = ?', 9000] }) do
      assert_equal developers(:poor_jamis), Developer.find(:first)
    end
  end

  def test_scoped_find_combines_and_sanitizes_conditions
    Developer.send(:with_scope, :find => { :conditions => ['salary = ?', 9000] }) do
      assert_equal developers(:poor_jamis), Developer.find(:first, :conditions => ['name = ?', 'Jamis'])
    end
  end

  def test_scoped_find_all
    Developer.send(:with_scope, :find => { :conditions => "name = 'David'" }) do
      assert_equal [developers(:david)], Developer.find(:all)
    end
  end

  def test_scoped_find_select
    Developer.send(:with_scope, :find => { :select => "id, name" }) do
      developer = Developer.find(:first, :conditions => "name = 'David'")
      assert_equal "David", developer.name
      assert !developer.has_attribute?(:salary)
    end
  end

  def test_options_select_replaces_scope_select
    Developer.send(:with_scope, :find => { :select => "id, name" }) do
      developer = Developer.find(:first, :select => 'id, salary', :conditions => "name = 'David'")
      assert_equal 80000, developer.salary
      assert !developer.has_attribute?(:name)
    end
  end

  def test_scoped_count
    Developer.send(:with_scope, :find => { :conditions => "name = 'David'" }) do
      assert_equal 1, Developer.count
    end

    Developer.send(:with_scope, :find => { :conditions => 'salary = 100000' }) do
      assert_equal 8, Developer.count
      assert_equal 1, Developer.count(:conditions => "name LIKE 'fixture_1%'")
    end
  end

  def test_scoped_find_include
    # with the include, will retrieve only developers for the given project
    scoped_developers = Developer.send(:with_scope, :find => { :include => :projects }) do
      Developer.find(:all, :conditions => 'projects.id = 2')
    end
    assert scoped_developers.include?(developers(:david))
    assert !scoped_developers.include?(developers(:jamis))
    assert_equal 1, scoped_developers.size
  end

  def test_scoped_find_joins
    scoped_developers = Developer.send(:with_scope, :find => { :joins => 'JOIN developers_projects ON id = developer_id' } ) do
      Developer.find(:all, :conditions => 'developers_projects.project_id = 2')
    end
    assert scoped_developers.include?(developers(:david))
    assert !scoped_developers.include?(developers(:jamis))
    assert_equal 1, scoped_developers.size
    assert_equal developers(:david).attributes, scoped_developers.first.attributes
  end

  def test_scoped_find_using_new_style_joins
    scoped_developers = Developer.send(:with_scope, :find => { :joins => :projects }) do
      Developer.find(:all, :conditions => 'projects.id = 2')
    end
    assert scoped_developers.include?(developers(:david))
    assert !scoped_developers.include?(developers(:jamis))
    assert_equal 1, scoped_developers.size
    assert_equal developers(:david).attributes, scoped_developers.first.attributes
  end

  def test_scoped_find_merges_old_style_joins
    scoped_authors = Author.send(:with_scope, :find => { :joins => 'INNER JOIN posts ON authors.id = posts.author_id ' }) do
      Author.find(:all, :select => 'DISTINCT authors.*', :joins => 'INNER JOIN comments ON posts.id = comments.post_id', :conditions => 'comments.id = 1')
    end
    assert scoped_authors.include?(authors(:david))
    assert !scoped_authors.include?(authors(:mary))
    assert_equal 1, scoped_authors.size
    assert_equal authors(:david).attributes, scoped_authors.first.attributes
  end

  def test_scoped_find_merges_new_style_joins
    scoped_authors = Author.send(:with_scope, :find => { :joins => :posts }) do
      Author.find(:all, :select => 'DISTINCT authors.*', :joins => :comments, :conditions => 'comments.id = 1')
    end
    assert scoped_authors.include?(authors(:david))
    assert !scoped_authors.include?(authors(:mary))
    assert_equal 1, scoped_authors.size
    assert_equal authors(:david).attributes, scoped_authors.first.attributes
  end

  def test_scoped_find_merges_new_and_old_style_joins
    scoped_authors = Author.send(:with_scope, :find => { :joins => :posts }) do
      Author.find(:all, :select => 'DISTINCT authors.*', :joins => 'JOIN comments ON posts.id = comments.post_id', :conditions => 'comments.id = 1')
    end
    assert scoped_authors.include?(authors(:david))
    assert !scoped_authors.include?(authors(:mary))
    assert_equal 1, scoped_authors.size
    assert_equal authors(:david).attributes, scoped_authors.first.attributes
  end

  def test_scoped_find_merges_string_array_style_and_string_style_joins
    scoped_authors = Author.send(:with_scope, :find => { :joins => ["INNER JOIN posts ON posts.author_id = authors.id"]}) do
      Author.find(:all, :select => 'DISTINCT authors.*', :joins => 'INNER JOIN comments ON posts.id = comments.post_id', :conditions => 'comments.id = 1')
    end
    assert scoped_authors.include?(authors(:david))
    assert !scoped_authors.include?(authors(:mary))
    assert_equal 1, scoped_authors.size
    assert_equal authors(:david).attributes, scoped_authors.first.attributes
  end

  def test_scoped_find_merges_string_array_style_and_hash_style_joins
    scoped_authors = Author.send(:with_scope, :find => { :joins => :posts}) do
      Author.find(:all, :select => 'DISTINCT authors.*', :joins => ['INNER JOIN comments ON posts.id = comments.post_id'], :conditions => 'comments.id = 1')
    end
    assert scoped_authors.include?(authors(:david))
    assert !scoped_authors.include?(authors(:mary))
    assert_equal 1, scoped_authors.size
    assert_equal authors(:david).attributes, scoped_authors.first.attributes
  end

  def test_scoped_find_merges_joins_and_eliminates_duplicate_string_joins
    scoped_authors = Author.send(:with_scope, :find => { :joins => 'INNER JOIN posts ON posts.author_id = authors.id'}) do
      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')
    end
    assert scoped_authors.include?(authors(:david))
    assert !scoped_authors.include?(authors(:mary))
    assert_equal 1, scoped_authors.size
    assert_equal authors(:david).attributes, scoped_authors.first.attributes
  end

  def test_scoped_find_strips_spaces_from_string_joins_and_eliminates_duplicate_string_joins
    scoped_authors = Author.send(:with_scope, :find => { :joins => ' INNER JOIN posts ON posts.author_id = authors.id '}) do
      Author.find(:all, :select => 'DISTINCT authors.*', :joins => ['INNER JOIN posts ON posts.author_id = authors.id'], :conditions => 'posts.id = 1')
    end
    assert scoped_authors.include?(authors(:david))
    assert !scoped_authors.include?(authors(:mary))
    assert_equal 1, scoped_authors.size
    assert_equal authors(:david).attributes, scoped_authors.first.attributes
  end

  def test_scoped_count_include
    # with the include, will retrieve only developers for the given project
    Developer.send(:with_scope, :find => { :include => :projects }) do
      assert_equal 1, Developer.count(:conditions => 'projects.id = 2')
    end
  end

  def test_scoped_create
    new_comment = nil

    VerySpecialComment.send(:with_scope, :create => { :post_id => 1 }) do
      assert_equal({:post_id => 1}, VerySpecialComment.scoped.send(:scope_for_create))
      new_comment = VerySpecialComment.create :body => "Wonderful world"
    end

    assert Post.find(1).comments.include?(new_comment)
  end

  def test_immutable_scope
    options = { :conditions => "name = 'David'" }
    Developer.send(:with_scope, :find => options) do
      assert_equal %w(David), Developer.find(:all).map { |d| d.name }
      options[:conditions] = "name != 'David'"
      assert_equal %w(David), Developer.find(:all).map { |d| d.name }
    end

    scope = { :find => { :conditions => "name = 'David'" }}
    Developer.send(:with_scope, scope) do
      assert_equal %w(David), Developer.find(:all).map { |d| d.name }
      scope[:find][:conditions] = "name != 'David'"
      assert_equal %w(David), Developer.find(:all).map { |d| d.name }
    end
  end

  def test_scoped_with_duck_typing
    scoping = Struct.new(:method_scoping).new(:find => { :conditions => ["name = ?", 'David'] })
    Developer.send(:with_scope, scoping) do
       assert_equal %w(David), Developer.find(:all).map { |d| d.name }
    end
  end

  def test_ensure_that_method_scoping_is_correctly_restored
    scoped_methods = Developer.instance_eval('current_scoped_methods')

    begin
      Developer.send(:with_scope, :find => { :conditions => "name = 'Jamis'" }) do
        raise "an exception"
      end
    rescue
    end
    assert_equal scoped_methods, Developer.instance_eval('current_scoped_methods')
  end
end

class NestedScopingTest < ActiveRecord::TestCase
  fixtures :authors, :developers, :projects, :comments, :posts

  def test_merge_options
    Developer.send(:with_scope, :find => { :conditions => 'salary = 80000' }) do
      Developer.send(:with_scope, :find => { :limit => 10 }) do
        devs = Developer.scoped
        assert_equal '(salary = 80000)', devs.arel.send(:where_clauses).join(' AND ')
        assert_equal 10, devs.taken
      end
    end
  end

  def test_merge_inner_scope_has_priority
    Developer.send(:with_scope, :find => { :limit => 5 }) do
      Developer.send(:with_scope, :find => { :limit => 10 }) do
        assert_equal 10, Developer.scoped.taken
      end
    end
  end

  def test_replace_options
    Developer.send(:with_scope, :find => { :conditions => {:name => 'David'} }) do
      Developer.send(:with_exclusive_scope, :find => { :conditions => {:name => 'Jamis'} }) do
        assert_equal 'Jamis', Developer.scoped.send(:scope_for_create)[:name]
      end

      assert_equal 'David', Developer.scoped.send(:scope_for_create)[:name]
    end
  end

  def test_append_conditions
    Developer.send(:with_scope, :find => { :conditions => "name = 'David'" }) do
      Developer.send(:with_scope, :find => { :conditions => 'salary = 80000' }) do
        devs = Developer.scoped
        assert_equal "(name = 'David') AND (salary = 80000)", devs.arel.send(:where_clauses).join(' AND ')
        assert_equal(1, Developer.count)
      end
      Developer.send(:with_scope, :find => { :conditions => "name = 'Maiha'" }) do
        assert_equal(0, Developer.count)
      end
    end
  end

  def test_merge_and_append_options
    Developer.send(:with_scope, :find => { :conditions => 'salary = 80000', :limit => 10 }) do
      Developer.send(:with_scope, :find => { :conditions => "name = 'David'" }) do
        devs = Developer.scoped
        assert_equal "(salary = 80000) AND (name = 'David')", devs.arel.send(:where_clauses).join(' AND ')
        assert_equal 10, devs.taken
      end
    end
  end

  def test_nested_scoped_find
    Developer.send(:with_scope, :find => { :conditions => "name = 'Jamis'" }) do
      Developer.send(:with_exclusive_scope, :find => { :conditions => "name = 'David'" }) do
        assert_nothing_raised { Developer.find(1) }
        assert_equal('David', Developer.find(:first).name)
      end
      assert_equal('Jamis', Developer.find(:first).name)
    end
  end

  def test_nested_scoped_find_include
    Developer.send(:with_scope, :find => { :include => :projects }) do
      Developer.send(:with_scope, :find => { :conditions => "projects.id = 2" }) do
        assert_nothing_raised { Developer.find(1) }
        assert_equal('David', Developer.find(:first).name)
      end
    end
  end

  def test_nested_scoped_find_merged_include
    # :include's remain unique and don't "double up" when merging
    Developer.send(:with_scope, :find => { :include => :projects, :conditions => "projects.id = 2" }) do
      Developer.send(:with_scope, :find => { :include => :projects }) do
        assert_equal 1, Developer.scoped.includes_values.uniq.length
        assert_equal 'David', Developer.find(:first).name
      end
    end

    # the nested scope doesn't remove the first :include
    Developer.send(:with_scope, :find => { :include => :projects, :conditions => "projects.id = 2" }) do
      Developer.send(:with_scope, :find => { :include => [] }) do
        assert_equal 1, Developer.scoped.includes_values.uniq.length
        assert_equal('David', Developer.find(:first).name)
      end
    end

    # mixing array and symbol include's will merge correctly
    Developer.send(:with_scope, :find => { :include => [:projects], :conditions => "projects.id = 2" }) do
      Developer.send(:with_scope, :find => { :include => :projects }) do
        assert_equal 1, Developer.scoped.includes_values.uniq.length
        assert_equal('David', Developer.find(:first).name)
      end
    end
  end

  def test_nested_scoped_find_replace_include
    Developer.send(:with_scope, :find => { :include => :projects }) do
      Developer.send(:with_exclusive_scope, :find => { :include => [] }) do
        assert_equal 0, Developer.scoped.includes_values.length
      end
    end
  end

  def test_three_level_nested_exclusive_scoped_find
    Developer.send(:with_scope, :find => { :conditions => "name = 'Jamis'" }) do
      assert_equal('Jamis', Developer.find(:first).name)

      Developer.send(:with_exclusive_scope, :find => { :conditions => "name = 'David'" }) do
        assert_equal('David', Developer.find(:first).name)

        Developer.send(:with_exclusive_scope, :find => { :conditions => "name = 'Maiha'" }) do
          assert_equal(nil, Developer.find(:first))
        end

        # ensure that scoping is restored
        assert_equal('David', Developer.find(:first).name)
      end

      # ensure that scoping is restored
      assert_equal('Jamis', Developer.find(:first).name)
    end
  end

  def test_merged_scoped_find
    poor_jamis = developers(:poor_jamis)
    Developer.send(:with_scope, :find => { :conditions => "salary < 100000" }) do
      Developer.send(:with_scope, :find => { :offset => 1, :order => 'id asc' }) do
        # Oracle adapter does not generated space after asc therefore trailing space removed from regex
        assert_sql(/ORDER BY  id asc/) do
          assert_equal(poor_jamis, Developer.find(:first, :order => 'id asc'))
        end
      end
    end
  end

  def test_merged_scoped_find_sanitizes_conditions
    Developer.send(:with_scope, :find => { :conditions => ["name = ?", 'David'] }) do
      Developer.send(:with_scope, :find => { :conditions => ['salary = ?', 9000] }) do
        assert_raise(ActiveRecord::RecordNotFound) { developers(:poor_jamis) }
      end
    end
  end

  def test_nested_scoped_find_combines_and_sanitizes_conditions
    Developer.send(:with_scope, :find => { :conditions => ["name = ?", 'David'] }) do
      Developer.send(:with_exclusive_scope, :find => { :conditions => ['salary = ?', 9000] }) do
        assert_equal developers(:poor_jamis), Developer.find(:first)
        assert_equal developers(:poor_jamis), Developer.find(:first, :conditions => ['name = ?', 'Jamis'])
      end
    end
  end

  def test_merged_scoped_find_combines_and_sanitizes_conditions
    Developer.send(:with_scope, :find => { :conditions => ["name = ?", 'David'] }) do
      Developer.send(:with_scope, :find => { :conditions => ['salary > ?', 9000] }) do
        assert_equal %w(David), Developer.find(:all).map { |d| d.name }
      end
    end
  end

  def test_nested_scoped_create
    comment = nil
    Comment.send(:with_scope, :create => { :post_id => 1}) do
      Comment.send(:with_scope, :create => { :post_id => 2}) do
        assert_equal({:post_id => 2}, Comment.scoped.send(:scope_for_create))
        comment = Comment.create :body => "Hey guys, nested scopes are broken. Please fix!"
      end
    end
    assert_equal 2, comment.post_id
  end

  def test_nested_exclusive_scope_for_create
    comment = nil

    Comment.send(:with_scope, :create => { :body => "Hey guys, nested scopes are broken. Please fix!" }) do
      Comment.send(:with_exclusive_scope, :create => { :post_id => 1 }) do
        assert_equal({:post_id => 1}, Comment.scoped.send(:scope_for_create))
        assert Comment.new.body.blank?
        comment = Comment.create :body => "Hey guys"
      end
    end
    assert_equal 1, comment.post_id
    assert_equal 'Hey guys', comment.body
  end

  def test_merged_scoped_find_on_blank_conditions
    [nil, " ", [], {}].each do |blank|
      Developer.send(:with_scope, :find => {:conditions => blank}) do
        Developer.send(:with_scope, :find => {:conditions => blank}) do
          assert_nothing_raised { Developer.find(:first) }
        end
      end
    end
  end

  def test_merged_scoped_find_on_blank_bind_conditions
    [ [""], ["",{}] ].each do |blank|
      Developer.send(:with_scope, :find => {:conditions => blank}) do
        Developer.send(:with_scope, :find => {:conditions => blank}) do
          assert_nothing_raised { Developer.find(:first) }
        end
      end
    end
  end

  def test_immutable_nested_scope
    options1 = { :conditions => "name = 'Jamis'" }
    options2 = { :conditions => "name = 'David'" }
    Developer.send(:with_scope, :find => options1) do
      Developer.send(:with_exclusive_scope, :find => options2) do
        assert_equal %w(David), Developer.find(:all).map { |d| d.name }
        options1[:conditions] = options2[:conditions] = nil
        assert_equal %w(David), Developer.find(:all).map { |d| d.name }
      end
    end
  end

  def test_immutable_merged_scope
    options1 = { :conditions => "name = 'Jamis'" }
    options2 = { :conditions => "salary > 10000" }
    Developer.send(:with_scope, :find => options1) do
      Developer.send(:with_scope, :find => options2) do
        assert_equal %w(Jamis), Developer.find(:all).map { |d| d.name }
        options1[:conditions] = options2[:conditions] = nil
        assert_equal %w(Jamis), Developer.find(:all).map { |d| d.name }
      end
    end
  end

  def test_ensure_that_method_scoping_is_correctly_restored
    Developer.send(:with_scope, :find => { :conditions => "name = 'David'" }) do
      scoped_methods = Developer.instance_eval('current_scoped_methods')
      begin
        Developer.send(:with_scope, :find => { :conditions => "name = 'Maiha'" }) do
          raise "an exception"
        end
      rescue
      end
      assert_equal scoped_methods, Developer.instance_eval('current_scoped_methods')
    end
  end

  def test_nested_scoped_find_merges_old_style_joins
    scoped_authors = Author.send(:with_scope, :find => { :joins => 'INNER JOIN posts ON authors.id = posts.author_id' }) do
      Author.send(:with_scope, :find => { :joins => 'INNER JOIN comments ON posts.id = comments.post_id' }) do
        Author.find(:all, :select => 'DISTINCT authors.*', :conditions => 'comments.id = 1')
      end
    end
    assert scoped_authors.include?(authors(:david))
    assert !scoped_authors.include?(authors(:mary))
    assert_equal 1, scoped_authors.size
    assert_equal authors(:david).attributes, scoped_authors.first.attributes
  end

  def test_nested_scoped_find_merges_new_style_joins
    scoped_authors = Author.send(:with_scope, :find => { :joins => :posts }) do
      Author.send(:with_scope, :find => { :joins => :comments }) do
        Author.find(:all, :select => 'DISTINCT authors.*', :conditions => 'comments.id = 1')
      end
    end
    assert scoped_authors.include?(authors(:david))
    assert !scoped_authors.include?(authors(:mary))
    assert_equal 1, scoped_authors.size
    assert_equal authors(:david).attributes, scoped_authors.first.attributes
  end

  def test_nested_scoped_find_merges_new_and_old_style_joins
    scoped_authors = Author.send(:with_scope, :find => { :joins => :posts }) do
      Author.send(:with_scope, :find => { :joins => 'INNER JOIN comments ON posts.id = comments.post_id' }) do
        Author.find(:all, :select => 'DISTINCT authors.*', :joins => '', :conditions => 'comments.id = 1')
      end
    end
    assert scoped_authors.include?(authors(:david))
    assert !scoped_authors.include?(authors(:mary))
    assert_equal 1, scoped_authors.size
    assert_equal authors(:david).attributes, scoped_authors.first.attributes
  end
end

class HasManyScopingTest< ActiveRecord::TestCase
  fixtures :comments, :posts

  def setup
    @welcome = Post.find(1)
  end

  def test_forwarding_of_static_methods
    assert_equal 'a comment...', Comment.what_are_you
    assert_equal 'a comment...', @welcome.comments.what_are_you
  end

  def test_forwarding_to_scoped
    assert_equal 4, Comment.search_by_type('Comment').size
    assert_equal 2, @welcome.comments.search_by_type('Comment').size
  end

  def test_forwarding_to_dynamic_finders
    assert_equal 4, Comment.find_all_by_type('Comment').size
    assert_equal 2, @welcome.comments.find_all_by_type('Comment').size
  end

  def test_nested_scope
    Comment.send(:with_scope, :find => { :conditions => '1=1' }) do
      assert_equal 'a comment...', @welcome.comments.what_are_you
    end
  end
end

class HasAndBelongsToManyScopingTest< ActiveRecord::TestCase
  fixtures :posts, :categories, :categories_posts

  def setup
    @welcome = Post.find(1)
  end

  def test_forwarding_of_static_methods
    assert_equal 'a category...', Category.what_are_you
    assert_equal 'a category...', @welcome.categories.what_are_you
  end

  def test_forwarding_to_dynamic_finders
    assert_equal 4, Category.find_all_by_type('SpecialCategory').size
    assert_equal 0, @welcome.categories.find_all_by_type('SpecialCategory').size
    assert_equal 2, @welcome.categories.find_all_by_type('Category').size
  end

  def test_nested_scope
    Category.send(:with_scope, :find => { :conditions => '1=1' }) do
      assert_equal 'a comment...', @welcome.comments.what_are_you
    end
  end
end

class DefaultScopingTest < ActiveRecord::TestCase
  fixtures :developers, :posts

  def test_default_scope
    expected = Developer.find(:all, :order => 'salary DESC').collect { |dev| dev.salary }
    received = DeveloperOrderedBySalary.find(:all).collect { |dev| dev.salary }
    assert_equal expected, received
  end

  def test_default_scope_with_conditions_string
    assert_equal Developer.find_all_by_name('David').map(&:id).sort, DeveloperCalledDavid.find(:all).map(&:id).sort
    assert_equal nil, DeveloperCalledDavid.create!.name
  end

  def test_default_scope_with_conditions_hash
    assert_equal Developer.find_all_by_name('Jamis').map(&:id).sort, DeveloperCalledJamis.find(:all).map(&:id).sort
    assert_equal 'Jamis', DeveloperCalledJamis.create!.name
  end

  def test_default_scoping_with_threads
    2.times do
      Thread.new { assert_equal ['salary DESC'], DeveloperOrderedBySalary.scoped.order_values }.join
    end
  end

  def test_default_scoping_with_inheritance
    # Inherit a class having a default scope and define a new default scope
    klass = Class.new(DeveloperOrderedBySalary)
    klass.send :default_scope, {}

    # Scopes added on children should append to parent scope
    assert klass.scoped.order_values.blank?

    # Parent should still have the original scope
    assert_equal ['salary DESC'], DeveloperOrderedBySalary.scoped.order_values
  end

  def test_method_scope
    expected = Developer.find(:all, :order => 'name DESC').collect { |dev| dev.salary }
    received = DeveloperOrderedBySalary.all_ordered_by_name.collect { |dev| dev.salary }
    assert_equal expected, received
  end

  def test_nested_scope
    expected = Developer.find(:all, :order => 'name DESC').collect { |dev| dev.salary }
    received = DeveloperOrderedBySalary.send(:with_scope, :find => { :order => 'name DESC'}) do
      DeveloperOrderedBySalary.find(:all).collect { |dev| dev.salary }
    end
    assert_equal expected, received
  end

  def test_named_scope_overwrites_default
    expected = Developer.find(:all, :order => 'name DESC').collect { |dev| dev.name }
    received = DeveloperOrderedBySalary.by_name.find(:all).collect { |dev| dev.name }
    assert_equal expected, received
  end

  def test_nested_exclusive_scope
    expected = Developer.find(:all, :limit => 100).collect { |dev| dev.salary }
    received = DeveloperOrderedBySalary.send(:with_exclusive_scope, :find => { :limit => 100 }) do
      DeveloperOrderedBySalary.find(:all).collect { |dev| dev.salary }
    end
    assert_equal expected, received
  end

  def test_overwriting_default_scope
    expected = Developer.find(:all, :order => 'salary').collect { |dev| dev.salary }
    received = DeveloperOrderedBySalary.find(:all, :order => 'salary').collect { |dev| dev.salary }
    assert_equal expected, received
  end

  def test_default_scope_using_relation
    posts = PostWithComment.scoped
    assert_equal 2, posts.count
    assert_equal posts(:thinking), posts.first
  end

  def test_create_attribute_overwrites_default_scoping
    assert_equal 'David', PoorDeveloperCalledJamis.create!(:name => 'David').name
    assert_equal 200000, PoorDeveloperCalledJamis.create!(:name => 'David', :salary => 200000).salary
  end
 
  def test_create_attribute_overwrites_default_values
    assert_equal nil, PoorDeveloperCalledJamis.create!(:salary => nil).salary
    assert_equal 50000, PoorDeveloperCalledJamis.create!(:name => 'David').salary
  end
end

=begin
# We disabled the scoping for has_one and belongs_to as we can't think of a proper use case

class BelongsToScopingTest< ActiveRecord::TestCase
  fixtures :comments, :posts

  def setup
    @greetings = Comment.find(1)
  end

  def test_forwarding_of_static_method
    assert_equal 'a post...', Post.what_are_you
    assert_equal 'a post...', @greetings.post.what_are_you
  end

  def test_forwarding_to_dynamic_finders
    assert_equal 4, Post.find_all_by_type('Post').size
    assert_equal 1, @greetings.post.find_all_by_type('Post').size
  end

end

class HasOneScopingTest< ActiveRecord::TestCase
  fixtures :comments, :posts

  def setup
    @sti_comments = Post.find(4)
  end

  def test_forwarding_of_static_methods
    assert_equal 'a comment...', Comment.what_are_you
    assert_equal 'a very special comment...', @sti_comments.very_special_comment.what_are_you
  end

  def test_forwarding_to_dynamic_finders
    assert_equal 1, Comment.find_all_by_type('VerySpecialComment').size
    assert_equal 1, @sti_comments.very_special_comment.find_all_by_type('VerySpecialComment').size
    assert_equal 0, @sti_comments.very_special_comment.find_all_by_type('Comment').size
  end

end

=end