Class: VersionedTest

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

Instance Method Summary collapse

Instance Method Details

#assert_page_title(p, i, version_field = :version) ⇒ Object


239
240
241
242
243
244
# File 'lib/plugins/acts_as_versioned/test/versioned_test.rb', line 239

def assert_page_title(p, i, version_field = :version)
  p.title = "title#{i}"
  p.save
  assert_equal "title#{i}", p.title
  assert_equal (i+4), p.send(version_field)
end

#test_association_optionsObject


288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
# File 'lib/plugins/acts_as_versioned/test/versioned_test.rb', line 288

def test_association_options
  association = Page.reflect_on_association(:versions)
  options = association.options
  assert_equal :delete_all, options[:dependent]
  assert_equal 'version', options[:order]

  association = Widget.reflect_on_association(:versions)
  options = association.options
  assert_equal :nullify, options[:dependent]
  assert_equal 'version desc', options[:order]
  assert_equal 'widget_id', options[:foreign_key]

  widget = Widget.create! :name => 'new widget'
  assert_equal 1, Widget.count
  assert_equal 1, Widget.versioned_class.count
  widget.destroy
  assert_equal 0, Widget.count
  assert_equal 1, Widget.versioned_class.count
end

#test_find_versionObject


254
255
256
257
258
259
260
261
262
263
264
265
# File 'lib/plugins/acts_as_versioned/test/versioned_test.rb', line 254

def test_find_version
  assert_equal page_versions(:welcome_1), Page.find_version(pages(:welcome).id, 23)
  assert_equal page_versions(:welcome_2), Page.find_version(pages(:welcome).id, 24)
  assert_equal pages(:welcome), Page.find_version(pages(:welcome).id)

  assert_equal page_versions(:welcome_1), pages(:welcome).find_version(23)
  assert_equal page_versions(:welcome_2), pages(:welcome).find_version(24)
  assert_equal pages(:welcome), pages(:welcome).find_version

  assert_raise(ActiveRecord::RecordNotFound) { Page.find_version(pages(:welcome).id, 1) }
  assert_raise(ActiveRecord::RecordNotFound) { Page.find_version(0, 23) }
end

#test_find_versionsObject


246
247
248
249
250
251
252
# File 'lib/plugins/acts_as_versioned/test/versioned_test.rb', line 246

def test_find_versions
  assert_equal 2, locked_pages(:welcome).versions.size
  assert_equal 1, locked_pages(:welcome).versions.find(:all, :conditions => ['title LIKE ?', '%weblog%']).length
  assert_equal 2, locked_pages(:welcome).versions.find(:all, :conditions => ['title LIKE ?', '%web%']).length
  assert_equal 0, locked_pages(:thinking).versions.find(:all, :conditions => ['title LIKE ?', '%web%']).length
  assert_equal 2, locked_pages(:welcome).versions.length
end

#test_has_many_throughObject


274
275
276
# File 'lib/plugins/acts_as_versioned/test/versioned_test.rb', line 274

def test_has_many_through
  assert_equal [authors(:caged), authors(:mly)], pages(:welcome).authors
end

#test_has_many_through_with_custom_associationObject


278
279
280
# File 'lib/plugins/acts_as_versioned/test/versioned_test.rb', line 278

def test_has_many_through_with_custom_association
  assert_equal [authors(:caged), authors(:mly)], pages(:welcome).revisors
end

#test_lock_version_works_with_versioningObject


116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/plugins/acts_as_versioned/test/versioned_test.rb', line 116

def test_lock_version_works_with_versioning
  p = locked_pages(:thinking)
  p2 = LockedPage.find(p.id)

  p.title = 'fresh title'
  p.save
  assert_equal 2, p.versions.size # limit!

  assert_raises(ActiveRecord::StaleObjectError) do
    p2.title = 'stale title'
    p2.save
  end
end

#test_referential_integrityObject


282
283
284
285
286
# File 'lib/plugins/acts_as_versioned/test/versioned_test.rb', line 282

def test_referential_integrity
  pages(:welcome).destroy
  assert_equal 0, Page.count
  assert_equal 0, Page::Version.count
end

#test_rollback_fails_with_invalid_revisionObject


67
68
69
70
# File 'lib/plugins/acts_as_versioned/test/versioned_test.rb', line 67

def test_rollback_fails_with_invalid_revision
  p = locked_pages(:welcome)
  assert !p.revert_to!(locked_pages(:thinking))
end

#test_rollback_with_version_classObject


57
58
59
60
61
62
63
64
65
# File 'lib/plugins/acts_as_versioned/test/versioned_test.rb', line 57

def test_rollback_with_version_class
  p = pages(:welcome)
  assert_equal 24, p.version
  assert_equal 'Welcome to the weblog', p.title

  assert p.revert_to!(p.versions.first), "Couldn't revert to 23"
  assert_equal 23, p.version
  assert_equal 'Welcome to the weblg', p.title
end

#test_rollback_with_version_class_with_optionsObject


89
90
91
92
93
94
95
96
97
# File 'lib/plugins/acts_as_versioned/test/versioned_test.rb', line 89

def test_rollback_with_version_class_with_options
  p = locked_pages(:welcome)
  assert_equal 'Welcome to the weblog', p.title
  assert_equal 'LockedPage', p.versions.first.version_type

  assert p.revert_to!(p.versions.first), "Couldn't revert to 1"
  assert_equal 'Welcome to the weblg', p.title
  assert_equal 'LockedPage', p.versions.first.version_type
end

#test_rollback_with_version_numberObject


30
31
32
33
34
35
36
37
38
# File 'lib/plugins/acts_as_versioned/test/versioned_test.rb', line 30

def test_rollback_with_version_number
  p = pages(:welcome)
  assert_equal 24, p.version
  assert_equal 'Welcome to the weblog', p.title

  assert p.revert_to!(p.versions.first.version), "Couldn't revert to 23"
  assert_equal 23, p.version
  assert_equal 'Welcome to the weblg', p.title
end

#test_rollback_with_version_number_with_optionsObject


79
80
81
82
83
84
85
86
87
# File 'lib/plugins/acts_as_versioned/test/versioned_test.rb', line 79

def test_rollback_with_version_number_with_options
  p = locked_pages(:welcome)
  assert_equal 'Welcome to the weblog', p.title
  assert_equal 'LockedPage', p.versions.first.version_type

  assert p.revert_to!(p.versions.first.version), "Couldn't revert to 23"
  assert_equal 'Welcome to the weblg', p.title
  assert_equal 'LockedPage', p.versions.first.version_type
end

#test_rollback_with_version_number_with_stiObject


107
108
109
110
111
112
113
114
# File 'lib/plugins/acts_as_versioned/test/versioned_test.rb', line 107

def test_rollback_with_version_number_with_sti
  p = locked_pages(:thinking)
  assert_equal 'So I was thinking', p.title

  assert p.revert_to!(p.versions.first.version), "Couldn't revert to 1"
  assert_equal 'So I was thinking!!!', p.title
  assert_equal 'SpecialLockedPage', p.versions.first.version_type
end

#test_saves_versioned_copyObject


9
10
11
12
13
14
15
# File 'lib/plugins/acts_as_versioned/test/versioned_test.rb', line 9

def test_saves_versioned_copy
  p = Page.create! :title => 'first title', :body => 'first body'
  assert !p.new_record?
  assert_equal 1, p.versions.size
  assert_equal 1, p.version
  assert_instance_of Page.versioned_class, p.versions.first
end

#test_saves_versioned_copy_with_optionsObject


72
73
74
75
76
77
# File 'lib/plugins/acts_as_versioned/test/versioned_test.rb', line 72

def test_saves_versioned_copy_with_options
  p = LockedPage.create! :title => 'first title'
  assert !p.new_record?
  assert_equal 1, p.versions.size
  assert_instance_of LockedPage.versioned_class, p.versions.first
end

#test_saves_versioned_copy_with_stiObject


99
100
101
102
103
104
105
# File 'lib/plugins/acts_as_versioned/test/versioned_test.rb', line 99

def test_saves_versioned_copy_with_sti
  p = SpecialLockedPage.create! :title => 'first title'
  assert !p.new_record?
  assert_equal 1, p.versions.size
  assert_instance_of LockedPage.versioned_class, p.versions.first
  assert_equal 'SpecialLockedPage', p.versions.first.version_type
end

#test_saves_without_revisionObject


17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/plugins/acts_as_versioned/test/versioned_test.rb', line 17

def test_saves_without_revision
  p = pages(:welcome)
  old_versions = p.versions.count

  p.save_without_revision

  p.without_revision do
    p.update_attributes :title => 'changed'
  end

  assert_equal old_versions, p.versions.count
end

#test_should_find_earliest_versionObject


324
325
326
# File 'lib/plugins/acts_as_versioned/test/versioned_test.rb', line 324

def test_should_find_earliest_version
  assert_equal page_versions(:welcome_1), pages(:welcome).versions.earliest
end

#test_should_find_latest_versionObject


328
329
330
# File 'lib/plugins/acts_as_versioned/test/versioned_test.rb', line 328

def test_should_find_latest_version
  assert_equal page_versions(:welcome_2), pages(:welcome).versions.latest
end

#test_should_find_next_versionObject


337
338
339
340
# File 'lib/plugins/acts_as_versioned/test/versioned_test.rb', line 337

def test_should_find_next_version
  assert_equal page_versions(:welcome_2), page_versions(:welcome_1).next
  assert_equal page_versions(:welcome_2), pages(:welcome).versions.after(page_versions(:welcome_1))
end

#test_should_find_previous_versionObject


332
333
334
335
# File 'lib/plugins/acts_as_versioned/test/versioned_test.rb', line 332

def test_should_find_previous_version
  assert_equal page_versions(:welcome_1), page_versions(:welcome_2).previous
  assert_equal page_versions(:welcome_1), pages(:welcome).versions.before(page_versions(:welcome_2))
end

#test_should_find_version_countObject


342
343
344
345
346
# File 'lib/plugins/acts_as_versioned/test/versioned_test.rb', line 342

def test_should_find_version_count
  assert_equal 24, pages(:welcome).versions_count
  assert_equal 24, page_versions(:welcome_1).versions_count
  assert_equal 24, page_versions(:welcome_2).versions_count
end

#test_special_methodsObject


50
51
52
53
54
55
# File 'lib/plugins/acts_as_versioned/test/versioned_test.rb', line 50

def test_special_methods
  assert_nothing_raised { pages(:welcome).feeling_good? }
  assert_nothing_raised { pages(:welcome).versions.first.feeling_good? }
  assert_nothing_raised { locked_pages(:welcome).hello_world }
  assert_nothing_raised { locked_pages(:welcome).versions.first.hello_world }
end

#test_track_altered_attributesObject


215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
# File 'lib/plugins/acts_as_versioned/test/versioned_test.rb', line 215

def test_track_altered_attributes
  p = LockedPage.create! :title => "title"
  assert_equal 1, p.lock_version
  assert_equal 1, p.versions(true).size

  p.title = 'title'
  assert !p.save_version?
  p.save
  assert_equal 2, p.lock_version # still increments version because of optimistic locking
  assert_equal 1, p.versions(true).size

  p.title = 'updated title'
  assert p.save_version?
  p.save
  assert_equal 3, p.lock_version
  assert_equal 1, p.versions(true).size # version 1 deleted

  p.title = 'updated title!'
  assert p.save_version?
  p.save
  assert_equal 4, p.lock_version
  assert_equal 2, p.versions(true).size # version 1 deleted
end

#test_track_altered_attributes_default_valueObject


204
205
206
207
208
# File 'lib/plugins/acts_as_versioned/test/versioned_test.rb', line 204

def test_track_altered_attributes_default_value
  assert !Page.track_altered_attributes
  assert LockedPage.track_altered_attributes
  assert SpecialLockedPage.track_altered_attributes
end

#test_unaltered_attributesObject


314
315
316
317
# File 'lib/plugins/acts_as_versioned/test/versioned_test.rb', line 314

def test_unaltered_attributes
  landmarks(:washington).attributes = landmarks(:washington).attributes.except("id")
  assert !landmarks(:washington).changed?
end

#test_unchanged_string_attributesObject


319
320
321
322
# File 'lib/plugins/acts_as_versioned/test/versioned_test.rb', line 319

def test_unchanged_string_attributes
  landmarks(:washington).attributes = landmarks(:washington).attributes.except("id").inject({}) { |params, (key, value)| params.update(key => value.to_s) }
  assert !landmarks(:washington).changed?
end

#test_version_if_conditionObject


130
131
132
133
134
135
136
137
138
# File 'lib/plugins/acts_as_versioned/test/versioned_test.rb', line 130

def test_version_if_condition
  p = Page.create! :title => "title"
  assert_equal 1, p.version

  Page.feeling_good = false
  p.save
  assert_equal 1, p.version
  Page.feeling_good = true
end

#test_version_if_condition2Object


140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/plugins/acts_as_versioned/test/versioned_test.rb', line 140

def test_version_if_condition2  # set new if condition

  Page.class_eval do
    def new_feeling_good() title[0..0] == 'a'; end
    alias_method :old_feeling_good, :feeling_good?
    alias_method :feeling_good?, :new_feeling_good
  end

  p = Page.create! :title => "title"
  assert_equal 1, p.version # version does not increment
  assert_equal 1, p.versions(true).size

  p.update_attributes(:title => 'new title')
  assert_equal 1, p.version # version does not increment
  assert_equal 1, p.versions(true).size

  p.update_attributes(:title => 'a title')
  assert_equal 2, p.version
  assert_equal 2, p.versions(true).size

  # reset original if condition
  Page.class_eval { alias_method :feeling_good?, :old_feeling_good }
end

#test_version_if_condition_with_blockObject


164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/plugins/acts_as_versioned/test/versioned_test.rb', line 164

def test_version_if_condition_with_block  # set new if condition

  old_condition = Page.version_condition
  Page.version_condition = Proc.new { |page| page.title[0..0] == 'b' }

  p = Page.create! :title => "title"
  assert_equal 1, p.version # version does not increment
  assert_equal 1, p.versions(true).size

  p.update_attributes(:title => 'a title')
  assert_equal 1, p.version # version does not increment
  assert_equal 1, p.versions(true).size

  p.update_attributes(:title => 'b title')
  assert_equal 2, p.version
  assert_equal 2, p.versions(true).size

  # reset original if condition
  Page.version_condition = old_condition
end

#test_version_max_limitObject


194
195
196
197
198
199
200
201
202
# File 'lib/plugins/acts_as_versioned/test/versioned_test.rb', line 194

def test_version_max_limit
  p = LockedPage.create! :title => "title"
  p.update_attributes(:title => "title1")
  p.update_attributes(:title => "title2")
  5.times do |i|
    assert_page_title p, i, :lock_version
    assert p.versions(true).size <= 2, "locked version can only store 2 versions"
  end
end

#test_version_no_limitObject


185
186
187
188
189
190
191
192
# File 'lib/plugins/acts_as_versioned/test/versioned_test.rb', line 185

def test_version_no_limit
  p = Page.create! :title => "title", :body => 'first body'
  p.save
  p.save
  5.times do |i|
    assert_page_title p, i
  end
end

#test_version_orderObject


210
211
212
213
# File 'lib/plugins/acts_as_versioned/test/versioned_test.rb', line 210

def test_version_order
  assert_equal 23, pages(:welcome).versions.first.version
  assert_equal 24, pages(:welcome).versions.last.version
end

#test_versioned_classObject


45
46
47
48
# File 'lib/plugins/acts_as_versioned/test/versioned_test.rb', line 45

def test_versioned_class
  assert_equal Page::Version,                  Page.versioned_class
  assert_equal LockedPage::LockedPageRevision, LockedPage.versioned_class
end

#test_versioned_class_nameObject


40
41
42
43
# File 'lib/plugins/acts_as_versioned/test/versioned_test.rb', line 40

def test_versioned_class_name
  assert_equal 'Version', Page.versioned_class_name
  assert_equal 'LockedPageRevision', LockedPage.versioned_class_name
end

#test_versioned_records_should_belong_to_parentObject


308
309
310
311
312
# File 'lib/plugins/acts_as_versioned/test/versioned_test.rb', line 308

def test_versioned_records_should_belong_to_parent
  page = pages(:welcome)
  page_version = page.versions.last
  assert_equal page, page_version.page
end

#test_with_sequenceObject


267
268
269
270
271
272
# File 'lib/plugins/acts_as_versioned/test/versioned_test.rb', line 267

def test_with_sequence
  assert_equal 'widgets_seq', Widget.versioned_class.sequence_name
  3.times { Widget.create! :name => 'new widget' }
  assert_equal 3, Widget.count
  assert_equal 3, Widget.versioned_class.count
end