-
Notifications
You must be signed in to change notification settings - Fork 629
/
active_record_persistence_spec.rb
485 lines (385 loc) · 14.2 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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
require 'active_record'
require 'logger'
require 'spec_helper'
load_schema
# if you want to see the statements while running the spec enable the following line
# ActiveRecord::Base.logger = Logger.new(STDERR)
shared_examples_for "aasm model" do
it "should include persistence mixins" do
expect(klass.included_modules).to be_include(AASM::Persistence::ActiveRecordPersistence)
expect(klass.included_modules).to be_include(AASM::Persistence::ActiveRecordPersistence::InstanceMethods)
end
end
describe "instance methods" do
let(:gate) {Gate.new}
it "should respond to aasm persistence methods" do
expect(gate).to respond_to(:aasm_read_state)
expect(gate).to respond_to(:aasm_write_state)
expect(gate).to respond_to(:aasm_write_state_without_persistence)
end
describe "aasm_column_looks_like_enum" do
subject { lambda{ gate.send(:aasm_column_looks_like_enum) } }
let(:column_name) { "value" }
let(:columns_hash) { Hash[column_name, column] }
before :each do
gate.class.aasm.stub(:attribute_name).and_return(column_name.to_sym)
gate.class.stub(:columns_hash).and_return(columns_hash)
end
context "when AASM column has integer type" do
let(:column) { double(Object, type: :integer) }
it "returns true" do
expect(subject.call).to be_true
end
end
context "when AASM column has string type" do
let(:column) { double(Object, type: :string) }
it "returns false" do
expect(subject.call).to be_false
end
end
end
describe "aasm_guess_enum_method" do
subject { lambda{ gate.send(:aasm_guess_enum_method) } }
before :each do
gate.class.aasm.stub(:attribute_name).and_return(:value)
end
it "pluralizes AASM column name" do
expect(subject.call).to eq :values
end
end
describe "aasm_enum" do
context "when AASM enum setting contains an explicit enum method name" do
let(:with_enum) { WithEnum.new }
it "returns whatever value was set in AASM config" do
expect(with_enum.send(:aasm_enum)).to eq :test
end
end
context "when AASM enum setting is simply set to true" do
let(:with_true_enum) { WithTrueEnum.new }
before :each do
WithTrueEnum.aasm.stub(:attribute_name).and_return(:value)
end
it "infers enum method name from pluralized column name" do
expect(with_true_enum.send(:aasm_enum)).to eq :values
end
end
context "when AASM enum setting is explicitly disabled" do
let(:with_false_enum) { WithFalseEnum.new }
it "returns nil" do
expect(with_false_enum.send(:aasm_enum)).to be_nil
end
end
context "when AASM enum setting is not enabled" do
before :each do
Gate.aasm.stub(:attribute_name).and_return(:value)
end
context "when AASM column looks like enum" do
before :each do
gate.stub(:aasm_column_looks_like_enum).and_return(true)
end
it "infers enum method name from pluralized column name" do
expect(gate.send(:aasm_enum)).to eq :values
end
end
context "when AASM column doesn't look like enum'" do
before :each do
gate.stub(:aasm_column_looks_like_enum)
.and_return(false)
end
it "returns nil, as we're not using enum" do
expect(gate.send(:aasm_enum)).to be_nil
end
end
end
end
context "when AASM is configured to use enum" do
let(:state_sym) { :running }
let(:state_code) { 2 }
let(:enum_name) { :states }
let(:enum) { Hash[state_sym, state_code] }
before :each do
gate
.stub(:aasm_enum)
.and_return(enum_name)
gate.stub(:aasm_write_attribute)
gate.stub(:write_attribute)
gate
.class
.stub(enum_name)
.and_return(enum)
end
describe "aasm_write_state" do
context "when AASM is configured to skip validations on save" do
before :each do
gate
.stub(:aasm_skipping_validations)
.and_return(true)
end
it "passes state code instead of state symbol to update_all" do
# stub_chain does not allow us to give expectations on call
# parameters in the middle of the chain, so we need to use
# intermediate object instead.
obj = double(Object, update_all: 1)
gate
.class
.stub(:where)
.and_return(obj)
gate.aasm_write_state state_sym
expect(obj).to have_received(:update_all)
.with(Hash[gate.class.aasm.attribute_name, state_code])
end
end
context "when AASM is not skipping validations" do
it "delegates state update to the helper method" do
# Let's pretend that validation is passed
gate.stub(:save).and_return(true)
gate.aasm_write_state state_sym
expect(gate).to have_received(:aasm_write_attribute).with(state_sym)
expect(gate).to_not have_received :write_attribute
end
end
end
describe "aasm_write_state_without_persistence" do
it "delegates state update to the helper method" do
gate.aasm_write_state_without_persistence state_sym
expect(gate).to have_received(:aasm_write_attribute).with(state_sym)
expect(gate).to_not have_received :write_attribute
end
end
describe "aasm_raw_attribute_value" do
it "converts state symbol to state code" do
expect(gate.send(:aasm_raw_attribute_value, state_sym))
.to eq state_code
end
end
end
context "when AASM is configured to use string field" do
let(:state_sym) { :running }
before :each do
gate
.stub(:aasm_enum)
.and_return(nil)
end
describe "aasm_raw_attribute_value" do
it "converts state symbol to string" do
expect(gate.send(:aasm_raw_attribute_value, state_sym))
.to eq state_sym.to_s
end
end
end
describe "aasm_write_attribute helper method" do
let(:sym) { :sym }
let(:value) { 42 }
before :each do
gate.stub(:write_attribute)
gate.stub(:aasm_raw_attribute_value)
.and_return(value)
gate.send(:aasm_write_attribute, sym)
end
it "generates attribute value using a helper method" do
expect(gate).to have_received(:aasm_raw_attribute_value).with(sym)
end
it "writes attribute to the model" do
expect(gate).to have_received(:write_attribute).with(:aasm_state, value)
end
end
it "should return the initial state when new and the aasm field is nil" do
expect(gate.aasm.current_state).to eq(:opened)
end
it "should return the aasm column when new and the aasm field is not nil" do
gate.aasm_state = "closed"
expect(gate.aasm.current_state).to eq(:closed)
end
it "should return the aasm column when not new and the aasm.attribute_name is not nil" do
allow(gate).to receive(:new_record?).and_return(false)
gate.aasm_state = "state"
expect(gate.aasm.current_state).to eq(:state)
end
it "should allow a nil state" do
allow(gate).to receive(:new_record?).and_return(false)
gate.aasm_state = nil
expect(gate.aasm.current_state).to be_nil
end
it "should initialize the aasm state on instantiation" do
expect(Gate.new.aasm_state).to eql 'opened'
expect(Gate.new.aasm.current_state).to eql :opened
end
end
describe 'subclasses' do
it "should have the same states as its parent class" do
expect(DerivateNewDsl.aasm.states).to eq(SimpleNewDsl.aasm.states)
end
it "should have the same events as its parent class" do
expect(DerivateNewDsl.aasm.events).to eq(SimpleNewDsl.aasm.events)
end
it "should have the same column as its parent even for the new dsl" do
expect(SimpleNewDsl.aasm.attribute_name).to eq(:status)
expect(DerivateNewDsl.aasm.attribute_name).to eq(:status)
end
end
describe "named scopes with the new DSL" do
context "Does not already respond_to? the scope name" do
it "should add a scope" do
expect(SimpleNewDsl).to respond_to(:unknown_scope)
expect(SimpleNewDsl.unknown_scope.is_a?(ActiveRecord::Relation)).to be_true
end
end
context "Already respond_to? the scope name" do
it "should not add a scope" do
expect(SimpleNewDsl).to respond_to(:new)
expect(SimpleNewDsl.new.class).to eq(SimpleNewDsl)
end
end
it "does not create scopes if requested" do
expect(NoScope).not_to respond_to(:pending)
end
end # scopes
describe "direct assignment" do
it "is allowed by default" do
obj = NoScope.create
expect(obj.aasm_state.to_sym).to eql :pending
obj.aasm_state = :running
expect(obj.aasm_state.to_sym).to eql :running
end
it "is forbidden if configured" do
obj = NoDirectAssignment.create
expect(obj.aasm_state.to_sym).to eql :pending
expect {obj.aasm_state = :running}.to raise_error(AASM::NoDirectAssignmentError)
expect(obj.aasm_state.to_sym).to eql :pending
end
end # direct assignment
describe 'initial states' do
it 'should support conditions' do
expect(Thief.new(:skilled => true).aasm.current_state).to eq(:rich)
expect(Thief.new(:skilled => false).aasm.current_state).to eq(:jailed)
end
end
describe 'transitions with persistence' do
it "should work for valid models" do
valid_object = Validator.create(:name => 'name')
expect(valid_object).to be_sleeping
valid_object.status = :running
expect(valid_object).to be_running
end
it 'should not store states for invalid models' do
validator = Validator.create(:name => 'name')
expect(validator).to be_valid
expect(validator).to be_sleeping
validator.name = nil
expect(validator).not_to be_valid
expect(validator.run!).to be_false
expect(validator).to be_sleeping
validator.reload
expect(validator).not_to be_running
expect(validator).to be_sleeping
validator.name = 'another name'
expect(validator).to be_valid
expect(validator.run!).to be_true
expect(validator).to be_running
validator.reload
expect(validator).to be_running
expect(validator).not_to be_sleeping
end
it 'should store states for invalid models if configured' do
persistor = InvalidPersistor.create(:name => 'name')
expect(persistor).to be_valid
expect(persistor).to be_sleeping
persistor.name = nil
expect(persistor).not_to be_valid
expect(persistor.run!).to be_true
expect(persistor).to be_running
persistor = InvalidPersistor.find(persistor.id)
persistor.valid?
expect(persistor).to be_valid
expect(persistor).to be_running
expect(persistor).not_to be_sleeping
persistor.reload
expect(persistor).to be_running
expect(persistor).not_to be_sleeping
end
describe 'transactions' do
let(:worker) { Worker.create!(:name => 'worker', :status => 'sleeping') }
let(:transactor) { Transactor.create!(:name => 'transactor', :worker => worker) }
it 'should rollback all changes' do
expect(transactor).to be_sleeping
expect(worker.status).to eq('sleeping')
expect {transactor.run!}.to raise_error(StandardError, 'failed on purpose')
expect(transactor).to be_running
expect(worker.reload.status).to eq('sleeping')
end
context "nested transactions" do
it "should rollback all changes in nested transaction" do
expect(transactor).to be_sleeping
expect(worker.status).to eq('sleeping')
Worker.transaction do
expect { transactor.run! }.to raise_error(StandardError, 'failed on purpose')
end
expect(transactor).to be_running
expect(worker.reload.status).to eq('sleeping')
end
it "should only rollback changes in the main transaction not the nested one" do
# change configuration to not require new transaction
AASM::StateMachine[Transactor].config.requires_new_transaction = false
expect(transactor).to be_sleeping
expect(worker.status).to eq('sleeping')
Worker.transaction do
expect { transactor.run! }.to raise_error(StandardError, 'failed on purpose')
end
expect(transactor).to be_running
expect(worker.reload.status).to eq('running')
end
end
describe "after_commit callback" do
it "should fire :after_commit if transaction was successful" do
validator = Validator.create(:name => 'name')
expect(validator).to be_sleeping
validator.run!
expect(validator).to be_running
expect(validator.name).not_to eq("name")
end
it "should not fire :after_commit if transaction failed" do
validator = Validator.create(:name => 'name')
expect { validator.fail! }.to raise_error(StandardError, 'failed on purpose')
expect(validator.name).to eq("name")
end
it "should not fire if not saving" do
validator = Validator.create(:name => 'name')
expect(validator).to be_sleeping
validator.run
expect(validator).to be_running
expect(validator.name).to eq("name")
end
end
context "when not persisting" do
it 'should not rollback all changes' do
expect(transactor).to be_sleeping
expect(worker.status).to eq('sleeping')
# Notice here we're calling "run" and not "run!" with a bang.
expect {transactor.run}.to raise_error(StandardError, 'failed on purpose')
expect(transactor).to be_running
expect(worker.reload.status).to eq('running')
end
it 'should not create a database transaction' do
expect(transactor.class).not_to receive(:transaction)
expect {transactor.run}.to raise_error(StandardError, 'failed on purpose')
end
end
end
end
describe "invalid states with persistence" do
it "should not store states" do
validator = Validator.create(:name => 'name')
validator.status = 'invalid_state'
expect(validator.save).to be_false
expect {validator.save!}.to raise_error(ActiveRecord::RecordInvalid)
validator.reload
expect(validator).to be_sleeping
end
it "should store invalid states if configured" do
persistor = InvalidPersistor.create(:name => 'name')
persistor.status = 'invalid_state'
expect(persistor.save).to be_true
persistor.reload
expect(persistor.status).to eq('invalid_state')
end
end