Skip to content
This repository
Browse code

Added acts_as_nested_set #1000 [wschenk]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@1186 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
commit 78d8503a94f043a932fea7321dbae262eb4a5c8b 1 parent 339f495
David Heinemeier Hansson authored April 17, 2005

Showing 1 changed file with 184 additions and 0 deletions. Show diff stats Hide diff stats

  1. 184  activerecord/test/mixin_nested_set_test.rb
184  activerecord/test/mixin_nested_set_test.rb
... ...
@@ -0,0 +1,184 @@
  1
+require 'abstract_unit'
  2
+require 'active_record/acts/nested_set'
  3
+require 'fixtures/mixin'
  4
+require 'pp'
  5
+
  6
+class MixinNestedSetTest < Test::Unit::TestCase
  7
+	fixtures :mixins
  8
+                             
  9
+	def test_mixing_in_methods
  10
+	  ns = NestedSet.new
  11
+		assert( ns.respond_to?( :all_children ) )
  12
+		assert_equal( ns.scope_condition, "ROOT_ID IS NULL" )
  13
+		
  14
+		check_method_mixins ns
  15
+	end
  16
+	
  17
+	def test_string_scope
  18
+	  ns = NestedSetWithStringScope.new
  19
+	  
  20
+	  ns.root_id = 1
  21
+	  assert_equal( ns.scope_condition, "root_id = 1" )
  22
+	  ns.root_id = 42
  23
+	  assert_equal( ns.scope_condition, "root_id = 42" )
  24
+	  check_method_mixins ns
  25
+  end
  26
+  
  27
+  def test_symbol_scope
  28
+    ns = NestedSetWithSymbolScope.new
  29
+    ns.root_id = 1
  30
+    assert_equal( ns.scope_condition, "root_id = 1" )
  31
+    ns.root_id = 42
  32
+    assert_equal( ns.scope_condition, "root_id = 42" )
  33
+    check_method_mixins ns
  34
+  end
  35
+  
  36
+  def check_method_mixins( obj )
  37
+    [:scope_condition, :left_col_name, :right_col_name, :parent_column, :root?, :add_child,
  38
+    :children_count, :full_set, :all_children, :direct_children].each { |symbol| assert( obj.respond_to? symbol ) }
  39
+  end
  40
+
  41
+  def set( id )
  42
+    NestedSet.find( 3000 + id )
  43
+  end
  44
+  
  45
+  def test_adding_children
  46
+    assert( set(1).unknown? )
  47
+    assert( set(2).unknown? )
  48
+    set(1).add_child set(2)
  49
+    
  50
+    # Did we maintain adding the parent_ids?
  51
+    assert( set(1).root? )
  52
+    assert( set(2).child? )
  53
+    assert( set(2).parent_id == set(1).id )
  54
+    
  55
+    # Check boundies
  56
+    assert_equal( set(1).lft, 1 )
  57
+    assert_equal( set(2).lft, 2 )
  58
+    assert_equal( set(2).rgt, 3 )
  59
+    assert_equal( set(1).rgt, 4 )
  60
+    
  61
+    # Check children cound
  62
+    assert_equal( set(1).children_count, 1 )
  63
+    
  64
+    set(1).add_child set(3)
  65
+    
  66
+    #check boundries
  67
+    assert_equal( set(1).lft, 1 )
  68
+    assert_equal( set(2).lft, 2 )
  69
+    assert_equal( set(2).rgt, 3 )
  70
+    assert_equal( set(3).lft, 4 )
  71
+    assert_equal( set(3).rgt, 5 )
  72
+    assert_equal( set(1).rgt, 6 )
  73
+    
  74
+    # How is the count looking?
  75
+    assert_equal( set(1).children_count, 2 )
  76
+
  77
+    set(2).add_child set(4)
  78
+
  79
+    # boundries
  80
+    assert_equal( set(1).lft, 1 )
  81
+    assert_equal( set(2).lft, 2 )
  82
+    assert_equal( set(4).lft, 3 )
  83
+    assert_equal( set(4).rgt, 4 )
  84
+    assert_equal( set(2).rgt, 5 )
  85
+    assert_equal( set(3).lft, 6 )
  86
+    assert_equal( set(3).rgt, 7 )
  87
+    assert_equal( set(1).rgt, 8 )
  88
+    
  89
+    # Children count
  90
+    assert_equal( set(1).children_count, 3 )
  91
+    assert_equal( set(2).children_count, 1 )
  92
+    assert_equal( set(3).children_count, 0 )
  93
+    assert_equal( set(4).children_count, 0 )
  94
+    
  95
+    set(2).add_child set(5)
  96
+    set(4).add_child set(6)
  97
+    
  98
+    assert_equal( set(2).children_count, 3 )
  99
+
  100
+
  101
+    # Children accessors
  102
+    assert_equal( set(1).full_set.length, 6 )
  103
+    assert_equal( set(2).full_set.length, 4 )
  104
+    assert_equal( set(4).full_set.length, 2 )
  105
+    
  106
+    assert_equal( set(1).all_children.length, 5 )
  107
+    assert_equal( set(6).all_children.length, 0 )
  108
+    
  109
+    assert_equal( set(1).direct_children.length, 2 )
  110
+    
  111
+  end
  112
+
  113
+  def test_snipping_tree
  114
+    big_tree = NestedSetWithStringScope.find( 4001 )
  115
+    
  116
+    # Make sure we have the right one
  117
+    assert_equal( 3, big_tree.direct_children.length )
  118
+    assert_equal( 10, big_tree.full_set.length )
  119
+    
  120
+    NestedSetWithStringScope.find( 4005 ).destroy
  121
+
  122
+    big_tree = NestedSetWithStringScope.find( 4001 )
  123
+    
  124
+    assert_equal( 7, big_tree.full_set.length )
  125
+    assert_equal( 2, big_tree.direct_children.length )
  126
+    
  127
+    assert_equal( 1, NestedSetWithStringScope.find(4001).lft )
  128
+    assert_equal( 2, NestedSetWithStringScope.find(4002).lft )
  129
+    assert_equal( 3, NestedSetWithStringScope.find(4003).lft )
  130
+    assert_equal( 4, NestedSetWithStringScope.find(4003).rgt )
  131
+    assert_equal( 5, NestedSetWithStringScope.find(4004).lft )
  132
+    assert_equal( 6, NestedSetWithStringScope.find(4004).rgt )
  133
+    assert_equal( 7, NestedSetWithStringScope.find(4002).rgt )
  134
+    assert_equal( 8, NestedSetWithStringScope.find(4008).lft )
  135
+    assert_equal( 9, NestedSetWithStringScope.find(4009).lft )
  136
+    assert_equal(10, NestedSetWithStringScope.find(4009).rgt )
  137
+    assert_equal(11, NestedSetWithStringScope.find(4010).lft )
  138
+    assert_equal(12, NestedSetWithStringScope.find(4010).rgt )
  139
+    assert_equal(13, NestedSetWithStringScope.find(4008).rgt )
  140
+    assert_equal(14, NestedSetWithStringScope.find(4001).rgt )
  141
+  end
  142
+  
  143
+  def test_deleting_root
  144
+    NestedSetWithStringScope.find(4001).destroy
  145
+    
  146
+    assert( NestedSetWithStringScope.find_all.length == 0 )
  147
+  end            
  148
+                               
  149
+  def test_common_usage
  150
+    @set_1.add_child( @set_2 )
  151
+    assert_equal( 1, @set_1.direct_children.length )
  152
+
  153
+    @set_2.add_child( @set_3 )                      
  154
+    assert_equal( 1, @set_1.direct_children.length )     
  155
+    
  156
+    # Local cache is now out of date!
  157
+    # Problem: the update_alls update all objects up the tree
  158
+    @set_1.reload
  159
+    assert_equal( 2, @set_1.all_children.length )              
  160
+    
  161
+    assert_equal( 1, @set_1.lft )
  162
+    assert_equal( 2, @set_2.lft )
  163
+    assert_equal( 3, @set_3.lft )
  164
+    assert_equal( 4, @set_3.rgt )
  165
+    assert_equal( 5, @set_2.rgt )
  166
+    assert_equal( 6, @set_1.rgt )
  167
+          
  168
+    assert( @set_1.root? )
  169
+                  
  170
+    begin
  171
+      @set_4.add_child( @set_1 )
  172
+      fail
  173
+    rescue
  174
+    end
  175
+    
  176
+    assert_equal( 2, @set_1.all_children.length )
  177
+    
  178
+    @set_1.add_child @set_4
  179
+
  180
+    assert_equal( 3, @set_1.all_children.length )
  181
+
  182
+
  183
+  end
  184
+end

0 notes on commit 78d8503

Please sign in to comment.
Something went wrong with that request. Please try again.