2009-01-30 03:45:01 +01:00
|
|
|
require File.join(File.dirname(__FILE__), '..', '..', 'spec_helper')
|
2009-06-08 03:51:31 +02:00
|
|
|
require File.join(FIXTURE_PATH, 'more', 'person')
|
2009-01-30 03:45:01 +01:00
|
|
|
require File.join(FIXTURE_PATH, 'more', 'card')
|
2009-02-03 04:21:32 +01:00
|
|
|
require File.join(FIXTURE_PATH, 'more', 'invoice')
|
2009-02-25 07:51:13 +01:00
|
|
|
require File.join(FIXTURE_PATH, 'more', 'service')
|
|
|
|
require File.join(FIXTURE_PATH, 'more', 'event')
|
2009-05-29 02:56:42 +02:00
|
|
|
require File.join(FIXTURE_PATH, 'more', 'cat')
|
2009-02-09 20:20:23 +01:00
|
|
|
|
2009-01-30 03:45:01 +01:00
|
|
|
|
|
|
|
describe "ExtendedDocument properties" do
|
|
|
|
|
|
|
|
before(:each) do
|
2009-03-08 15:27:30 +01:00
|
|
|
reset_test_db!
|
2009-01-30 03:45:01 +01:00
|
|
|
@card = Card.new(:first_name => "matt")
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should be accessible from the object" do
|
|
|
|
@card.properties.should be_an_instance_of(Array)
|
|
|
|
@card.properties.map{|p| p.name}.should include("first_name")
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should let you access a property value (getter)" do
|
|
|
|
@card.first_name.should == "matt"
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should let you set a property value (setter)" do
|
|
|
|
@card.last_name = "Aimonetti"
|
|
|
|
@card.last_name.should == "Aimonetti"
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should not let you set a property value if it's read only" do
|
|
|
|
lambda{@card.read_only_value = "test"}.should raise_error
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should let you use an alias for an attribute" do
|
|
|
|
@card.last_name = "Aimonetti"
|
|
|
|
@card.family_name.should == "Aimonetti"
|
|
|
|
@card.family_name.should == @card.last_name
|
|
|
|
end
|
|
|
|
|
2009-06-08 03:51:31 +02:00
|
|
|
it "should let you use an alias for a casted attribute" do
|
|
|
|
@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.cast_alias.name.should == "Aimonetti"
|
|
|
|
card.calias.name.should == "Aimonetti"
|
|
|
|
end
|
|
|
|
|
2009-02-04 02:33:31 +01:00
|
|
|
it "should be auto timestamped" do
|
|
|
|
@card.created_at.should be_nil
|
|
|
|
@card.updated_at.should be_nil
|
2009-02-11 01:10:35 +01:00
|
|
|
@card.save.should be_true
|
2009-02-04 02:33:31 +01:00
|
|
|
@card.created_at.should_not be_nil
|
|
|
|
@card.updated_at.should_not be_nil
|
|
|
|
end
|
|
|
|
|
2009-02-03 04:21:32 +01:00
|
|
|
describe "validation" do
|
|
|
|
before(:each) do
|
|
|
|
@invoice = Invoice.new(:client_name => "matt", :employee_name => "Chris", :location => "San Diego, CA")
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should be able to be validated" do
|
2009-02-06 02:06:12 +01:00
|
|
|
@card.valid?.should == true
|
2009-02-03 01:10:07 +01:00
|
|
|
end
|
2009-02-09 20:20:23 +01:00
|
|
|
|
2009-02-03 04:21:32 +01:00
|
|
|
it "should let you validate the presence of an attribute" do
|
|
|
|
@card.first_name = nil
|
|
|
|
@card.should_not be_valid
|
|
|
|
@card.errors.should_not be_empty
|
|
|
|
@card.errors.on(:first_name).should == ["First name must not be blank"]
|
|
|
|
end
|
2009-03-24 23:37:52 +01:00
|
|
|
|
|
|
|
it "should let you look up errors for a field by a string name" do
|
|
|
|
@card.first_name = nil
|
|
|
|
@card.should_not be_valid
|
|
|
|
@card.errors.on('first_name').should == ["First name must not be blank"]
|
|
|
|
end
|
2009-02-09 20:20:23 +01:00
|
|
|
|
2009-02-03 04:21:32 +01:00
|
|
|
it "should validate the presence of 2 attributes" do
|
|
|
|
@invoice.clear
|
|
|
|
@invoice.should_not be_valid
|
|
|
|
@invoice.errors.should_not be_empty
|
2009-02-10 11:15:39 +01:00
|
|
|
@invoice.errors.on(:client_name).first.should == "Client name must not be blank"
|
2009-02-03 04:21:32 +01:00
|
|
|
@invoice.errors.on(:employee_name).should_not be_empty
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should let you set an error message" do
|
|
|
|
@invoice.location = nil
|
|
|
|
@invoice.valid?
|
2009-02-10 11:15:39 +01:00
|
|
|
@invoice.errors.on(:location).should == ["Hey stupid!, you forgot the location"]
|
2009-02-03 04:21:32 +01:00
|
|
|
end
|
2009-02-04 02:33:31 +01:00
|
|
|
|
|
|
|
it "should validate before saving" do
|
|
|
|
@invoice.location = nil
|
|
|
|
@invoice.should_not be_valid
|
|
|
|
@invoice.save.should be_false
|
2009-06-05 05:44:44 +02:00
|
|
|
@invoice.should be_new
|
2009-02-04 02:33:31 +01:00
|
|
|
end
|
2009-02-03 01:10:07 +01:00
|
|
|
end
|
|
|
|
|
2009-02-06 02:06:12 +01:00
|
|
|
describe "autovalidation" do
|
|
|
|
before(:each) do
|
|
|
|
@service = Service.new(:name => "Coumpound analysis", :price => 3_000)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should be valid" do
|
|
|
|
@service.should be_valid
|
|
|
|
end
|
|
|
|
|
2009-02-09 20:20:23 +01:00
|
|
|
it "should not respond to properties not setup" do
|
|
|
|
@service.respond_to?(:client_name).should be_false
|
|
|
|
end
|
|
|
|
|
2009-02-06 02:06:12 +01:00
|
|
|
describe "property :name, :length => 4...20" do
|
2009-02-09 20:20:23 +01:00
|
|
|
|
2009-02-06 02:06:12 +01:00
|
|
|
it "should autovalidate the presence when length is set" do
|
|
|
|
@service.name = nil
|
|
|
|
@service.should_not be_valid
|
|
|
|
@service.errors.should_not be_nil
|
2009-02-11 01:10:35 +01:00
|
|
|
@service.errors.on(:name).first.should == "Name must be between 4 and 19 characters long"
|
2009-02-06 02:06:12 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should autovalidate the correct length" do
|
|
|
|
@service.name = "a"
|
|
|
|
@service.should_not be_valid
|
|
|
|
@service.errors.should_not be_nil
|
|
|
|
@service.errors.on(:name).first.should == "Name must be between 4 and 19 characters long"
|
|
|
|
end
|
|
|
|
end
|
2009-02-25 07:51:13 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
describe "casting" do
|
|
|
|
describe "cast keys to any type" do
|
|
|
|
before(:all) do
|
|
|
|
event_doc = { :subject => "Some event", :occurs_at => Time.now }
|
|
|
|
e = Event.database.save_doc event_doc
|
|
|
|
|
|
|
|
@event = Event.get e['id']
|
|
|
|
end
|
|
|
|
it "should cast created_at to Time" do
|
|
|
|
@event['occurs_at'].should be_an_instance_of(Time)
|
|
|
|
end
|
|
|
|
end
|
2009-05-29 02:56:42 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "a newly created casted model" do
|
|
|
|
before(:each) do
|
|
|
|
reset_test_db!
|
|
|
|
@cat = Cat.new(:name => 'Toonces')
|
|
|
|
@squeaky_mouse = CatToy.new(:name => 'Squeaky')
|
2009-02-06 02:06:12 +01:00
|
|
|
end
|
|
|
|
|
2009-05-29 02:56:42 +02:00
|
|
|
describe "assigned assigned to a casted property" do
|
|
|
|
it "should have casted_by set to its parent" do
|
|
|
|
@squeaky_mouse.casted_by.should be_nil
|
|
|
|
@cat.favorite_toy = @squeaky_mouse
|
|
|
|
@squeaky_mouse.casted_by.should === @cat
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "appended to a casted collection" do
|
|
|
|
it "should have casted_by set to its parent" do
|
|
|
|
@squeaky_mouse.casted_by.should be_nil
|
|
|
|
@cat.toys << @squeaky_mouse
|
|
|
|
@squeaky_mouse.casted_by.should === @cat
|
|
|
|
@cat.save
|
|
|
|
@cat.toys.first.casted_by.should === @cat
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "list assigned to a casted collection" do
|
|
|
|
it "should have casted_by set on all elements" do
|
|
|
|
toy1 = CatToy.new(:name => 'Feather')
|
|
|
|
toy2 = CatToy.new(:name => 'Mouse')
|
|
|
|
@cat.toys = [toy1, toy2]
|
|
|
|
toy1.casted_by.should === @cat
|
|
|
|
toy2.casted_by.should === @cat
|
|
|
|
@cat.save
|
|
|
|
@cat = Cat.get(@cat.id)
|
|
|
|
@cat.toys[0].casted_by.should === @cat
|
|
|
|
@cat.toys[1].casted_by.should === @cat
|
|
|
|
end
|
|
|
|
end
|
2009-03-08 15:27:30 +01:00
|
|
|
end
|
2009-05-29 02:56:42 +02:00
|
|
|
|
|
|
|
describe "a casted model retrieved from the database" do
|
|
|
|
before(:each) do
|
|
|
|
reset_test_db!
|
|
|
|
@cat = Cat.new(:name => 'Stimpy')
|
|
|
|
@cat.favorite_toy = CatToy.new(:name => 'Stinky')
|
|
|
|
@cat.toys << CatToy.new(:name => 'Feather')
|
|
|
|
@cat.toys << CatToy.new(:name => 'Mouse')
|
|
|
|
@cat.save
|
|
|
|
@cat = Cat.get(@cat.id)
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "as a casted property" do
|
|
|
|
it "should already be casted_by its parent" do
|
|
|
|
@cat.favorite_toy.casted_by.should === @cat
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "from a casted collection" do
|
|
|
|
it "should already be casted_by its parent" do
|
|
|
|
@cat.toys[0].casted_by.should === @cat
|
|
|
|
@cat.toys[1].casted_by.should === @cat
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|