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


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

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


71
72
73
# File 'lib/plugins/acts_as_tree/test/acts_as_tree_test.rb', line 71

def teardown
  teardown_db
end

#test_ancestorsObject


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

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


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

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


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

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


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

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


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

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

#test_rootObject


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

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


129
130
131
# File 'lib/plugins/acts_as_tree/test/acts_as_tree_test.rb', line 129

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

#test_self_and_siblingsObject


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

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


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

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