From 48286f7de9f1c8e13a9528a68e60eeac87cec99a Mon Sep 17 00:00:00 2001 From: Bertocq Date: Tue, 13 Mar 2018 18:27:55 +0100 Subject: [PATCH] Validate ValuatorGroup#name presence & uniqueness Why: ValuatorGroup name should be unique and present to be able to identify correctly each of them. How: - Adding a presence & uniqueness validation at the model - Adding a sequenced value for name attribute at its factory - Adding missing model spec that covers validations --- app/models/valuator_group.rb | 4 +++- spec/factories.rb | 1 + spec/models/valuator_group_spec.rb | 20 ++++++++++++++++++++ 3 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 spec/models/valuator_group_spec.rb diff --git a/app/models/valuator_group.rb b/app/models/valuator_group.rb index c5bf8d29b..61a4e4458 100644 --- a/app/models/valuator_group.rb +++ b/app/models/valuator_group.rb @@ -2,4 +2,6 @@ class ValuatorGroup < ActiveRecord::Base has_many :valuators has_many :valuator_group_assignments, dependent: :destroy, class_name: 'Budget::ValuatorGroupAssignment' has_many :investments, through: :valuator_group_assignments, class_name: 'Budget::Investment' -end \ No newline at end of file + + validates :name, presence: true, uniqueness: true +end diff --git a/spec/factories.rb b/spec/factories.rb index 05ddd2fc0..8f872daaf 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -62,6 +62,7 @@ FactoryBot.define do end factory :valuator_group, class: ValuatorGroup do + sequence(:name) { |n| "Valuator Group #{n}" } end factory :identity do diff --git a/spec/models/valuator_group_spec.rb b/spec/models/valuator_group_spec.rb new file mode 100644 index 000000000..98a1effd8 --- /dev/null +++ b/spec/models/valuator_group_spec.rb @@ -0,0 +1,20 @@ +require 'rails_helper' + +describe ValuatorGroup do + + describe 'Validations' do + it "should be valid" do + expect(build(:valuator_group)).to be_valid + end + + it "should not be valid without a name" do + expect(build(:valuator_group, name: nil)).not_to be_valid + end + + it "should not be valid with the same name as an existing one" do + create(:valuator_group, name: 'The Valuators') + + expect(build(:valuator_group, name: 'The Valuators')).not_to be_valid + end + end +end