Class: ListTest
- Inherits:
-
ActiveSupport::TestCase
- Object
- ActiveSupport::TestCase
- ListTest
- Defined in:
- test/acts_as_list_test.rb
Instance Method Summary collapse
- #setup ⇒ Object
- #teardown ⇒ Object
- #test_delete_middle ⇒ Object
-
#test_delete_middle_with_holes ⇒ Object
special thanks to openhood on github.
- #test_injection ⇒ Object
- #test_insert ⇒ Object
- #test_insert_at ⇒ Object
- #test_move_to_bottom_with_next_to_last_item ⇒ Object
- #test_move_to_higher_in_list ⇒ Object
- #test_move_to_lower_in_list ⇒ Object
- #test_next_prev ⇒ Object
- #test_nil_scope ⇒ Object
- #test_remove_before_destroy_does_not_shift_lower_items_twice ⇒ Object
- #test_remove_from_list_should_set_position_to_nil ⇒ Object
- #test_remove_from_list_should_then_fail_in_list? ⇒ Boolean
- #test_reordering ⇒ Object
- #test_should_not_trigger_unexpected_callbacks_on_destroy ⇒ Object
- #test_with_string_based_scope ⇒ Object
Instance Method Details
#setup ⇒ Object
10 11 12 13 |
# File 'test/acts_as_list_test.rb', line 10 def setup setup_db (1..4).each { |counter| ListMixin.create! :pos => counter, :parent_id => 5 } end |
#teardown ⇒ Object
15 16 17 |
# File 'test/acts_as_list_test.rb', line 15 def teardown teardown_db end |
#test_delete_middle ⇒ Object
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'test/acts_as_list_test.rb', line 119 def test_delete_middle assert_equal [1, 2, 3, 4], ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id) ListMixin.find(2).destroy assert_equal [1, 3, 4], ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id) assert_equal 1, ListMixin.find(1).pos assert_equal 2, ListMixin.find(3).pos assert_equal 3, ListMixin.find(4).pos ListMixin.find(1).destroy assert_equal [3, 4], ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id) assert_equal 1, ListMixin.find(3).pos assert_equal 2, ListMixin.find(4).pos end |
#test_delete_middle_with_holes ⇒ Object
special thanks to openhood on github
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 |
# File 'test/acts_as_list_test.rb', line 211 def test_delete_middle_with_holes # first we check everything is at expected place assert_equal [1, 2, 3, 4], ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id) # then we create a hole in the list, say you're working with existing data in which you already have holes # or your scope is very complex ListMixin.delete(2) # we ensure the hole is really here assert_equal [1, 3, 4], ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id) assert_equal 1, ListMixin.find(1).pos assert_equal 3, ListMixin.find(3).pos assert_equal 4, ListMixin.find(4).pos # can we retrieve lower item despite the hole? assert_equal 3, ListMixin.find(1).lower_item.id # can we move an item lower jumping more than one position? ListMixin.find(1).move_lower assert_equal [3, 1, 4], ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id) assert_equal 2, ListMixin.find(3).pos assert_equal 3, ListMixin.find(1).pos assert_equal 4, ListMixin.find(4).pos # create another hole ListMixin.delete(1) # can we retrieve higher item despite the hole? assert_equal 3, ListMixin.find(4).higher_item.id # can we move an item higher jumping more than one position? ListMixin.find(4).move_higher assert_equal [4, 3], ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id) assert_equal 2, ListMixin.find(4).pos assert_equal 3, ListMixin.find(3).pos end |
#test_injection ⇒ Object
54 55 56 57 58 |
# File 'test/acts_as_list_test.rb', line 54 def test_injection item = ListMixin.new(:parent_id => 1) assert_equal ["parent_id = 1"], item.scope_condition assert_equal "pos", item.position_column end |
#test_insert ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'test/acts_as_list_test.rb', line 60 def test_insert new = ListMixin.create(:parent_id => 20) # insert as last assert_equal 1, new.pos assert new.first? assert new.last? new = ListMixin.create(:parent_id => 20) assert_equal 2, new.pos assert !new.first? assert new.last? new = ListMixin.create(:parent_id => 20) assert_equal 3, new.pos assert !new.first? assert new.last? new = ListMixin.create(:parent_id => 0) assert_equal 1, new.pos assert new.first? assert new.last? end |
#test_insert_at ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'test/acts_as_list_test.rb', line 83 def test_insert_at # insert as last new = ListMixin.create(:parent_id => 20) assert_equal 1, new.pos new = ListMixin.create(:parent_id => 20) assert_equal 2, new.pos new = ListMixin.create(:parent_id => 20) assert_equal 3, new.pos new4 = ListMixin.create(:parent_id => 20) assert_equal 4, new4.pos new4.insert_at(3) assert_equal 3, new4.pos new.reload assert_equal 4, new.pos new.insert_at(2) assert_equal 2, new.pos new4.reload assert_equal 4, new4.pos new5 = ListMixin.create(:parent_id => 20) assert_equal 5, new5.pos new5.insert_at(1) assert_equal 1, new5.pos new4.reload assert_equal 5, new4.pos end |
#test_move_to_bottom_with_next_to_last_item ⇒ Object
41 42 43 44 45 |
# File 'test/acts_as_list_test.rb', line 41 def test_move_to_bottom_with_next_to_last_item assert_equal [1, 2, 3, 4], ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id) ListMixin.find(3).move_to_bottom assert_equal [1, 2, 4, 3], ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id) end |
#test_move_to_higher_in_list ⇒ Object
180 181 182 183 184 185 186 187 188 |
# File 'test/acts_as_list_test.rb', line 180 def test_move_to_higher_in_list assert_equal [1, 2, 3, 4], ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id) item = ListMixin.find(2) item.move_to(1) assert_equal [2, 1, 3, 4], ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id) item.move_to(-2) assert_equal [2, 1, 3, 4], ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id) assert_equal 1, item.pos end |
#test_move_to_lower_in_list ⇒ Object
170 171 172 173 174 175 176 177 178 |
# File 'test/acts_as_list_test.rb', line 170 def test_move_to_lower_in_list assert_equal [1, 2, 3, 4], ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id) item = ListMixin.find(2) item.move_to(3) assert_equal [1, 3, 2, 4], ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id) item.move_to(7) assert_equal [1, 3, 4, 2], ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id) assert_equal 4, item.pos end |
#test_next_prev ⇒ Object
47 48 49 50 51 52 |
# File 'test/acts_as_list_test.rb', line 47 def test_next_prev assert_equal ListMixin.find(2), ListMixin.find(1).lower_item assert_nil ListMixin.find(1).higher_item assert_equal ListMixin.find(3), ListMixin.find(4).higher_item assert_nil ListMixin.find(4).lower_item end |
#test_nil_scope ⇒ Object
145 146 147 148 149 |
# File 'test/acts_as_list_test.rb', line 145 def test_nil_scope new1, new2, new3 = ListMixin.create, ListMixin.create, ListMixin.create new2.move_higher assert_equal [new2, new1, new3], ListMixin.find(:all, :conditions => 'parent_id IS NULL', :order => 'pos') end |
#test_remove_before_destroy_does_not_shift_lower_items_twice ⇒ Object
190 191 192 193 194 195 196 197 198 199 200 201 |
# File 'test/acts_as_list_test.rb', line 190 def test_remove_before_destroy_does_not_shift_lower_items_twice assert_equal [1, 2, 3, 4], ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id) ListMixin.find(2).remove_from_list ListMixin.find(2).destroy assert_equal [1, 3, 4], ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id) assert_equal 1, ListMixin.find(1).pos assert_equal 2, ListMixin.find(3).pos assert_equal 3, ListMixin.find(4).pos end |
#test_remove_from_list_should_set_position_to_nil ⇒ Object
157 158 159 160 161 162 163 164 165 166 167 168 |
# File 'test/acts_as_list_test.rb', line 157 def test_remove_from_list_should_set_position_to_nil assert_equal [1, 2, 3, 4], ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id) ListMixin.find(2).remove_from_list assert_equal [2, 1, 3, 4], ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id) assert_equal 1, ListMixin.find(1).pos assert_equal nil, ListMixin.find(2).pos assert_equal 2, ListMixin.find(3).pos assert_equal 3, ListMixin.find(4).pos end |
#test_remove_from_list_should_then_fail_in_list? ⇒ Boolean
151 152 153 154 155 |
# File 'test/acts_as_list_test.rb', line 151 def test_remove_from_list_should_then_fail_in_list? assert_equal true, ListMixin.find(1).in_list? ListMixin.find(1).remove_from_list assert_equal false, ListMixin.find(1).in_list? end |
#test_reordering ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'test/acts_as_list_test.rb', line 19 def test_reordering assert_equal [1, 2, 3, 4], ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id) ListMixin.find(2).move_lower assert_equal [1, 3, 2, 4], ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id) ListMixin.find(2).move_higher assert_equal [1, 2, 3, 4], ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id) ListMixin.find(1).move_to_bottom assert_equal [2, 3, 4, 1], ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id) ListMixin.find(1).move_to_top assert_equal [1, 2, 3, 4], ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id) ListMixin.find(2).move_to_bottom assert_equal [1, 3, 4, 2], ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id) ListMixin.find(4).move_to_top assert_equal [4, 1, 3, 2], ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id) end |
#test_should_not_trigger_unexpected_callbacks_on_destroy ⇒ Object
203 204 205 206 207 208 |
# File 'test/acts_as_list_test.rb', line 203 def test_should_not_trigger_unexpected_callbacks_on_destroy element = ListMixin.find(2) assert !element.before_save_triggered element.destroy assert !element.before_save_triggered end |
#test_with_string_based_scope ⇒ Object
138 139 140 141 142 143 |
# File 'test/acts_as_list_test.rb', line 138 def test_with_string_based_scope new = ListWithStringScopeMixin.create(:parent_id => 500) assert_equal 1, new.pos assert new.first? assert new.last? end |