Class: TreeTestWithEagerLoading

Inherits:
Test::Unit::TestCase
  • Object
show all
Defined in:
lib/plugins/acts_as_tree/test/acts_as_tree_test.rb

Instance Method Summary collapse

Instance Method Details

#setupObject



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/plugins/acts_as_tree/test/acts_as_tree_test.rb', line 156

def setup 
  teardown_db
  setup_db
  @root1 = TreeMixin.create!
  @root_child1 = TreeMixin.create! :parent_id => @root1.id
  @child1_child = TreeMixin.create! :parent_id => @root_child1.id
  @root_child2 = TreeMixin.create! :parent_id => @root1.id
  @root2 = TreeMixin.create!
  @root3 = TreeMixin.create!
  
  @rc1 = RecursivelyCascadedTreeMixin.create!
  @rc2 = RecursivelyCascadedTreeMixin.create! :parent_id => @rc1.id 
  @rc3 = RecursivelyCascadedTreeMixin.create! :parent_id => @rc2.id
  @rc4 = RecursivelyCascadedTreeMixin.create! :parent_id => @rc3.id
end

#teardownObject



172
173
174
# File 'lib/plugins/acts_as_tree/test/acts_as_tree_test.rb', line 172

def teardown
  teardown_db
end

#test_eager_association_loadingObject



176
177
178
179
180
181
182
183
184
# File 'lib/plugins/acts_as_tree/test/acts_as_tree_test.rb', line 176

def test_eager_association_loading
  roots = TreeMixin.find(:all, :include => :children, :conditions => "mixins.parent_id IS NULL", :order => "mixins.id")
  assert_equal [@root1, @root2, @root3], roots                     
  assert_no_queries do
    assert_equal 2, roots[0].children.size
    assert_equal 0, roots[1].children.size
    assert_equal 0, roots[2].children.size
  end   
end

#test_eager_association_loading_with_recursive_cascading_three_levels_belongs_toObject



196
197
198
199
# File 'lib/plugins/acts_as_tree/test/acts_as_tree_test.rb', line 196

def test_eager_association_loading_with_recursive_cascading_three_levels_belongs_to
  leaf_node = RecursivelyCascadedTreeMixin.find(:first, :include => { :parent => { :parent => :parent } }, :order => 'mixins.id DESC')
  assert_equal @rc1, assert_no_queries { leaf_node.parent.parent.parent }
end

#test_eager_association_loading_with_recursive_cascading_three_levels_has_manyObject



186
187
188
189
# File 'lib/plugins/acts_as_tree/test/acts_as_tree_test.rb', line 186

def test_eager_association_loading_with_recursive_cascading_three_levels_has_many
  root_node = RecursivelyCascadedTreeMixin.find(:first, :include => { :children => { :children => :children } }, :order => 'mixins.id')
  assert_equal @rc4, assert_no_queries { root_node.children.first.children.first.children.first }
end

#test_eager_association_loading_with_recursive_cascading_three_levels_has_oneObject



191
192
193
194
# File 'lib/plugins/acts_as_tree/test/acts_as_tree_test.rb', line 191

def test_eager_association_loading_with_recursive_cascading_three_levels_has_one
  root_node = RecursivelyCascadedTreeMixin.find(:first, :include => { :first_child => { :first_child => :first_child } }, :order => 'mixins.id')
  assert_equal @rc4, assert_no_queries { root_node.first_child.first_child.first_child }
end