Array Properties accept hash with ordered keys and raise error for anything else

This commit is contained in:
Sam Lown 2010-06-18 01:24:49 +02:00
parent dd55466764
commit 1b89f1e1df
7 changed files with 82 additions and 10 deletions

View file

@ -146,6 +146,12 @@ describe "Assocations" do
@invoice.entries.should be_empty
end
it "should ignore nil entries" do
@invoice.entries = [ nil ]
@invoice.entry_ids.should be_empty
@invoice.entries.should be_empty
end
describe "proxy" do
it "should ensure new entries to proxy are matched" do

View file

@ -348,7 +348,7 @@ describe CouchRest::CastedModel do
describe "calling base_doc from a nested casted model" do
before :each do
@course = Course.new(:title => 'Science 101')
@professor = Person.new(:name => 'Professor Plum')
@professor = Person.new(:name => ['Professor', 'Plum'])
@cat = Cat.new(:name => 'Scratchy')
@toy1 = CatToy.new
@toy2 = CatToy.new

View file

@ -42,13 +42,14 @@ describe "ExtendedDocument properties" do
end
it "should let you use an alias for a casted attribute" do
@card.cast_alias = Person.new(:name => "Aimonetti")
@card.cast_alias = Person.new(:name => ["Aimonetti"])
@card.cast_alias.name.should == ["Aimonetti"]
@card.calias.name.should == ["Aimonetti"]
card = Card.new(:first_name => "matt", :cast_alias => {:name => "Aimonetti"})
card = Card.new(:first_name => "matt", :cast_alias => {:name => ["Aimonetti"]})
card.cast_alias.name.should == ["Aimonetti"]
card.calias.name.should == ["Aimonetti"]
end
it "should be auto timestamped" do
@card.created_at.should be_nil
@ -660,6 +661,57 @@ describe "ExtendedDocument properties" do
end
end
describe "properties of array of casted models" do
before(:each) do
@course = Course.new :title => 'Test Course'
end
it "should allow attribute to be set from an array of objects" do
@course.questions = [Question.new(:q => "works?"), Question.new(:q => "Meaning of Life?")]
@course.questions.length.should eql(2)
end
it "should allow attribute to be set from an array of hashes" do
@course.questions = [{:q => "works?"}, {:q => "Meaning of Life?"}]
@course.questions.length.should eql(2)
@course.questions.last.q.should eql("Meaning of Life?")
@course.questions.last.class.should eql(Question) # typecasting
end
it "should allow attribute to be set from hash with ordered keys and objects" do
@course.questions = { '0' => Question.new(:q => "Test1"), '1' => Question.new(:q => 'Test2') }
@course.questions.length.should eql(2)
@course.questions.last.q.should eql('Test2')
@course.questions.last.class.should eql(Question)
end
it "should allow attribute to be set from hash with ordered keys and sub-hashes" do
@course.questions = { '0' => {:q => "Test1"}, '1' => {:q => 'Test2'} }
@course.questions.length.should eql(2)
@course.questions.last.q.should eql('Test2')
@course.questions.last.class.should eql(Question)
end
it "should allow attribute to be set from hash with ordered keys and HashWithIndifferentAccess" do
# This is similar to what you'd find in an HTML POST parameters
hash = HashWithIndifferentAccess.new({ '0' => {:q => "Test1"}, '1' => {:q => 'Test2'} })
@course.questions = hash
@course.questions.length.should eql(2)
@course.questions.last.q.should eql('Test2')
@course.questions.last.class.should eql(Question)
end
it "should raise an error if attempting to set single value for array type" do
lambda {
@course.questions = Question.new(:q => 'test1')
}.should raise_error
end
end
describe "a casted model retrieved from the database" do
before(:each) do
reset_test_db!