Sha256: a246a7febb7747eb10ca308072e6845837040ec4840683ceffaf14305a2fd402

Contents?: true

Size: 1.8 KB

Versions: 3

Compression:

Stored size: 1.8 KB

Contents

require 'pathname'
require Pathname(__FILE__).dirname.expand_path.parent + 'spec_helper'

[HAS_MYSQL, HAS_POSTGRES].each do |adapter|

  describe 'DataMapper::Constraints' do

    before :all do
      DataMapper::Repository.adapters[:default] =  DataMapper::Repository.adapters[adapter]

      class Stable
        include DataMapper::Resource

        property :id,        Serial
        property :location,  String
        property :size,      Integer
      end

      class Farmer
        include DataMapper::Resource

        property :first_name, String, :key => true
        property :last_name, String,  :key => true
      end

      class Cow
        include DataMapper::Resource
        include DataMapper::Constraints

        property :id,        Serial
        property :name,      String
        property :breed,     String
        belongs_to :stable
        belongs_to :farmer
      end

      class Stable
        has n, :cows
      end

      class Farmer
        has n, :cows
      end

      DataMapper.auto_migrate!
    end

    it "is included when DataMapper::Searchable is loaded" do
      Cow.new.should be_kind_of(DataMapper::Constraints)
    end

    it "should be able to create related objects with a foreign key constraint" do
      @s  = Stable.create(:location => "Hometown")
      @c1 = Cow.create(:name => "Bea", :stable => @s)
    end

    it "should be able to create related objects with a composite foreign key constraint" do
      @f  = Farmer.create(:first_name => "John", :last_name => "Doe")
      @c1 = Cow.create(:name => "Bea", :farmer => @f)
    end

    it "should not be able to create related objects with a failing foreign key constraint" do
      s = Stable.first(:order => [:id.desc])
      lambda { @c1 = Cow.create(:name => "Bea", :stable_id => s.id + 1) }.should raise_error
    end

  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
dm-constraints-0.9.4 spec/integration/constraints_spec.rb
dm-constraints-0.9.5 spec/integration/constraints_spec.rb
dm-constraints-0.9.6 spec/integration/constraints_spec.rb