1
0
Fork 0
mirror of https://github.com/aasm/aasm synced 2023-03-27 23:22:41 -04:00

add a complex example spec for Sequel

This commit is contained in:
Thorsten Böttger 2015-07-11 22:27:48 +12:00
parent abcad714a9
commit 5acb9a4921
2 changed files with 102 additions and 0 deletions

View file

@ -0,0 +1,45 @@
db = Sequel.connect(SEQUEL_DB)
# if you want to see the statements while running the spec enable the following line
# db.loggers << Logger.new($stderr)
db.create_table(:complex_sequel_examples) do
primary_key :id
String :left
String :right
end
class ComplexSequelExample < Sequel::Model(db)
set_dataset(:complex_sequel_examples)
include AASM
aasm :left, :column => 'left' do
state :one, :initial => true
state :two
state :three
event :increment do
transitions :from => :one, :to => :two
transitions :from => :two, :to => :three
end
event :reset do
transitions :from => :three, :to => :one
end
end
aasm :right, :column => 'right' do
state :alpha, :initial => true
state :beta
state :gamma
event :level_up do
transitions :from => :alpha, :to => :beta
transitions :from => :beta, :to => :gamma
end
event :level_down do
transitions :from => :gamma, :to => :beta
transitions :from => :beta, :to => :alpha
end
end
end

View file

@ -4,6 +4,10 @@ describe 'sequel' do
require 'logger' require 'logger'
require 'spec_helper' require 'spec_helper'
Dir[File.dirname(__FILE__) + "/../../models/sequel/*.rb"].sort.each do |f|
require File.expand_path(f)
end
before(:all) do before(:all) do
db = Sequel.connect(SEQUEL_DB) db = Sequel.connect(SEQUEL_DB)
@ -112,6 +116,59 @@ describe 'sequel' do
end end
end end
describe "complex example" do
it "works" do
record = ComplexSequelExample.new
expect(record.aasm(:left).current_state).to eql :one
expect(record.left).to be_nil
expect(record.aasm(:right).current_state).to eql :alpha
expect(record.right).to be_nil
record.save
expect_aasm_states record, :one, :alpha
record.reload
expect_aasm_states record, :one, :alpha
record.increment!
expect_aasm_states record, :two, :alpha
record.reload
expect_aasm_states record, :two, :alpha
record.level_up!
expect_aasm_states record, :two, :beta
record.reload
expect_aasm_states record, :two, :beta
record.increment!
expect { record.increment! }.to raise_error(AASM::InvalidTransition)
expect_aasm_states record, :three, :beta
record.reload
expect_aasm_states record, :three, :beta
record.level_up!
expect_aasm_states record, :three, :gamma
record.reload
expect_aasm_states record, :three, :gamma
record.level_down # without saving
expect_aasm_states record, :three, :beta
record.reload
expect_aasm_states record, :three, :gamma
record.level_down # without saving
expect_aasm_states record, :three, :beta
record.reset!
expect_aasm_states record, :one, :beta
end
def expect_aasm_states(record, left_state, right_state)
expect(record.aasm(:left).current_state).to eql left_state.to_sym
expect(record.left).to eql left_state.to_s
expect(record.aasm(:right).current_state).to eql right_state.to_sym
expect(record.right).to eql right_state.to_s
end
end
rescue LoadError rescue LoadError
puts "------------------------------------------------------------------------" puts "------------------------------------------------------------------------"
puts "Not running Sequel multiple-specs because sequel gem is not installed!!!" puts "Not running Sequel multiple-specs because sequel gem is not installed!!!"