Sha256: af701bfc83655721536c059309c153926f27bf0d5256e29de3006ec62a820dda

Contents?: true

Size: 1.6 KB

Versions: 1

Compression:

Stored size: 1.6 KB

Contents

require 'spec_helper'
require 'integration/required_field_validator/spec_helper'

if HAS_SQLITE3 || HAS_MYSQL || HAS_POSTGRES
  class Artist
    #
    # Behaviors
    #

    include DataMapper::Resource

    #
    # Properties
    #

    property :id,   Serial
    property :name, String,  :auto_validation => false

    #
    # Associations
    #

    has n, :albums

    #
    # Validations
    #

    validates_present :name
  end

  class Album
    #
    # Behaviors
    #

    include DataMapper::Resource

    #
    # Properties
    #

    property :id,        Serial
    property :name,      String,  :auto_validation => false
    property :artist_id, Integer, :index => :artist

    #
    # Associations
    #

    belongs_to :artist

    #
    # Validations
    #

    validates_present :name, :artist
  end
  Artist.auto_migrate!
  Album.auto_migrate!



  describe 'Album' do
    before :all do
      Artist.auto_migrate!
      Album.auto_migrate!
    end

    before do
      @artist = Artist.create(:name => "Oceanlab")
      @album  = @artist.albums.new(:name => "Sirens of the sea")
    end

    describe 'with a missing artist' do
      before do
        @album.artist = nil
      end

      it 'is not valid' do
        @album.should_not be_valid
      end

      it 'has a meaninful error messages on association key property' do
        @album.valid?
        @album.errors.on(:artist).should == [ 'Artist must not be blank' ]
      end
    end

    describe 'with specified artist and name' do
      before do
        # no op
      end

      it 'is valid' do
        @album.should be_valid
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
dm-validations-0.10.2 spec/integration/required_field_validator/association_spec.rb