/vendor/plugins/awesome_nested_set/test/awesome_nested_set_test.rb

https://github.com/chartmann/bettermeans · Ruby · 603 lines · 487 code · 111 blank · 5 comment · 0 complexity · d177339aefb3c4a8f280efff48c974b6 MD5 · raw file

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