Class: ListTest

Inherits:
Test::Unit::TestCase
  • Object
show all
Defined in:
lib/plugins/acts_as_list/test/list_test.rb

Instance Method Summary collapse

Instance Method Details

#setupObject


52
53
54
55
# File 'lib/plugins/acts_as_list/test/list_test.rb', line 52

def setup
  setup_db
  (1..4).each { |counter| ListMixin.create! :pos => counter, :parent_id => 5 }
end

#teardownObject


57
58
59
# File 'lib/plugins/acts_as_list/test/list_test.rb', line 57

def teardown
  teardown_db
end

#test_delete_middleObject


159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/plugins/acts_as_list/test/list_test.rb', line 159

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_injectionObject


96
97
98
99
100
# File 'lib/plugins/acts_as_list/test/list_test.rb', line 96

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_insertObject


102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/plugins/acts_as_list/test/list_test.rb', line 102

def test_insert
  new = ListMixin.create(:parent_id => 20)
  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_atObject


124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/plugins/acts_as_list/test/list_test.rb', line 124

def test_insert_at
  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_itemObject


83
84
85
86
87
# File 'lib/plugins/acts_as_list/test/list_test.rb', line 83

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_next_prevObject


89
90
91
92
93
94
# File 'lib/plugins/acts_as_list/test/list_test.rb', line 89

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_scopeObject


185
186
187
188
189
# File 'lib/plugins/acts_as_list/test/list_test.rb', line 185

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_twiceObject


211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/plugins/acts_as_list/test/list_test.rb', line 211

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_nilObject


198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/plugins/acts_as_list/test/list_test.rb', line 198

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


192
193
194
195
196
# File 'lib/plugins/acts_as_list/test/list_test.rb', line 192

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_reorderingObject


61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/plugins/acts_as_list/test/list_test.rb', line 61

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_with_string_based_scopeObject


178
179
180
181
182
183
# File 'lib/plugins/acts_as_list/test/list_test.rb', line 178

def test_with_string_based_scope
  new = ListWithStringScopeMixin.create(:parent_id => 500)
  assert_equal 1, new.pos
  assert new.first?
  assert new.last?
end