test: faster dossier clone spec by merging similar it

This commit is contained in:
Colin Darie 2024-04-03 16:34:24 +02:00
parent 2e763d5e92
commit 5a2ddcb471
No known key found for this signature in database
GPG key ID: 4FB865FDBCA4BCC4
2 changed files with 120 additions and 103 deletions

View file

@ -19,45 +19,44 @@ RSpec.describe DossierCloneConcern do
let(:types_de_champ_public) { [{}] }
let(:types_de_champ_private) { [{}] }
let(:fork) { false }
let(:new_dossier) { dossier.clone(fork:) }
subject(:new_dossier) { dossier.clone(fork:) }
context 'reset most attributes' do
it { expect(new_dossier.id).not_to eq(dossier.id) }
it { expect(new_dossier.api_entreprise_job_exceptions).to be_nil }
it { expect(new_dossier.archived).to be_falsey }
it { expect(new_dossier.brouillon_close_to_expiration_notice_sent_at).to be_nil }
it { expect(new_dossier.conservation_extension).to eq(0.seconds) }
it { expect(new_dossier.declarative_triggered_at).to be_nil }
it { expect(new_dossier.deleted_user_email_never_send).to be_nil }
it { expect(new_dossier.depose_at).to be_nil }
it { expect(new_dossier.en_construction_at).to be_nil }
it { expect(new_dossier.en_construction_close_to_expiration_notice_sent_at).to be_nil }
it { expect(new_dossier.en_instruction_at).to be_nil }
it { expect(new_dossier.for_procedure_preview).to be_falsey }
it { expect(new_dossier.groupe_instructeur_updated_at).to be_nil }
it { expect(new_dossier.hidden_at).to be_nil }
it { expect(new_dossier.hidden_by_administration_at).to be_nil }
it { expect(new_dossier.hidden_by_reason).to be_nil }
it { expect(new_dossier.hidden_by_user_at).to be_nil }
it { expect(new_dossier.identity_updated_at).to be_nil }
it { expect(new_dossier.last_avis_updated_at).to be_nil }
it { expect(new_dossier.last_champ_private_updated_at).to be_nil }
it { expect(new_dossier.last_champ_updated_at).to be_nil }
it { expect(new_dossier.last_commentaire_updated_at).to be_nil }
it { expect(new_dossier.motivation).to be_nil }
it { expect(new_dossier.processed_at).to be_nil }
it { expect(new_dossier.termine_close_to_expiration_notice_sent_at).to be_nil }
it { expect(new_dossier.dossier_transfer_id).to be_nil }
it 'resets most of the attributes for the cloned dossier' do
expect(new_dossier.id).not_to eq(dossier.id)
expect(new_dossier.api_entreprise_job_exceptions).to be_nil
expect(new_dossier.archived).to be_falsey
expect(new_dossier.brouillon_close_to_expiration_notice_sent_at).to be_nil
expect(new_dossier.conservation_extension).to eq(0.seconds)
expect(new_dossier.declarative_triggered_at).to be_nil
expect(new_dossier.deleted_user_email_never_send).to be_nil
expect(new_dossier.depose_at).to be_nil
expect(new_dossier.en_construction_at).to be_nil
expect(new_dossier.en_construction_close_to_expiration_notice_sent_at).to be_nil
expect(new_dossier.en_instruction_at).to be_nil
expect(new_dossier.for_procedure_preview).to be_falsey
expect(new_dossier.groupe_instructeur_updated_at).to be_nil
expect(new_dossier.hidden_at).to be_nil
expect(new_dossier.hidden_by_administration_at).to be_nil
expect(new_dossier.hidden_by_reason).to be_nil
expect(new_dossier.hidden_by_user_at).to be_nil
expect(new_dossier.identity_updated_at).to be_nil
expect(new_dossier.last_avis_updated_at).to be_nil
expect(new_dossier.last_champ_private_updated_at).to be_nil
expect(new_dossier.last_champ_updated_at).to be_nil
expect(new_dossier.last_commentaire_updated_at).to be_nil
expect(new_dossier.motivation).to be_nil
expect(new_dossier.processed_at).to be_nil
end
it "update search terms" do
new_dossier
perform_enqueued_jobs(only: DossierUpdateSearchTermsJob)
sql = "SELECT search_terms, private_search_terms FROM dossiers where id = :id"
result = Dossier.connection.execute(Dossier.sanitize_sql_array([sql, id: new_dossier.id])).first
it "updates search terms" do
subject
expect(result["search_terms"]).to match(dossier.user.email)
expect(result["private_search_terms"]).to eq("")
end
perform_enqueued_jobs(only: DossierUpdateSearchTermsJob)
sql = "SELECT search_terms, private_search_terms FROM dossiers where id = :id"
result = Dossier.connection.execute(Dossier.sanitize_sql_array([sql, id: new_dossier.id])).first
expect(result["search_terms"]).to match(dossier.user.email)
expect(result["private_search_terms"]).to eq("")
end
context 'copies some attributes' do
@ -67,18 +66,22 @@ RSpec.describe DossierCloneConcern do
end
context 'when not forked' do
it { expect(new_dossier.groupe_instructeur).to be_nil }
it "copies or reset attributes" do
expect(new_dossier.groupe_instructeur).to be_nil
expect(new_dossier.autorisation_donnees).to eq(dossier.autorisation_donnees)
expect(new_dossier.revision_id).to eq(dossier.revision_id)
expect(new_dossier.user_id).to eq(dossier.user_id)
end
end
it { expect(new_dossier.autorisation_donnees).to eq(dossier.autorisation_donnees) }
it { expect(new_dossier.revision_id).to eq(dossier.revision_id) }
it { expect(new_dossier.user_id).to eq(dossier.user_id) }
end
context 'forces some attributes' do
let(:dossier) { create(:dossier, :accepte) }
it { expect(new_dossier.brouillon?).to eq(true) }
it { expect(new_dossier.parent_dossier).to eq(dossier) }
it do
expect(new_dossier.brouillon?).to eq(true)
expect(new_dossier.parent_dossier).to eq(dossier)
end
context 'destroy parent' do
before { new_dossier }
@ -91,22 +94,28 @@ RSpec.describe DossierCloneConcern do
context 'procedure with_individual' do
let(:procedure) { create(:procedure, :for_individual) }
it { expect(new_dossier.individual.slice(:nom, :prenom, :gender)).to eq(dossier.individual.slice(:nom, :prenom, :gender)) }
it { expect(new_dossier.individual.id).not_to eq(dossier.individual.id) }
it do
expect(new_dossier.individual.slice(:nom, :prenom, :gender)).to eq(dossier.individual.slice(:nom, :prenom, :gender))
expect(new_dossier.individual.id).not_to eq(dossier.individual.id)
end
end
context 'procedure with etablissement' do
let(:dossier) { create(:dossier, :with_entreprise) }
it { expect(new_dossier.etablissement.slice(:siret)).to eq(dossier.etablissement.slice(:siret)) }
it { expect(new_dossier.etablissement.id).not_to eq(dossier.etablissement.id) }
it do
expect(new_dossier.etablissement.slice(:siret)).to eq(dossier.etablissement.slice(:siret))
expect(new_dossier.etablissement.id).not_to eq(dossier.etablissement.id)
end
end
describe 'champs' do
it { expect(new_dossier.id).not_to eq(dossier.id) }
context 'public are duplicated' do
it { expect(new_dossier.champs_public.count).to eq(dossier.champs_public.count) }
it { expect(new_dossier.champs_public.ids).not_to eq(dossier.champs_public.ids) }
it do
expect(new_dossier.champs_public.count).to eq(dossier.champs_public.count)
expect(new_dossier.champs_public.ids).not_to eq(dossier.champs_public.ids)
end
it 'keeps champs.values' do
original_first_champ = dossier.champs_public.first
@ -121,8 +130,10 @@ RSpec.describe DossierCloneConcern do
let(:champ_repetition) { create(:champ_repetition, type_de_champ: type_de_champ_repetition, dossier: dossier) }
before { dossier.champs_public << champ_repetition }
it { expect(Champs::RepetitionChamp.where(dossier: new_dossier).first.champs.count).to eq(4) }
it { expect(Champs::RepetitionChamp.where(dossier: new_dossier).first.champs.ids).not_to eq(champ_repetition.champs.ids) }
it do
expect(Champs::RepetitionChamp.where(dossier: new_dossier).first.champs.count).to eq(4)
expect(Champs::RepetitionChamp.where(dossier: new_dossier).first.champs.ids).not_to eq(champ_repetition.champs.ids)
end
end
context 'for Champs::CarteChamp with geo areas, original_champ.geo_areas are duped' do
@ -132,8 +143,10 @@ RSpec.describe DossierCloneConcern do
let(:champ_carte) { create(:champ_carte, type_de_champ: type_de_champ_carte, geo_areas: [geo_area]) }
before { dossier.champs_public << champ_carte }
it { expect(Champs::CarteChamp.where(dossier: new_dossier).first.geo_areas.count).to eq(1) }
it { expect(Champs::CarteChamp.where(dossier: new_dossier).first.geo_areas.ids).not_to eq(champ_carte.geo_areas.ids) }
it do
expect(Champs::CarteChamp.where(dossier: new_dossier).first.geo_areas.count).to eq(1)
expect(Champs::CarteChamp.where(dossier: new_dossier).first.geo_areas.ids).not_to eq(champ_carte.geo_areas.ids)
end
end
context 'for Champs::SiretChamp, original_champ.etablissement is duped' do
@ -143,8 +156,10 @@ RSpec.describe DossierCloneConcern do
let(:champ_siret) { create(:champ_siret, type_de_champ: type_de_champs_siret, etablissement: create(:etablissement)) }
before { dossier.champs_public << champ_siret }
it { expect(Champs::SiretChamp.where(dossier: dossier).first.etablissement).not_to be_nil }
it { expect(Champs::SiretChamp.where(dossier: new_dossier).first.etablissement.id).not_to eq(champ_siret.etablissement.id) }
it do
expect(Champs::SiretChamp.where(dossier: dossier).first.etablissement).not_to be_nil
expect(Champs::SiretChamp.where(dossier: new_dossier).first.etablissement.id).not_to eq(champ_siret.etablissement.id)
end
end
context 'for Champs::PieceJustificative, original_champ.piece_justificative_file is duped' do
@ -161,18 +176,19 @@ RSpec.describe DossierCloneConcern do
let(:champ_address) { create(:champ_address, type_de_champ: type_de_champs_adress, external_id: 'Address', data: { city_code: '75019' }) }
before { dossier.champs_public << champ_address }
it { expect(Champs::AddressChamp.where(dossier: dossier).first.data).not_to be_nil }
it { expect(Champs::AddressChamp.where(dossier: dossier).first.external_id).not_to be_nil }
it { expect(Champs::AddressChamp.where(dossier: new_dossier).first.external_id).to eq(champ_address.external_id) }
it { expect(Champs::AddressChamp.where(dossier: new_dossier).first.data).to eq(champ_address.data) }
it do
expect(Champs::AddressChamp.where(dossier: dossier).first.data).not_to be_nil
expect(Champs::AddressChamp.where(dossier: dossier).first.external_id).not_to be_nil
expect(Champs::AddressChamp.where(dossier: new_dossier).first.external_id).to eq(champ_address.external_id)
expect(Champs::AddressChamp.where(dossier: new_dossier).first.data).to eq(champ_address.data)
end
end
end
context 'private are renewd' do
it { expect(new_dossier.champs_private.count).to eq(dossier.champs_private.count) }
it { expect(new_dossier.champs_private.ids).not_to eq(dossier.champs_private.ids) }
it 'reset champs private values' do
expect(new_dossier.champs_private.count).to eq(dossier.champs_private.count)
expect(new_dossier.champs_private.ids).not_to eq(dossier.champs_private.ids)
original_first_champs_private = dossier.champs_private.first
original_first_champs_private.update!(value: 'kthxbye')
@ -186,10 +202,12 @@ RSpec.describe DossierCloneConcern do
let(:new_dossier) { dossier.clone(fork: true) }
before { dossier.champs_public.reload } # we compare timestamps so we have to get the precision limit from the db }
it { expect(new_dossier.editing_fork_origin).to eq(dossier) }
it { expect(new_dossier.champs_public[0].id).not_to eq(dossier.champs_public[0].id) }
it { expect(new_dossier.champs_public[0].created_at).to eq(dossier.champs_public[0].created_at) }
it { expect(new_dossier.champs_public[0].updated_at).to eq(dossier.champs_public[0].updated_at) }
it do
expect(new_dossier.editing_fork_origin).to eq(dossier)
expect(new_dossier.champs_public[0].id).not_to eq(dossier.champs_public[0].id)
expect(new_dossier.champs_public[0].created_at).to eq(dossier.champs_public[0].created_at)
expect(new_dossier.champs_public[0].updated_at).to eq(dossier.champs_public[0].updated_at)
end
context "piece justificative champ" do
let(:types_de_champ_public) { [{ type: :piece_justificative }] }
@ -261,8 +279,10 @@ RSpec.describe DossierCloneConcern do
forked_dossier.assign_to_groupe_instructeur(dossier.procedure.defaut_groupe_instructeur, DossierAssignment.modes.fetch(:manual))
}
it { is_expected.to eq(added: [], updated: [], removed: []) }
it { expect(forked_dossier.forked_with_changes?).to be_truthy }
it do
expect(subject).to eq(added: [], updated: [], removed: [])
expect(forked_dossier.forked_with_changes?).to be_truthy
end
end
context 'with updated champ' do
@ -270,8 +290,8 @@ RSpec.describe DossierCloneConcern do
before { updated_champ.update(value: 'new value') }
it { is_expected.to eq(added: [], updated: [updated_champ], removed: []) }
it 'forked_with_changes? should reflect dossier state' do
expect(subject).to eq(added: [], updated: [updated_champ], removed: [])
expect(dossier.forked_with_changes?).to be_falsey
expect(forked_dossier.forked_with_changes?).to be_truthy
expect(updated_champ.forked_with_changes?).to be_truthy
@ -318,13 +338,10 @@ RSpec.describe DossierCloneConcern do
it { expect { subject }.to change { dossier.reload.champs.size }.by(0) }
it { expect { subject }.not_to change { dossier.reload.champs.order(:created_at).reject { _1.stable_id.in?([99, 994]) }.map(&:value) } }
it { expect { subject }.to have_enqueued_job(DossierUpdateSearchTermsJob).with(dossier) }
it { expect { subject }.to change { dossier.reload.champs.find { _1.stable_id == 99 }.value }.from('old value').to('new value') }
it { expect { subject }.to change { dossier.reload.champs.find { _1.stable_id == 994 }.value }.from('old value').to('new value in repetition') }
it 'update dossier search terms' do
expect { subject }.to have_enqueued_job(DossierUpdateSearchTermsJob).with(dossier)
end
it 'fork is hidden after merge' do
subject
expect(forked_dossier.reload.hidden_by_reason).to eq("stale_fork")

View file

@ -995,28 +995,28 @@ describe Dossier, type: :model do
allow(NotificationMailer).to receive(:send_accepte_notification).and_return(double(deliver_later: true))
allow(dossier).to receive(:build_attestation).and_return(attestation)
Timecop.freeze(now)
travel_to now
dossier.accepter!(instructeur: instructeur, motivation: 'motivation')
dossier.reload
end
after { Timecop.return }
it { expect(dossier.traitements.last.motivation).to eq('motivation') }
it { expect(dossier.motivation).to eq('motivation') }
it { expect(dossier.traitements.last.instructeur_email).to eq(instructeur.email) }
it { expect(dossier.en_instruction_at).to eq(dossier.en_instruction_at) }
it { expect(dossier.traitements.last.processed_at).to eq(now) }
it { expect(dossier.processed_at).to eq(now) }
it { expect(dossier.state).to eq('accepte') }
it { expect(last_operation.operation).to eq('accepter') }
it { expect(last_operation.automatic_operation?).to be_falsey }
it { expect(operation_serialized['operation']).to eq('accepter') }
it { expect(operation_serialized['dossier_id']).to eq(dossier.id) }
it { expect(operation_serialized['executed_at']).to eq(last_operation.executed_at.iso8601) }
it { expect(NotificationMailer).to have_received(:send_accepte_notification).with(dossier) }
it { expect(dossier.attestation).to eq(attestation) }
it { expect(dossier.commentaires.count).to eq(1) }
it "update attributes" do
expect(dossier.traitements.last.motivation).to eq('motivation')
expect(dossier.motivation).to eq('motivation')
expect(dossier.traitements.last.instructeur_email).to eq(instructeur.email)
expect(dossier.en_instruction_at).to eq(dossier.en_instruction_at)
expect(dossier.traitements.last.processed_at).to eq(now)
expect(dossier.processed_at).to eq(now)
expect(dossier.state).to eq('accepte')
expect(last_operation.operation).to eq('accepter')
expect(last_operation.automatic_operation?).to be_falsey
expect(operation_serialized['operation']).to eq('accepter')
expect(operation_serialized['dossier_id']).to eq(dossier.id)
expect(operation_serialized['executed_at']).to eq(last_operation.executed_at.iso8601)
expect(NotificationMailer).to have_received(:send_accepte_notification).with(dossier)
expect(dossier.attestation).to eq(attestation)
expect(dossier.commentaires.count).to eq(1)
end
end
describe '#accepter_automatiquement!' do
@ -1642,24 +1642,24 @@ describe Dossier, type: :model do
let(:last_operation) { dossier.dossier_operation_logs.last }
before do
Timecop.freeze
freeze_time
allow(NotificationMailer).to receive(:send_repasser_en_instruction_notification).and_return(double(deliver_later: true))
dossier.repasser_en_instruction!(instructeur: instructeur)
dossier.reload
end
it { expect(dossier.state).to eq('en_instruction') }
it { expect(dossier.archived).to be_falsey }
it { expect(dossier.motivation).to be_nil }
it { expect(dossier.justificatif_motivation.attached?).to be_falsey }
it { expect(dossier.attestation).to be_nil }
it { expect(dossier.sva_svr_decision_on).to be_nil }
it { expect(dossier.termine_close_to_expiration_notice_sent_at).to be_nil }
it { expect(last_operation.operation).to eq('repasser_en_instruction') }
it { expect(last_operation.data['author']['email']).to eq(instructeur.email) }
it { expect(NotificationMailer).to have_received(:send_repasser_en_instruction_notification).with(dossier) }
after { Timecop.return }
it "update attributes" do
expect(dossier.state).to eq('en_instruction')
expect(dossier.archived).to be_falsey
expect(dossier.motivation).to be_nil
expect(dossier.justificatif_motivation.attached?).to be_falsey
expect(dossier.attestation).to be_nil
expect(dossier.sva_svr_decision_on).to be_nil
expect(dossier.termine_close_to_expiration_notice_sent_at).to be_nil
expect(last_operation.operation).to eq('repasser_en_instruction')
expect(last_operation.data['author']['email']).to eq(instructeur.email)
expect(NotificationMailer).to have_received(:send_repasser_en_instruction_notification).with(dossier)
end
end
describe '#notify_draft_not_submitted' do