Skip to content

Commit

Permalink
Added bundler, fixed persistence tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryan Oberholzer committed Dec 7, 2010
1 parent aab191d commit 6edb5c0
Show file tree
Hide file tree
Showing 8 changed files with 198 additions and 70 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ doc
*.gem
.redcar
**.swp

.bundle/
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
source "http://rubygems.org"
gemspec
50 changes: 50 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
PATH
remote: .
specs:
stateflow (0.2.3)

GEM
remote: http://rubygems.org/
specs:
activemodel (3.0.3)
activesupport (= 3.0.3)
builder (~> 2.1.2)
i18n (~> 0.4)
activerecord (3.0.3)
activemodel (= 3.0.3)
activesupport (= 3.0.3)
arel (~> 2.0.2)
tzinfo (~> 0.3.23)
activesupport (3.0.3)
arel (2.0.6)
bson (1.1.4)
builder (2.1.2)
diff-lcs (1.1.2)
durran-validatable (2.0.1)
i18n (0.5.0)
leshill-will_paginate (2.3.11)
mongo (1.1.4)
bson (>= 1.1.1)
mongoid (1.0.6)
activesupport (>= 2.2.2)
durran-validatable (>= 2.0.1)
leshill-will_paginate (>= 2.3.11)
mongo (>= 0.18.2)
rspec (2.2.0)
rspec-core (~> 2.2)
rspec-expectations (~> 2.2)
rspec-mocks (~> 2.2)
rspec-core (2.2.1)
rspec-expectations (2.2.0)
diff-lcs (~> 1.1.2)
rspec-mocks (2.2.0)
tzinfo (0.3.23)

PLATFORMS
ruby

DEPENDENCIES
activerecord
mongoid
rspec
stateflow!
4 changes: 2 additions & 2 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ Echoe.new('stateflow', '0.2.3') do |p|
p.author = "Ryan Oberholzer"
p.email = "[email protected]"
p.ignore_pattern = ["tmp/*", "script/*"]
p.development_dependencies = ["rspec"]
end
p.development_dependencies = ["rspec", "activerecord", "mongoid"]
end
102 changes: 70 additions & 32 deletions spec/orm/activerecord_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,18 @@

class TestMigration < ActiveRecord::Migration
def self.up
create_table :robots, :force => true do |t|
create_table :active_record_robots, :force => true do |t|
t.column :state, :string
t.column :name, :string
end
end

def self.down
drop_table :robots
drop_table :active_record_robots
end
end

class Robot < ActiveRecord::Base
class ActiveRecordRobot < ActiveRecord::Base
include Stateflow

stateflow do
Expand All @@ -42,51 +42,89 @@ class Robot < ActiveRecord::Base
describe Stateflow::Persistence::ActiveRecord do
before(:all) { TestMigration.up }
after(:all) { TestMigration.down }
after { Robot.delete_all }
after { ActiveRecordRobot.delete_all }

let(:robot) { Robot.new }
let(:robot) { ActiveRecordRobot.new }

it "should save to persistence with bang method" do
@robot = robot
@robot.state = "red"
@robot.new_record?.should be_true
describe "includes" do
it "should include current_state" do
robot.respond_to?(:current_state).should be_true
end

it "should include current_state=" do
robot.respond_to?(:set_current_state).should be_true
end

it "should include save_to_persistence" do
robot.respond_to?(:save_to_persistence).should be_true
end

it "should include load_from_persistence" do
robot.respond_to?(:load_from_persistence).should be_true
end
end

describe "bang method" do
before do
@robot = robot
@robot.state = "red"
end

@robot.change!
it "should call the set_current_state with save being true" do
@robot.should_receive(:set_current_state).with(@robot.machine.states[:green], {:save=>true})
@robot.change!
end

@robot.new_record?.should be_false
@robot.reload.state.should == "green"
it "should save the record" do
@robot.new_record?.should be_true
@robot.change!
@robot.new_record?.should be_false
@robot.reload.state.should == "green"
end
end

it "should not save to persistence with no bang method" do
@robot = robot
@robot.state = "red"
@robot.new_record?.should be_true
describe "non bang method" do
before do
@robot = robot
@robot.state = "red"
end

@robot.change
it "should call the set_current_state with save being false" do
@robot.should_receive(:set_current_state).with(@robot.machine.states[:green], {:save=>false})
@robot.change
end

@robot.new_record?.should be_true
@robot.state.should == "green"
it "should not save the record" do
@robot.new_record?.should be_true
@robot.change
@robot.new_record?.should be_true
@robot.state.should == "green"
end
end

it "Make sure stateflow saves the initial state if no state is set" do
@robot = robot
@robot3 = robot

@robot.save
@robot.reload
@robot3.save
@robot3.reload

@robot.state.should == "red"
@robot3.state.should == "red"
end

it "should load state from persistence" do
@robot = robot
@robot.state = "green"
@robot.name = "Bottie"
@robot.save
describe "load from persistence" do
before do
@robot = robot
@robot.state = "green"
@robot.name = "Bottie"
@robot.save
end

it "should call the load_from_persistence method" do
@robot.reload
@robot.should_receive(:load_from_persistence)

result = Robot.find_by_name("Bottie")
result.should_not be_blank
result.state.should == "green"
result.current_state.name.should == :green
@robot.current_state
end
end
end

94 changes: 66 additions & 28 deletions spec/orm/mongoid_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
connection = Mongo::Connection.new
Mongoid.database = connection.db("stateflow_test")

class Robot
class MongoRobot
include Mongoid::Document
include Stateflow

Expand All @@ -24,31 +24,65 @@ class Robot
end
end

describe Stateflow::Persistence::ActiveRecord do
after { Robot.collection.drop }
describe Stateflow::Persistence::Mongoid do
after { MongoRobot.collection.drop }

let(:robot) { Robot.new }
let(:robot) { MongoRobot.new }

describe "includes" do
it "should include current_state" do
robot.respond_to?(:current_state).should be_true
end

it "should save to persistence with bang method" do
@robot = robot
@robot.state = "red"
@robot.new_record?.should be_true
it "should include current_state=" do
robot.respond_to?(:set_current_state).should be_true
end

@robot.change!
it "should include save_to_persistence" do
robot.respond_to?(:save_to_persistence).should be_true
end

@robot.new_record?.should be_false
@robot.reload.state.should == "green"
it "should include load_from_persistence" do
robot.respond_to?(:load_from_persistence).should be_true
end
end

describe "bang method" do
before do
@robot = robot
@robot.state = "red"
end

it "should not save to persistence with no bang method" do
@robot = robot
@robot.state = "red"
@robot.new_record?.should be_true
it "should call the set_current_state with save being true" do
@robot.should_receive(:set_current_state).with(@robot.machine.states[:green], {:save=>true})
@robot.change!
end

@robot.change
it "should save the record" do
@robot.new_record?.should be_true
@robot.change!
@robot.new_record?.should be_false
@robot.reload.state.should == "green"
end
end

@robot.new_record?.should be_true
@robot.state.should == "green"
describe "non bang method" do
before do
@robot = robot
@robot.state = "red"
end

it "should call the set_current_state with save being false" do
@robot.should_receive(:set_current_state).with(@robot.machine.states[:green], {:save=>false})
@robot.change
end

it "should not save the record" do
@robot.new_record?.should be_true
@robot.change
@robot.new_record?.should be_true
@robot.state.should == "green"
end
end

it "Make sure stateflow saves the initial state if no state is set" do
Expand All @@ -59,17 +93,21 @@ class Robot

@robot.state.should == "red"
end

describe "load from persistence" do
before do
@robot = robot
@robot.state = "green"
@robot.name = "Bottie"
@robot.save
end

it "should call the load_from_persistence method" do
@robot.reload
@robot.should_receive(:load_from_persistence)

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
@robot.current_state
end
end
end

6 changes: 0 additions & 6 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,3 @@
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))

require 'stateflow'

require 'spec'
require 'spec/autorun'

Spec::Runner.configure do |config|
end
8 changes: 7 additions & 1 deletion stateflow.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Gem::Specification.new do |s|

s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
s.authors = ["Ryan Oberholzer"]
s.date = %q{2010-11-16}
s.date = %q{2010-12-07}
s.description = %q{State machine that allows dynamic transitions for business workflows}
s.email = %q{[email protected]}
s.extra_rdoc_files = ["CHANGELOG.rdoc", "README.rdoc", "lib/stateflow.rb", "lib/stateflow/event.rb", "lib/stateflow/exception.rb", "lib/stateflow/machine.rb", "lib/stateflow/persistence.rb", "lib/stateflow/persistence/active_record.rb", "lib/stateflow/persistence/mongo_mapper.rb", "lib/stateflow/persistence/mongoid.rb", "lib/stateflow/persistence/none.rb", "lib/stateflow/state.rb", "lib/stateflow/transition.rb"]
Expand All @@ -24,10 +24,16 @@ Gem::Specification.new do |s|

if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
s.add_development_dependency(%q<rspec>, [">= 0"])
s.add_development_dependency(%q<activerecord>, [">= 0"])
s.add_development_dependency(%q<mongoid>, [">= 0"])
else
s.add_dependency(%q<rspec>, [">= 0"])
s.add_dependency(%q<activerecord>, [">= 0"])
s.add_dependency(%q<mongoid>, [">= 0"])
end
else
s.add_dependency(%q<rspec>, [">= 0"])
s.add_dependency(%q<activerecord>, [">= 0"])
s.add_dependency(%q<mongoid>, [">= 0"])
end
end

0 comments on commit 6edb5c0

Please sign in to comment.