diff options
| author | hukl <hukl@eight.local> | 2009-01-31 13:04:43 +0100 |
|---|---|---|
| committer | hukl <hukl@eight.local> | 2009-01-31 13:04:43 +0100 |
| commit | be83d467b5b6f92b0a6a175ee365d043f250d631 (patch) | |
| tree | 87209c119576d1ac7ff7dcf26d3e65b706c91fef /vendor/plugins/acts_as_list/test | |
| parent | 89d3dc4a676ee82cc6bad4d9d00535897318f1c3 (diff) | |
added acts_as_list plugin
Diffstat (limited to 'vendor/plugins/acts_as_list/test')
| -rw-r--r-- | vendor/plugins/acts_as_list/test/list_test.rb | 332 |
1 files changed, 332 insertions, 0 deletions
diff --git a/vendor/plugins/acts_as_list/test/list_test.rb b/vendor/plugins/acts_as_list/test/list_test.rb new file mode 100644 index 00000000..e89cb8e1 --- /dev/null +++ b/vendor/plugins/acts_as_list/test/list_test.rb | |||
| @@ -0,0 +1,332 @@ | |||
| 1 | require 'test/unit' | ||
| 2 | |||
| 3 | require 'rubygems' | ||
| 4 | gem 'activerecord', '>= 1.15.4.7794' | ||
| 5 | require 'active_record' | ||
| 6 | |||
| 7 | require "#{File.dirname(__FILE__)}/../init" | ||
| 8 | |||
| 9 | ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :dbfile => ":memory:") | ||
| 10 | |||
| 11 | def setup_db | ||
| 12 | ActiveRecord::Schema.define(:version => 1) do | ||
| 13 | create_table :mixins do |t| | ||
| 14 | t.column :pos, :integer | ||
| 15 | t.column :parent_id, :integer | ||
| 16 | t.column :created_at, :datetime | ||
| 17 | t.column :updated_at, :datetime | ||
| 18 | end | ||
| 19 | end | ||
| 20 | end | ||
| 21 | |||
| 22 | def teardown_db | ||
| 23 | ActiveRecord::Base.connection.tables.each do |table| | ||
| 24 | ActiveRecord::Base.connection.drop_table(table) | ||
| 25 | end | ||
| 26 | end | ||
| 27 | |||
| 28 | class Mixin < ActiveRecord::Base | ||
| 29 | end | ||
| 30 | |||
| 31 | class ListMixin < Mixin | ||
| 32 | acts_as_list :column => "pos", :scope => :parent | ||
| 33 | |||
| 34 | def self.table_name() "mixins" end | ||
| 35 | end | ||
| 36 | |||
| 37 | class ListMixinSub1 < ListMixin | ||
| 38 | end | ||
| 39 | |||
| 40 | class ListMixinSub2 < ListMixin | ||
| 41 | end | ||
| 42 | |||
| 43 | class ListWithStringScopeMixin < ActiveRecord::Base | ||
| 44 | acts_as_list :column => "pos", :scope => 'parent_id = #{parent_id}' | ||
| 45 | |||
| 46 | def self.table_name() "mixins" end | ||
| 47 | end | ||
| 48 | |||
| 49 | |||
| 50 | class ListTest < Test::Unit::TestCase | ||
| 51 | |||
| 52 | def setup | ||
| 53 | setup_db | ||
| 54 | (1..4).each { |counter| ListMixin.create! :pos => counter, :parent_id => 5 } | ||
| 55 | end | ||
| 56 | |||
| 57 | def teardown | ||
| 58 | teardown_db | ||
| 59 | end | ||
| 60 | |||
| 61 | def test_reordering | ||
| 62 | assert_equal [1, 2, 3, 4], ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id) | ||
| 63 | |||
| 64 | ListMixin.find(2).move_lower | ||
| 65 | assert_equal [1, 3, 2, 4], ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id) | ||
| 66 | |||
| 67 | ListMixin.find(2).move_higher | ||
| 68 | assert_equal [1, 2, 3, 4], ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id) | ||
| 69 | |||
| 70 | ListMixin.find(1).move_to_bottom | ||
| 71 | assert_equal [2, 3, 4, 1], ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id) | ||
| 72 | |||
| 73 | ListMixin.find(1).move_to_top | ||
| 74 | assert_equal [1, 2, 3, 4], ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id) | ||
| 75 | |||
| 76 | ListMixin.find(2).move_to_bottom | ||
| 77 | assert_equal [1, 3, 4, 2], ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id) | ||
| 78 | |||
| 79 | ListMixin.find(4).move_to_top | ||
| 80 | assert_equal [4, 1, 3, 2], ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id) | ||
| 81 | end | ||
| 82 | |||
| 83 | def test_move_to_bottom_with_next_to_last_item | ||
| 84 | assert_equal [1, 2, 3, 4], ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id) | ||
| 85 | ListMixin.find(3).move_to_bottom | ||
| 86 | assert_equal [1, 2, 4, 3], ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id) | ||
| 87 | end | ||
| 88 | |||
| 89 | def test_next_prev | ||
| 90 | assert_equal ListMixin.find(2), ListMixin.find(1).lower_item | ||
| 91 | assert_nil ListMixin.find(1).higher_item | ||
| 92 | assert_equal ListMixin.find(3), ListMixin.find(4).higher_item | ||
| 93 | assert_nil ListMixin.find(4).lower_item | ||
| 94 | end | ||
| 95 | |||
| 96 | def test_injection | ||
| 97 | item = ListMixin.new(:parent_id => 1) | ||
| 98 | assert_equal "parent_id = 1", item.scope_condition | ||
| 99 | assert_equal "pos", item.position_column | ||
| 100 | end | ||
| 101 | |||
| 102 | def test_insert | ||
| 103 | new = ListMixin.create(:parent_id => 20) | ||
| 104 | assert_equal 1, new.pos | ||
| 105 | assert new.first? | ||
| 106 | assert new.last? | ||
| 107 | |||
| 108 | new = ListMixin.create(:parent_id => 20) | ||
| 109 | assert_equal 2, new.pos | ||
| 110 | assert !new.first? | ||
| 111 | assert new.last? | ||
| 112 | |||
| 113 | new = ListMixin.create(:parent_id => 20) | ||
| 114 | assert_equal 3, new.pos | ||
| 115 | assert !new.first? | ||
| 116 | assert new.last? | ||
| 117 | |||
| 118 | new = ListMixin.create(:parent_id => 0) | ||
| 119 | assert_equal 1, new.pos | ||
| 120 | assert new.first? | ||
| 121 | assert new.last? | ||
| 122 | end | ||
| 123 | |||
| 124 | def test_insert_at | ||
| 125 | new = ListMixin.create(:parent_id => 20) | ||
| 126 | assert_equal 1, new.pos | ||
| 127 | |||
| 128 | new = ListMixin.create(:parent_id => 20) | ||
| 129 | assert_equal 2, new.pos | ||
| 130 | |||
| 131 | new = ListMixin.create(:parent_id => 20) | ||
| 132 | assert_equal 3, new.pos | ||
| 133 | |||
| 134 | new4 = ListMixin.create(:parent_id => 20) | ||
| 135 | assert_equal 4, new4.pos | ||
| 136 | |||
| 137 | new4.insert_at(3) | ||
| 138 | assert_equal 3, new4.pos | ||
| 139 | |||
| 140 | new.reload | ||
| 141 | assert_equal 4, new.pos | ||
| 142 | |||
| 143 | new.insert_at(2) | ||
| 144 | assert_equal 2, new.pos | ||
| 145 | |||
| 146 | new4.reload | ||
| 147 | assert_equal 4, new4.pos | ||
| 148 | |||
| 149 | new5 = ListMixin.create(:parent_id => 20) | ||
| 150 | assert_equal 5, new5.pos | ||
| 151 | |||
| 152 | new5.insert_at(1) | ||
| 153 | assert_equal 1, new5.pos | ||
| 154 | |||
| 155 | new4.reload | ||
| 156 | assert_equal 5, new4.pos | ||
| 157 | end | ||
| 158 | |||
| 159 | def test_delete_middle | ||
| 160 | assert_equal [1, 2, 3, 4], ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id) | ||
| 161 | |||
| 162 | ListMixin.find(2).destroy | ||
| 163 | |||
| 164 | assert_equal [1, 3, 4], ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id) | ||
| 165 | |||
| 166 | assert_equal 1, ListMixin.find(1).pos | ||
| 167 | assert_equal 2, ListMixin.find(3).pos | ||
| 168 | assert_equal 3, ListMixin.find(4).pos | ||
| 169 | |||
| 170 | ListMixin.find(1).destroy | ||
| 171 | |||
| 172 | assert_equal [3, 4], ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id) | ||
| 173 | |||
| 174 | assert_equal 1, ListMixin.find(3).pos | ||
| 175 | assert_equal 2, ListMixin.find(4).pos | ||
| 176 | end | ||
| 177 | |||
| 178 | def test_with_string_based_scope | ||
| 179 | new = ListWithStringScopeMixin.create(:parent_id => 500) | ||
| 180 | assert_equal 1, new.pos | ||
| 181 | assert new.first? | ||
| 182 | assert new.last? | ||
| 183 | end | ||
| 184 | |||
| 185 | def test_nil_scope | ||
| 186 | new1, new2, new3 = ListMixin.create, ListMixin.create, ListMixin.create | ||
| 187 | new2.move_higher | ||
| 188 | assert_equal [new2, new1, new3], ListMixin.find(:all, :conditions => 'parent_id IS NULL', :order => 'pos') | ||
| 189 | end | ||
| 190 | |||
| 191 | |||
| 192 | def test_remove_from_list_should_then_fail_in_list? | ||
| 193 | assert_equal true, ListMixin.find(1).in_list? | ||
| 194 | ListMixin.find(1).remove_from_list | ||
| 195 | assert_equal false, ListMixin.find(1).in_list? | ||
| 196 | end | ||
| 197 | |||
| 198 | def test_remove_from_list_should_set_position_to_nil | ||
| 199 | assert_equal [1, 2, 3, 4], ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id) | ||
| 200 | |||
| 201 | ListMixin.find(2).remove_from_list | ||
| 202 | |||
| 203 | assert_equal [2, 1, 3, 4], ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id) | ||
| 204 | |||
| 205 | assert_equal 1, ListMixin.find(1).pos | ||
| 206 | assert_equal nil, ListMixin.find(2).pos | ||
| 207 | assert_equal 2, ListMixin.find(3).pos | ||
| 208 | assert_equal 3, ListMixin.find(4).pos | ||
| 209 | end | ||
| 210 | |||
| 211 | def test_remove_before_destroy_does_not_shift_lower_items_twice | ||
| 212 | assert_equal [1, 2, 3, 4], ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id) | ||
| 213 | |||
| 214 | ListMixin.find(2).remove_from_list | ||
| 215 | ListMixin.find(2).destroy | ||
| 216 | |||
| 217 | assert_equal [1, 3, 4], ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id) | ||
| 218 | |||
| 219 | assert_equal 1, ListMixin.find(1).pos | ||
| 220 | assert_equal 2, ListMixin.find(3).pos | ||
| 221 | assert_equal 3, ListMixin.find(4).pos | ||
| 222 | end | ||
| 223 | |||
| 224 | end | ||
| 225 | |||
| 226 | class ListSubTest < Test::Unit::TestCase | ||
| 227 | |||
| 228 | def setup | ||
| 229 | setup_db | ||
| 230 | (1..4).each { |i| ((i % 2 == 1) ? ListMixinSub1 : ListMixinSub2).create! :pos => i, :parent_id => 5000 } | ||
| 231 | end | ||
| 232 | |||
| 233 | def teardown | ||
| 234 | teardown_db | ||
| 235 | end | ||
| 236 | |||
| 237 | def test_reordering | ||
| 238 | assert_equal [1, 2, 3, 4], ListMixin.find(:all, :conditions => 'parent_id = 5000', :order => 'pos').map(&:id) | ||
| 239 | |||
| 240 | ListMixin.find(2).move_lower | ||
| 241 | assert_equal [1, 3, 2, 4], ListMixin.find(:all, :conditions => 'parent_id = 5000', :order => 'pos').map(&:id) | ||
| 242 | |||
| 243 | ListMixin.find(2).move_higher | ||
| 244 | assert_equal [1, 2, 3, 4], ListMixin.find(:all, :conditions => 'parent_id = 5000', :order => 'pos').map(&:id) | ||
| 245 | |||
| 246 | ListMixin.find(1).move_to_bottom | ||
| 247 | assert_equal [2, 3, 4, 1], ListMixin.find(:all, :conditions => 'parent_id = 5000', :order => 'pos').map(&:id) | ||
| 248 | |||
| 249 | ListMixin.find(1).move_to_top | ||
| 250 | assert_equal [1, 2, 3, 4], ListMixin.find(:all, :conditions => 'parent_id = 5000', :order => 'pos').map(&:id) | ||
| 251 | |||
| 252 | ListMixin.find(2).move_to_bottom | ||
| 253 | assert_equal [1, 3, 4, 2], ListMixin.find(:all, :conditions => 'parent_id = 5000', :order => 'pos').map(&:id) | ||
| 254 | |||
| 255 | ListMixin.find(4).move_to_top | ||
| 256 | assert_equal [4, 1, 3, 2], ListMixin.find(:all, :conditions => 'parent_id = 5000', :order => 'pos').map(&:id) | ||
| 257 | end | ||
| 258 | |||
| 259 | def test_move_to_bottom_with_next_to_last_item | ||
| 260 | assert_equal [1, 2, 3, 4], ListMixin.find(:all, :conditions => 'parent_id = 5000', :order => 'pos').map(&:id) | ||
| 261 | ListMixin.find(3).move_to_bottom | ||
| 262 | assert_equal [1, 2, 4, 3], ListMixin.find(:all, :conditions => 'parent_id = 5000', :order => 'pos').map(&:id) | ||
| 263 | end | ||
| 264 | |||
| 265 | def test_next_prev | ||
| 266 | assert_equal ListMixin.find(2), ListMixin.find(1).lower_item | ||
| 267 | assert_nil ListMixin.find(1).higher_item | ||
| 268 | assert_equal ListMixin.find(3), ListMixin.find(4).higher_item | ||
| 269 | assert_nil ListMixin.find(4).lower_item | ||
| 270 | end | ||
| 271 | |||
| 272 | def test_injection | ||
| 273 | item = ListMixin.new("parent_id"=>1) | ||
| 274 | assert_equal "parent_id = 1", item.scope_condition | ||
| 275 | assert_equal "pos", item.position_column | ||
| 276 | end | ||
| 277 | |||
| 278 | def test_insert_at | ||
| 279 | new = ListMixin.create("parent_id" => 20) | ||
| 280 | assert_equal 1, new.pos | ||
| 281 | |||
| 282 | new = ListMixinSub1.create("parent_id" => 20) | ||
| 283 | assert_equal 2, new.pos | ||
| 284 | |||
| 285 | new = ListMixinSub2.create("parent_id" => 20) | ||
| 286 | assert_equal 3, new.pos | ||
| 287 | |||
| 288 | new4 = ListMixin.create("parent_id" => 20) | ||
| 289 | assert_equal 4, new4.pos | ||
| 290 | |||
| 291 | new4.insert_at(3) | ||
| 292 | assert_equal 3, new4.pos | ||
| 293 | |||
| 294 | new.reload | ||
| 295 | assert_equal 4, new.pos | ||
| 296 | |||
| 297 | new.insert_at(2) | ||
| 298 | assert_equal 2, new.pos | ||
| 299 | |||
| 300 | new4.reload | ||
| 301 | assert_equal 4, new4.pos | ||
| 302 | |||
| 303 | new5 = ListMixinSub1.create("parent_id" => 20) | ||
| 304 | assert_equal 5, new5.pos | ||
| 305 | |||
| 306 | new5.insert_at(1) | ||
| 307 | assert_equal 1, new5.pos | ||
| 308 | |||
| 309 | new4.reload | ||
| 310 | assert_equal 5, new4.pos | ||
| 311 | end | ||
| 312 | |||
| 313 | def test_delete_middle | ||
| 314 | assert_equal [1, 2, 3, 4], ListMixin.find(:all, :conditions => 'parent_id = 5000', :order => 'pos').map(&:id) | ||
| 315 | |||
| 316 | ListMixin.find(2).destroy | ||
| 317 | |||
| 318 | assert_equal [1, 3, 4], ListMixin.find(:all, :conditions => 'parent_id = 5000', :order => 'pos').map(&:id) | ||
| 319 | |||
| 320 | assert_equal 1, ListMixin.find(1).pos | ||
| 321 | assert_equal 2, ListMixin.find(3).pos | ||
| 322 | assert_equal 3, ListMixin.find(4).pos | ||
| 323 | |||
| 324 | ListMixin.find(1).destroy | ||
| 325 | |||
| 326 | assert_equal [3, 4], ListMixin.find(:all, :conditions => 'parent_id = 5000', :order => 'pos').map(&:id) | ||
| 327 | |||
| 328 | assert_equal 1, ListMixin.find(3).pos | ||
| 329 | assert_equal 2, ListMixin.find(4).pos | ||
| 330 | end | ||
| 331 | |||
| 332 | end | ||
