/
test_migration.rb
80 lines (57 loc) · 2.6 KB
/
test_migration.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
require File.join(File.dirname(__FILE__), 'helper')
if ActiveRecord::Base.connection.supports_migrations?
class Thing < ActiveRecord::Base
acts_as_soft_deletable
end
class TestMigration < SoftDeleteTestCase
def test_should_create_deleted_table
assert_raises(ActiveRecord::StatementInvalid) { Thing.create :title => 'blah blah' }
migrate_up(3)
t = Thing.create! :title => 'blah blah', :price => 123.45, :type => 'Thing'
assert_model_soft_deletes(t)
migrate_down
assert_raises(ActiveRecord::StatementInvalid) { Thing.create :title => 'blah blah' }
assert_raises(ActiveRecord::StatementInvalid) { Thing::Deleted.find_by_title('blah blah') }
end
def test_should_help_when_adding_columns
assert_raises(ActiveRecord::StatementInvalid) { Thing.create :title => 'blah blah' }
migrate_up(4)
t = Thing.create! :title => 'blah blah', :price => 123.45, :type => 'Thing'
assert_model_soft_deletes(t)
migrate_down
assert_raises(ActiveRecord::StatementInvalid) { Thing.create :title => 'blah blah' }
assert_raises(ActiveRecord::StatementInvalid) { Thing::Deleted.find_by_title('blah blah') }
end
def teardown
migrate_down
rescue ActiveRecord::StatementInvalid
# force attempting to leave test db in a good state
Thing.connection.drop_table "things" rescue nil
Thing.connection.drop_table "deleted_things" rescue nil
#ActiveRecord::Base.connection.initialize_schema_information
ActiveRecord::Base.connection.update "UPDATE schema_info SET version = 1"
ensure
Thing.reset_column_information
Thing::Deleted.reset_column_information
end
private
def migrate_up(version=nil)
ActiveRecord::Migrator.up(File.dirname(__FILE__) + '/fixtures/migrations/', version)
end
def migrate_down(version=nil)
ActiveRecord::Migrator.down(File.dirname(__FILE__) + '/fixtures/migrations/', version)
end
# takes a saved model and runs assertions testing whether soft deleting is working
def assert_model_soft_deletes(model)
klass = model.class
deleted_klass = model.class.deleted_class
assert_raises(ActiveRecord::RecordNotFound) { deleted_klass.find model.id }
model.destroy
assert(deleted = deleted_klass.find(model.id))
assert_raises(ActiveRecord::RecordNotFound) { klass.find model.id }
deleted.undestroy!
assert_soft_delete_models_are_equal deleted, klass.find(model.id)
assert_raises(ActiveRecord::RecordNotFound) { deleted_klass.find model.id }
end
end
end