2009-02-06 03:57:11 +01:00
|
|
|
require File.dirname(__FILE__) + '/../../spec_helper'
|
|
|
|
|
|
|
|
describe "ExtendedDocument" do
|
|
|
|
|
2009-02-10 11:15:39 +01:00
|
|
|
class WithDefaultValues < CouchRest::ExtendedDocument
|
|
|
|
use_database TEST_SERVER.default_database
|
|
|
|
property :preset, :default => {:right => 10, :top_align => false}
|
|
|
|
property :set_by_proc, :default => Proc.new{Time.now}, :cast_as => 'Time'
|
|
|
|
property :name
|
|
|
|
timestamps!
|
|
|
|
end
|
|
|
|
|
2009-02-10 00:12:22 +01:00
|
|
|
before(:each) do
|
|
|
|
@obj = WithDefaultValues.new
|
|
|
|
end
|
|
|
|
|
2009-02-06 03:57:11 +01:00
|
|
|
describe "with default" do
|
|
|
|
|
2009-02-09 21:08:55 +01:00
|
|
|
it "should have the default value set at initalization" do
|
2009-02-06 03:57:11 +01:00
|
|
|
@obj.preset.should == {:right => 10, :top_align => false}
|
|
|
|
end
|
2009-02-09 21:08:55 +01:00
|
|
|
|
|
|
|
it "should automatically call a proc default at initialization" do
|
|
|
|
@obj.set_by_proc.should be_an_instance_of(Time)
|
|
|
|
@obj.set_by_proc.should == @obj.set_by_proc
|
|
|
|
@obj.set_by_proc.should < Time.now
|
|
|
|
end
|
2009-02-06 03:57:11 +01:00
|
|
|
end
|
|
|
|
|
2009-02-10 00:12:22 +01:00
|
|
|
describe "timestamping" do
|
|
|
|
|
|
|
|
it "should define the updated_at and created_at getters and set the values" do
|
|
|
|
@obj.save
|
|
|
|
obj = WithDefaultValues.get(@obj.id)
|
2009-02-10 11:15:39 +01:00
|
|
|
obj.should be_an_instance_of(WithDefaultValues)
|
2009-02-10 00:12:22 +01:00
|
|
|
obj.created_at.should be_an_instance_of(Time)
|
|
|
|
obj.updated_at.should be_an_instance_of(Time)
|
|
|
|
obj.created_at.to_s.should == @obj.updated_at.to_s
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "saving and retrieving" do
|
|
|
|
|
|
|
|
it "should work fine" do
|
|
|
|
@obj.name = "should be easily saved and retrieved"
|
|
|
|
@obj.save
|
|
|
|
saved_obj = WithDefaultValues.get(@obj.id)
|
|
|
|
saved_obj.should_not be_nil
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should parse the Time attributes automatically" do
|
|
|
|
@obj.name = "should parse the Time attributes automatically"
|
|
|
|
@obj.set_by_proc.should be_an_instance_of(Time)
|
|
|
|
@obj.save
|
|
|
|
@obj.set_by_proc.should be_an_instance_of(Time)
|
|
|
|
saved_obj = WithDefaultValues.get(@obj.id)
|
|
|
|
saved_obj.set_by_proc.should be_an_instance_of(Time)
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2009-02-06 03:57:11 +01:00
|
|
|
end
|