spec(dossier): improuve dossier with state factories

This commit is contained in:
Paul Chavard 2023-11-14 15:54:34 +01:00
parent 1b4010205a
commit a2ed35d44e
5 changed files with 67 additions and 31 deletions

View file

@ -0,0 +1,34 @@
class DossierWithReferenceDate
def self.assign(dossier, state: nil, reference_date: nil)
created_at = reference_date.presence || default_created_at(dossier)
case (state || dossier.state)
when Dossier.states.fetch(:en_construction)
dossier.created_at = created_at
dossier.en_construction_at ||= created_at + 1.minute
dossier.depose_at ||= dossier.en_construction_at
when Dossier.states.fetch(:en_instruction)
assign(dossier, state: Dossier.states.fetch(:en_construction), reference_date: created_at)
dossier.en_instruction_at ||= created_at + 2.minutes
when Dossier.states.fetch(:accepte), Dossier.states.fetch(:refuse), Dossier.states.fetch(:sans_suite)
assign(dossier, state: Dossier.states.fetch(:en_instruction), reference_date: created_at)
dossier.processed_at ||= created_at + 3.minutes
end
end
def self.default_created_at(dossier)
reference_date, delta = case dossier.state
when Dossier.states.fetch(:en_construction)
[dossier.depose_at || dossier.en_construction_at, 1.minute]
when Dossier.states.fetch(:en_instruction)
[dossier.en_instruction_at, 2.minutes]
when Dossier.states.fetch(:accepte), Dossier.states.fetch(:refuse), Dossier.states.fetch(:sans_suite)
[dossier.processed_at, 3.minutes]
end
if reference_date.present?
reference_date - delta
else
10.minutes.ago
end
end
end

View file

@ -79,7 +79,7 @@ describe API::V1::DossiersController do
it do it do
expect(subject[:id]).to eq(dossier.id) expect(subject[:id]).to eq(dossier.id)
expect(subject[:updated_at]).to eq("2008-09-01T08:05:00.000Z") expect(subject[:updated_at]).to eq("2008-09-01T08:05:00.000Z")
expect(subject[:initiated_at]).to eq("2008-09-01T08:06:00.000Z") expect(subject[:initiated_at]).to eq("2008-09-01T07:56:00.000Z")
expect(subject[:state]).to eq("initiated") expect(subject[:state]).to eq("initiated")
expect(subject.keys.size).to eq(4) expect(subject.keys.size).to eq(4)
end end
@ -189,7 +189,7 @@ describe API::V1::DossiersController do
it do it do
expect(subject[:id]).to eq(dossier.id) expect(subject[:id]).to eq(dossier.id)
expect(subject[:state]).to eq('closed') expect(subject[:state]).to eq('closed')
expect(subject[:created_at]).to eq('2008-09-01T08:05:00.000Z') expect(subject[:created_at]).to eq('2008-09-01T07:55:00.000Z')
expect(subject[:updated_at]).to eq('2008-09-01T08:05:00.000Z') expect(subject[:updated_at]).to eq('2008-09-01T08:05:00.000Z')
expect(subject[:archived]).to eq(dossier.archived) expect(subject[:archived]).to eq(dossier.archived)
expect(subject.keys).to match_array(field_list) expect(subject.keys).to match_array(field_list)

View file

@ -141,8 +141,10 @@ FactoryBot.define do
after(:create) do |dossier, _evaluator| after(:create) do |dossier, _evaluator|
dossier.state = Dossier.states.fetch(:en_construction) dossier.state = Dossier.states.fetch(:en_construction)
dossier.groupe_instructeur ||= dossier.procedure&.defaut_groupe_instructeur dossier.groupe_instructeur ||= dossier.procedure&.defaut_groupe_instructeur
dossier.en_construction_at ||= dossier.created_at + 1.minute
dossier.depose_at ||= dossier.en_construction_at processed_at = DossierWithReferenceDate.assign(dossier)
dossier.traitements.passer_en_construction(processed_at:)
dossier.save! dossier.save!
end end
end end
@ -151,9 +153,10 @@ FactoryBot.define do
after(:create) do |dossier, _evaluator| after(:create) do |dossier, _evaluator|
dossier.state = Dossier.states.fetch(:en_instruction) dossier.state = Dossier.states.fetch(:en_instruction)
dossier.groupe_instructeur ||= dossier.procedure&.defaut_groupe_instructeur dossier.groupe_instructeur ||= dossier.procedure&.defaut_groupe_instructeur
dossier.en_construction_at ||= dossier.created_at + 1.minute
dossier.depose_at ||= dossier.en_construction_at processed_at = DossierWithReferenceDate.assign(dossier)
dossier.en_instruction_at ||= dossier.en_construction_at + 1.minute dossier.traitements.passer_en_instruction(processed_at:)
dossier.save! dossier.save!
end end
end end
@ -166,42 +169,42 @@ FactoryBot.define do
after(:create) do |dossier, evaluator| after(:create) do |dossier, evaluator|
dossier.state = Dossier.states.fetch(:accepte) dossier.state = Dossier.states.fetch(:accepte)
dossier.groupe_instructeur ||= dossier.procedure&.defaut_groupe_instructeur dossier.groupe_instructeur ||= dossier.procedure&.defaut_groupe_instructeur
if dossier.processed_at.present?
dossier.en_construction_at ||= dossier.processed_at - 2.minutes processed_at = DossierWithReferenceDate.assign(dossier)
dossier.depose_at ||= dossier.en_construction_at dossier.traitements.accepter(motivation: evaluator.motivation, processed_at:)
dossier.en_instruction_at ||= dossier.processed_at - 1.minute
dossier.traitements.accepter(motivation: evaluator.motivation, processed_at: dossier.processed_at)
else
dossier.en_construction_at ||= dossier.created_at + 1.minute
dossier.depose_at ||= dossier.en_construction_at
dossier.en_instruction_at ||= dossier.en_construction_at + 1.minute
dossier.processed_at = dossier.en_instruction_at + 1.minute
dossier.traitements.accepter(motivation: evaluator.motivation, processed_at: dossier.processed_at)
end
dossier.save! dossier.save!
end end
end end
trait :refuse do trait :refuse do
after(:create) do |dossier, _evaluator| transient do
motivation { nil }
end
after(:create) do |dossier, evaluator|
dossier.state = Dossier.states.fetch(:refuse) dossier.state = Dossier.states.fetch(:refuse)
dossier.groupe_instructeur ||= dossier.procedure&.defaut_groupe_instructeur dossier.groupe_instructeur ||= dossier.procedure&.defaut_groupe_instructeur
dossier.en_construction_at ||= dossier.created_at + 1.minute
dossier.depose_at ||= dossier.en_construction_at processed_at = DossierWithReferenceDate.assign(dossier)
dossier.en_instruction_at ||= dossier.en_construction_at + 1.minute dossier.traitements.refuser(motivation: evaluator.motivation, processed_at:)
dossier.traitements.refuser(processed_at: dossier.en_instruction_at + 1.minute)
dossier.save! dossier.save!
end end
end end
trait :sans_suite do trait :sans_suite do
after(:create) do |dossier, _evaluator| transient do
motivation { nil }
end
after(:create) do |dossier, evaluator|
dossier.state = Dossier.states.fetch(:sans_suite) dossier.state = Dossier.states.fetch(:sans_suite)
dossier.groupe_instructeur ||= dossier.procedure&.defaut_groupe_instructeur dossier.groupe_instructeur ||= dossier.procedure&.defaut_groupe_instructeur
dossier.en_construction_at ||= dossier.created_at + 1.minute
dossier.depose_at ||= dossier.en_construction_at processed_at = DossierWithReferenceDate.assign(dossier)
dossier.en_instruction_at ||= dossier.en_construction_at + 1.minute dossier.traitements.classer_sans_suite(motivation: evaluator.motivation, processed_at:)
dossier.traitements.classer_sans_suite(processed_at: dossier.en_instruction_at + 1.minute)
dossier.save! dossier.save!
end end
end end

View file

@ -1641,7 +1641,6 @@ describe Dossier, type: :model do
it { expect(dossier.state).to eq('en_instruction') } it { expect(dossier.state).to eq('en_instruction') }
it { expect(dossier.archived).to be_falsey } it { expect(dossier.archived).to be_falsey }
it { expect(dossier.processed_at).to be_nil }
it { expect(dossier.motivation).to be_nil } it { expect(dossier.motivation).to be_nil }
it { expect(dossier.justificatif_motivation.attached?).to be_falsey } it { expect(dossier.justificatif_motivation.attached?).to be_falsey }
it { expect(dossier.attestation).to be_nil } it { expect(dossier.attestation).to be_nil }

View file

@ -45,7 +45,7 @@ describe Stat, type: :model do
it 'works' do it 'works' do
expect(stats["not_brouillon"]).to eq(18) expect(stats["not_brouillon"]).to eq(18)
expect(stats["dossiers_depose_avant_30_jours"]).to eq(2) expect(stats["dossiers_depose_avant_30_jours"]).to eq(15)
expect(stats["dossiers_deposes_entre_60_et_30_jours"]).to eq(3) expect(stats["dossiers_deposes_entre_60_et_30_jours"]).to eq(3)
expect(stats["brouillon"]).to eq(1) expect(stats["brouillon"]).to eq(1)
expect(stats["en_construction"]).to eq(5) expect(stats["en_construction"]).to eq(5)