annotate .svn/pristine/a7/a7d2f426cb2319278bd482b53b5c849fd8c928c2.svn-base @ 1524:82fac3dcf466 redmine-2.5-integration

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