-
Notifications
You must be signed in to change notification settings - Fork 7
/
active_record_persistence_spec.rb
252 lines (213 loc) · 7.95 KB
/
active_record_persistence_spec.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
81
82
83
84
85
86
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
begin
require 'rubygems'
require 'active_record'
# A dummy class for mocking the activerecord connection class
class Connection
end
class FooBar < ActiveRecord::Base
include AASM
# Fake this column for testing purposes
attr_accessor :aasm_state
aasm_state :open
aasm_state :closed
aasm_event :view do
transitions :to => :read, :from => [:needs_attention]
end
end
class Fi < ActiveRecord::Base
def aasm_read_state
"fi"
end
include AASM
end
class Fo < ActiveRecord::Base
def aasm_write_state(state)
"fo"
end
include AASM
end
class Fum < ActiveRecord::Base
def aasm_write_state_without_persistence(state)
"fum"
end
include AASM
end
class June < ActiveRecord::Base
include AASM
aasm_column :status
end
class Beaver < June
end
class Thief < ActiveRecord::Base
include AASM
aasm_initial_state Proc.new { |thief| thief.skilled ? :rich : :jailed }
aasm_state :rich
aasm_state :jailed
attr_accessor :skilled, :aasm_state
end
describe "aasm model", :shared => true do
it "should include AASM::Persistence::ActiveRecordPersistence" do
@klass.included_modules.should be_include(AASM::Persistence::ActiveRecordPersistence)
end
it "should include AASM::Persistence::ActiveRecordPersistence::InstanceMethods" do
@klass.included_modules.should be_include(AASM::Persistence::ActiveRecordPersistence::InstanceMethods)
end
end
describe FooBar, "class methods" do
before(:each) do
@klass = FooBar
end
it_should_behave_like "aasm model"
it "should include AASM::Persistence::ActiveRecordPersistence::ReadState" do
@klass.included_modules.should be_include(AASM::Persistence::ActiveRecordPersistence::ReadState)
end
it "should include AASM::Persistence::ActiveRecordPersistence::WriteState" do
@klass.included_modules.should be_include(AASM::Persistence::ActiveRecordPersistence::WriteState)
end
it "should include AASM::Persistence::ActiveRecordPersistence::WriteStateWithoutPersistence" do
@klass.included_modules.should be_include(AASM::Persistence::ActiveRecordPersistence::WriteStateWithoutPersistence)
end
end
describe Fi, "class methods" do
before(:each) do
@klass = Fi
end
it_should_behave_like "aasm model"
it "should not include AASM::Persistence::ActiveRecordPersistence::ReadState" do
@klass.included_modules.should_not be_include(AASM::Persistence::ActiveRecordPersistence::ReadState)
end
it "should include AASM::Persistence::ActiveRecordPersistence::WriteState" do
@klass.included_modules.should be_include(AASM::Persistence::ActiveRecordPersistence::WriteState)
end
it "should include AASM::Persistence::ActiveRecordPersistence::WriteStateWithoutPersistence" do
@klass.included_modules.should be_include(AASM::Persistence::ActiveRecordPersistence::WriteStateWithoutPersistence)
end
end
describe Fo, "class methods" do
before(:each) do
@klass = Fo
end
it_should_behave_like "aasm model"
it "should include AASM::Persistence::ActiveRecordPersistence::ReadState" do
@klass.included_modules.should be_include(AASM::Persistence::ActiveRecordPersistence::ReadState)
end
it "should not include AASM::Persistence::ActiveRecordPersistence::WriteState" do
@klass.included_modules.should_not be_include(AASM::Persistence::ActiveRecordPersistence::WriteState)
end
it "should include AASM::Persistence::ActiveRecordPersistence::WriteStateWithoutPersistence" do
@klass.included_modules.should be_include(AASM::Persistence::ActiveRecordPersistence::WriteStateWithoutPersistence)
end
end
describe Fum, "class methods" do
before(:each) do
@klass = Fum
end
it_should_behave_like "aasm model"
it "should include AASM::Persistence::ActiveRecordPersistence::ReadState" do
@klass.included_modules.should be_include(AASM::Persistence::ActiveRecordPersistence::ReadState)
end
it "should include AASM::Persistence::ActiveRecordPersistence::WriteState" do
@klass.included_modules.should be_include(AASM::Persistence::ActiveRecordPersistence::WriteState)
end
it "should not include AASM::Persistence::ActiveRecordPersistence::WriteStateWithoutPersistence" do
@klass.included_modules.should_not be_include(AASM::Persistence::ActiveRecordPersistence::WriteStateWithoutPersistence)
end
end
describe FooBar, "instance methods" do
before(:each) do
connection = mock(Connection, :columns => [])
FooBar.stub!(:connection).and_return(connection)
end
it "should respond to aasm read state when not previously defined" do
FooBar.new.should respond_to(:aasm_read_state)
end
it "should respond to aasm write state when not previously defined" do
FooBar.new.should respond_to(:aasm_write_state)
end
it "should respond to aasm write state without persistence when not previously defined" do
FooBar.new.should respond_to(:aasm_write_state_without_persistence)
end
it "should return the initial state when new and the aasm field is nil" do
FooBar.new.aasm_current_state.should == :open
end
it "should return the aasm column when new and the aasm field is not nil" do
foo = FooBar.new
foo.aasm_state = "closed"
foo.aasm_current_state.should == :closed
end
it "should return the aasm column when not new and the aasm_column is not nil" do
foo = FooBar.new
foo.stub!(:new_record?).and_return(false)
foo.aasm_state = "state"
foo.aasm_current_state.should == :state
end
it "should allow a nil state" do
foo = FooBar.new
foo.stub!(:new_record?).and_return(false)
foo.aasm_state = nil
foo.aasm_current_state.should be_nil
end
it "should have aasm_ensure_initial_state" do
foo = FooBar.new
foo.send :aasm_ensure_initial_state
end
it "should call aasm_ensure_initial_state on validation before create" do
foo = FooBar.new
foo.should_receive(:aasm_ensure_initial_state).and_return(true)
foo.valid?
end
it "should call aasm_ensure_initial_state on validation before create" do
foo = FooBar.new
foo.stub!(:new_record?).and_return(false)
foo.should_not_receive(:aasm_ensure_initial_state)
foo.valid?
end
end
describe 'Beavers' do
it "should have the same states as it's parent" do
Beaver.aasm_states.should == June.aasm_states
end
it "should have the same events as it's parent" do
Beaver.aasm_events.should == June.aasm_events
end
it "should have the same column as it's parent" do
Beaver.aasm_column.should == :status
end
end
describe AASM::Persistence::ActiveRecordPersistence::NamedScopeMethods do
class NamedScopeExample < ActiveRecord::Base
include AASM
end
context "Does not already respond_to? the scope name" do
it "should add a scope" do
NamedScopeExample.should_receive(:scope)
NamedScopeExample.aasm_state :unknown_scope
end
end
context "Already respond_to? the scope name" do
it "should not add a scope" do
NamedScopeExample.should_not_receive(:scope)
NamedScopeExample.aasm_state :new
end
end
end
describe 'Thieves' do
before(:each) do
connection = mock(Connection, :columns => [])
Thief.stub!(:connection).and_return(connection)
end
it 'should be rich if they\'re skilled' do
Thief.new(:skilled => true).aasm_current_state.should == :rich
end
it 'should be jailed if they\'re unskilled' do
Thief.new(:skilled => false).aasm_current_state.should == :jailed
end
end
# TODO: figure out how to test ActiveRecord reload! without a database
rescue LoadError => e
if e.message == "no such file to load -- active_record"
puts "You must install active record to run this spec. Install with sudo gem install activerecord"
else
raise
end
end