Class: TreeTest

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



63
64
65
66
67
68
69
70
71
# File 'lib/plugins/acts_as_tree/test/acts_as_tree_test.rb', line 63

def setup
  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!
end

#teardownObject



73
74
75
# File 'lib/plugins/acts_as_tree/test/acts_as_tree_test.rb', line 73

def teardown
  teardown_db
end

#test_ancestorsObject



112
113
114
115
116
117
118
119
# File 'lib/plugins/acts_as_tree/test/acts_as_tree_test.rb', line 112

def test_ancestors
  assert_equal [], @root1.ancestors
  assert_equal [@root1], @root_child1.ancestors
  assert_equal [@root_child1, @root1], @child1_child.ancestors
  assert_equal [@root1], @root_child2.ancestors
  assert_equal [], @root2.ancestors
  assert_equal [], @root3.ancestors
end

#test_childrenObject



77
78
79
80
81
82
# File 'lib/plugins/acts_as_tree/test/acts_as_tree_test.rb', line 77

def test_children
  assert_equal @root1.children, [@root_child1, @root_child2]
  assert_equal @root_child1.children, [@child1_child]
  assert_equal @child1_child.children, []
  assert_equal @root_child2.children, []
end

#test_deleteObject



90
91
92
93
94
95
96
97
# File 'lib/plugins/acts_as_tree/test/acts_as_tree_test.rb', line 90

def test_delete
  assert_equal 6, TreeMixin.count
  @root1.destroy
  assert_equal 2, TreeMixin.count
  @root2.destroy
  @root3.destroy
  assert_equal 0, TreeMixin.count
end

#test_insertObject



99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/plugins/acts_as_tree/test/acts_as_tree_test.rb', line 99

def test_insert
  @extra = @root1.children.create

  assert @extra

  assert_equal @extra.parent, @root1

  assert_equal 3, @root1.children.size
  assert @root1.children.include?(@extra)
  assert @root1.children.include?(@root_child1)
  assert @root1.children.include?(@root_child2)
end

#test_parentObject



84
85
86
87
88
# File 'lib/plugins/acts_as_tree/test/acts_as_tree_test.rb', line 84

def test_parent
  assert_equal @root_child1.parent, @root1
  assert_equal @root_child1.parent, @root_child2.parent
  assert_nil @root1.parent
end

#test_rootObject



121
122
123
124
125
126
127
128
129
# File 'lib/plugins/acts_as_tree/test/acts_as_tree_test.rb', line 121

def test_root
  assert_equal @root1, TreeMixin.root
  assert_equal @root1, @root1.root
  assert_equal @root1, @root_child1.root
  assert_equal @root1, @child1_child.root
  assert_equal @root1, @root_child2.root
  assert_equal @root2, @root2.root
  assert_equal @root3, @root3.root
end

#test_rootsObject



131
132
133
# File 'lib/plugins/acts_as_tree/test/acts_as_tree_test.rb', line 131

def test_roots
  assert_equal [@root1, @root2, @root3], TreeMixin.roots
end

#test_self_and_siblingsObject



144
145
146
147
148
149
150
151
# File 'lib/plugins/acts_as_tree/test/acts_as_tree_test.rb', line 144

def test_self_and_siblings
  assert_equal [@root1, @root2, @root3], @root1.self_and_siblings
  assert_equal [@root_child1, @root_child2], @root_child1.self_and_siblings
  assert_equal [@child1_child], @child1_child.self_and_siblings
  assert_equal [@root_child1, @root_child2], @root_child2.self_and_siblings
  assert_equal [@root1, @root2, @root3], @root2.self_and_siblings
  assert_equal [@root1, @root2, @root3], @root3.self_and_siblings
end

#test_siblingsObject



135
136
137
138
139
140
141
142
# File 'lib/plugins/acts_as_tree/test/acts_as_tree_test.rb', line 135

def test_siblings
  assert_equal [@root2, @root3], @root1.siblings
  assert_equal [@root_child2], @root_child1.siblings
  assert_equal [], @child1_child.siblings
  assert_equal [@root_child1], @root_child2.siblings
  assert_equal [@root1, @root3], @root2.siblings
  assert_equal [@root1, @root2], @root3.siblings
end