Skip to content

Commit

Permalink
Added tests for persistence layers
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryan Oberholzer committed Dec 7, 2010
1 parent 87fcd87 commit aab191d
Show file tree
Hide file tree
Showing 5 changed files with 170 additions and 3 deletions.
Empty file added .rvmrc
Empty file.
92 changes: 92 additions & 0 deletions spec/orm/activerecord_spec.rb
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

75 changes: 75 additions & 0 deletions spec/orm/mongoid_spec.rb
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

4 changes: 1 addition & 3 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,5 @@
require 'spec'
require 'spec/autorun'

Stateflow.persistence = :none

Spec::Runner.configure do |config|
end
end
2 changes: 2 additions & 0 deletions spec/stateflow_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
require 'spec_helper'

Stateflow.persistence = :none

class Robot
include Stateflow

Expand Down

0 comments on commit aab191d

Please sign in to comment.