default values and lambda unique_ids

This commit is contained in:
Chris Anderson 2008-10-02 14:11:04 -07:00
parent 0b0ac14b19
commit 8ac6b78170
2 changed files with 86 additions and 3 deletions

View file

@ -1,7 +1,17 @@
require File.dirname(__FILE__) + '/../../spec_helper'
class Basic < CouchRest::Model
end
class WithTemplate < CouchRest::Model
unique_id do |model|
model['important-field']
end
set_default({
:preset => 'value',
'more-template' => [1,2,3]
})
key_accessor :preset
end
class Article < CouchRest::Model
@ -105,6 +115,15 @@ describe CouchRest::Model do
end
end
describe "a model with template values" do
before(:all) do
@tmpl = WithTemplate.new
end
it "should have fields set when new" do
@tmpl.preset.should == 'value'
end
end
describe "getting a model" do
before(:all) do
@art = Article.new(:title => 'All About Getting')
@ -184,6 +203,48 @@ describe CouchRest::Model do
end
end
describe "saving a model with a unique_id lambda" do
before(:each) do
@templated = WithTemplate.new
@old = WithTemplate.get('very-important') rescue nil
@old.destroy if @old
end
it "should require the field" do
lambda{@templated.save}.should raise_error
@templated['important-field'] = 'very-important'
@templated.save.should == true
end
it "should save with the id" do
@templated['important-field'] = 'very-important'
@templated.save.should == true
t = WithTemplate.get('very-important')
t.should == @templated
end
it "should not change the id on update" do
@templated['important-field'] = 'very-important'
@templated.save.should == true
@templated['important-field'] = 'not-important'
@templated.save.should == true
t = WithTemplate.get('very-important')
t.should == @templated
end
it "should raise an error when the id is taken" do
@templated['important-field'] = 'very-important'
@templated.save.should == true
lambda{WithTemplate.new('important-field' => 'very-important').save}.should raise_error
end
it "should set the id" do
@templated['important-field'] = 'very-important'
@templated.save.should == true
@templated.id.should == 'very-important'
end
end
describe "a model with timestamps" do
before(:all) do
@art = Article.new(:title => "Saving this")