Sha256: 5636b763c6aded5919e87d97f3288ccb78e0d68468a4e404da81c61b6277a874

Contents?: true

Size: 1 KB

Versions: 4

Compression:

Stored size: 1 KB

Contents

require_relative '../../spec_helper'

# TODO: combine this into many_to_one_spec.rb

describe 'Many to One Associations when foreign key is a property subclass' do
  before :all do
    class ::CustomPK < DataMapper::Property::String
      key true
    end

    class ::Animal
      include DataMapper::Resource

      property :id,   Serial
      property :name, String

      belongs_to :zoo
    end

    class ::Zoo
      include DataMapper::Resource

      property :id, ::CustomPK

      has n, :animals
    end

    DataMapper.finalize
  end

  supported_by :all do
    before :all do
      @zoo    = Zoo.create(:id => 'foo')
      @animal = @zoo.animals.create(:name => 'marty')
    end

    it 'has FK of the same property type as zoo PK' do
      expect(Animal.properties[:zoo_id].class).to be(Zoo.properties[:id].class)
    end

    it 'is able to access parent' do
      expect(@animal.zoo).to eq @zoo
    end

    it 'is able to access the children' do
      expect(@zoo.animals).to eq [@animal]
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
sbf-dm-core-1.5.0 spec/public/associations/many_to_one_with_custom_fk_spec.rb
sbf-dm-core-1.4.0 spec/public/associations/many_to_one_with_custom_fk_spec.rb
sbf-dm-core-1.3.0 spec/public/associations/many_to_one_with_custom_fk_spec.rb
sbf-dm-core-1.3.0.beta spec/public/associations/many_to_one_with_custom_fk_spec.rb