From b1296df4c31853fad520422db22a9b38da01193a Mon Sep 17 00:00:00 2001 From: Pierre de La Morinerie Date: Tue, 13 Nov 2018 16:20:08 +0100 Subject: [PATCH 1/3] procedure: refactor usual_instruction_time spec --- spec/models/procedure_spec.rb | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/spec/models/procedure_spec.rb b/spec/models/procedure_spec.rb index ab4fb73d7..d347592ae 100644 --- a/spec/models/procedure_spec.rb +++ b/spec/models/procedure_spec.rb @@ -708,12 +708,14 @@ describe Procedure do describe '#usual_instruction_time' do let(:procedure) { create(:procedure) } + def create_dossier(instruction_date:, processed_date:) + dossier = create(:dossier, :accepte, procedure: procedure) + dossier.update!(en_instruction_at: instruction_date, processed_at: processed_date) + end + before do processed_delays.each do |delay| - dossier = create :dossier, :accepte, procedure: procedure - instruction_date = 1.month.ago - processed_date = instruction_date + delay - dossier.update!(en_instruction_at: instruction_date, processed_at: processed_date) + create_dossier(instruction_date: 1.week.ago - delay, processed_date: 1.week.ago) end end From b693e055b7599f53e0a86509548eefd4d0c3b252 Mon Sep 17 00:00:00 2001 From: Pierre de La Morinerie Date: Tue, 13 Nov 2018 16:21:02 +0100 Subject: [PATCH 2/3] procedure: make usual_instruction_time spec more robust --- spec/models/procedure_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/models/procedure_spec.rb b/spec/models/procedure_spec.rb index d347592ae..c87a32593 100644 --- a/spec/models/procedure_spec.rb +++ b/spec/models/procedure_spec.rb @@ -729,7 +729,7 @@ describe Procedure do context 'when there is only one processed dossier' do let(:processed_delays) { [1.day] } - it { expect(procedure.usual_instruction_time).to eq(1.day) } + it { expect(procedure.usual_instruction_time).to be_within(10.seconds).of(1.day) } end context 'where there is no processed dossier' do From 83031b5cdd9b7b2f3d8498610c5ba2d114aa70b9 Mon Sep 17 00:00:00 2001 From: Pierre de La Morinerie Date: Tue, 13 Nov 2018 15:21:15 +0000 Subject: [PATCH 3/3] procedure: compute the usual instruction time only for the last month --- app/models/procedure.rb | 1 + spec/models/procedure_spec.rb | 9 +++++++++ 2 files changed, 10 insertions(+) diff --git a/app/models/procedure.rb b/app/models/procedure.rb index 44984c564..8242e67c4 100644 --- a/app/models/procedure.rb +++ b/app/models/procedure.rb @@ -426,6 +426,7 @@ class Procedure < ApplicationRecord def percentile_time(start_attribute, end_attribute, p) times = dossiers .state_termine + .where(end_attribute => 1.month.ago..DateTime.current) .pluck(start_attribute, end_attribute) .map { |(start_date, end_date)| end_date - start_date } diff --git a/spec/models/procedure_spec.rb b/spec/models/procedure_spec.rb index c87a32593..0f26cdb33 100644 --- a/spec/models/procedure_spec.rb +++ b/spec/models/procedure_spec.rb @@ -727,6 +727,15 @@ describe Procedure do end end + context 'when there are very old dossiers' do + let(:processed_delays) { [2.days, 2.days] } + let!(:old_dossier) { create_dossier(instruction_date: 3.months.ago, processed_date: 2.months.ago) } + + it 'ignores dossiers older than 1 month' do + expect(procedure.usual_instruction_time).to be_within(10.seconds).of(2.days) + end + end + context 'when there is only one processed dossier' do let(:processed_delays) { [1.day] } it { expect(procedure.usual_instruction_time).to be_within(10.seconds).of(1.day) }