Modified:
trunk/spec/base/after_delete_spec.rb
trunk/spec/base/after_save_spec.rb
trunk/spec/base/before_delete_spec.rb
trunk/spec/base/before_save_spec.rb
Log:
- Added specs for callbacks which pass blocks, or objects to the
callback instead of method names
Modified: trunk/spec/base/after_delete_spec.rb
==============================================================================
--- trunk/spec/base/after_delete_spec.rb (original)
+++ trunk/spec/base/after_delete_spec.rb Tue Jan 22 08:05:41 2008
@@ -39,10 +39,72 @@
end
end
-describe "ActiveCouch::Base #after_save method with a block as
argument" do
- it "should execute the block as a param to after_save"
+describe "ActiveCouch::Base #after_delete method with a block as
argument" do
+ before(:each) do
+ class Person < ActiveCouch::Base
+ site 'http://localhost:5984/'
+ has :name
+ has :delete_status
+ # Callback, after the actual save happens
+ after_delete { |record| record.delete_status = 'Deleted McLovin' }
+ end
+ # Migration needed for this spec
+ ActiveCouch::Migrator.create_database('http://localhost:5984/', 'people')
+ end
+
+ after(:each) do
+ # Migration needed for this spec
+ ActiveCouch::Migrator.delete_database('http://localhost:5984/', 'people')
+ end
+
+ it "should execute the block as a param to after_delete" do
+ p = Person.new(:name => 'McLovin')
+ # First, it must be empty...
+ p.delete_status.should == ""
+ # then it must be saved...
+ p.save
+ # Delete should return true...
+ p.delete.should == true
+ p.delete_status.should == "Deleted McLovin"
+ end
end
describe "ActiveCouch::Base #after_save method with an Object (which
implements after_save) as argument" do
- it "should call before_save in the object passed as a param to after_save"
+ before(:each) do
+ class DeleteStatusSetter
+ def initialize(attribute)
+ @attribute = attribute
+ end
+
+ def after_delete(record)
+ record.delete_status = 'Deleted McLovin'
+ end
+ end
+
+ class Person < ActiveCouch::Base
+ site 'http://localhost:5984/'
+ has :name
+ has :delete_status
+ # Callback, after the actual save happens
+ after_delete DeleteStatusSetter.new("delete_status")
+ end
+ # Migration needed for this spec
+ ActiveCouch::Migrator.create_database('http://localhost:5984/', 'people')
+ end
+
+ after(:each) do
+ # Migration needed for this spec
+ ActiveCouch::Migrator.delete_database('http://localhost:5984/', 'people')
+ end
+
+ it "should call before_save in the object passed as a param to
after_delete" do
+ p = Person.new(:name => 'McLovin')
+ # First, it must be empty...
+ p.delete_status.should == ""
+ # then it must be saved...
+ p.save
+ # Delete should return true...
+ p.delete.should == true
+ p.delete_status.should == "Deleted McLovin"
+ end
end
Modified: trunk/spec/base/after_save_spec.rb
==============================================================================
--- trunk/spec/base/after_save_spec.rb (original)
+++ trunk/spec/base/after_save_spec.rb Tue Jan 22 08:05:41 2008
@@ -39,9 +39,65 @@
end
describe "ActiveCouch::Base #after_save method with a block as
argument" do
- it "should execute the block as a param to after_save"
+ before(:each) do
+ class Person < ActiveCouch::Base
+ site 'http://localhost:5984/'
+ has :name
+ has :saved_revision
+ # Callback, after the actual save happens
+ after_save { |record| record.saved_revision = record.rev }
+ end
+ # Migration needed for this spec
+ ActiveCouch::Migrator.create_database('http://localhost:5984/', 'people')
+ end
+
+ after(:each) do
+ # Migration needed for this spec
+ ActiveCouch::Migrator.delete_database('http://localhost:5984/', 'people')
+ end
+
+ it "should execute the block as a param to after_save" do
+ p = Person.new(:name => 'McLovin')
+ # Save should return true...
+ p.save.should == true
+ # ...and saved_id must not be nil (because it is set to the revision)
+ p.saved_revision.should_not == nil
+ end
end
describe "ActiveCouch::Base #after_save method with an Object (which
implements after_save) as argument" do
- it "should call before_save in the object passed as a param to after_save"
-end
+ before(:each) do
+ class RevisionSetter
+ def initialize(attribute)
+ @attribute = attribute
+ end
+
+ def after_save(record)
+ record.saved_revision = record.rev
+ end
+ end
+
+ class Person < ActiveCouch::Base
+ site 'http://localhost:5984/'
+ has :name
+ has :saved_revision
+ # Callback, after the actual save happens
+ after_save RevisionSetter.new("saved_revision")
+ end
+ # Migration needed for this spec
+ ActiveCouch::Migrator.create_database('http://localhost:5984/', 'people')
+ end
+
+ after(:each) do
+ # Migration needed for this spec
+ ActiveCouch::Migrator.delete_database('http://localhost:5984/', 'people')
+ end
+
+ it "should call before_save in the object passed as a param to
after_save" do
+ p = Person.new(:name => 'McLovin')
+ # Save should return true...
+ p.save.should == true
+ # ...and saved_id must not be nil (because it is set to the revision)
+ p.saved_revision.should_not == nil
+ end
+end
\ No newline at end of file
Modified: trunk/spec/base/before_delete_spec.rb
==============================================================================
--- trunk/spec/base/before_delete_spec.rb (original)
+++ trunk/spec/base/before_delete_spec.rb Tue Jan 22 08:05:41 2008
@@ -40,9 +40,71 @@
end
describe "ActiveCouch::Base #before_save method with a block as
argument" do
- it "should execute the block as a param to before_save"
+ before(:each) do
+ class Person < ActiveCouch::Base
+ site 'http://localhost:5984/'
+ has :name
+ has :age, :which_is => :number
+ # Callback, before the actual save happens
+ before_delete { |record| record.age = 0 }
+ end
+ # Migration needed for this spec
+ ActiveCouch::Migrator.create_database('http://localhost:5984/', 'people')
+ end
+
+ after(:each) do
+ # Migration needed for this spec
+ ActiveCouch::Migrator.delete_database('http://localhost:5984/', 'people')
+ end
+
+ it "should execute the block as a param to before_save" do
+ # First save the object
+ p = Person.create(:name => 'McLovin', :age => 10)
+ # Before deleting, age must be 10
+ p.age.should == 10
+ # Delete the object, and...
+ p.delete.should == true
+ # ...age must equal 0
+ p.age.should == 0
+ end
end
describe "ActiveCouch::Base #before_save method with an Object (which
implements before_save) as argument" do
- it "should call before_save in the object passed as a param to before_save"
+ before(:each) do
+ class AgeSetter
+ def initialize(attribute)
+ @attribute = attribute
+ end
+
+ def before_delete(record)
+ record.age = 0
+ end
+ end
+
+ class Person < ActiveCouch::Base
+ site 'http://localhost:5984/'
+ has :name
+ has :age, :which_is => :number
+ # Callback, before the actual save happens
+ before_delete AgeSetter.new("age")
+ end
+ # Migration needed for this spec
+ ActiveCouch::Migrator.create_database('http://localhost:5984/', 'people')
+ end
+
+ after(:each) do
+ # Migration needed for this spec
+ ActiveCouch::Migrator.delete_database('http://localhost:5984/', 'people')
+ end
+
+ it "should call before_save in the object passed as a param to
before_delete" do
+ # First save the object
+ p = Person.create(:name => 'McLovin', :age => 10)
+ # Before deleting, age must be 10
+ p.age.should == 10
+ # Delete the object, and...
+ p.delete.should == true
+ # ...age must equal 0
+ p.age.should == 0
+ end
end
Modified: trunk/spec/base/before_save_spec.rb
==============================================================================
--- trunk/spec/base/before_save_spec.rb (original)
+++ trunk/spec/base/before_save_spec.rb Tue Jan 22 08:05:41 2008
@@ -37,9 +37,66 @@
end
describe "ActiveCouch::Base #before_save method with a block as
argument" do
- it "should execute the block as a param to before_save"
+ before(:each) do
+ class Person < ActiveCouch::Base
+ site 'http://localhost:5984/'
+ has :first_name; has :last_name
+ # Callback, before the actual save happens
+ before_save { |record| record.first_name = 'Seth' }
+ end
+
+ # Migration needed for this spec
+ ActiveCouch::Migrator.create_database('http://localhost:5984/', 'people')
+ end
+
+ after(:each) do
+ # Migration needed for this spec
+ ActiveCouch::Migrator.delete_database('http://localhost:5984/', 'people')
+ end
+
+ it "should execute the block as a param to before_save" do
+ p = Person.new(:last_name => 'McLovin')
+ # Save should return true...
+ p.save.should == true
+ # ...and the first_name must be set to 'Seth'
+ p.first_name.should == 'Seth'
+ end
end
describe "ActiveCouch::Base #before_save method with an Object (which
implements before_save) as argument" do
- it "should call before_save in the object passed as a param to before_save"
+ before(:each) do
+ class NameSetter
+ def initialize(attribute)
+ @attribute = attribute
+ end
+
+ def before_save(record)
+ record.first_name = 'Seth'
+ end
+ end
+
+ class Person < ActiveCouch::Base
+ site 'http://localhost:5984/'
+ has :first_name; has :last_name
+ # Callback, before the actual save happens
+ before_save NameSetter.new("first_name")
+ end
+
+ # Migration needed for this spec
+ ActiveCouch::Migrator.create_database('http://localhost:5984/', 'people')
+ end
+
+ after(:each) do
+ # Migration needed for this spec
+ ActiveCouch::Migrator.delete_database('http://localhost:5984/', 'people')
+ end
+
+ it "should call before_save in the object passed as a param to
before_save" do
+ p = Person.new(:last_name => 'McLovin')
+ # Save should return true...
+ p.save.should == true
+ # ...and the first_name must be set to 'Seth'
+ p.first_name.should == 'Seth'
+ end
+
end