| | 193 | |
|---|
| | 194 | class ListSubTest < Test::Unit::TestCase |
|---|
| | 195 | |
|---|
| | 196 | def setup |
|---|
| | 197 | setup_db |
|---|
| | 198 | (1..4).each { |i| ((i % 2 == 1) ? ListMixinSub1 : ListMixinSub2).create! :pos => i, :parent_id => 5000 } |
|---|
| | 199 | end |
|---|
| | 200 | |
|---|
| | 201 | def teardown |
|---|
| | 202 | teardown_db |
|---|
| | 203 | end |
|---|
| | 204 | |
|---|
| | 205 | def test_reordering |
|---|
| | 206 | assert_equal [1, 2, 3, 4], ListMixin.find(:all, :conditions => 'parent_id = 5000', :order => 'pos').map(&:id) |
|---|
| | 207 | |
|---|
| | 208 | ListMixin.find(2).move_lower |
|---|
| | 209 | assert_equal [1, 3, 2, 4], ListMixin.find(:all, :conditions => 'parent_id = 5000', :order => 'pos').map(&:id) |
|---|
| | 210 | |
|---|
| | 211 | ListMixin.find(2).move_higher |
|---|
| | 212 | assert_equal [1, 2, 3, 4], ListMixin.find(:all, :conditions => 'parent_id = 5000', :order => 'pos').map(&:id) |
|---|
| | 213 | |
|---|
| | 214 | ListMixin.find(1).move_to_bottom |
|---|
| | 215 | assert_equal [2, 3, 4, 1], ListMixin.find(:all, :conditions => 'parent_id = 5000', :order => 'pos').map(&:id) |
|---|
| | 216 | |
|---|
| | 217 | ListMixin.find(1).move_to_top |
|---|
| | 218 | assert_equal [1, 2, 3, 4], ListMixin.find(:all, :conditions => 'parent_id = 5000', :order => 'pos').map(&:id) |
|---|
| | 219 | |
|---|
| | 220 | ListMixin.find(2).move_to_bottom |
|---|
| | 221 | assert_equal [1, 3, 4, 2], ListMixin.find(:all, :conditions => 'parent_id = 5000', :order => 'pos').map(&:id) |
|---|
| | 222 | |
|---|
| | 223 | ListMixin.find(4).move_to_top |
|---|
| | 224 | assert_equal [4, 1, 3, 2], ListMixin.find(:all, :conditions => 'parent_id = 5000', :order => 'pos').map(&:id) |
|---|
| | 225 | end |
|---|
| | 226 | |
|---|
| | 227 | def test_move_to_bottom_with_next_to_last_item |
|---|
| | 228 | assert_equal [1, 2, 3, 4], ListMixin.find(:all, :conditions => 'parent_id = 5000', :order => 'pos').map(&:id) |
|---|
| | 229 | ListMixin.find(3).move_to_bottom |
|---|
| | 230 | assert_equal [1, 2, 4, 3], ListMixin.find(:all, :conditions => 'parent_id = 5000', :order => 'pos').map(&:id) |
|---|
| | 231 | end |
|---|
| | 232 | |
|---|
| | 233 | def test_next_prev |
|---|
| | 234 | assert_equal ListMixin.find(2), ListMixin.find(1).lower_item |
|---|
| | 235 | assert_nil ListMixin.find(1).higher_item |
|---|
| | 236 | assert_equal ListMixin.find(3), ListMixin.find(4).higher_item |
|---|
| | 237 | assert_nil ListMixin.find(4).lower_item |
|---|
| | 238 | end |
|---|
| | 239 | |
|---|
| | 240 | def test_injection |
|---|
| | 241 | item = ListMixin.new("parent_id"=>1) |
|---|
| | 242 | assert_equal "parent_id = 1", item.scope_condition |
|---|
| | 243 | assert_equal "pos", item.position_column |
|---|
| | 244 | end |
|---|
| | 245 | |
|---|
| | 246 | def test_insert_at |
|---|
| | 247 | new = ListMixin.create("parent_id" => 20) |
|---|
| | 248 | assert_equal 1, new.pos |
|---|
| | 249 | |
|---|
| | 250 | new = ListMixinSub1.create("parent_id" => 20) |
|---|
| | 251 | assert_equal 2, new.pos |
|---|
| | 252 | |
|---|
| | 253 | new = ListMixinSub2.create("parent_id" => 20) |
|---|
| | 254 | assert_equal 3, new.pos |
|---|
| | 255 | |
|---|
| | 256 | new4 = ListMixin.create("parent_id" => 20) |
|---|
| | 257 | assert_equal 4, new4.pos |
|---|
| | 258 | |
|---|
| | 259 | new4.insert_at(3) |
|---|
| | 260 | assert_equal 3, new4.pos |
|---|
| | 261 | |
|---|
| | 262 | new.reload |
|---|
| | 263 | assert_equal 4, new.pos |
|---|
| | 264 | |
|---|
| | 265 | new.insert_at(2) |
|---|
| | 266 | assert_equal 2, new.pos |
|---|
| | 267 | |
|---|
| | 268 | new4.reload |
|---|
| | 269 | assert_equal 4, new4.pos |
|---|
| | 270 | |
|---|
| | 271 | new5 = ListMixinSub1.create("parent_id" => 20) |
|---|
| | 272 | assert_equal 5, new5.pos |
|---|
| | 273 | |
|---|
| | 274 | new5.insert_at(1) |
|---|
| | 275 | assert_equal 1, new5.pos |
|---|
| | 276 | |
|---|
| | 277 | new4.reload |
|---|
| | 278 | assert_equal 5, new4.pos |
|---|
| | 279 | end |
|---|
| | 280 | |
|---|
| | 281 | def test_delete_middle |
|---|
| | 282 | assert_equal [1, 2, 3, 4], ListMixin.find(:all, :conditions => 'parent_id = 5000', :order => 'pos').map(&:id) |
|---|
| | 283 | |
|---|
| | 284 | ListMixin.find(2).destroy |
|---|
| | 285 | |
|---|
| | 286 | assert_equal [1, 3, 4], ListMixin.find(:all, :conditions => 'parent_id = 5000', :order => 'pos').map(&:id) |
|---|
| | 287 | |
|---|
| | 288 | assert_equal 1, ListMixin.find(1).pos |
|---|
| | 289 | assert_equal 2, ListMixin.find(3).pos |
|---|
| | 290 | assert_equal 3, ListMixin.find(4).pos |
|---|
| | 291 | |
|---|
| | 292 | ListMixin.find(1).destroy |
|---|
| | 293 | |
|---|
| | 294 | assert_equal [3, 4], ListMixin.find(:all, :conditions => 'parent_id = 5000', :order => 'pos').map(&:id) |
|---|
| | 295 | |
|---|
| | 296 | assert_equal 1, ListMixin.find(3).pos |
|---|
| | 297 | assert_equal 2, ListMixin.find(4).pos |
|---|
| | 298 | end |
|---|
| | 299 | |
|---|
| | 300 | end |
|---|