Class: VersionedTest

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

Instance Method Summary collapse

Instance Method Details

#test_association_optionsObject



269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
# File 'lib/acts_as_versioned/test/versioned_test.rb', line 269

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

  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



244
245
246
# File 'lib/acts_as_versioned/test/versioned_test.rb', line 244

def test_find_version
  assert_equal page_versions(:welcome_1), pages(:welcome).versions.find_by_version(23)
end

#test_find_versionsObject



240
241
242
# File 'lib/acts_as_versioned/test/versioned_test.rb', line 240

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

#test_has_many_throughObject



255
256
257
# File 'lib/acts_as_versioned/test/versioned_test.rb', line 255

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

#test_has_many_through_with_custom_associationObject



259
260
261
# File 'lib/acts_as_versioned/test/versioned_test.rb', line 259

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

#test_if_changed_creates_version_if_a_listed_column_is_changedObject



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

def test_if_changed_creates_version_if_a_listed_column_is_changed
  landmarks(:washington).name = "Washington"
  assert landmarks(:washington).changed?
  assert landmarks(:washington).altered?
end

#test_if_changed_creates_version_if_all_listed_columns_are_changedObject



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

def test_if_changed_creates_version_if_all_listed_columns_are_changed
  landmarks(:washington).name = "Washington"
  landmarks(:washington).latitude = 1.0
  landmarks(:washington).longitude = 1.0
  assert landmarks(:washington).changed?
  assert landmarks(:washington).altered?
end

#test_if_changed_does_not_create_new_version_if_unlisted_column_is_changedObject



340
341
342
343
344
# File 'lib/acts_as_versioned/test/versioned_test.rb', line 340

def test_if_changed_does_not_create_new_version_if_unlisted_column_is_changed
  landmarks(:washington).doesnt_trigger_version = "This should not trigger version"
  assert landmarks(:washington).changed?
  assert !landmarks(:washington).altered?
end

#test_lock_version_works_with_versioningObject



116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/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



263
264
265
266
267
# File 'lib/acts_as_versioned/test/versioned_test.rb', line 263

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/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/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.find_by_version(23)), "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/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/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!(23), "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/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.lock_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/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.lock_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/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/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/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/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



304
305
306
# File 'lib/acts_as_versioned/test/versioned_test.rb', line 304

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

#test_should_find_latest_versionObject



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

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

#test_should_find_next_versionObject



317
318
319
320
# File 'lib/acts_as_versioned/test/versioned_test.rb', line 317

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



312
313
314
315
# File 'lib/acts_as_versioned/test/versioned_test.rb', line 312

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



322
323
324
# File 'lib/acts_as_versioned/test/versioned_test.rb', line 322

def test_should_find_version_count
  assert_equal 2, pages(:welcome).versions.size
end

#test_special_methodsObject



50
51
52
53
54
55
# File 'lib/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



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

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

  p.body = 'whoa'
  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



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

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



294
295
296
297
# File 'lib/acts_as_versioned/test/versioned_test.rb', line 294

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

#test_unchanged_string_attributesObject



299
300
301
302
# File 'lib/acts_as_versioned/test/versioned_test.rb', line 299

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/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/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.count

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

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

  # 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/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.count

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

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

  # reset original if condition
  Page.version_condition = old_condition
end

#test_version_max_limitObject



197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/acts_as_versioned/test/versioned_test.rb', line 197

def test_version_max_limit
  p = LockedPage.create! :title => "title"
  p.update_attributes(:title => "title1")
  p.update_attributes(:title => "title2")
  5.times do |i|
    p.title = "title#{i}"
    p.save
    assert_equal "title#{i}", p.title
    assert_equal (i+4), p.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
193
194
195
# File 'lib/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|
    p.title = "title#{i}"
    p.save
    assert_equal "title#{i}", p.title
    assert_equal (i+2), p.version
  end
end

#test_versioned_classObject



45
46
47
48
# File 'lib/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/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



288
289
290
291
292
# File 'lib/acts_as_versioned/test/versioned_test.rb', line 288

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



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

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

#test_without_locking_reverts_optimistic_locking_settings_if_block_raises_exceptionObject



362
363
364
365
366
367
368
369
# File 'lib/acts_as_versioned/test/versioned_test.rb', line 362

def test_without_locking_reverts_optimistic_locking_settings_if_block_raises_exception
  assert_raises(RuntimeError) do
    LockedPage.without_locking do
      raise RuntimeError, "oh noes"
    end
  end
  assert ActiveRecord::Base.lock_optimistically
end

#test_without_locking_temporarily_disables_optimistic_lockingObject



346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
# File 'lib/acts_as_versioned/test/versioned_test.rb', line 346

def test_without_locking_temporarily_disables_optimistic_locking
  enabled1 = false
  block_called = false
  
  ActiveRecord::Base.lock_optimistically = true
  LockedPage.without_locking do
    enabled1 = ActiveRecord::Base.lock_optimistically
    block_called = true
  end
  enabled2 = ActiveRecord::Base.lock_optimistically
  
  assert block_called
  assert !enabled1
  assert enabled2
end