Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test support for Sequel and multiple state machines per class
- Loading branch information
Showing
2 changed files
with
122 additions
and
1 deletion.
There are no files selected for viewing
120 changes: 120 additions & 0 deletions
120
spec/unit/persistence/sequel_persistence_multiple_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
describe 'sequel' do | ||
begin | ||
require 'sequel' | ||
require 'logger' | ||
require 'spec_helper' | ||
|
||
before(:all) do | ||
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(:models) do | ||
primary_key :id | ||
String :status | ||
end | ||
|
||
@model = Class.new(Sequel::Model(db)) do | ||
set_dataset(:models) | ||
include AASM | ||
|
||
attr_accessor :default | ||
|
||
aasm :left, :column => :status | ||
aasm :left do | ||
state :alpha, :initial => true | ||
state :beta | ||
state :gamma | ||
event :release do | ||
transitions :from => [:alpha, :beta, :gamma], :to => :beta | ||
end | ||
end | ||
end | ||
end | ||
|
||
describe "instance methods" do | ||
let(:model) {@model.new} | ||
|
||
it "should respond to aasm persistence methods" do | ||
expect(model).to respond_to(:aasm_read_state) | ||
expect(model).to respond_to(:aasm_write_state) | ||
expect(model).to respond_to(:aasm_write_state_without_persistence) | ||
end | ||
|
||
it "should return the initial state when new and the aasm field is nil" do | ||
expect(model.aasm(:left).current_state).to eq(:alpha) | ||
end | ||
|
||
it "should save the initial state" do | ||
model.save | ||
expect(model.status).to eq("alpha") | ||
end | ||
|
||
it "should return the aasm column when new and the aasm field is not nil" do | ||
model.status = "beta" | ||
expect(model.aasm(:left).current_state).to eq(:beta) | ||
end | ||
|
||
it "should return the aasm column when not new and the aasm_column is not nil" do | ||
allow(model).to receive(:new?).and_return(false) | ||
model.status = "gamma" | ||
expect(model.aasm(:left).current_state).to eq(:gamma) | ||
end | ||
|
||
it "should allow a nil state" do | ||
allow(model).to receive(:new?).and_return(false) | ||
model.status = nil | ||
expect(model.aasm(:left).current_state).to be_nil | ||
end | ||
|
||
it "should not change the state if state is not loaded" do | ||
model.release | ||
model.save | ||
model.class.select(:id).first.save | ||
model.reload | ||
expect(model.aasm(:left).current_state).to eq(:beta) | ||
end | ||
|
||
it "should call aasm_ensure_initial_state on validation before create" do | ||
expect(model).to receive(:aasm_ensure_initial_state).and_return(true) | ||
model.valid? | ||
end | ||
|
||
it "should call aasm_ensure_initial_state before create, even if skipping validations" do | ||
expect(model).to receive(:aasm_ensure_initial_state).and_return(true) | ||
model.save(:validate => false) | ||
end | ||
end | ||
|
||
describe 'subclasses' do | ||
it "should have the same states as its parent class" do | ||
expect(Class.new(@model).aasm(:left).states).to eq(@model.aasm(:left).states) | ||
end | ||
|
||
it "should have the same events as its parent class" do | ||
expect(Class.new(@model).aasm(:left).events).to eq(@model.aasm(:left).events) | ||
end | ||
|
||
it "should have the same column as its parent even for the new dsl" do | ||
expect(@model.aasm(:left).attribute_name).to eq(:status) | ||
expect(Class.new(@model).aasm(:left).attribute_name).to eq(:status) | ||
end | ||
end | ||
|
||
describe 'initial states' do | ||
it 'should support conditions' do | ||
@model.aasm(:left) do | ||
initial_state lambda{ |m| m.default } | ||
end | ||
|
||
expect(@model.new(:default => :beta).aasm(:left).current_state).to eq(:beta) | ||
expect(@model.new(:default => :gamma).aasm(:left).current_state).to eq(:gamma) | ||
end | ||
end | ||
|
||
rescue LoadError | ||
puts "------------------------------------------------------------------------" | ||
puts "Not running Sequel multiple-specs because sequel gem is not installed!!!" | ||
puts "------------------------------------------------------------------------" | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters