|
| 1 | +require "test_helper" |
| 2 | + |
| 3 | +class TestHealing < Minitest::Test |
| 4 | + include Minitest::Hooks |
| 5 | + |
| 6 | + def around |
| 7 | + ActiveRecord::Base.transaction do |
| 8 | + super |
| 9 | + raise ActiveRecord::Rollback |
| 10 | + end |
| 11 | + end |
| 12 | + |
| 13 | + def test_heal_position |
| 14 | + first_list = List.create name: "First List" |
| 15 | + second_list = List.create name: "Second List" |
| 16 | + |
| 17 | + first_item = first_list.new_items.create name: "First Item" |
| 18 | + second_item = first_list.new_items.create name: "Second Item" |
| 19 | + third_item = first_list.new_items.create name: "Third Item" |
| 20 | + |
| 21 | + fourth_item = second_list.new_items.create name: "Fourth Item" |
| 22 | + fifth_item = second_list.new_items.create name: "Fifth Item" |
| 23 | + sixth_item = second_list.new_items.create name: "Sixth Item" |
| 24 | + |
| 25 | + first_item.update_columns position: 9 |
| 26 | + second_item.update_columns position: nil |
| 27 | + third_item.update_columns position: -42 |
| 28 | + |
| 29 | + fourth_item.update_columns position: 0 |
| 30 | + fifth_item.update_columns position: 998 |
| 31 | + sixth_item.update_columns position: 800 |
| 32 | + |
| 33 | + NewItem.heal_position_column! |
| 34 | + |
| 35 | + if ENV["DB"] == "postgresql" |
| 36 | + assert_equal [1, 2, 3], [third_item.reload, first_item.reload, second_item.reload].map(&:position) |
| 37 | + else |
| 38 | + assert_equal [1, 2, 3], [second_item.reload, third_item.reload, first_item.reload].map(&:position) |
| 39 | + end |
| 40 | + |
| 41 | + assert_equal [1, 2, 3], [fourth_item.reload, sixth_item.reload, fifth_item.reload].map(&:position) |
| 42 | + |
| 43 | + NewItem.heal_position_column! name: :desc |
| 44 | + |
| 45 | + assert_equal [1, 2, 3], [third_item.reload, second_item.reload, first_item.reload].map(&:position) |
| 46 | + assert_equal [1, 2, 3], [sixth_item.reload, fourth_item.reload, fifth_item.reload].map(&:position) |
| 47 | + end |
| 48 | + |
| 49 | + def test_heal_position_on_a_tree |
| 50 | + first_category = Category.create name: "First Category" |
| 51 | + second_category = Category.create name: "Second Category" |
| 52 | + third_category = Category.create name: "Third Category", parent: first_category |
| 53 | + fourth_category = Category.create name: "Fourth Category", parent: second_category |
| 54 | + fifth_category = Category.create name: "Fifth Category", parent: second_category |
| 55 | + sixth_category = Category.create name: "Sixth Category", parent: second_category |
| 56 | + |
| 57 | + first_category.update_columns position: 9 |
| 58 | + second_category.update_columns position: 0 |
| 59 | + third_category.update_columns position: -42 |
| 60 | + fourth_category.update_columns position: 998 |
| 61 | + fifth_category.update_columns position: 800 |
| 62 | + sixth_category.update_columns position: 1000 |
| 63 | + |
| 64 | + Category.heal_position_column! |
| 65 | + |
| 66 | + assert_equal [1, 2, 1], [second_category.reload, first_category.reload, third_category.reload].map(&:position) |
| 67 | + assert_equal [1, 2, 3], [fifth_category.reload, fourth_category.reload, sixth_category.reload].map(&:position) |
| 68 | + end |
| 69 | + |
| 70 | + def test_heal_position_with_no_scope |
| 71 | + first_product = Product.create name: "First Product" |
| 72 | + second_product = Product.create name: "Second Product" |
| 73 | + third_product = Product.create name: "Third Product" |
| 74 | + |
| 75 | + first_product.update_columns position: 9 |
| 76 | + second_product.update_columns position: 0 |
| 77 | + third_product.update_columns position: -42 |
| 78 | + |
| 79 | + Product.heal_position_column! |
| 80 | + |
| 81 | + assert_equal [1, 2, 3], [third_product.reload, second_product.reload, first_product.reload].map(&:position) |
| 82 | + end |
| 83 | + |
| 84 | + def test_heal_position_with_default_scope |
| 85 | + first_list = List.create name: "First List" |
| 86 | + |
| 87 | + first_item = first_list.default_scope_items.create name: "First Item" |
| 88 | + second_item = first_list.default_scope_items.create name: "Second Item" |
| 89 | + third_item = first_list.default_scope_items.create name: "Third Item" |
| 90 | + |
| 91 | + first_item.update_columns position: 10 |
| 92 | + second_item.update_columns position: 15 |
| 93 | + third_item.update_columns position: 5 |
| 94 | + |
| 95 | + DefaultScopeItem.heal_position_column! |
| 96 | + |
| 97 | + assert_equal [1, 2, 3], [third_item.reload, first_item.reload, second_item.reload].map(&:position) |
| 98 | + end |
| 99 | +end |
0 commit comments