-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathafter_commit_test.rb
More file actions
78 lines (66 loc) · 2.5 KB
/
after_commit_test.rb
File metadata and controls
78 lines (66 loc) · 2.5 KB
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
$LOAD_PATH.unshift(File.dirname(__FILE__) + '/../lib')
require 'test/unit'
require 'rubygems'
require 'activerecord'
require 'after_commit'
require 'after_commit/active_record'
require 'after_commit/connection_adapters'
ActiveRecord::Base.establish_connection({"adapter" => "sqlite3", "database" => 'test.sqlite3'})
begin
ActiveRecord::Base.connection.execute("drop table mock_records")
ActiveRecord::Base.connection.execute("drop table mock_non_callbacks")
rescue
end
ActiveRecord::Base.connection.execute("create table mock_records(id int)")
ActiveRecord::Base.connection.execute("create table mock_non_callbacks(id int)")
require File.dirname(__FILE__) + '/../init.rb'
class MockNonCallback < ActiveRecord::Base; end
class MockRecord < ActiveRecord::Base
attr_accessor :after_commit_called
attr_accessor :after_commit_on_create_called
attr_accessor :after_commit_on_update_called
attr_accessor :after_commit_on_destroy_called
def clear_flags
@after_commit_called = @after_commit_on_create_called = @after_commit_on_update_called = @after_commit_on_destroy_called = nil
end
after_commit :do_commit
def do_commit
raise "Re-called on commit!" if self.after_commit_called
self.after_commit_called = true
MockNonCallback.transaction{ MockNonCallback.create! }
end
after_commit_on_create :do_create
def do_create
raise "Re-called on create!" if self.after_commit_on_create_called
self.after_commit_on_create_called = true
MockNonCallback.transaction{ MockNonCallback.create! }
end
after_commit_on_update :do_update
def do_update
raise "Re-called on update!" if self.after_commit_on_update_called
self.after_commit_on_update_called = true
MockNonCallback.transaction{ MockNonCallback.create! }
end
after_commit_on_destroy :do_destroy
def do_destroy
raise "Re-called on destroy!" if self.after_commit_on_destroy_called
self.after_commit_on_destroy_called = true
MockNonCallback.transaction{ MockNonCallback.create! }
end
end
class AfterCommitTest < Test::Unit::TestCase
def test_after_commit_on_create_is_called
assert_equal true, MockRecord.create!.after_commit_on_create_called
end
def test_after_commit_on_update_is_called
record = MockRecord.create!
record.clear_flags
record.save
assert_equal true, record.after_commit_on_update_called
end
def test_after_commit_on_destroy_is_called
record = MockRecord.create!
record.clear_flags
assert_equal true, record.destroy.after_commit_on_destroy_called
end
end