2009-05-14 05:44:57 +02:00
|
|
|
# encoding: utf-8
|
|
|
|
|
2009-02-09 20:20:23 +01:00
|
|
|
require File.join(File.dirname(__FILE__), '..', '..', 'spec_helper')
|
|
|
|
require File.join(FIXTURE_PATH, 'more', 'card')
|
2009-05-12 00:17:53 +02:00
|
|
|
require File.join(FIXTURE_PATH, 'more', 'cat')
|
2009-05-14 07:50:29 +02:00
|
|
|
require File.join(FIXTURE_PATH, 'more', 'person')
|
|
|
|
|
2009-02-09 20:20:23 +01:00
|
|
|
|
2009-02-10 11:15:39 +01:00
|
|
|
class WithCastedModelMixin < Hash
|
|
|
|
include CouchRest::CastedModel
|
|
|
|
property :name
|
2009-04-02 16:00:28 +02:00
|
|
|
property :no_value
|
2009-02-10 11:15:39 +01:00
|
|
|
end
|
2009-02-09 20:20:23 +01:00
|
|
|
|
2009-02-10 11:15:39 +01:00
|
|
|
class DummyModel < CouchRest::ExtendedDocument
|
|
|
|
use_database TEST_SERVER.default_database
|
|
|
|
raise "Default DB not set" if TEST_SERVER.default_database.nil?
|
|
|
|
property :casted_attribute, :cast_as => 'WithCastedModelMixin'
|
2009-02-13 05:28:07 +01:00
|
|
|
property :keywords, :cast_as => ["String"]
|
2009-02-10 11:15:39 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
describe CouchRest::CastedModel do
|
2009-02-09 20:20:23 +01:00
|
|
|
|
|
|
|
describe "A non hash class including CastedModel" do
|
|
|
|
it "should fail raising and include error" do
|
|
|
|
lambda do
|
|
|
|
class NotAHashButWithCastedModelMixin
|
|
|
|
include CouchRest::CastedModel
|
|
|
|
property :name
|
|
|
|
end
|
|
|
|
|
|
|
|
end.should raise_error
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "isolated" do
|
|
|
|
before(:each) do
|
|
|
|
@obj = WithCastedModelMixin.new
|
|
|
|
end
|
|
|
|
it "should automatically include the property mixin and define getters and setters" do
|
|
|
|
@obj.name = 'Matt'
|
|
|
|
@obj.name.should == 'Matt'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "casted as attribute" do
|
|
|
|
before(:each) do
|
|
|
|
@obj = DummyModel.new(:casted_attribute => {:name => 'whatever'})
|
|
|
|
@casted_obj = @obj.casted_attribute
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should be available from its parent" do
|
|
|
|
@casted_obj.should be_an_instance_of(WithCastedModelMixin)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should have the getters defined" do
|
|
|
|
@casted_obj.name.should == 'whatever'
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should know who casted it" do
|
|
|
|
@casted_obj.casted_by.should == @obj
|
|
|
|
end
|
2009-04-02 16:00:28 +02:00
|
|
|
|
|
|
|
it "should return nil for the 'no_value' attribute" do
|
|
|
|
@casted_obj.no_value.should be_nil
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should return nil for the unknown attribute" do
|
|
|
|
@casted_obj["unknown"].should be_nil
|
|
|
|
end
|
2009-02-10 11:15:39 +01:00
|
|
|
end
|
|
|
|
|
2009-02-13 05:28:07 +01:00
|
|
|
describe "casted as an array of a different type" do
|
|
|
|
before(:each) do
|
2009-05-14 05:44:57 +02:00
|
|
|
@obj = DummyModel.new(:keywords => ['couch', 'sofa', 'relax', 'canapé'])
|
2009-02-13 05:28:07 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should cast the array propery" do
|
|
|
|
@obj.keywords.should be_an_instance_of(Array)
|
|
|
|
@obj.keywords.first.should == 'couch'
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2009-02-10 11:15:39 +01:00
|
|
|
describe "saved document with casted models" do
|
|
|
|
before(:each) do
|
2009-03-08 15:27:30 +01:00
|
|
|
reset_test_db!
|
2009-02-10 11:15:39 +01:00
|
|
|
@obj = DummyModel.new(:casted_attribute => {:name => 'whatever'})
|
2009-02-11 01:10:35 +01:00
|
|
|
@obj.save.should be_true
|
2009-02-17 09:36:11 +01:00
|
|
|
@obj = DummyModel.get(@obj.id)
|
2009-02-10 11:15:39 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should be able to load with the casted models" do
|
|
|
|
casted_obj = @obj.casted_attribute
|
|
|
|
casted_obj.should_not be_nil
|
|
|
|
casted_obj.should be_an_instance_of(WithCastedModelMixin)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should have defined getters for the casted model" do
|
|
|
|
casted_obj = @obj.casted_attribute
|
|
|
|
casted_obj.name.should == "whatever"
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should have defined setters for the casted model" do
|
|
|
|
casted_obj = @obj.casted_attribute
|
|
|
|
casted_obj.name = "test"
|
|
|
|
casted_obj.name.should == "test"
|
|
|
|
end
|
2009-02-09 20:20:23 +01:00
|
|
|
|
|
|
|
end
|
2009-05-12 00:17:53 +02:00
|
|
|
|
|
|
|
describe "saving document with array of casted models and validation" do
|
|
|
|
before :each do
|
|
|
|
@cat = Cat.new
|
|
|
|
@cat.save
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should save" do
|
|
|
|
toy = CatToy.new :name => "Mouse"
|
|
|
|
@cat.toys.push(toy)
|
|
|
|
@cat.save.should be_true
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should fail because name is not present" do
|
|
|
|
toy = CatToy.new
|
|
|
|
@cat.toys.push(toy)
|
2009-05-14 07:50:29 +02:00
|
|
|
@cat.should_not be_valid
|
2009-05-12 00:17:53 +02:00
|
|
|
@cat.save.should be_false
|
|
|
|
end
|
2009-05-14 07:50:29 +02:00
|
|
|
|
|
|
|
it "should not fail if the casted model doesn't have validation" do
|
|
|
|
Cat.property :masters, :cast_as => ['Person'], :default => []
|
|
|
|
Cat.validates_present :name
|
|
|
|
cat = Cat.new(:name => 'kitty')
|
|
|
|
cat.should be_valid
|
|
|
|
cat.masters.push Person.new
|
|
|
|
cat.should be_valid
|
|
|
|
end
|
|
|
|
|
2009-05-12 00:17:53 +02:00
|
|
|
end
|
2009-02-09 20:20:23 +01:00
|
|
|
|
2009-03-08 15:27:30 +01:00
|
|
|
end
|