Updated README.rdoc again
[feedcatcher.git] / vendor / rails / activerecord / test / cases / associations / eager_load_nested_include_test.rb
1 require 'cases/helper'
2 require 'models/author'
3 require 'models/post'
4 require 'models/comment'
5 require 'models/category'
6 require 'models/categorization'
7
8 module Remembered
9 def self.included(base)
10 base.extend ClassMethods
11 base.class_eval do
12 after_create :remember
13 protected
14 def remember; self.class.remembered << self; end
15 end
16 end
17
18 module ClassMethods
19 def remembered; @@remembered ||= []; end
20 def rand; @@remembered.rand; end
21 end
22 end
23
24 class ShapeExpression < ActiveRecord::Base
25 belongs_to :shape, :polymorphic => true
26 belongs_to :paint, :polymorphic => true
27 end
28
29 class Circle < ActiveRecord::Base
30 has_many :shape_expressions, :as => :shape
31 include Remembered
32 end
33 class Square < ActiveRecord::Base
34 has_many :shape_expressions, :as => :shape
35 include Remembered
36 end
37 class Triangle < ActiveRecord::Base
38 has_many :shape_expressions, :as => :shape
39 include Remembered
40 end
41 class PaintColor < ActiveRecord::Base
42 has_many :shape_expressions, :as => :paint
43 belongs_to :non_poly, :foreign_key => "non_poly_one_id", :class_name => "NonPolyOne"
44 include Remembered
45 end
46 class PaintTexture < ActiveRecord::Base
47 has_many :shape_expressions, :as => :paint
48 belongs_to :non_poly, :foreign_key => "non_poly_two_id", :class_name => "NonPolyTwo"
49 include Remembered
50 end
51 class NonPolyOne < ActiveRecord::Base
52 has_many :paint_colors
53 include Remembered
54 end
55 class NonPolyTwo < ActiveRecord::Base
56 has_many :paint_textures
57 include Remembered
58 end
59
60
61
62 class EagerLoadPolyAssocsTest < ActiveRecord::TestCase
63 NUM_SIMPLE_OBJS = 50
64 NUM_SHAPE_EXPRESSIONS = 100
65
66 def setup
67 generate_test_object_graphs
68 end
69
70 def teardown
71 [Circle, Square, Triangle, PaintColor, PaintTexture,
72 ShapeExpression, NonPolyOne, NonPolyTwo].each do |c|
73 c.delete_all
74 end
75
76 end
77
78
79 def generate_test_object_graphs
80 1.upto(NUM_SIMPLE_OBJS) do
81 [Circle, Square, Triangle, NonPolyOne, NonPolyTwo].map(&:create!)
82 end
83 1.upto(NUM_SIMPLE_OBJS) do
84 PaintColor.create!(:non_poly_one_id => NonPolyOne.rand.id)
85 PaintTexture.create!(:non_poly_two_id => NonPolyTwo.rand.id)
86 end
87 1.upto(NUM_SHAPE_EXPRESSIONS) do
88 shape_type = [Circle, Square, Triangle].rand
89 paint_type = [PaintColor, PaintTexture].rand
90 ShapeExpression.create!(:shape_type => shape_type.to_s, :shape_id => shape_type.rand.id,
91 :paint_type => paint_type.to_s, :paint_id => paint_type.rand.id)
92 end
93 end
94
95 def test_include_query
96 res = 0
97 res = ShapeExpression.find :all, :include => [ :shape, { :paint => :non_poly } ]
98 assert_equal NUM_SHAPE_EXPRESSIONS, res.size
99 assert_queries(0) do
100 res.each do |se|
101 assert_not_nil se.paint.non_poly, "this is the association that was loading incorrectly before the change"
102 assert_not_nil se.shape, "just making sure other associations still work"
103 end
104 end
105 end
106 end
107
108 class EagerLoadNestedIncludeWithMissingDataTest < ActiveRecord::TestCase
109 def setup
110 @davey_mcdave = Author.create(:name => 'Davey McDave')
111 @first_post = @davey_mcdave.posts.create(:title => 'Davey Speaks', :body => 'Expressive wordage')
112 @first_comment = @first_post.comments.create(:body => 'Inflamatory doublespeak')
113 @first_categorization = @davey_mcdave.categorizations.create(:category => Category.first, :post => @first_post)
114 end
115
116 def teardown
117 @davey_mcdave.destroy
118 @first_post.destroy
119 @first_comment.destroy
120 @first_categorization.destroy
121 end
122
123 def test_missing_data_in_a_nested_include_should_not_cause_errors_when_constructing_objects
124 assert_nothing_raised do
125 # @davey_mcdave doesn't have any author_favorites
126 includes = {:posts => :comments, :categorizations => :category, :author_favorites => :favorite_author }
127 Author.all :include => includes, :conditions => {:authors => {:name => @davey_mcdave.name}}, :order => 'categories.name'
128 end
129 end
130 end