Sha256: 8e4c693cb1416cd9042e7cb3dec060ca0a5ee0196f2d9354eea90f7a0650a6a7

Contents?: true

Size: 1.79 KB

Versions: 6

Compression:

Stored size: 1.79 KB

Contents

require File.join(File.dirname(__FILE__), "..", "..", "spec_helper.rb")

describe 'has_one relationships', has_one:true do
  before :all do
    Redis.new.flushall
    Ampere.connect
    
    # These are used by the has_one/belongs_to example below
    class Car
      include Ampere::Model
      
      field :make
      field :model
      field :year

      has_one :engine
    end

    class Engine
      include Ampere::Model
      
      field :displacement
      field :cylinders
      field :configuration
      
      belongs_to :car
    end
    
    @car    = Car.create :make  => "Lamborghini",
                         :model => "Countach",
                         :year  => "1974"
    @engine = Engine.create :displacement  => "5167",
                            :cylinders     => "12",
                            :configuration => "V"
  end
  
  ###
  
  it 'can store a relationship to one model instance from another using an attr_accessor' do
    @car.engine_id = @engine.id
    @car.save
    @car.reload
    @engine.reload
    @car.engine_id.should == @engine.id
    @car.engine.should == @engine
    
    @car.engine_id = nil
    @car.save
    @car.engine.should be_nil
  end
  
  it 'can store a relationship to one model instance from another using custom accessor methods' do
    @car.engine = @engine
    @car.save
    @car.reload
    @engine.reload
    @car.engine_id.should == @engine.id
    @car.engine.should == @engine
    
    @car.engine_id = nil
    @car.save
    @car.engine.should be_nil
  end
  
  it 'can delete the object related to it in a has_one relationship' do
    @car.engine = @engine
    @car.engine.destroy
    @car.reload
    @car.engine.should be_nil
    Engine.find(@engine.id).should be_nil
  end
  
  ###
  
  after :all do
    Ampere.disconnect
    Redis.new.flushall
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
ampere-1.2.2 spec/models/relationships/has_one_spec.rb
ampere-1.2.1 spec/models/relationships/has_one_spec.rb
ampere-1.2.0 spec/models/relationships/has_one_spec.rb
ampere-1.1.1 spec/models/relationships/has_one_spec.rb
ampere-1.1.0 spec/models/relationships/has_one_spec.rb
ampere-1.0.0 spec/models/relationships/has_one_spec.rb