forked from ryanza/stateflow
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Ryan Oberholzer
committed
Dec 7, 2010
1 parent
87fcd87
commit aab191d
Showing
5 changed files
with
170 additions
and
3 deletions.
There are no files selected for viewing
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,92 @@ | ||
require 'spec_helper' | ||
require 'active_record' | ||
|
||
Stateflow.persistence = :active_record | ||
|
||
# change this if sqlite is unavailable | ||
dbconfig = { | ||
:adapter => 'sqlite3', | ||
:database => ':memory:' | ||
} | ||
|
||
ActiveRecord::Base.establish_connection(dbconfig) | ||
ActiveRecord::Migration.verbose = false | ||
|
||
class TestMigration < ActiveRecord::Migration | ||
def self.up | ||
create_table :robots, :force => true do |t| | ||
t.column :state, :string | ||
t.column :name, :string | ||
end | ||
end | ||
|
||
def self.down | ||
drop_table :robots | ||
end | ||
end | ||
|
||
class Robot < ActiveRecord::Base | ||
include Stateflow | ||
|
||
stateflow do | ||
initial :red | ||
|
||
state :red, :green | ||
|
||
event :change do | ||
transitions :from => :red, :to => :green | ||
end | ||
end | ||
end | ||
|
||
describe Stateflow::Persistence::ActiveRecord do | ||
before(:all) { TestMigration.up } | ||
after(:all) { TestMigration.down } | ||
after { Robot.delete_all } | ||
|
||
let(:robot) { Robot.new } | ||
|
||
it "should save to persistence with bang method" do | ||
@robot = robot | ||
@robot.state = "red" | ||
@robot.new_record?.should be_true | ||
|
||
@robot.change! | ||
|
||
@robot.new_record?.should be_false | ||
@robot.reload.state.should == "green" | ||
end | ||
|
||
it "should not save to persistence with no bang method" do | ||
@robot = robot | ||
@robot.state = "red" | ||
@robot.new_record?.should be_true | ||
|
||
@robot.change | ||
|
||
@robot.new_record?.should be_true | ||
@robot.state.should == "green" | ||
end | ||
|
||
it "Make sure stateflow saves the initial state if no state is set" do | ||
@robot = robot | ||
|
||
@robot.save | ||
@robot.reload | ||
|
||
@robot.state.should == "red" | ||
end | ||
|
||
it "should load state from persistence" do | ||
@robot = robot | ||
@robot.state = "green" | ||
@robot.name = "Bottie" | ||
@robot.save | ||
|
||
result = Robot.find_by_name("Bottie") | ||
result.should_not be_blank | ||
result.state.should == "green" | ||
result.current_state.name.should == :green | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
require 'spec_helper' | ||
require 'mongoid' | ||
|
||
Stateflow.persistence = :mongoid | ||
|
||
connection = Mongo::Connection.new | ||
Mongoid.database = connection.db("stateflow_test") | ||
|
||
class Robot | ||
include Mongoid::Document | ||
include Stateflow | ||
|
||
field :state | ||
field :name | ||
|
||
stateflow do | ||
initial :red | ||
|
||
state :red, :green | ||
|
||
event :change do | ||
transitions :from => :red, :to => :green | ||
end | ||
end | ||
end | ||
|
||
describe Stateflow::Persistence::ActiveRecord do | ||
after { Robot.collection.drop } | ||
|
||
let(:robot) { Robot.new } | ||
|
||
it "should save to persistence with bang method" do | ||
@robot = robot | ||
@robot.state = "red" | ||
@robot.new_record?.should be_true | ||
|
||
@robot.change! | ||
|
||
@robot.new_record?.should be_false | ||
@robot.reload.state.should == "green" | ||
end | ||
|
||
it "should not save to persistence with no bang method" do | ||
@robot = robot | ||
@robot.state = "red" | ||
@robot.new_record?.should be_true | ||
|
||
@robot.change | ||
|
||
@robot.new_record?.should be_true | ||
@robot.state.should == "green" | ||
end | ||
|
||
it "Make sure stateflow saves the initial state if no state is set" do | ||
@robot = robot | ||
|
||
@robot.save | ||
@robot.reload | ||
|
||
@robot.state.should == "red" | ||
end | ||
|
||
it "should load state from persistence" do | ||
@robot = robot | ||
@robot.state = "green" | ||
@robot.name = "Bottie" | ||
@robot.save | ||
|
||
result = Robot.where(:name => "Bottie").first | ||
result.should_not be_blank | ||
result.state.should == "green" | ||
result.current_state.name.should == :green | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,5 @@ | |
require 'spec' | ||
require 'spec/autorun' | ||
|
||
Stateflow.persistence = :none | ||
|
||
Spec::Runner.configure do |config| | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
require 'spec_helper' | ||
|
||
Stateflow.persistence = :none | ||
|
||
class Robot | ||
include Stateflow | ||
|
||
|