From 0c0b6ecff23444fe10e67d1841afcb424157892b Mon Sep 17 00:00:00 2001 From: Nathan Date: Mon, 11 May 2009 17:17:53 -0500 Subject: [PATCH] added spec to show problem with validation of array of casted models --- spec/couchrest/more/casted_model_spec.rb | 22 +++++++++++++++++++++- spec/fixtures/more/cat.rb | 18 ++++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 spec/fixtures/more/cat.rb diff --git a/spec/couchrest/more/casted_model_spec.rb b/spec/couchrest/more/casted_model_spec.rb index 82558c1..0a4428f 100644 --- a/spec/couchrest/more/casted_model_spec.rb +++ b/spec/couchrest/more/casted_model_spec.rb @@ -1,5 +1,6 @@ require File.join(File.dirname(__FILE__), '..', '..', 'spec_helper') require File.join(FIXTURE_PATH, 'more', 'card') +require File.join(FIXTURE_PATH, 'more', 'cat') class WithCastedModelMixin < Hash include CouchRest::CastedModel @@ -67,7 +68,7 @@ describe CouchRest::CastedModel do describe "casted as an array of a different type" do before(:each) do - @obj = DummyModel.new(:keywords => ['couch', 'sofa', 'relax', 'canapé']) + @obj = DummyModel.new(:keywords => ['couch', 'sofa', 'relax', 'canape']) end it "should cast the array propery" do @@ -103,5 +104,24 @@ describe CouchRest::CastedModel do end end + + 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) + @cat.save.should be_false + end + end end diff --git a/spec/fixtures/more/cat.rb b/spec/fixtures/more/cat.rb new file mode 100644 index 0000000..f53a2a2 --- /dev/null +++ b/spec/fixtures/more/cat.rb @@ -0,0 +1,18 @@ +class Cat < CouchRest::ExtendedDocument + include ::CouchRest::Validation + + # Set the default database to use + use_database TEST_SERVER.default_database + + property :name + property :toys, :cast_as => ['CatToy'], :default => [] +end + +class CatToy < Hash + include ::CouchRest::CastedModel + include ::CouchRest::Validation + + property :name + + validates_present :name +end \ No newline at end of file