spec/base_spec.rb in datamapper-0.2.4 vs spec/base_spec.rb in datamapper-0.2.5

- old
+ new

@@ -1,8 +1,8 @@ require File.dirname(__FILE__) + "/spec_helper" -describe DataMapper::Base do +describe DataMapper::Persistence do it "attributes method should load all lazy-loaded values" do Animal.first(:name => 'Cup').attributes[:notes].should == 'I am a Cup!' end @@ -135,11 +135,11 @@ x.original_values[:name].should == "a" end it "should have original values when loaded from the database" do Person.create(:name => 'a') - x = Person[:name => 'a'] + x = Person.first(:name => 'a') x.original_values.should_not be_empty x.original_values.keys.should include(:name) x.original_values[:name].should == "a" end @@ -163,100 +163,128 @@ end describe 'Properties' do it 'should default to public method visibility' do - class SoftwareEngineer < DataMapper::Base + class SoftwareEngineer + include DataMapper::Persistence + set_table_name 'people' property :name, :string end public_properties = SoftwareEngineer.public_instance_methods.select { |m| ["name", "name="].include?(m) } public_properties.length.should == 2 end it 'should respect protected property options' do - class SanitationEngineer < DataMapper::Base + class SanitationEngineer + include DataMapper::Persistence + set_table_name 'people' property :name, :string, :reader => :protected property :age, :integer, :writer => :protected end protected_properties = SanitationEngineer.protected_instance_methods.select { |m| ["name", "age="].include?(m) } protected_properties.length.should == 2 end it 'should respect private property options' do - class ElectricalEngineer < DataMapper::Base + class ElectricalEngineer + include DataMapper::Persistence + set_table_name 'people' property :name, :string, :reader => :private property :age, :integer, :writer => :private end private_properties = ElectricalEngineer.private_instance_methods.select { |m| ["name", "age="].include?(m) } private_properties.length.should == 2 end it 'should set both reader and writer visibiliy when accessor option is passed' do - class TrainEngineer < DataMapper::Base + class TrainEngineer + include DataMapper::Persistence + property :name, :string, :accessor => :private end private_properties = TrainEngineer.private_instance_methods.select { |m| ["name", "name="].include?(m) } private_properties.length.should == 2 end it 'should only be listed in attributes if they have public getters' do - class SalesEngineer < DataMapper::Base + class SalesEngineer + include DataMapper::Persistence + set_table_name 'people' property :name, :string property :age, :integer, :reader => :private end - @sam = SalesEngineer[:name => 'Sam'] + @sam = SalesEngineer.first(:name => 'Sam') # note: id default key gets a public reader by default (but writer is protected) @sam.attributes.should == {:id => @sam.id, :name => @sam.name} end it 'should not allow mass assignment if private or protected' do - class ChemicalEngineer < DataMapper::Base + class ChemicalEngineer + include DataMapper::Persistence + set_table_name 'people' property :name, :string, :writer => :private property :age, :integer end - @sam = ChemicalEngineer[:name => 'Sam'] + @sam = ChemicalEngineer.first(:name => 'Sam') @sam.attributes = {:name => 'frank', :age => 101} @sam.age.should == 101 @sam.name.should == 'Sam' end it 'should allow :protected to be passed as an alias for a public reader, protected writer' do - class CivilEngineer < DataMapper::Base + class CivilEngineer + include DataMapper::Persistence + set_table_name 'people' property :name, :string, :protected => true end CivilEngineer.public_instance_methods.should include("name") CivilEngineer.protected_instance_methods.should include("name=") end it 'should allow :private to be passed as an alias for a public reader, private writer' do - class AudioEngineer < DataMapper::Base + class AudioEngineer + include DataMapper::Persistence + set_table_name 'people' property :name, :string, :private => true end AudioEngineer.public_instance_methods.should include("name") AudioEngineer.private_instance_methods.should include("name=") end it 'should raise an error when invalid options are passsed' do lambda do - class JumpyCow < DataMapper::Base + class JumpyCow + include DataMapper::Persistence + set_table_name 'animals' property :name, :string, :laze => true end end.should raise_error(ArgumentError) end + it 'should raise an error when the first argument to index isnt an array' do + lambda do + class JumpyCow + include DataMapper::Persistence + + set_table_name 'animals' + index :name, :parent + end + end.should raise_error(ArgumentError) + end end