diff options
| author | hukl <hukl@berlin.ccc.de> | 2009-01-29 22:12:11 +0100 |
|---|---|---|
| committer | hukl <hukl@berlin.ccc.de> | 2009-01-29 22:12:11 +0100 |
| commit | be47a4bbeac59d85af16b5e83e10ff242628519d (patch) | |
| tree | e25870a53dc19d10e7112ece98b82112d64cfe02 /vendor/plugins/awesome_nested_set/test | |
| parent | d4dfdb40329b8e15e6bba022b966984a8a994a87 (diff) | |
added node model to make basic functional tests pass. added awesome_nested_set plugin
Diffstat (limited to 'vendor/plugins/awesome_nested_set/test')
7 files changed, 765 insertions, 0 deletions
diff --git a/vendor/plugins/awesome_nested_set/test/awesome_nested_set/helper_test.rb b/vendor/plugins/awesome_nested_set/test/awesome_nested_set/helper_test.rb new file mode 100644 index 0000000..6122a0e --- /dev/null +++ b/vendor/plugins/awesome_nested_set/test/awesome_nested_set/helper_test.rb | |||
| @@ -0,0 +1,41 @@ | |||
| 1 | require File.dirname(__FILE__) + '/../test_helper' | ||
| 2 | |||
| 3 | module CollectiveIdea | ||
| 4 | module Acts #:nodoc: | ||
| 5 | module NestedSet #:nodoc: | ||
| 6 | class AwesomeNestedSetTest < Test::Unit::TestCase | ||
| 7 | include Helper | ||
| 8 | fixtures :categories | ||
| 9 | |||
| 10 | def test_nested_set_options | ||
| 11 | expected = [ | ||
| 12 | [" Top Level", 1], | ||
| 13 | ["- Child 1", 2], | ||
| 14 | ['- Child 2', 3], | ||
| 15 | ['-- Child 2.1', 4], | ||
| 16 | ['- Child 3', 5], | ||
| 17 | [" Top Level 2", 6] | ||
| 18 | ] | ||
| 19 | actual = nested_set_options(Category) do |c| | ||
| 20 | "#{'-' * c.level} #{c.name}" | ||
| 21 | end | ||
| 22 | assert_equal expected, actual | ||
| 23 | end | ||
| 24 | |||
| 25 | def test_nested_set_options_with_mover | ||
| 26 | expected = [ | ||
| 27 | [" Top Level", 1], | ||
| 28 | ["- Child 1", 2], | ||
| 29 | ['- Child 3', 5], | ||
| 30 | [" Top Level 2", 6] | ||
| 31 | ] | ||
| 32 | actual = nested_set_options(Category, categories(:child_2)) do |c| | ||
| 33 | "#{'-' * c.level} #{c.name}" | ||
| 34 | end | ||
| 35 | assert_equal expected, actual | ||
| 36 | end | ||
| 37 | |||
| 38 | end | ||
| 39 | end | ||
| 40 | end | ||
| 41 | end \ No newline at end of file | ||
diff --git a/vendor/plugins/awesome_nested_set/test/awesome_nested_set_test.rb b/vendor/plugins/awesome_nested_set/test/awesome_nested_set_test.rb new file mode 100644 index 0000000..5252d80 --- /dev/null +++ b/vendor/plugins/awesome_nested_set/test/awesome_nested_set_test.rb | |||
| @@ -0,0 +1,603 @@ | |||
| 1 | require File.dirname(__FILE__) + '/test_helper' | ||
| 2 | |||
| 3 | class Note < ActiveRecord::Base | ||
| 4 | acts_as_nested_set :scope => [:notable_id, :notable_type] | ||
| 5 | end | ||
| 6 | |||
| 7 | class AwesomeNestedSetTest < Test::Unit::TestCase | ||
| 8 | |||
| 9 | class Default < ActiveRecord::Base | ||
| 10 | acts_as_nested_set | ||
| 11 | set_table_name 'categories' | ||
| 12 | end | ||
| 13 | class Scoped < ActiveRecord::Base | ||
| 14 | acts_as_nested_set :scope => :organization | ||
| 15 | set_table_name 'categories' | ||
| 16 | end | ||
| 17 | |||
| 18 | def test_left_column_default | ||
| 19 | assert_equal 'lft', Default.acts_as_nested_set_options[:left_column] | ||
| 20 | end | ||
| 21 | |||
| 22 | def test_right_column_default | ||
| 23 | assert_equal 'rgt', Default.acts_as_nested_set_options[:right_column] | ||
| 24 | end | ||
| 25 | |||
| 26 | def test_parent_column_default | ||
| 27 | assert_equal 'parent_id', Default.acts_as_nested_set_options[:parent_column] | ||
| 28 | end | ||
| 29 | |||
| 30 | def test_scope_default | ||
| 31 | assert_nil Default.acts_as_nested_set_options[:scope] | ||
| 32 | end | ||
| 33 | |||
| 34 | def test_left_column_name | ||
| 35 | assert_equal 'lft', Default.left_column_name | ||
| 36 | assert_equal 'lft', Default.new.left_column_name | ||
| 37 | end | ||
| 38 | |||
| 39 | def test_right_column_name | ||
| 40 | assert_equal 'rgt', Default.right_column_name | ||
| 41 | assert_equal 'rgt', Default.new.right_column_name | ||
| 42 | end | ||
| 43 | |||
| 44 | def test_parent_column_name | ||
| 45 | assert_equal 'parent_id', Default.parent_column_name | ||
| 46 | assert_equal 'parent_id', Default.new.parent_column_name | ||
| 47 | end | ||
| 48 | |||
| 49 | def test_quoted_left_column_name | ||
| 50 | quoted = Default.connection.quote_column_name('lft') | ||
| 51 | assert_equal quoted, Default.quoted_left_column_name | ||
| 52 | assert_equal quoted, Default.new.quoted_left_column_name | ||
| 53 | end | ||
| 54 | |||
| 55 | def test_quoted_right_column_name | ||
| 56 | quoted = Default.connection.quote_column_name('rgt') | ||
| 57 | assert_equal quoted, Default.quoted_right_column_name | ||
| 58 | assert_equal quoted, Default.new.quoted_right_column_name | ||
| 59 | end | ||
| 60 | |||
| 61 | def test_left_column_protected_from_assignment | ||
| 62 | assert_raises(ActiveRecord::ActiveRecordError) { Category.new.lft = 1 } | ||
| 63 | end | ||
| 64 | |||
| 65 | def test_right_column_protected_from_assignment | ||
| 66 | assert_raises(ActiveRecord::ActiveRecordError) { Category.new.rgt = 1 } | ||
| 67 | end | ||
| 68 | |||
| 69 | def test_parent_column_protected_from_assignment | ||
| 70 | assert_raises(ActiveRecord::ActiveRecordError) { Category.new.parent_id = 1 } | ||
| 71 | end | ||
| 72 | |||
| 73 | def test_colums_protected_on_initialize | ||
| 74 | c = Category.new(:lft => 1, :rgt => 2, :parent_id => 3) | ||
| 75 | assert_nil c.lft | ||
| 76 | assert_nil c.rgt | ||
| 77 | assert_nil c.parent_id | ||
| 78 | end | ||
| 79 | |||
| 80 | def test_scoped_appends_id | ||
| 81 | assert_equal :organization_id, Scoped.acts_as_nested_set_options[:scope] | ||
| 82 | end | ||
| 83 | |||
| 84 | def test_roots_class_method | ||
| 85 | assert_equal Category.find_all_by_parent_id(nil), Category.roots | ||
| 86 | end | ||
| 87 | |||
| 88 | def test_root_class_method | ||
| 89 | assert_equal categories(:top_level), Category.root | ||
| 90 | end | ||
| 91 | |||
| 92 | def test_root | ||
| 93 | assert_equal categories(:top_level), categories(:child_3).root | ||
| 94 | end | ||
| 95 | |||
| 96 | def test_root? | ||
| 97 | assert categories(:top_level).root? | ||
| 98 | assert categories(:top_level_2).root? | ||
| 99 | end | ||
| 100 | |||
| 101 | def test_leaves_class_method | ||
| 102 | assert_equal Category.find(:all, :conditions => "#{Category.right_column_name} - #{Category.left_column_name} = 1"), Category.leaves | ||
| 103 | assert_equal Category.leaves.count, 4 | ||
| 104 | assert (Category.leaves.include? categories(:child_1)) | ||
| 105 | assert (Category.leaves.include? categories(:child_2_1)) | ||
| 106 | assert (Category.leaves.include? categories(:child_3)) | ||
| 107 | assert (Category.leaves.include? categories(:top_level_2)) | ||
| 108 | end | ||
| 109 | |||
| 110 | def test_leaf | ||
| 111 | assert categories(:child_1).leaf? | ||
| 112 | assert categories(:child_2_1).leaf? | ||
| 113 | assert categories(:child_3).leaf? | ||
| 114 | assert categories(:top_level_2).leaf? | ||
| 115 | |||
| 116 | assert !categories(:top_level).leaf? | ||
| 117 | assert !categories(:child_2).leaf? | ||
| 118 | end | ||
| 119 | |||
| 120 | def test_parent | ||
| 121 | assert_equal categories(:child_2), categories(:child_2_1).parent | ||
| 122 | end | ||
| 123 | |||
| 124 | def test_self_and_ancestors | ||
| 125 | child = categories(:child_2_1) | ||
| 126 | self_and_ancestors = [categories(:top_level), categories(:child_2), child] | ||
| 127 | assert_equal self_and_ancestors, child.self_and_ancestors | ||
| 128 | end | ||
| 129 | |||
| 130 | def test_ancestors | ||
| 131 | child = categories(:child_2_1) | ||
| 132 | ancestors = [categories(:top_level), categories(:child_2)] | ||
| 133 | assert_equal ancestors, child.ancestors | ||
| 134 | end | ||
| 135 | |||
| 136 | def test_self_and_siblings | ||
| 137 | child = categories(:child_2) | ||
| 138 | self_and_siblings = [categories(:child_1), child, categories(:child_3)] | ||
| 139 | assert_equal self_and_siblings, child.self_and_siblings | ||
| 140 | assert_nothing_raised do | ||
| 141 | tops = [categories(:top_level), categories(:top_level_2)] | ||
| 142 | assert_equal tops, categories(:top_level).self_and_siblings | ||
| 143 | end | ||
| 144 | end | ||
| 145 | |||
| 146 | def test_siblings | ||
| 147 | child = categories(:child_2) | ||
| 148 | siblings = [categories(:child_1), categories(:child_3)] | ||
| 149 | assert_equal siblings, child.siblings | ||
| 150 | end | ||
| 151 | |||
| 152 | def test_leaves | ||
| 153 | leaves = [categories(:child_1), categories(:child_2_1), categories(:child_3), categories(:top_level_2)] | ||
| 154 | assert categories(:top_level).leaves, leaves | ||
| 155 | end | ||
| 156 | |||
| 157 | def test_level | ||
| 158 | assert_equal 0, categories(:top_level).level | ||
| 159 | assert_equal 1, categories(:child_1).level | ||
| 160 | assert_equal 2, categories(:child_2_1).level | ||
| 161 | end | ||
| 162 | |||
| 163 | def test_has_children? | ||
| 164 | assert categories(:child_2_1).children.empty? | ||
| 165 | assert !categories(:child_2).children.empty? | ||
| 166 | assert !categories(:top_level).children.empty? | ||
| 167 | end | ||
| 168 | |||
| 169 | def test_self_and_descendents | ||
| 170 | parent = categories(:top_level) | ||
| 171 | self_and_descendants = [parent, categories(:child_1), categories(:child_2), | ||
| 172 | categories(:child_2_1), categories(:child_3)] | ||
| 173 | assert_equal self_and_descendants, parent.self_and_descendants | ||
| 174 | assert_equal self_and_descendants, parent.self_and_descendants.count | ||
| 175 | end | ||
| 176 | |||
| 177 | def test_descendents | ||
| 178 | lawyers = Category.create!(:name => "lawyers") | ||
| 179 | us = Category.create!(:name => "United States") | ||
| 180 | us.move_to_child_of(lawyers) | ||
| 181 | patent = Category.create!(:name => "Patent Law") | ||
| 182 | patent.move_to_child_of(us) | ||
| 183 | lawyers.reload | ||
| 184 | |||
| 185 | assert_equal 1, lawyers.children.size | ||
| 186 | assert_equal 1, us.children.size | ||
| 187 | assert_equal 2, lawyers.descendants.size | ||
| 188 | end | ||
| 189 | |||
| 190 | def test_self_and_descendents | ||
| 191 | parent = categories(:top_level) | ||
| 192 | descendants = [categories(:child_1), categories(:child_2), | ||
| 193 | categories(:child_2_1), categories(:child_3)] | ||
| 194 | assert_equal descendants, parent.descendants | ||
| 195 | end | ||
| 196 | |||
| 197 | def test_children | ||
| 198 | category = categories(:top_level) | ||
| 199 | category.children.each {|c| assert_equal category.id, c.parent_id } | ||
| 200 | end | ||
| 201 | |||
| 202 | def test_is_or_is_ancestor_of? | ||
| 203 | assert categories(:top_level).is_or_is_ancestor_of?(categories(:child_1)) | ||
| 204 | assert categories(:top_level).is_or_is_ancestor_of?(categories(:child_2_1)) | ||
| 205 | assert categories(:child_2).is_or_is_ancestor_of?(categories(:child_2_1)) | ||
| 206 | assert !categories(:child_2_1).is_or_is_ancestor_of?(categories(:child_2)) | ||
| 207 | assert !categories(:child_1).is_or_is_ancestor_of?(categories(:child_2)) | ||
| 208 | assert categories(:child_1).is_or_is_ancestor_of?(categories(:child_1)) | ||
| 209 | end | ||
| 210 | |||
| 211 | def test_is_ancestor_of? | ||
| 212 | assert categories(:top_level).is_ancestor_of?(categories(:child_1)) | ||
| 213 | assert categories(:top_level).is_ancestor_of?(categories(:child_2_1)) | ||
| 214 | assert categories(:child_2).is_ancestor_of?(categories(:child_2_1)) | ||
| 215 | assert !categories(:child_2_1).is_ancestor_of?(categories(:child_2)) | ||
| 216 | assert !categories(:child_1).is_ancestor_of?(categories(:child_2)) | ||
| 217 | assert !categories(:child_1).is_ancestor_of?(categories(:child_1)) | ||
| 218 | end | ||
| 219 | |||
| 220 | def test_is_or_is_ancestor_of_with_scope | ||
| 221 | root = Scoped.root | ||
| 222 | child = root.children.first | ||
| 223 | assert root.is_or_is_ancestor_of?(child) | ||
| 224 | child.update_attribute :organization_id, 'different' | ||
| 225 | assert !root.is_or_is_ancestor_of?(child) | ||
| 226 | end | ||
| 227 | |||
| 228 | def test_is_or_is_descendant_of? | ||
| 229 | assert categories(:child_1).is_or_is_descendant_of?(categories(:top_level)) | ||
| 230 | assert categories(:child_2_1).is_or_is_descendant_of?(categories(:top_level)) | ||
| 231 | assert categories(:child_2_1).is_or_is_descendant_of?(categories(:child_2)) | ||
| 232 | assert !categories(:child_2).is_or_is_descendant_of?(categories(:child_2_1)) | ||
| 233 | assert !categories(:child_2).is_or_is_descendant_of?(categories(:child_1)) | ||
| 234 | assert categories(:child_1).is_or_is_descendant_of?(categories(:child_1)) | ||
| 235 | end | ||
| 236 | |||
| 237 | def test_is_descendant_of? | ||
| 238 | assert categories(:child_1).is_descendant_of?(categories(:top_level)) | ||
| 239 | assert categories(:child_2_1).is_descendant_of?(categories(:top_level)) | ||
| 240 | assert categories(:child_2_1).is_descendant_of?(categories(:child_2)) | ||
| 241 | assert !categories(:child_2).is_descendant_of?(categories(:child_2_1)) | ||
| 242 | assert !categories(:child_2).is_descendant_of?(categories(:child_1)) | ||
| 243 | assert !categories(:child_1).is_descendant_of?(categories(:child_1)) | ||
| 244 | end | ||
| 245 | |||
| 246 | def test_is_or_is_descendant_of_with_scope | ||
| 247 | root = Scoped.root | ||
| 248 | child = root.children.first | ||
| 249 | assert child.is_or_is_descendant_of?(root) | ||
| 250 | child.update_attribute :organization_id, 'different' | ||
| 251 | assert !child.is_or_is_descendant_of?(root) | ||
| 252 | end | ||
| 253 | |||
| 254 | def test_same_scope? | ||
| 255 | root = Scoped.root | ||
| 256 | child = root.children.first | ||
| 257 | assert child.same_scope?(root) | ||
| 258 | child.update_attribute :organization_id, 'different' | ||
| 259 | assert !child.same_scope?(root) | ||
| 260 | end | ||
| 261 | |||
| 262 | def test_left_sibling | ||
| 263 | assert_equal categories(:child_1), categories(:child_2).left_sibling | ||
| 264 | assert_equal categories(:child_2), categories(:child_3).left_sibling | ||
| 265 | end | ||
| 266 | |||
| 267 | def test_left_sibling_of_root | ||
| 268 | assert_nil categories(:top_level).left_sibling | ||
| 269 | end | ||
| 270 | |||
| 271 | def test_left_sibling_without_siblings | ||
| 272 | assert_nil categories(:child_2_1).left_sibling | ||
| 273 | end | ||
| 274 | |||
| 275 | def test_left_sibling_of_leftmost_node | ||
| 276 | assert_nil categories(:child_1).left_sibling | ||
| 277 | end | ||
| 278 | |||
| 279 | def test_right_sibling | ||
| 280 | assert_equal categories(:child_3), categories(:child_2).right_sibling | ||
| 281 | assert_equal categories(:child_2), categories(:child_1).right_sibling | ||
| 282 | end | ||
| 283 | |||
| 284 | def test_right_sibling_of_root | ||
| 285 | assert_equal categories(:top_level_2), categories(:top_level).right_sibling | ||
| 286 | assert_nil categories(:top_level_2).right_sibling | ||
| 287 | end | ||
| 288 | |||
| 289 | def test_right_sibling_without_siblings | ||
| 290 | assert_nil categories(:child_2_1).right_sibling | ||
| 291 | end | ||
| 292 | |||
| 293 | def test_right_sibling_of_rightmost_node | ||
| 294 | assert_nil categories(:child_3).right_sibling | ||
| 295 | end | ||
| 296 | |||
| 297 | def test_move_left | ||
| 298 | categories(:child_2).move_left | ||
| 299 | assert_nil categories(:child_2).left_sibling | ||
| 300 | assert_equal categories(:child_1), categories(:child_2).right_sibling | ||
| 301 | assert Category.valid? | ||
| 302 | end | ||
| 303 | |||
| 304 | def test_move_right | ||
| 305 | categories(:child_2).move_right | ||
| 306 | assert_nil categories(:child_2).right_sibling | ||
| 307 | assert_equal categories(:child_3), categories(:child_2).left_sibling | ||
| 308 | assert Category.valid? | ||
| 309 | end | ||
| 310 | |||
| 311 | def test_move_to_left_of | ||
| 312 | categories(:child_3).move_to_left_of(categories(:child_1)) | ||
| 313 | assert_nil categories(:child_3).left_sibling | ||
| 314 | assert_equal categories(:child_1), categories(:child_3).right_sibling | ||
| 315 | assert Category.valid? | ||
| 316 | end | ||
| 317 | |||
| 318 | def test_move_to_right_of | ||
| 319 | categories(:child_1).move_to_right_of(categories(:child_3)) | ||
| 320 | assert_nil categories(:child_1).right_sibling | ||
| 321 | assert_equal categories(:child_3), categories(:child_1).left_sibling | ||
| 322 | assert Category.valid? | ||
| 323 | end | ||
| 324 | |||
| 325 | def test_move_to_root | ||
| 326 | categories(:child_2).move_to_root | ||
| 327 | assert_nil categories(:child_2).parent | ||
| 328 | assert_equal 0, categories(:child_2).level | ||
| 329 | assert_equal 1, categories(:child_2_1).level | ||
| 330 | assert_equal 1, categories(:child_2).left | ||
| 331 | assert_equal 4, categories(:child_2).right | ||
| 332 | assert Category.valid? | ||
| 333 | end | ||
| 334 | |||
| 335 | def test_move_to_child_of | ||
| 336 | categories(:child_1).move_to_child_of(categories(:child_3)) | ||
| 337 | assert_equal categories(:child_3).id, categories(:child_1).parent_id | ||
| 338 | assert Category.valid? | ||
| 339 | end | ||
| 340 | |||
| 341 | def test_move_to_child_of_appends_to_end | ||
| 342 | child = Category.create! :name => 'New Child' | ||
| 343 | child.move_to_child_of categories(:top_level) | ||
| 344 | assert_equal child, categories(:top_level).children.last | ||
| 345 | end | ||
| 346 | |||
| 347 | def test_subtree_move_to_child_of | ||
| 348 | assert_equal 4, categories(:child_2).left | ||
| 349 | assert_equal 7, categories(:child_2).right | ||
| 350 | |||
| 351 | assert_equal 2, categories(:child_1).left | ||
| 352 | assert_equal 3, categories(:child_1).right | ||
| 353 | |||
| 354 | categories(:child_2).move_to_child_of(categories(:child_1)) | ||
| 355 | assert Category.valid? | ||
| 356 | assert_equal categories(:child_1).id, categories(:child_2).parent_id | ||
| 357 | |||
| 358 | assert_equal 3, categories(:child_2).left | ||
| 359 | assert_equal 6, categories(:child_2).right | ||
| 360 | assert_equal 2, categories(:child_1).left | ||
| 361 | assert_equal 7, categories(:child_1).right | ||
| 362 | end | ||
| 363 | |||
| 364 | def test_slightly_difficult_move_to_child_of | ||
| 365 | assert_equal 11, categories(:top_level_2).left | ||
| 366 | assert_equal 12, categories(:top_level_2).right | ||
| 367 | |||
| 368 | # create a new top-level node and move single-node top-level tree inside it. | ||
| 369 | new_top = Category.create(:name => 'New Top') | ||
| 370 | assert_equal 13, new_top.left | ||
| 371 | assert_equal 14, new_top.right | ||
| 372 | |||
| 373 | categories(:top_level_2).move_to_child_of(new_top) | ||
| 374 | |||
| 375 | assert Category.valid? | ||
| 376 | assert_equal new_top.id, categories(:top_level_2).parent_id | ||
| 377 | |||
| 378 | assert_equal 12, categories(:top_level_2).left | ||
| 379 | assert_equal 13, categories(:top_level_2).right | ||
| 380 | assert_equal 11, new_top.left | ||
| 381 | assert_equal 14, new_top.right | ||
| 382 | end | ||
| 383 | |||
| 384 | def test_difficult_move_to_child_of | ||
| 385 | assert_equal 1, categories(:top_level).left | ||
| 386 | assert_equal 10, categories(:top_level).right | ||
| 387 | assert_equal 5, categories(:child_2_1).left | ||
| 388 | assert_equal 6, categories(:child_2_1).right | ||
| 389 | |||
| 390 | # create a new top-level node and move an entire top-level tree inside it. | ||
| 391 | new_top = Category.create(:name => 'New Top') | ||
| 392 | categories(:top_level).move_to_child_of(new_top) | ||
| 393 | categories(:child_2_1).reload | ||
| 394 | assert Category.valid? | ||
| 395 | assert_equal new_top.id, categories(:top_level).parent_id | ||
| 396 | |||
| 397 | assert_equal 4, categories(:top_level).left | ||
| 398 | assert_equal 13, categories(:top_level).right | ||
| 399 | assert_equal 8, categories(:child_2_1).left | ||
| 400 | assert_equal 9, categories(:child_2_1).right | ||
| 401 | end | ||
| 402 | |||
| 403 | #rebuild swaps the position of the 2 children when added using move_to_child twice onto same parent | ||
| 404 | def test_move_to_child_more_than_once_per_parent_rebuild | ||
| 405 | root1 = Category.create(:name => 'Root1') | ||
| 406 | root2 = Category.create(:name => 'Root2') | ||
| 407 | root3 = Category.create(:name => 'Root3') | ||
| 408 | |||
| 409 | root2.move_to_child_of root1 | ||
| 410 | root3.move_to_child_of root1 | ||
| 411 | |||
| 412 | output = Category.roots.last.to_text | ||
| 413 | Category.update_all('lft = null, rgt = null') | ||
| 414 | Category.rebuild! | ||
| 415 | |||
| 416 | assert_equal Category.roots.last.to_text, output | ||
| 417 | end | ||
| 418 | |||
| 419 | # doing move_to_child twice onto same parent from the furthest right first | ||
| 420 | def test_move_to_child_more_than_once_per_parent_outside_in | ||
| 421 | node1 = Category.create(:name => 'Node-1') | ||
| 422 | node2 = Category.create(:name => 'Node-2') | ||
| 423 | node3 = Category.create(:name => 'Node-3') | ||
| 424 | |||
| 425 | node2.move_to_child_of node1 | ||
| 426 | node3.move_to_child_of node1 | ||
| 427 | |||
| 428 | output = Category.roots.last.to_text | ||
| 429 | Category.update_all('lft = null, rgt = null') | ||
| 430 | Category.rebuild! | ||
| 431 | |||
| 432 | assert_equal Category.roots.last.to_text, output | ||
| 433 | end | ||
| 434 | |||
| 435 | |||
| 436 | def test_valid_with_null_lefts | ||
| 437 | assert Category.valid? | ||
| 438 | Category.update_all('lft = null') | ||
| 439 | assert !Category.valid? | ||
| 440 | end | ||
| 441 | |||
| 442 | def test_valid_with_null_rights | ||
| 443 | assert Category.valid? | ||
| 444 | Category.update_all('rgt = null') | ||
| 445 | assert !Category.valid? | ||
| 446 | end | ||
| 447 | |||
| 448 | def test_valid_with_missing_intermediate_node | ||
| 449 | # Even though child_2_1 will still exist, it is a sign of a sloppy delete, not an invalid tree. | ||
| 450 | assert Category.valid? | ||
| 451 | Category.delete(categories(:child_2).id) | ||
| 452 | assert Category.valid? | ||
| 453 | end | ||
| 454 | |||
| 455 | def test_valid_with_overlapping_and_rights | ||
| 456 | assert Category.valid? | ||
| 457 | categories(:top_level_2)['lft'] = 0 | ||
| 458 | categories(:top_level_2).save | ||
| 459 | assert !Category.valid? | ||
| 460 | end | ||
| 461 | |||
| 462 | def test_rebuild | ||
| 463 | assert Category.valid? | ||
| 464 | before_text = Category.root.to_text | ||
| 465 | Category.update_all('lft = null, rgt = null') | ||
| 466 | Category.rebuild! | ||
| 467 | assert Category.valid? | ||
| 468 | assert_equal before_text, Category.root.to_text | ||
| 469 | end | ||
| 470 | |||
| 471 | def test_move_possible_for_sibling | ||
| 472 | assert categories(:child_2).move_possible?(categories(:child_1)) | ||
| 473 | end | ||
| 474 | |||
| 475 | def test_move_not_possible_to_self | ||
| 476 | assert !categories(:top_level).move_possible?(categories(:top_level)) | ||
| 477 | end | ||
| 478 | |||
| 479 | def test_move_not_possible_to_parent | ||
| 480 | categories(:top_level).descendants.each do |descendant| | ||
| 481 | assert !categories(:top_level).move_possible?(descendant) | ||
| 482 | assert descendant.move_possible?(categories(:top_level)) | ||
| 483 | end | ||
| 484 | end | ||
| 485 | |||
| 486 | def test_is_or_is_ancestor_of? | ||
| 487 | [:child_1, :child_2, :child_2_1, :child_3].each do |c| | ||
| 488 | assert categories(:top_level).is_or_is_ancestor_of?(categories(c)) | ||
| 489 | end | ||
| 490 | assert !categories(:top_level).is_or_is_ancestor_of?(categories(:top_level_2)) | ||
| 491 | end | ||
| 492 | |||
| 493 | def test_left_and_rights_valid_with_blank_left | ||
| 494 | assert Category.left_and_rights_valid? | ||
| 495 | categories(:child_2)[:lft] = nil | ||
| 496 | categories(:child_2).save(false) | ||
| 497 | assert !Category.left_and_rights_valid? | ||
| 498 | end | ||
| 499 | |||
| 500 | def test_left_and_rights_valid_with_blank_right | ||
| 501 | assert Category.left_and_rights_valid? | ||
| 502 | categories(:child_2)[:rgt] = nil | ||
| 503 | categories(:child_2).save(false) | ||
| 504 | assert !Category.left_and_rights_valid? | ||
| 505 | end | ||
| 506 | |||
| 507 | def test_left_and_rights_valid_with_equal | ||
| 508 | assert Category.left_and_rights_valid? | ||
| 509 | categories(:top_level_2)[:lft] = categories(:top_level_2)[:rgt] | ||
| 510 | categories(:top_level_2).save(false) | ||
| 511 | assert !Category.left_and_rights_valid? | ||
| 512 | end | ||
| 513 | |||
| 514 | def test_left_and_rights_valid_with_left_equal_to_parent | ||
| 515 | assert Category.left_and_rights_valid? | ||
| 516 | categories(:child_2)[:lft] = categories(:top_level)[:lft] | ||
| 517 | categories(:child_2).save(false) | ||
| 518 | assert !Category.left_and_rights_valid? | ||
| 519 | end | ||
| 520 | |||
| 521 | def test_left_and_rights_valid_with_right_equal_to_parent | ||
| 522 | assert Category.left_and_rights_valid? | ||
| 523 | categories(:child_2)[:rgt] = categories(:top_level)[:rgt] | ||
| 524 | categories(:child_2).save(false) | ||
| 525 | assert !Category.left_and_rights_valid? | ||
| 526 | end | ||
| 527 | |||
| 528 | def test_moving_dirty_objects_doesnt_invalidate_tree | ||
| 529 | r1 = Category.create | ||
| 530 | r2 = Category.create | ||
| 531 | r3 = Category.create | ||
| 532 | r4 = Category.create | ||
| 533 | nodes = [r1, r2, r3, r4] | ||
| 534 | |||
| 535 | r2.move_to_child_of(r1) | ||
| 536 | assert Category.valid? | ||
| 537 | |||
| 538 | r3.move_to_child_of(r1) | ||
| 539 | assert Category.valid? | ||
| 540 | |||
| 541 | r4.move_to_child_of(r2) | ||
| 542 | assert Category.valid? | ||
| 543 | end | ||
| 544 | |||
| 545 | def test_multi_scoped_no_duplicates_for_columns? | ||
| 546 | assert_nothing_raised do | ||
| 547 | Note.no_duplicates_for_columns? | ||
| 548 | end | ||
| 549 | end | ||
| 550 | |||
| 551 | def test_multi_scoped_all_roots_valid? | ||
| 552 | assert_nothing_raised do | ||
| 553 | Note.all_roots_valid? | ||
| 554 | end | ||
| 555 | end | ||
| 556 | |||
| 557 | def test_multi_scoped | ||
| 558 | note1 = Note.create!(:body => "A", :notable_id => 2, :notable_type => 'Category') | ||
| 559 | note2 = Note.create!(:body => "B", :notable_id => 2, :notable_type => 'Category') | ||
| 560 | note3 = Note.create!(:body => "C", :notable_id => 2, :notable_type => 'Default') | ||
| 561 | |||
| 562 | assert_equal [note1, note2], note1.self_and_siblings | ||
| 563 | assert_equal [note3], note3.self_and_siblings | ||
| 564 | end | ||
| 565 | |||
| 566 | def test_multi_scoped_rebuild | ||
| 567 | root = Note.create!(:body => "A", :notable_id => 3, :notable_type => 'Category') | ||
| 568 | child1 = Note.create!(:body => "B", :notable_id => 3, :notable_type => 'Category') | ||
| 569 | child2 = Note.create!(:body => "C", :notable_id => 3, :notable_type => 'Category') | ||
| 570 | |||
| 571 | child1.move_to_child_of root | ||
| 572 | child2.move_to_child_of root | ||
| 573 | |||
| 574 | Note.update_all('lft = null, rgt = null') | ||
| 575 | Note.rebuild! | ||
| 576 | |||
| 577 | assert_equal Note.roots.find_by_body('A'), root | ||
| 578 | assert_equal [child1, child2], Note.roots.find_by_body('A').children | ||
| 579 | end | ||
| 580 | |||
| 581 | def test_same_scope_with_multi_scopes | ||
| 582 | assert_nothing_raised do | ||
| 583 | notes(:scope1).same_scope?(notes(:child_1)) | ||
| 584 | end | ||
| 585 | assert notes(:scope1).same_scope?(notes(:child_1)) | ||
| 586 | assert notes(:child_1).same_scope?(notes(:scope1)) | ||
| 587 | assert !notes(:scope1).same_scope?(notes(:scope2)) | ||
| 588 | end | ||
| 589 | |||
| 590 | def test_quoting_of_multi_scope_column_names | ||
| 591 | assert_equal ["\"notable_id\"", "\"notable_type\""], Note.quoted_scope_column_names | ||
| 592 | end | ||
| 593 | |||
| 594 | def test_equal_in_same_scope | ||
| 595 | assert_equal notes(:scope1), notes(:scope1) | ||
| 596 | assert_not_equal notes(:scope1), notes(:child_1) | ||
| 597 | end | ||
| 598 | |||
| 599 | def test_equal_in_different_scopes | ||
| 600 | assert_not_equal notes(:scope1), notes(:scope2) | ||
| 601 | end | ||
| 602 | |||
| 603 | end | ||
diff --git a/vendor/plugins/awesome_nested_set/test/fixtures/categories.yml b/vendor/plugins/awesome_nested_set/test/fixtures/categories.yml new file mode 100644 index 0000000..bc8e078 --- /dev/null +++ b/vendor/plugins/awesome_nested_set/test/fixtures/categories.yml | |||
| @@ -0,0 +1,34 @@ | |||
| 1 | top_level: | ||
| 2 | id: 1 | ||
| 3 | name: Top Level | ||
| 4 | lft: 1 | ||
| 5 | rgt: 10 | ||
| 6 | child_1: | ||
| 7 | id: 2 | ||
| 8 | name: Child 1 | ||
| 9 | parent_id: 1 | ||
| 10 | lft: 2 | ||
| 11 | rgt: 3 | ||
| 12 | child_2: | ||
| 13 | id: 3 | ||
| 14 | name: Child 2 | ||
| 15 | parent_id: 1 | ||
| 16 | lft: 4 | ||
| 17 | rgt: 7 | ||
| 18 | child_2_1: | ||
| 19 | id: 4 | ||
| 20 | name: Child 2.1 | ||
| 21 | parent_id: 3 | ||
| 22 | lft: 5 | ||
| 23 | rgt: 6 | ||
| 24 | child_3: | ||
| 25 | id: 5 | ||
| 26 | name: Child 3 | ||
| 27 | parent_id: 1 | ||
| 28 | lft: 8 | ||
| 29 | rgt: 9 | ||
| 30 | top_level_2: | ||
| 31 | id: 6 | ||
| 32 | name: Top Level 2 | ||
| 33 | lft: 11 | ||
| 34 | rgt: 12 | ||
diff --git a/vendor/plugins/awesome_nested_set/test/fixtures/category.rb b/vendor/plugins/awesome_nested_set/test/fixtures/category.rb new file mode 100644 index 0000000..506b0da --- /dev/null +++ b/vendor/plugins/awesome_nested_set/test/fixtures/category.rb | |||
| @@ -0,0 +1,15 @@ | |||
| 1 | class Category < ActiveRecord::Base | ||
| 2 | acts_as_nested_set | ||
| 3 | |||
| 4 | def to_s | ||
| 5 | name | ||
| 6 | end | ||
| 7 | |||
| 8 | def recurse &block | ||
| 9 | block.call self, lambda{ | ||
| 10 | self.children.each do |child| | ||
| 11 | child.recurse &block | ||
| 12 | end | ||
| 13 | } | ||
| 14 | end | ||
| 15 | end \ No newline at end of file | ||
diff --git a/vendor/plugins/awesome_nested_set/test/fixtures/departments.yml b/vendor/plugins/awesome_nested_set/test/fixtures/departments.yml new file mode 100644 index 0000000..e50a944 --- /dev/null +++ b/vendor/plugins/awesome_nested_set/test/fixtures/departments.yml | |||
| @@ -0,0 +1,3 @@ | |||
| 1 | top: | ||
| 2 | id: 1 | ||
| 3 | name: Top \ No newline at end of file | ||
diff --git a/vendor/plugins/awesome_nested_set/test/fixtures/notes.yml b/vendor/plugins/awesome_nested_set/test/fixtures/notes.yml new file mode 100644 index 0000000..004a533 --- /dev/null +++ b/vendor/plugins/awesome_nested_set/test/fixtures/notes.yml | |||
| @@ -0,0 +1,38 @@ | |||
| 1 | scope1: | ||
| 2 | id: 1 | ||
| 3 | body: Top Level | ||
| 4 | lft: 1 | ||
| 5 | rgt: 10 | ||
| 6 | notable_id: 1 | ||
| 7 | notable_type: Category | ||
| 8 | child_1: | ||
| 9 | id: 2 | ||
| 10 | body: Child 1 | ||
| 11 | parent_id: 1 | ||
| 12 | lft: 2 | ||
| 13 | rgt: 3 | ||
| 14 | notable_id: 1 | ||
| 15 | notable_type: Category | ||
| 16 | child_2: | ||
| 17 | id: 3 | ||
| 18 | body: Child 2 | ||
| 19 | parent_id: 1 | ||
| 20 | lft: 4 | ||
| 21 | rgt: 7 | ||
| 22 | notable_id: 1 | ||
| 23 | notable_type: Category | ||
| 24 | child_3: | ||
| 25 | id: 4 | ||
| 26 | body: Child 3 | ||
| 27 | parent_id: 1 | ||
| 28 | lft: 8 | ||
| 29 | rgt: 9 | ||
| 30 | notable_id: 1 | ||
| 31 | notable_type: Category | ||
| 32 | scope2: | ||
| 33 | id: 5 | ||
| 34 | body: Top Level 2 | ||
| 35 | lft: 1 | ||
| 36 | rgt: 2 | ||
| 37 | notable_id: 1 | ||
| 38 | notable_type: Departments | ||
diff --git a/vendor/plugins/awesome_nested_set/test/test_helper.rb b/vendor/plugins/awesome_nested_set/test/test_helper.rb new file mode 100644 index 0000000..6939822 --- /dev/null +++ b/vendor/plugins/awesome_nested_set/test/test_helper.rb | |||
| @@ -0,0 +1,31 @@ | |||
| 1 | $:.unshift(File.dirname(__FILE__) + '/../lib') | ||
| 2 | plugin_test_dir = File.dirname(__FILE__) | ||
| 3 | |||
| 4 | require 'rubygems' | ||
| 5 | require 'test/unit' | ||
| 6 | require 'multi_rails_init' | ||
| 7 | # gem 'activerecord', '>= 2.0' | ||
| 8 | require 'active_record' | ||
| 9 | require 'action_controller' | ||
| 10 | require 'action_view' | ||
| 11 | require 'active_record/fixtures' | ||
| 12 | |||
| 13 | require plugin_test_dir + '/../init.rb' | ||
| 14 | |||
| 15 | ActiveRecord::Base.logger = Logger.new(plugin_test_dir + "/debug.log") | ||
| 16 | |||
| 17 | ActiveRecord::Base.configurations = YAML::load(IO.read(plugin_test_dir + "/db/database.yml")) | ||
| 18 | ActiveRecord::Base.establish_connection(ENV["DB"] || "sqlite3mem") | ||
| 19 | ActiveRecord::Migration.verbose = false | ||
| 20 | load(File.join(plugin_test_dir, "db", "schema.rb")) | ||
| 21 | |||
| 22 | Dir["#{plugin_test_dir}/fixtures/*.rb"].each {|file| require file } | ||
| 23 | |||
| 24 | |||
| 25 | class Test::Unit::TestCase #:nodoc: | ||
| 26 | self.fixture_path = File.dirname(__FILE__) + "/fixtures/" | ||
| 27 | self.use_transactional_fixtures = true | ||
| 28 | self.use_instantiated_fixtures = false | ||
| 29 | |||
| 30 | fixtures :categories, :notes, :departments | ||
| 31 | end \ No newline at end of file | ||
