From 3a17c86a747e196ef2dad67392af0f805c01f439 Mon Sep 17 00:00:00 2001 From: Simon Lehericey Date: Tue, 18 Jul 2017 15:03:08 +0200 Subject: [PATCH] Follow: follow must be uniq --- ...iq_constraint_on_gestionnaire_dossier_couple.rb | 7 +++++++ db/schema.rb | 7 ++++--- spec/models/dossier_spec.rb | 4 ++-- .../dossier_table_export_serializer_spec.rb | 14 +++++++------- 4 files changed, 20 insertions(+), 12 deletions(-) create mode 100644 db/migrate/20170713151123_add_uniq_constraint_on_gestionnaire_dossier_couple.rb diff --git a/db/migrate/20170713151123_add_uniq_constraint_on_gestionnaire_dossier_couple.rb b/db/migrate/20170713151123_add_uniq_constraint_on_gestionnaire_dossier_couple.rb new file mode 100644 index 000000000..d4ce457d0 --- /dev/null +++ b/db/migrate/20170713151123_add_uniq_constraint_on_gestionnaire_dossier_couple.rb @@ -0,0 +1,7 @@ +class AddUniqConstraintOnGestionnaireDossierCouple < ActiveRecord::Migration[5.0] + def up + change_column_null :follows, :gestionnaire_id, false + change_column_null :follows, :dossier_id, false + add_index :follows, [:gestionnaire_id, :dossier_id], unique: true + end +end diff --git a/db/schema.rb b/db/schema.rb index 875a6c007..a5fb4a7b5 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20170711125726) do +ActiveRecord::Schema.define(version: 20170713151123) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -230,9 +230,10 @@ ActiveRecord::Schema.define(version: 20170711125726) do end create_table "follows", force: :cascade do |t| - t.integer "gestionnaire_id" - t.integer "dossier_id" + t.integer "gestionnaire_id", null: false + t.integer "dossier_id", null: false t.index ["dossier_id"], name: "index_follows_on_dossier_id", using: :btree + t.index ["gestionnaire_id", "dossier_id"], name: "index_follows_on_gestionnaire_id_and_dossier_id", unique: true, using: :btree t.index ["gestionnaire_id"], name: "index_follows_on_gestionnaire_id", using: :btree end diff --git a/spec/models/dossier_spec.rb b/spec/models/dossier_spec.rb index 39a6e6794..6d5cfc268 100644 --- a/spec/models/dossier_spec.rb +++ b/spec/models/dossier_spec.rb @@ -488,11 +488,11 @@ describe Dossier do context 'when dossier is followed' do let(:procedure) { create(:procedure, :with_type_de_champ, :with_type_de_champ_private) } let(:gestionnaire) { create(:gestionnaire) } - let(:follow) { create(:follow, gestionnaire: gestionnaire) } let(:date1) { 1.day.ago } let(:date2) { 1.hour.ago } let(:date3) { 1.minute.ago } - let(:dossier) { create(:dossier, :with_entreprise, user: user, procedure: procedure, follows: [follow], initiated_at: date1, received_at: date2, processed_at: date3, motivation: "Motivation") } + let(:dossier) { create(:dossier, :with_entreprise, user: user, procedure: procedure, initiated_at: date1, received_at: date2, processed_at: date3, motivation: "Motivation") } + let!(:follow) { create(:follow, gestionnaire: gestionnaire, dossier: dossier) } describe '#export_headers' do subject { dossier.export_headers } diff --git a/spec/serializers/dossier_table_export_serializer_spec.rb b/spec/serializers/dossier_table_export_serializer_spec.rb index 0dcd1598e..7f20c03aa 100644 --- a/spec/serializers/dossier_table_export_serializer_spec.rb +++ b/spec/serializers/dossier_table_export_serializer_spec.rb @@ -3,26 +3,26 @@ require 'spec_helper' describe DossierTableExportSerializer do describe '#emails_accompagnateurs' do let(:gestionnaire){ create(:gestionnaire) } - let(:follow) { create(:follow, gestionnaire: gestionnaire) } + let(:gestionnaire2) { create :gestionnaire} + let(:dossier) { create(:dossier) } subject { DossierTableExportSerializer.new(dossier).emails_accompagnateurs } context 'when there is no accompagnateurs' do - let(:dossier) { create(:dossier, follows: []) } - it { is_expected.to eq('') } end context 'when there one accompagnateur' do - let(:dossier) { create(:dossier, follows: [follow]) } + before { gestionnaire.followed_dossiers << dossier } it { is_expected.to eq(gestionnaire.email) } end context 'when there is 2 followers' do - let(:gestionnaire2) { create :gestionnaire} - let(:follow2) { create(:follow, gestionnaire: gestionnaire2) } - let(:dossier) { create(:dossier, follows: [follow, follow2]) } + before do + gestionnaire.followed_dossiers << dossier + gestionnaire2.followed_dossiers << dossier + end it { is_expected.to eq "#{gestionnaire.email} #{gestionnaire2.email}" } end