/
active_record_persistence_spec.rb
785 lines (624 loc) · 26.6 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
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
require 'spec_helper'
if defined?(ActiveRecord)
Dir[File.dirname(__FILE__) + "/../../models/active_record/*.rb"].sort.each do |f|
require File.expand_path(f)
end
load_schema
# if you want to see the statements while running the spec enable the following line
# require 'logger'
# ActiveRecord::Base.logger = Logger.new(STDERR)
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
allow(gate.class.aasm).to receive(:attribute_name).and_return(column_name.to_sym)
allow(gate.class).to receive(: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_truthy
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_falsey
end
end
end
describe "aasm_guess_enum_method" do
subject { lambda{ gate.send(:aasm_guess_enum_method) } }
before :each do
allow(gate.class.aasm).to receive(: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
allow(WithTrueEnum.aasm).to receive(: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
allow(Gate.aasm).to receive(:attribute_name).and_return(:value)
end
context "when AASM column looks like enum" do
before :each do
allow(gate).to receive(: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
allow(gate).to receive(: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
if ActiveRecord::VERSION::MAJOR >= 4 && ActiveRecord::VERSION::MINOR >= 1 # won't work with Rails <= 4.1
# Enum are introduced from Rails 4.1, therefore enum syntax will not work on Rails <= 4.1
context "when AASM enum setting is not enabled and aasm column not present" do
let(:with_enum_without_column) {WithEnumWithoutColumn.new}
it "should raise NoMethodError for transitions" do
expect{with_enum_without_column.send(:view)}.to raise_error(NoMethodError, /undefined method .status./)
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
allow(gate).to receive(:aasm_enum).and_return(enum_name)
allow(gate).to receive(:aasm_write_state_attribute)
allow(gate).to receive(:write_attribute)
allow(Gate).to receive(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
allow(gate).to receive(: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)
allow(Gate).to receive_message_chain(:unscoped, :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
it "searches model outside of default_scope when 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.
unscoped = double(Object, update_all: 1)
scoped = double(Object, update_all: 1)
allow(Gate).to receive(:unscoped).and_return(unscoped)
allow(Gate).to receive(:where).and_return(scoped)
allow(unscoped).to receive(:where).and_return(unscoped)
gate.aasm_write_state state_sym
expect(unscoped).to have_received(:update_all)
.with(Hash[gate.class.aasm.attribute_name, state_code])
expect(scoped).to_not 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
allow(gate).to receive(:save).and_return(true)
gate.aasm_write_state state_sym
expect(gate).to have_received(:aasm_write_state_attribute).with(state_sym, :default)
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_state_attribute).with(state_sym, :default)
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
allow(gate).to receive(: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
allow(gate).to receive(:write_attribute)
allow(gate).to receive(:aasm_raw_attribute_value).and_return(value)
gate.send(:aasm_write_state_attribute, sym)
end
it "generates attribute value using a helper method" do
expect(gate).to have_received(:aasm_raw_attribute_value).with(sym, :default)
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
context 'on initialization' do
it "should initialize the aasm state" do
expect(Gate.new.aasm_state).to eql 'opened'
expect(Gate.new.aasm.current_state).to eql :opened
end
it "should not initialize the aasm state if it has not been loaded" do
# we have to create a gate in the database, for which we only want to
# load the id, and not the state
gate = Gate.create!
# then we just load the gate ids
Gate.select(:id).where(id: gate.id).first
end
end
end
if ActiveRecord::VERSION::MAJOR < 4 && ActiveRecord::VERSION::MINOR < 2 # won't work with Rails >= 4.2
describe "direct state column access" do
it "accepts false states" do
f = FalseState.create!
expect(f.aasm_state).to eql false
expect {
f.aasm.events.map(&:name)
}.to_not raise_error
end
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 for each state" do
expect(SimpleNewDsl).to respond_to(:unknown_scope)
expect(SimpleNewDsl).to respond_to(:another_unknown_scope)
expect(SimpleNewDsl.unknown_scope.is_a?(ActiveRecord::Relation)).to be_truthy
expect(SimpleNewDsl.another_unknown_scope.is_a?(ActiveRecord::Relation)).to be_truthy
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
# Scopes on abstract classes didn't work until Rails 5.
#
# Reference:
# https://github.com/rails/rails/issues/10658
if ActiveRecord::VERSION::MAJOR >= 5
context "For a descendant of an abstract model" do
it "should add the scope without the table_name" do
expect(ImplementedAbstractClassDsl).to respond_to(:unknown_scope)
expect(ImplementedAbstractClassDsl).to respond_to(:another_unknown_scope)
expect(ImplementedAbstractClassDsl.unknown_scope.is_a?(ActiveRecord::Relation)).to be_truthy
expect(ImplementedAbstractClassDsl.another_unknown_scope.is_a?(ActiveRecord::Relation)).to be_truthy
end
end
end
it "does not create scopes if requested" do
expect(NoScope).not_to respond_to(:pending)
end
context "result of scope" do
let!(:dsl1) { SimpleNewDsl.create!(status: :new) }
let!(:dsl2) { SimpleNewDsl.create!(status: :unknown_scope) }
after do
SimpleNewDsl.destroy_all
end
it "created scope works as where(name: :scope_name)" do
expect(SimpleNewDsl.unknown_scope).to contain_exactly(dsl2)
end
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
it 'can be turned off and on again' 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
# allow it temporarily
NoDirectAssignment.aasm.state_machine.config.no_direct_assignment = false
obj.aasm_state = :running
expect(obj.aasm_state.to_sym).to eql :running
# and forbid it again
NoDirectAssignment.aasm.state_machine.config.no_direct_assignment = true
expect {obj.aasm_state = :pending}.to raise_error(AASM::NoDirectAssignmentError)
expect(obj.aasm_state.to_sym).to eql :running
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 raise_error(ActiveRecord::RecordInvalid)
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_truthy
expect(validator).to be_running
validator.reload
expect(validator).to be_running
expect(validator).not_to be_sleeping
end
it 'should not store states for invalid models silently if configured' do
validator = SilentPersistor.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_falsey
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_truthy
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_truthy
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 'pessimistic locking' do
let(:worker) { Worker.create!(:name => 'worker', :status => 'sleeping') }
subject { transactor.run! }
context 'no lock' do
let(:transactor) { NoLockTransactor.create!(:name => 'no_lock_transactor', :worker => worker) }
it 'should not invoke lock!' do
expect(transactor).to_not receive(:lock!)
subject
end
end
context 'a default lock' do
let(:transactor) { LockTransactor.create!(:name => 'lock_transactor', :worker => worker) }
it 'should invoke lock! with true' do
expect(transactor).to receive(:lock!).with(true).and_call_original
subject
end
end
context 'a FOR UPDATE NOWAIT lock' do
let(:transactor) { LockNoWaitTransactor.create!(:name => 'lock_no_wait_transactor', :worker => worker) }
it 'should invoke lock! with FOR UPDATE NOWAIT' do
expect(transactor).to receive(:lock!).with('FOR UPDATE NOWAIT').and_call_original
subject
end
end
end
describe 'without transactions' do
let(:worker) { Worker.create!(:name => 'worker', :status => 'sleeping') }
let(:no_transactor) { NoTransactor.create!(:name => 'transactor', :worker => worker) }
it 'should not rollback all changes' do
expect(no_transactor).to be_sleeping
expect(worker.status).to eq('sleeping')
expect {no_transactor.run!}.to raise_error(StandardError, 'failed on purpose')
expect(no_transactor).to be_running
expect(worker.reload.status).to eq('running')
end
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::StateMachineStore[Transactor][:default].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).to eq("name changed")
validator.sleep!("sleeper")
expect(validator).to be_sleeping
expect(validator.name).to eq("sleeper")
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 :after_commit if validation failed when saving object" do
validator = Validator.create(:name => 'name')
validator.invalid = true
expect { validator.run! }.to raise_error(ActiveRecord::RecordInvalid, 'Invalid record')
expect(validator).to be_sleeping
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
describe 'before and after transaction callbacks' do
[:after, :before].each do |event_type|
describe "#{event_type}_transaction callback" do
it "should fire :#{event_type}_transaction if transaction was successful" do
validator = Validator.create(:name => 'name')
expect(validator).to be_sleeping
expect { validator.run! }.to change { validator.send("#{event_type}_transaction_performed_on_run") }.from(nil).to(true)
expect(validator).to be_running
end
it "should fire :#{event_type}_transaction if transaction failed" do
validator = Validator.create(:name => 'name')
expect do
begin
validator.fail!
rescue => ignored
end
end.to change { validator.send("#{event_type}_transaction_performed_on_fail") }.from(nil).to(true)
expect(validator).to_not be_running
end
it "should not fire :#{event_type}_transaction if not saving" do
validator = Validator.create(:name => 'name')
expect(validator).to be_sleeping
expect { validator.run }.to_not change { validator.send("#{event_type}_transaction_performed_on_run") }
expect(validator).to be_running
expect(validator.name).to eq("name")
end
end
end
end
describe 'before and after all transactions callbacks' do
[:after, :before].each do |event_type|
describe "#{event_type}_all_transactions callback" do
it "should fire :#{event_type}_all_transactions if transaction was successful" do
validator = Validator.create(:name => 'name')
expect(validator).to be_sleeping
expect { validator.run! }.to change { validator.send("#{event_type}_all_transactions_performed") }.from(nil).to(true)
expect(validator).to be_running
end
it "should fire :#{event_type}_all_transactions if transaction failed" do
validator = Validator.create(:name => 'name')
expect do
begin
validator.fail!
rescue => ignored
end
end.to change { validator.send("#{event_type}_all_transactions_performed") }.from(nil).to(true)
expect(validator).to_not be_running
end
it "should not fire :#{event_type}_all_transactions if not saving" do
validator = Validator.create(:name => 'name')
expect(validator).to be_sleeping
expect { validator.run }.to_not change { validator.send("#{event_type}_all_transactions_performed") }
expect(validator).to be_running
expect(validator.name).to eq("name")
end
end
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_falsey
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_truthy
persistor.reload
expect(persistor.status).to eq('invalid_state')
end
end
describe 'basic example with two state machines' do
let(:example) { BasicActiveRecordTwoStateMachinesExample.new }
it 'should initialise properly' do
expect(example.aasm(:search).current_state).to eql :initialised
expect(example.aasm(:sync).current_state).to eql :unsynced
end
end
describe 'testing the README examples' do
it 'Usage' do
job = ReadmeJob.new
expect(job.sleeping?).to eql true
expect(job.may_run?).to eql true
job.run
expect(job.running?).to eql true
expect(job.sleeping?).to eql false
expect(job.may_run?).to eql false
expect { job.run }.to raise_error(AASM::InvalidTransition)
end
end
describe 'testing the instance_level skip validation with _without_validation method' do
let(:example) do
obj = InstanceLevelSkipValidationExample.new(state: 'new')
obj.save(validate: false)
obj
end
it 'should be able to change the state with invalid record' do
expect(example.valid?).to be_falsey
expect(example.complete!).to be_falsey
expect(example.complete_without_validation!).to be_truthy
expect(example.state).to eq('complete')
end
it 'shouldn\'t affect the behaviour of existing method after calling _without_validation! method' do
expect(example.set_draft!).to be_falsey
expect(example.set_draft_without_validation!).to be_truthy
expect(example.state).to eq('draft')
expect(example.complete!).to be_falsey
end
end
describe 'testing the timestamps option' do
let(:example) { TimestampExample.create! }
it 'should update existing timestamp columns' do
expect { example.open! }.to change { example.reload.opened_at }.from(nil).to(instance_of(::Time))
end
it 'should not fail if there is no corresponding timestamp column' do
expect { example.close! }.to change { example.reload.aasm_state }
end
end
end