Class: ListWithStringIdsTest

Inherits:
ActiveSupport::TestCase
  • Object
show all
Defined in:
test/list_w_string_ids_test.rb

Instance Method Summary collapse

Instance Method Details

#setupObject



11
12
13
14
15
16
17
18
# File 'test/list_w_string_ids_test.rb', line 11

def setup
  setup_db
  (1..4).each do |counter|
    m = MixinWithStrings.new(:pos => counter, :parent_id => 5)
    m.id = counter.to_s
    m.save!
  end
end

#teardownObject



20
21
22
# File 'test/list_w_string_ids_test.rb', line 20

def teardown
  teardown_db
end

#test_delete_middleObject



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'test/list_w_string_ids_test.rb', line 122

def test_delete_middle
  assert_equal [1, 2, 3, 4], MixinWithStrings.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id)

  MixinWithStrings.find(2).destroy

  assert_equal [1, 3, 4], MixinWithStrings.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id)

  assert_equal 1, MixinWithStrings.find(1).pos
  assert_equal 2, MixinWithStrings.find(3).pos
  assert_equal 3, MixinWithStrings.find(4).pos

  MixinWithStrings.find(1).destroy

  assert_equal [3, 4], MixinWithStrings.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id)

  assert_equal 1, MixinWithStrings.find(3).pos
  assert_equal 2, MixinWithStrings.find(4).pos
end

#test_injectionObject



59
60
61
62
63
# File 'test/list_w_string_ids_test.rb', line 59

def test_injection
  item = MixinWithStrings.new(:parent_id => 1)
  assert_equal ["parent_id = 1"], item.scope_condition
  assert_equal "pos", item.position_column
end

#test_insertObject



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'test/list_w_string_ids_test.rb', line 65

def test_insert
  new = MixinWithStrings.create(:parent_id => 20)
  assert_equal 1, new.pos
  assert new.first?
  assert new.last?

  new = MixinWithStrings.create(:parent_id => 20)
  assert_equal 2, new.pos
  assert !new.first?
  assert new.last?

  new = MixinWithStrings.create(:parent_id => 20)
  assert_equal 3, new.pos
  assert !new.first?
  assert new.last?

  new = MixinWithStrings.create(:parent_id => 0)
  assert_equal 1, new.pos
  assert new.first?
  assert new.last?
end

#test_insert_atObject



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
118
119
120
# File 'test/list_w_string_ids_test.rb', line 87

def test_insert_at
  new = MixinWithStrings.create(:parent_id => 20)
  assert_equal 1, new.pos

  new = MixinWithStrings.create(:parent_id => 20)
  assert_equal 2, new.pos

  new = MixinWithStrings.create(:parent_id => 20)
  assert_equal 3, new.pos

  new4 = MixinWithStrings.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 = MixinWithStrings.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



46
47
48
49
50
# File 'test/list_w_string_ids_test.rb', line 46

def test_move_to_bottom_with_next_to_last_item
  assert_equal [1, 2, 3, 4], MixinWithStrings.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id)
  MixinWithStrings.find(3).move_to_bottom
  assert_equal [1, 2, 4, 3], MixinWithStrings.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id)
end

#test_next_prevObject



52
53
54
55
56
57
# File 'test/list_w_string_ids_test.rb', line 52

def test_next_prev
  assert_equal MixinWithStrings.find(2), MixinWithStrings.find(1).lower_item
  assert_nil MixinWithStrings.find(1).higher_item
  assert_equal MixinWithStrings.find(3), MixinWithStrings.find(4).higher_item
  assert_nil MixinWithStrings.find(4).lower_item
end

#test_nil_scopeObject



148
149
150
151
152
# File 'test/list_w_string_ids_test.rb', line 148

def test_nil_scope
  new1, new2, new3 = MixinWithStrings.create, MixinWithStrings.create, MixinWithStrings.create
  new2.move_higher
  assert_equal [new2, new1, new3], MixinWithStrings.find(:all, :conditions => 'parent_id IS NULL', :order => 'pos')
end

#test_remove_before_destroy_does_not_shift_lower_items_twiceObject



174
175
176
177
178
179
180
181
182
183
184
185
# File 'test/list_w_string_ids_test.rb', line 174

def test_remove_before_destroy_does_not_shift_lower_items_twice
  assert_equal [1, 2, 3, 4], MixinWithStrings.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id)

  MixinWithStrings.find(2).remove_from_list
  MixinWithStrings.find(2).destroy

  assert_equal [1, 3, 4], MixinWithStrings.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id)

  assert_equal 1, MixinWithStrings.find(1).pos
  assert_equal 2, MixinWithStrings.find(3).pos
  assert_equal 3, MixinWithStrings.find(4).pos
end

#test_remove_from_list_should_set_position_to_nilObject



161
162
163
164
165
166
167
168
169
170
171
172
# File 'test/list_w_string_ids_test.rb', line 161

def test_remove_from_list_should_set_position_to_nil
  assert_equal [1, 2, 3, 4], MixinWithStrings.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id)

  MixinWithStrings.find(2).remove_from_list

  assert_equal [2, 1, 3, 4], MixinWithStrings.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id)

  assert_equal 1, MixinWithStrings.find(1).pos
  assert_equal nil, MixinWithStrings.find(2).pos
  assert_equal 2, MixinWithStrings.find(3).pos
  assert_equal 3, MixinWithStrings.find(4).pos
end

#test_remove_from_list_should_then_fail_in_list?Boolean

Returns:

  • (Boolean)


155
156
157
158
159
# File 'test/list_w_string_ids_test.rb', line 155

def test_remove_from_list_should_then_fail_in_list?
  assert_equal true, MixinWithStrings.find(1).in_list?
  MixinWithStrings.find(1).remove_from_list
  assert_equal false, MixinWithStrings.find(1).in_list?
end

#test_reorderingObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'test/list_w_string_ids_test.rb', line 24

def test_reordering
  assert_equal [1, 2, 3, 4], MixinWithStrings.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id)

  MixinWithStrings.find(2).move_lower
  assert_equal [1, 3, 2, 4], MixinWithStrings.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id)

  MixinWithStrings.find(2).move_higher
  assert_equal [1, 2, 3, 4], MixinWithStrings.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id)

  MixinWithStrings.find(1).move_to_bottom
  assert_equal [2, 3, 4, 1], MixinWithStrings.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id)

  MixinWithStrings.find(1).move_to_top
  assert_equal [1, 2, 3, 4], MixinWithStrings.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id)

  MixinWithStrings.find(2).move_to_bottom
  assert_equal [1, 3, 4, 2], MixinWithStrings.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id)

  MixinWithStrings.find(4).move_to_top
  assert_equal [4, 1, 3, 2], MixinWithStrings.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id)
end

#test_with_string_based_scopeObject



141
142
143
144
145
146
# File 'test/list_w_string_ids_test.rb', line 141

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