Add constraints to GroupeInstructeur

This commit is contained in:
simon lehericey 2019-10-09 18:05:43 +02:00
parent 4b058001aa
commit 1fa149575c
2 changed files with 43 additions and 0 deletions

View file

@ -4,4 +4,9 @@ class GroupeInstructeur < ApplicationRecord
has_many :assign_tos
has_many :instructeurs, through: :assign_tos, dependent: :destroy
has_many :dossiers
validates :label, presence: { message: 'doit être renseigné' }, allow_nil: false
validates :label, uniqueness: { scope: :procedure, message: 'existe déjà' }
before_validation -> { label&.strip! }
end

View file

@ -0,0 +1,38 @@
require 'spec_helper'
describe GroupeInstructeur, type: :model do
let(:procedure) { create(:procedure) }
subject { GroupeInstructeur.new(label: label, procedure: procedure) }
context 'with no label provided' do
let(:label) { '' }
it { is_expected.to be_invalid }
end
context 'with a valid label' do
let(:label) { 'Préfecture de la Marne' }
it { is_expected.to be_valid }
end
context 'with a label with extra spaces' do
let(:label) { 'Préfecture de la Marne ' }
before do
subject.save
subject.reload
end
it { is_expected.to be_valid }
it { expect(subject.label).to eq("Préfecture de la Marne") }
end
context 'with a label already used for this procedure' do
let(:label) { 'Préfecture de la Marne' }
before do
GroupeInstructeur.create!(label: label, procedure: procedure)
end
it { is_expected.to be_invalid }
end
end