aasm 4.0.8 → 4.5.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.travis.yml +7 -2
- data/CHANGELOG.md +43 -2
- data/CODE_OF_CONDUCT.md +13 -0
- data/Gemfile +6 -3
- data/LICENSE +1 -1
- data/PLANNED_CHANGES.md +8 -4
- data/README.md +171 -14
- data/aasm.gemspec +1 -5
- data/gemfiles/rails_3.2.gemfile +3 -2
- data/gemfiles/rails_4.0.gemfile +2 -6
- data/gemfiles/rails_4.0_mongo_mapper.gemfile +14 -0
- data/gemfiles/rails_4.1.gemfile +2 -6
- data/gemfiles/rails_4.1_mongo_mapper.gemfile +14 -0
- data/gemfiles/rails_4.2.gemfile +1 -5
- data/gemfiles/rails_4.2_mongo_mapper.gemfile +14 -0
- data/gemfiles/rails_4.2_mongoid_5.gemfile +12 -0
- data/lib/aasm/aasm.rb +69 -36
- data/lib/aasm/base.rb +51 -21
- data/lib/aasm/core/event.rb +7 -6
- data/lib/aasm/core/state.rb +11 -8
- data/lib/aasm/core/transition.rb +7 -5
- data/lib/aasm/errors.rb +15 -1
- data/lib/aasm/instance_base.rb +17 -13
- data/lib/aasm/localizer.rb +1 -1
- data/lib/aasm/persistence/active_record_persistence.rb +63 -73
- data/lib/aasm/persistence/base.rb +55 -20
- data/lib/aasm/persistence/mongo_mapper_persistence.rb +157 -0
- data/lib/aasm/persistence/mongoid_persistence.rb +16 -41
- data/lib/aasm/persistence/plain_persistence.rb +8 -7
- data/lib/aasm/persistence/sequel_persistence.rb +12 -9
- data/lib/aasm/persistence.rb +2 -0
- data/lib/aasm/rspec/allow_event.rb +22 -0
- data/lib/aasm/rspec/allow_transition_to.rb +22 -0
- data/lib/aasm/rspec/have_state.rb +22 -0
- data/lib/aasm/rspec/transition_from.rb +32 -0
- data/lib/aasm/rspec.rb +5 -0
- data/lib/aasm/state_machine.rb +20 -6
- data/lib/aasm/version.rb +1 -1
- data/spec/database.rb +27 -1
- data/spec/models/active_record/basic_active_record_two_state_machines_example.rb +25 -0
- data/spec/models/active_record/complex_active_record_example.rb +33 -0
- data/spec/models/active_record/derivate_new_dsl.rb +7 -0
- data/spec/models/active_record/false_state.rb +35 -0
- data/spec/models/active_record/gate.rb +39 -0
- data/spec/models/active_record/localizer_test_model.rb +34 -0
- data/spec/models/active_record/no_direct_assignment.rb +21 -0
- data/spec/models/active_record/no_scope.rb +21 -0
- data/spec/models/active_record/persisted_state.rb +12 -0
- data/spec/models/active_record/provided_and_persisted_state.rb +24 -0
- data/spec/models/active_record/reader.rb +7 -0
- data/spec/models/active_record/readme_job.rb +21 -0
- data/spec/models/active_record/simple_new_dsl.rb +17 -0
- data/spec/models/active_record/thief.rb +29 -0
- data/spec/models/active_record/transient.rb +6 -0
- data/spec/models/active_record/with_enum.rb +39 -0
- data/spec/models/active_record/with_false_enum.rb +31 -0
- data/spec/models/active_record/with_true_enum.rb +39 -0
- data/spec/models/active_record/writer.rb +6 -0
- data/spec/models/basic_two_state_machines_example.rb +25 -0
- data/spec/models/callbacks/basic.rb +3 -0
- data/spec/models/callbacks/basic_multiple.rb +75 -0
- data/spec/models/callbacks/guard_within_block_multiple.rb +66 -0
- data/spec/models/callbacks/multiple_transitions_transition_guard_multiple.rb +65 -0
- data/spec/models/callbacks/private_method_multiple.rb +44 -0
- data/spec/models/callbacks/with_args.rb +9 -9
- data/spec/models/callbacks/with_args_multiple.rb +61 -0
- data/spec/models/callbacks/{with_state_args.rb → with_state_arg.rb} +1 -1
- data/spec/models/callbacks/with_state_arg_multiple.rb +26 -0
- data/spec/models/complex_example.rb +222 -0
- data/spec/models/conversation.rb +47 -1
- data/spec/models/default_state.rb +12 -0
- data/spec/models/foo.rb +57 -0
- data/spec/models/foo_callback_multiple.rb +45 -0
- data/spec/models/guardian.rb +10 -0
- data/spec/models/guardian_multiple.rb +48 -0
- data/spec/models/initial_state_proc.rb +31 -0
- data/spec/models/invalid_persistor.rb +15 -0
- data/spec/models/mongo_mapper/complex_mongo_mapper_example.rb +37 -0
- data/spec/models/mongo_mapper/no_scope_mongo_mapper.rb +21 -0
- data/spec/models/mongo_mapper/simple_mongo_mapper.rb +23 -0
- data/spec/models/mongo_mapper/simple_new_dsl_mongo_mapper.rb +25 -0
- data/spec/models/mongoid/complex_mongoid_example.rb +37 -0
- data/spec/models/mongoid/no_scope_mongoid.rb +11 -0
- data/spec/models/mongoid/simple_mongoid.rb +12 -0
- data/spec/models/mongoid/simple_new_dsl_mongoid.rb +14 -1
- data/spec/models/no_initial_state.rb +25 -0
- data/spec/models/parametrised_event.rb +1 -1
- data/spec/models/parametrised_event_multiple.rb +29 -0
- data/spec/models/provided_state.rb +24 -0
- data/spec/models/sequel/complex_sequel_example.rb +45 -0
- data/spec/models/sequel/sequel_multiple.rb +25 -0
- data/spec/models/sequel/sequel_simple.rb +25 -0
- data/spec/models/silencer.rb +5 -0
- data/spec/models/simple_example.rb +15 -0
- data/spec/models/simple_multiple_example.rb +30 -0
- data/spec/models/state_machine_with_failed_event.rb +12 -0
- data/spec/models/sub_class.rb +7 -0
- data/spec/models/sub_class_with_more_states.rb +18 -0
- data/spec/models/super_class.rb +46 -0
- data/spec/models/transactor.rb +27 -0
- data/spec/models/valid_state_name.rb +23 -0
- data/spec/models/validator.rb +47 -0
- data/spec/spec_helper.rb +4 -4
- data/spec/unit/api_spec.rb +6 -1
- data/spec/unit/basic_two_state_machines_example_spec.rb +10 -0
- data/spec/unit/callback_multiple_spec.rb +295 -0
- data/spec/unit/callbacks_spec.rb +3 -2
- data/spec/unit/complex_example_spec.rb +2 -2
- data/spec/unit/complex_multiple_example_spec.rb +99 -0
- data/spec/unit/edge_cases_spec.rb +16 -0
- data/spec/unit/event_multiple_spec.rb +73 -0
- data/spec/unit/event_naming_spec.rb +2 -15
- data/spec/unit/event_spec.rb +11 -6
- data/spec/unit/guard_multiple_spec.rb +60 -0
- data/spec/unit/guard_spec.rb +12 -0
- data/spec/unit/initial_state_multiple_spec.rb +15 -0
- data/spec/unit/initial_state_spec.rb +3 -18
- data/spec/unit/inspection_multiple_spec.rb +201 -0
- data/spec/unit/inspection_spec.rb +12 -7
- data/spec/unit/localizer_spec.rb +0 -38
- data/spec/unit/persistence/active_record_persistence_multiple_spec.rb +573 -0
- data/spec/unit/persistence/active_record_persistence_spec.rb +70 -13
- data/spec/unit/persistence/mongo_mapper_persistence_multiple_spec.rb +146 -0
- data/spec/unit/persistence/mongo_mapper_persistence_spec.rb +93 -0
- data/spec/unit/persistence/mongoid_persistence_multiple_spec.rb +127 -0
- data/spec/unit/persistence/mongoid_persistence_spec.rb +79 -0
- data/spec/unit/persistence/sequel_persistence_multiple_spec.rb +153 -0
- data/spec/unit/persistence/sequel_persistence_spec.rb +20 -23
- data/spec/unit/readme_spec.rb +42 -0
- data/spec/unit/reloading_spec.rb +1 -1
- data/spec/unit/rspec_matcher_spec.rb +79 -0
- data/spec/unit/simple_example_spec.rb +26 -42
- data/spec/unit/simple_multiple_example_spec.rb +63 -0
- data/spec/unit/state_spec.rb +3 -1
- data/spec/unit/subclassing_multiple_spec.rb +39 -0
- data/spec/unit/subclassing_spec.rb +10 -10
- data/spec/unit/transition_spec.rb +39 -25
- metadata +152 -23
- data/spec/models/active_record/api.rb +0 -75
- data/spec/models/argument.rb +0 -11
- data/spec/models/auth_machine.rb +0 -88
- data/spec/models/bar.rb +0 -15
- data/spec/models/father.rb +0 -21
- data/spec/models/persistence.rb +0 -164
- data/spec/models/son.rb +0 -3
- data/spec/unit/persistence/mongoid_persistance_spec.rb +0 -152
|
@@ -1,19 +1,15 @@
|
|
|
1
1
|
require 'active_record'
|
|
2
|
-
require 'logger'
|
|
3
2
|
require 'spec_helper'
|
|
3
|
+
Dir[File.dirname(__FILE__) + "/../../models/active_record/*.rb"].sort.each do |f|
|
|
4
|
+
require File.expand_path(f)
|
|
5
|
+
end
|
|
4
6
|
|
|
5
7
|
load_schema
|
|
6
8
|
|
|
7
9
|
# if you want to see the statements while running the spec enable the following line
|
|
10
|
+
# require 'logger'
|
|
8
11
|
# ActiveRecord::Base.logger = Logger.new(STDERR)
|
|
9
12
|
|
|
10
|
-
shared_examples_for "aasm model" do
|
|
11
|
-
it "should include persistence mixins" do
|
|
12
|
-
expect(klass.included_modules).to be_include(AASM::Persistence::ActiveRecordPersistence)
|
|
13
|
-
expect(klass.included_modules).to be_include(AASM::Persistence::ActiveRecordPersistence::InstanceMethods)
|
|
14
|
-
end
|
|
15
|
-
end
|
|
16
|
-
|
|
17
13
|
describe "instance methods" do
|
|
18
14
|
let(:gate) {Gate.new}
|
|
19
15
|
|
|
@@ -160,7 +156,7 @@ describe "instance methods" do
|
|
|
160
156
|
|
|
161
157
|
gate.aasm_write_state state_sym
|
|
162
158
|
|
|
163
|
-
expect(gate).to have_received(:aasm_write_attribute).with(state_sym)
|
|
159
|
+
expect(gate).to have_received(:aasm_write_attribute).with(state_sym, :default)
|
|
164
160
|
expect(gate).to_not have_received :write_attribute
|
|
165
161
|
end
|
|
166
162
|
end
|
|
@@ -170,7 +166,7 @@ describe "instance methods" do
|
|
|
170
166
|
it "delegates state update to the helper method" do
|
|
171
167
|
gate.aasm_write_state_without_persistence state_sym
|
|
172
168
|
|
|
173
|
-
expect(gate).to have_received(:aasm_write_attribute).with(state_sym)
|
|
169
|
+
expect(gate).to have_received(:aasm_write_attribute).with(state_sym, :default)
|
|
174
170
|
expect(gate).to_not have_received :write_attribute
|
|
175
171
|
end
|
|
176
172
|
end
|
|
@@ -210,7 +206,7 @@ describe "instance methods" do
|
|
|
210
206
|
end
|
|
211
207
|
|
|
212
208
|
it "generates attribute value using a helper method" do
|
|
213
|
-
expect(gate).to have_received(:aasm_raw_attribute_value).with(sym)
|
|
209
|
+
expect(gate).to have_received(:aasm_raw_attribute_value).with(sym, :default)
|
|
214
210
|
end
|
|
215
211
|
|
|
216
212
|
it "writes attribute to the model" do
|
|
@@ -303,6 +299,18 @@ describe "named scopes with the new DSL" do
|
|
|
303
299
|
expect(NoScope).not_to respond_to(:pending)
|
|
304
300
|
end
|
|
305
301
|
|
|
302
|
+
context "result of scope" do
|
|
303
|
+
let!(:dsl1) { SimpleNewDsl.create!(status: :new) }
|
|
304
|
+
let!(:dsl2) { SimpleNewDsl.create!(status: :unknown_scope) }
|
|
305
|
+
|
|
306
|
+
after do
|
|
307
|
+
SimpleNewDsl.destroy_all
|
|
308
|
+
end
|
|
309
|
+
|
|
310
|
+
it "created scope works as where(name: :scope_name)" do
|
|
311
|
+
expect(SimpleNewDsl.unknown_scope).to contain_exactly(dsl2)
|
|
312
|
+
end
|
|
313
|
+
end
|
|
306
314
|
end # scopes
|
|
307
315
|
|
|
308
316
|
describe "direct assignment" do
|
|
@@ -321,6 +329,24 @@ describe "direct assignment" do
|
|
|
321
329
|
expect {obj.aasm_state = :running}.to raise_error(AASM::NoDirectAssignmentError)
|
|
322
330
|
expect(obj.aasm_state.to_sym).to eql :pending
|
|
323
331
|
end
|
|
332
|
+
|
|
333
|
+
it 'can be turned off and on again' do
|
|
334
|
+
obj = NoDirectAssignment.create
|
|
335
|
+
expect(obj.aasm_state.to_sym).to eql :pending
|
|
336
|
+
|
|
337
|
+
expect {obj.aasm_state = :running}.to raise_error(AASM::NoDirectAssignmentError)
|
|
338
|
+
expect(obj.aasm_state.to_sym).to eql :pending
|
|
339
|
+
|
|
340
|
+
# allow it temporarily
|
|
341
|
+
NoDirectAssignment.aasm.state_machine.config.no_direct_assignment = false
|
|
342
|
+
obj.aasm_state = :pending
|
|
343
|
+
expect(obj.aasm_state.to_sym).to eql :pending
|
|
344
|
+
|
|
345
|
+
# and forbid it again
|
|
346
|
+
NoDirectAssignment.aasm.state_machine.config.no_direct_assignment = true
|
|
347
|
+
expect {obj.aasm_state = :running}.to raise_error(AASM::NoDirectAssignmentError)
|
|
348
|
+
expect(obj.aasm_state.to_sym).to eql :pending
|
|
349
|
+
end
|
|
324
350
|
end # direct assignment
|
|
325
351
|
|
|
326
352
|
describe 'initial states' do
|
|
@@ -413,7 +439,7 @@ describe 'transitions with persistence' do
|
|
|
413
439
|
|
|
414
440
|
it "should only rollback changes in the main transaction not the nested one" do
|
|
415
441
|
# change configuration to not require new transaction
|
|
416
|
-
AASM::StateMachine[Transactor].config.requires_new_transaction = false
|
|
442
|
+
AASM::StateMachine[Transactor][:default].config.requires_new_transaction = false
|
|
417
443
|
|
|
418
444
|
expect(transactor).to be_sleeping
|
|
419
445
|
expect(worker.status).to eq('sleeping')
|
|
@@ -431,9 +457,14 @@ describe 'transitions with persistence' do
|
|
|
431
457
|
it "should fire :after_commit if transaction was successful" do
|
|
432
458
|
validator = Validator.create(:name => 'name')
|
|
433
459
|
expect(validator).to be_sleeping
|
|
460
|
+
|
|
434
461
|
validator.run!
|
|
435
462
|
expect(validator).to be_running
|
|
436
|
-
expect(validator.name).
|
|
463
|
+
expect(validator.name).to eq("name changed")
|
|
464
|
+
|
|
465
|
+
validator.sleep!("sleeper")
|
|
466
|
+
expect(validator).to be_sleeping
|
|
467
|
+
expect(validator.name).to eq("sleeper")
|
|
437
468
|
end
|
|
438
469
|
|
|
439
470
|
it "should not fire :after_commit if transaction failed" do
|
|
@@ -493,3 +524,29 @@ describe "invalid states with persistence" do
|
|
|
493
524
|
end
|
|
494
525
|
|
|
495
526
|
end
|
|
527
|
+
|
|
528
|
+
describe 'basic example with two state machines' do
|
|
529
|
+
let(:example) { BasicActiveRecordTwoStateMachinesExample.new }
|
|
530
|
+
|
|
531
|
+
it 'should initialise properly' do
|
|
532
|
+
expect(example.aasm(:search).current_state).to eql :initialised
|
|
533
|
+
expect(example.aasm(:sync).current_state).to eql :unsynced
|
|
534
|
+
end
|
|
535
|
+
end
|
|
536
|
+
|
|
537
|
+
describe 'testing the README examples' do
|
|
538
|
+
it 'Usage' do
|
|
539
|
+
job = ReadmeJob.new
|
|
540
|
+
|
|
541
|
+
expect(job.sleeping?).to eql true
|
|
542
|
+
expect(job.may_run?).to eql true
|
|
543
|
+
|
|
544
|
+
job.run
|
|
545
|
+
|
|
546
|
+
expect(job.running?).to eql true
|
|
547
|
+
expect(job.sleeping?).to eql false
|
|
548
|
+
expect(job.may_run?).to eql false
|
|
549
|
+
|
|
550
|
+
expect { job.run }.to raise_error(AASM::InvalidTransition)
|
|
551
|
+
end
|
|
552
|
+
end
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
describe 'mongo_mapper' do
|
|
2
|
+
begin
|
|
3
|
+
require 'mongo_mapper'
|
|
4
|
+
require 'logger'
|
|
5
|
+
require 'spec_helper'
|
|
6
|
+
|
|
7
|
+
Dir[File.dirname(__FILE__) + "/../../models/mongo_mapper/*.rb"].sort.each do |f|
|
|
8
|
+
require File.expand_path(f)
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
before(:all) do
|
|
12
|
+
config = {
|
|
13
|
+
'test' => {
|
|
14
|
+
'database' => "mongo_mapper_#{Process.pid}"
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
MongoMapper.setup(config, 'test') #, :logger => Logger.new(STDERR))
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
after do
|
|
22
|
+
# Clear Out all non-system Mongo collections between tests
|
|
23
|
+
MongoMapper.database.collections.each do |collection|
|
|
24
|
+
collection.drop unless collection.capped? || (collection.name =~ /\Asystem/)
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
describe "named scopes with the old DSL" do
|
|
29
|
+
|
|
30
|
+
context "Does not already respond_to? the scope name" do
|
|
31
|
+
it "should add a scope" do
|
|
32
|
+
expect(SimpleMongoMapperMultiple).to respond_to(:unknown_scope)
|
|
33
|
+
expect(SimpleMongoMapperMultiple.unknown_scope.class).to eq(MongoMapper::Plugins::Querying::DecoratedPluckyQuery)
|
|
34
|
+
#expect(SimpleMongoMapperMultiple.unknown_scope.is_a?(ActiveRecord::Relation)).to be_truthy
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
context "Already respond_to? the scope name" do
|
|
39
|
+
it "should not add a scope" do
|
|
40
|
+
expect(SimpleMongoMapperMultiple).to respond_to(:next)
|
|
41
|
+
expect(SimpleMongoMapperMultiple.new.class).to eq(SimpleMongoMapperMultiple)
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
describe "named scopes with the new DSL" do
|
|
48
|
+
|
|
49
|
+
context "Does not already respond_to? the scope name" do
|
|
50
|
+
it "should add a scope" do
|
|
51
|
+
expect(SimpleNewDslMongoMapperMultiple).to respond_to(:unknown_scope)
|
|
52
|
+
expect(SimpleNewDslMongoMapperMultiple.unknown_scope.class).to eq(MongoMapper::Plugins::Querying::DecoratedPluckyQuery)
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
context "Already respond_to? the scope name" do
|
|
57
|
+
it "should not add a scope" do
|
|
58
|
+
expect(SimpleNewDslMongoMapperMultiple).to respond_to(:next)
|
|
59
|
+
expect(SimpleNewDslMongoMapperMultiple.new.class).to eq(SimpleNewDslMongoMapperMultiple)
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
it "does not create scopes if requested" do
|
|
64
|
+
expect(NoScopeMongoMapperMultiple).not_to respond_to(:ignored_scope)
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
describe "instance methods" do
|
|
70
|
+
|
|
71
|
+
let(:simple) {SimpleNewDslMongoMapperMultiple.new}
|
|
72
|
+
|
|
73
|
+
it "should call aasm_ensure_initial_state on validation before create" do
|
|
74
|
+
expect(SimpleNewDslMongoMapperMultiple.aasm(:left).initial_state).to eq(:unknown_scope)
|
|
75
|
+
expect(SimpleNewDslMongoMapperMultiple.aasm(:left).attribute_name).to eq(:status)
|
|
76
|
+
expect(simple.status).to eq(nil)
|
|
77
|
+
simple.valid?
|
|
78
|
+
expect(simple.status).to eq('unknown_scope')
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
it "should call aasm_ensure_initial_state before create, even if skipping validations" do
|
|
82
|
+
expect(simple.status).to eq(nil)
|
|
83
|
+
simple.save(:validate => false)
|
|
84
|
+
expect(simple.status).to eq('unknown_scope')
|
|
85
|
+
end
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
describe "complex example" do
|
|
89
|
+
it "works" do
|
|
90
|
+
record = ComplexMongoMapperExample.new
|
|
91
|
+
expect(record.aasm(:left).current_state).to eql :one
|
|
92
|
+
expect(record.left).to be_nil
|
|
93
|
+
expect(record.aasm(:right).current_state).to eql :alpha
|
|
94
|
+
expect(record.right).to be_nil
|
|
95
|
+
|
|
96
|
+
record.save!
|
|
97
|
+
expect_aasm_states record, :one, :alpha
|
|
98
|
+
record.reload
|
|
99
|
+
expect_aasm_states record, :one, :alpha
|
|
100
|
+
|
|
101
|
+
record.increment!
|
|
102
|
+
expect_aasm_states record, :two, :alpha
|
|
103
|
+
record.reload
|
|
104
|
+
expect_aasm_states record, :two, :alpha
|
|
105
|
+
|
|
106
|
+
record.level_up!
|
|
107
|
+
expect_aasm_states record, :two, :beta
|
|
108
|
+
record.reload
|
|
109
|
+
expect_aasm_states record, :two, :beta
|
|
110
|
+
|
|
111
|
+
record.increment!
|
|
112
|
+
expect { record.increment! }.to raise_error(AASM::InvalidTransition)
|
|
113
|
+
expect_aasm_states record, :three, :beta
|
|
114
|
+
record.reload
|
|
115
|
+
expect_aasm_states record, :three, :beta
|
|
116
|
+
|
|
117
|
+
record.level_up!
|
|
118
|
+
expect_aasm_states record, :three, :gamma
|
|
119
|
+
record.reload
|
|
120
|
+
expect_aasm_states record, :three, :gamma
|
|
121
|
+
|
|
122
|
+
record.level_down # without saving
|
|
123
|
+
expect_aasm_states record, :three, :beta
|
|
124
|
+
record.reload
|
|
125
|
+
expect_aasm_states record, :three, :gamma
|
|
126
|
+
|
|
127
|
+
record.level_down # without saving
|
|
128
|
+
expect_aasm_states record, :three, :beta
|
|
129
|
+
record.reset!
|
|
130
|
+
expect_aasm_states record, :one, :beta
|
|
131
|
+
end
|
|
132
|
+
|
|
133
|
+
def expect_aasm_states(record, left_state, right_state)
|
|
134
|
+
expect(record.aasm(:left).current_state).to eql left_state.to_sym
|
|
135
|
+
expect(record.left).to eql left_state.to_s
|
|
136
|
+
expect(record.aasm(:right).current_state).to eql right_state.to_sym
|
|
137
|
+
expect(record.right).to eql right_state.to_s
|
|
138
|
+
end
|
|
139
|
+
end
|
|
140
|
+
|
|
141
|
+
rescue LoadError
|
|
142
|
+
puts "--------------------------------------------------------------------------"
|
|
143
|
+
puts "Not running MongoMapper specs because mongo_mapper gem is not installed!!!"
|
|
144
|
+
puts "--------------------------------------------------------------------------"
|
|
145
|
+
end
|
|
146
|
+
end
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
describe 'mongo_mapper' do
|
|
2
|
+
begin
|
|
3
|
+
require 'mongo_mapper'
|
|
4
|
+
require 'logger'
|
|
5
|
+
require 'spec_helper'
|
|
6
|
+
|
|
7
|
+
Dir[File.dirname(__FILE__) + "/../../models/mongo_mapper/*.rb"].sort.each do |f|
|
|
8
|
+
require File.expand_path(f)
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
before(:all) do
|
|
12
|
+
config = {
|
|
13
|
+
'test' => {
|
|
14
|
+
'database' => "mongo_mapper_#{Process.pid}"
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
MongoMapper.setup(config, 'test') #, :logger => Logger.new(STDERR))
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
after do
|
|
22
|
+
# Clear Out all non-system Mongo collections between tests
|
|
23
|
+
MongoMapper.database.collections.each do |collection|
|
|
24
|
+
collection.drop unless collection.capped? || (collection.name =~ /\Asystem/)
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
describe "named scopes with the old DSL" do
|
|
29
|
+
|
|
30
|
+
context "Does not already respond_to? the scope name" do
|
|
31
|
+
it "should add a scope" do
|
|
32
|
+
expect(SimpleMongoMapper).to respond_to(:unknown_scope)
|
|
33
|
+
expect(SimpleMongoMapper.unknown_scope.class).to eq(MongoMapper::Plugins::Querying::DecoratedPluckyQuery)
|
|
34
|
+
#expect(SimpleMongoMapper.unknown_scope.is_a?(ActiveRecord::Relation)).to be_truthy
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
context "Already respond_to? the scope name" do
|
|
39
|
+
it "should not add a scope" do
|
|
40
|
+
expect(SimpleMongoMapper).to respond_to(:next)
|
|
41
|
+
expect(SimpleMongoMapper.new.class).to eq(SimpleMongoMapper)
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
describe "named scopes with the new DSL" do
|
|
48
|
+
|
|
49
|
+
context "Does not already respond_to? the scope name" do
|
|
50
|
+
it "should add a scope" do
|
|
51
|
+
expect(SimpleNewDslMongoMapper).to respond_to(:unknown_scope)
|
|
52
|
+
expect(SimpleNewDslMongoMapper.unknown_scope.class).to eq(MongoMapper::Plugins::Querying::DecoratedPluckyQuery)
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
context "Already respond_to? the scope name" do
|
|
57
|
+
it "should not add a scope" do
|
|
58
|
+
expect(SimpleNewDslMongoMapper).to respond_to(:next)
|
|
59
|
+
expect(SimpleNewDslMongoMapper.new.class).to eq(SimpleNewDslMongoMapper)
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
it "does not create scopes if requested" do
|
|
64
|
+
expect(NoScopeMongoMapper).not_to respond_to(:ignored_scope)
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
describe "instance methods" do
|
|
70
|
+
|
|
71
|
+
let(:simple) {SimpleNewDslMongoMapper.new}
|
|
72
|
+
|
|
73
|
+
it "should call aasm_ensure_initial_state on validation before create" do
|
|
74
|
+
expect(SimpleNewDslMongoMapper.aasm.initial_state).to eq(:unknown_scope)
|
|
75
|
+
expect(SimpleNewDslMongoMapper.aasm.attribute_name).to eq(:status)
|
|
76
|
+
expect(simple.status).to eq(nil)
|
|
77
|
+
simple.valid?
|
|
78
|
+
expect(simple.status).to eq('unknown_scope')
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
it "should call aasm_ensure_initial_state before create, even if skipping validations" do
|
|
82
|
+
expect(simple.status).to eq(nil)
|
|
83
|
+
simple.save(:validate => false)
|
|
84
|
+
expect(simple.status).to eq('unknown_scope')
|
|
85
|
+
end
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
rescue LoadError
|
|
89
|
+
puts "--------------------------------------------------------------------------"
|
|
90
|
+
puts "Not running MongoMapper multiple-specs because mongo_mapper gem is not installed!!!"
|
|
91
|
+
puts "--------------------------------------------------------------------------"
|
|
92
|
+
end
|
|
93
|
+
end
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
describe 'mongoid' do
|
|
2
|
+
begin
|
|
3
|
+
require 'mongoid'
|
|
4
|
+
require 'logger'
|
|
5
|
+
require 'spec_helper'
|
|
6
|
+
|
|
7
|
+
Dir[File.dirname(__FILE__) + "/../../models/mongoid/*.rb"].sort.each do |f|
|
|
8
|
+
require File.expand_path(f)
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
before(:all) do
|
|
12
|
+
# if you want to see the statements while running the spec enable the following line
|
|
13
|
+
# Mongoid.logger = Logger.new(STDERR)
|
|
14
|
+
|
|
15
|
+
Mongoid.configure do |config|
|
|
16
|
+
config.connect_to "mongoid_#{Process.pid}"
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
after do
|
|
21
|
+
Mongoid.purge!
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
describe "named scopes with the old DSL" do
|
|
25
|
+
|
|
26
|
+
context "Does not already respond_to? the scope name" do
|
|
27
|
+
it "should add a scope" do
|
|
28
|
+
expect(SimpleMongoidMultiple).to respond_to(:unknown_scope)
|
|
29
|
+
expect(SimpleMongoidMultiple.unknown_scope.class).to eq(Mongoid::Criteria)
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
context "Already respond_to? the scope name" do
|
|
34
|
+
it "should not add a scope" do
|
|
35
|
+
expect(SimpleMongoidMultiple).to respond_to(:new)
|
|
36
|
+
expect(SimpleMongoidMultiple.new.class).to eq(SimpleMongoidMultiple)
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
describe "named scopes with the new DSL" do
|
|
43
|
+
context "Does not already respond_to? the scope name" do
|
|
44
|
+
it "should add a scope" do
|
|
45
|
+
expect(SimpleNewDslMongoidMultiple).to respond_to(:unknown_scope)
|
|
46
|
+
expect(SimpleNewDslMongoidMultiple.unknown_scope.class).to eq(Mongoid::Criteria)
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
context "Already respond_to? the scope name" do
|
|
51
|
+
it "should not add a scope" do
|
|
52
|
+
expect(SimpleNewDslMongoidMultiple).to respond_to(:new)
|
|
53
|
+
expect(SimpleNewDslMongoidMultiple.new.class).to eq(SimpleNewDslMongoidMultiple)
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
it "does not create scopes if requested" do
|
|
58
|
+
expect(NoScopeMongoidMultiple).not_to respond_to(:ignored_scope)
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
describe "instance methods" do
|
|
63
|
+
let(:simple) {SimpleNewDslMongoidMultiple.new}
|
|
64
|
+
|
|
65
|
+
it "should initialize the aasm state on instantiation" do
|
|
66
|
+
expect(SimpleNewDslMongoidMultiple.new.status).to eql 'unknown_scope'
|
|
67
|
+
expect(SimpleNewDslMongoidMultiple.new.aasm(:left).current_state).to eql :unknown_scope
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
describe "complex example" do
|
|
73
|
+
it "works" do
|
|
74
|
+
record = ComplexMongoidExample.new
|
|
75
|
+
expect_aasm_states record, :one, :alpha
|
|
76
|
+
|
|
77
|
+
record.save!
|
|
78
|
+
expect_aasm_states record, :one, :alpha
|
|
79
|
+
record.reload
|
|
80
|
+
expect_aasm_states record, :one, :alpha
|
|
81
|
+
|
|
82
|
+
record.increment!
|
|
83
|
+
expect_aasm_states record, :two, :alpha
|
|
84
|
+
record.reload
|
|
85
|
+
expect_aasm_states record, :two, :alpha
|
|
86
|
+
|
|
87
|
+
record.level_up!
|
|
88
|
+
expect_aasm_states record, :two, :beta
|
|
89
|
+
record.reload
|
|
90
|
+
expect_aasm_states record, :two, :beta
|
|
91
|
+
|
|
92
|
+
record.increment!
|
|
93
|
+
expect { record.increment! }.to raise_error(AASM::InvalidTransition)
|
|
94
|
+
expect_aasm_states record, :three, :beta
|
|
95
|
+
record.reload
|
|
96
|
+
expect_aasm_states record, :three, :beta
|
|
97
|
+
|
|
98
|
+
record.level_up!
|
|
99
|
+
expect_aasm_states record, :three, :gamma
|
|
100
|
+
record.reload
|
|
101
|
+
expect_aasm_states record, :three, :gamma
|
|
102
|
+
|
|
103
|
+
record.level_down # without saving
|
|
104
|
+
expect_aasm_states record, :three, :beta
|
|
105
|
+
record.reload
|
|
106
|
+
expect_aasm_states record, :three, :gamma
|
|
107
|
+
|
|
108
|
+
record.level_down # without saving
|
|
109
|
+
expect_aasm_states record, :three, :beta
|
|
110
|
+
record.reset!
|
|
111
|
+
expect_aasm_states record, :one, :beta
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
def expect_aasm_states(record, left_state, right_state)
|
|
115
|
+
expect(record.aasm(:left).current_state).to eql left_state.to_sym
|
|
116
|
+
expect(record.left).to eql left_state.to_s
|
|
117
|
+
expect(record.aasm(:right).current_state).to eql right_state.to_sym
|
|
118
|
+
expect(record.right).to eql right_state.to_s
|
|
119
|
+
end
|
|
120
|
+
end
|
|
121
|
+
|
|
122
|
+
rescue LoadError
|
|
123
|
+
puts "--------------------------------------------------------------------------"
|
|
124
|
+
puts "Not running Mongoid multiple-specs because mongoid gem is not installed!!!"
|
|
125
|
+
puts "--------------------------------------------------------------------------"
|
|
126
|
+
end
|
|
127
|
+
end
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
describe 'mongoid' do
|
|
2
|
+
begin
|
|
3
|
+
require 'mongoid'
|
|
4
|
+
require 'logger'
|
|
5
|
+
require 'spec_helper'
|
|
6
|
+
|
|
7
|
+
Dir[File.dirname(__FILE__) + "/../../models/mongoid/*.rb"].sort.each do |f|
|
|
8
|
+
require File.expand_path(f)
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
before(:all) do
|
|
12
|
+
# if you want to see the statements while running the spec enable the following line
|
|
13
|
+
# Mongoid.logger = Logger.new(STDERR)
|
|
14
|
+
|
|
15
|
+
Mongoid.configure do |config|
|
|
16
|
+
config.connect_to "mongoid_#{Process.pid}"
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
after do
|
|
21
|
+
Mongoid.purge!
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
describe "named scopes with the old DSL" do
|
|
25
|
+
|
|
26
|
+
context "Does not already respond_to? the scope name" do
|
|
27
|
+
it "should add a scope" do
|
|
28
|
+
expect(SimpleMongoid).to respond_to(:unknown_scope)
|
|
29
|
+
expect(SimpleMongoid.unknown_scope.class).to eq(Mongoid::Criteria)
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
context "Already respond_to? the scope name" do
|
|
34
|
+
it "should not add a scope" do
|
|
35
|
+
expect(SimpleMongoid).to respond_to(:new)
|
|
36
|
+
expect(SimpleMongoid.new.class).to eq(SimpleMongoid)
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
describe "named scopes with the new DSL" do
|
|
43
|
+
|
|
44
|
+
context "Does not already respond_to? the scope name" do
|
|
45
|
+
it "should add a scope" do
|
|
46
|
+
expect(SimpleNewDslMongoid).to respond_to(:unknown_scope)
|
|
47
|
+
expect(SimpleNewDslMongoid.unknown_scope.class).to eq(Mongoid::Criteria)
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
context "Already respond_to? the scope name" do
|
|
52
|
+
it "should not add a scope" do
|
|
53
|
+
expect(SimpleNewDslMongoid).to respond_to(:new)
|
|
54
|
+
expect(SimpleNewDslMongoid.new.class).to eq(SimpleNewDslMongoid)
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
it "does not create scopes if requested" do
|
|
59
|
+
expect(NoScopeMongoid).not_to respond_to(:ignored_scope)
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
describe "instance methods" do
|
|
65
|
+
let(:simple) {SimpleNewDslMongoid.new}
|
|
66
|
+
|
|
67
|
+
it "should initialize the aasm state on instantiation" do
|
|
68
|
+
expect(SimpleNewDslMongoid.new.status).to eql 'unknown_scope'
|
|
69
|
+
expect(SimpleNewDslMongoid.new.aasm.current_state).to eql :unknown_scope
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
rescue LoadError
|
|
75
|
+
puts "--------------------------------------------------------------------------"
|
|
76
|
+
puts "Not running Mongoid specs because mongoid gem is not installed!!!"
|
|
77
|
+
puts "--------------------------------------------------------------------------"
|
|
78
|
+
end
|
|
79
|
+
end
|