require File.dirname(__FILE__) + '/spec_helper' describe Blueprints do describe "scenario files" do it "should be loaded from specified dirs" do Blueprints::PLAN_FILES.should == ["blueprint.rb", "blueprint/*.rb", "blueprints.rb", "blueprints/*.rb", "spec/blueprint.rb", "spec/blueprint/*.rb", "spec/blueprints.rb", "spec/blueprints/*.rb", "test/blueprint.rb", "test/blueprint/*.rb", "test/blueprints.rb", "test/blueprints/*.rb"] end end describe "with apple scenario" do before do build :apple end it "should create @apple" do @apple.should_not be_nil end it "should create Fruit @apple" do @apple.should be_instance_of(Fruit) end it "should not create @banana" do @banana.should be_nil end it "should have correct species" do @apple.species.should == 'apple' end end describe "with bananas_and_apples scenario" do before do build :bananas_and_apples end it "should have correct @apple species" do @apple.species.should == 'apple' end it "should have correct @banana species" do @banana.species.should == 'banana' end end describe "with fruit scenario" do before do build :fruit end it "should have 2 fruits" do @fruit.should have(2).items end it "should have an @apple" do @apple.species.should == 'apple' end it "should have an @orange" do @orange.species.should == 'orange' end it "should have no @banana" do @banana.should be_nil end end describe 'with preloaded cherry scenario' do it "should have correct size after changed by second test" do @cherry.average_diameter.should == 3 @cherry.update_attribute(:average_diameter, 1) @cherry.average_diameter.should == 1 end it "should have correct size" do @cherry.average_diameter.should == 3 @cherry.update_attribute(:average_diameter, 5) @cherry.average_diameter.should == 5 end it "should create big cherry" do @big_cherry.species.should == 'cherry' end end describe 'demolish' do before do build :apple end it "should clear scenarios when calling demolish" do demolish Fruit.count.should == 0 end it "should clear only tables passed" do Tree.create!(:name => 'oak') demolish :fruits Tree.count.should == 1 Fruit.count.should == 0 end it "should mark scenarios as undone when passed :undone option" do build :fruit demolish :undo => [:apple] Fruit.count.should == 0 build :fruit Fruit.count.should == 1 end it "should mark all scenarios as undone when passed :undone option as :all" do build :fruit demolish :undo => :all Fruit.count.should == 0 build :fruit Fruit.count.should == 2 end it "should raise error when not executed scenarios passed to :undo option" do lambda { demolish :undo => :orange }.should raise_error(ArgumentError) end end describe 'delete policies' do before do Blueprints::Plan.plans.should_receive(:empty?).and_return(true) Blueprints.should_receive(:load_scenarios_files).with(Blueprints::PLAN_FILES) Blueprints::Plan.should_receive(:prebuild).with(nil) end it "should allow using custom delete policy" do ActiveRecord::Base.connection.should_receive(:delete).with("TRUNCATE fruits") ActiveRecord::Base.connection.should_receive(:delete).with("TRUNCATE trees") Blueprints.load(:delete_policy => :truncate) end it "should default to :delete policy if unexisting policy given" do ActiveRecord::Base.connection.should_receive(:delete).with("DELETE FROM fruits") ActiveRecord::Base.connection.should_receive(:delete).with("DELETE FROM trees") Blueprints.load(:delete_policy => :ukndown) end end describe 'with many apples scenario' do before do build :many_apples, :cherry, :cherry_basket end it "should create only one apple" do Fruit.all(:conditions => 'species = "apple"').size.should == 1 end it "should create only two cherries even if they were preloaded" do Fruit.all(:conditions => 'species = "cherry"').size.should == 2 end it "should contain cherries in basket if basket is loaded in test and cherries preloaded" do @cherry_basket.should == [@cherry, @big_cherry] end end describe 'transactions' do before do build :apple end it "should drop only inner transaction" do @apple.reload.should_not be_nil begin ActiveRecord::Base.transaction do f = Fruit.create(:species => 'orange') f.reload.should_not be_nil raise 'some error' end rescue end @apple.reload.should_not be_nil Fruit.find_by_species('orange').should be_nil end end describe 'errors' do it 'should raise ScenarioNotFoundError when scenario could not be found' do lambda { build :not_existing }.should raise_error(Blueprints::PlanNotFoundError, "Plan(s) not found 'not_existing'") end it 'should raise ScenarioNotFoundError when scenario parent could not be found' do lambda { build :parent_not_existing }.should raise_error(Blueprints::PlanNotFoundError, "Plan(s) not found 'not_existing'") end it 'should raise TypeError when scenario name is not symbol or string' do lambda { Blueprints::Plan.new(1) }.should raise_error(TypeError, "Pass plan names as strings or symbols only, cannot build plan 1") end end #describe "with pitted namespace" do # before do # Hornsby.build('pitted:peach').copy_ivars(self) # end # it "should have @peach" do # @peach.species.should == 'peach' # end #end end