Merge pull request #7951 from mfo/US/fix-export-cache-key

bug(export): la creation d'un export avec un filtre different devrait etre effective, ce n'est pas le cas
This commit is contained in:
LeSim 2022-10-31 09:35:26 +01:00 committed by GitHub
commit 96147330ba
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 44 additions and 12 deletions

View file

@ -24,7 +24,11 @@ class AssignTo < ApplicationRecord
def procedure_presentation_or_default_and_errors
errors = reset_procedure_presentation_if_invalid
[procedure_presentation || build_procedure_presentation, errors]
if self.procedure_presentation.nil?
self.procedure_presentation = build_procedure_presentation
self.procedure_presentation.save if procedure_presentation.valid? && !procedure_presentation.persisted?
end
[self.procedure_presentation, errors]
end
private

View file

@ -89,13 +89,12 @@ class Export < ApplicationRecord
.create_or_find_by(format: format,
time_span_type: time_span_type,
statut: statut,
key: generate_cache_key(groupe_instructeurs.map(&:id), procedure_presentation&.id))
key: generate_cache_key(groupe_instructeurs.map(&:id), procedure_presentation))
end
def self.find_for_groupe_instructeurs(groupe_instructeurs_ids, procedure_presentation)
exports = if procedure_presentation.present?
where(key: generate_cache_key(groupe_instructeurs_ids))
.or(where(key: generate_cache_key(groupe_instructeurs_ids, procedure_presentation.id)))
where(key: generate_cache_key(groupe_instructeurs_ids, procedure_presentation))
else
where(key: generate_cache_key(groupe_instructeurs_ids))
end
@ -121,9 +120,13 @@ class Export < ApplicationRecord
}
end
def self.generate_cache_key(groupe_instructeurs_ids, procedure_presentation_id = nil)
if procedure_presentation_id.present?
"#{groupe_instructeurs_ids.sort.join('-')}--#{procedure_presentation_id}"
def self.generate_cache_key(groupe_instructeurs_ids, procedure_presentation = nil)
if procedure_presentation.present?
[
groupe_instructeurs_ids.sort.join('-'),
procedure_presentation.id,
Digest::MD5.hexdigest(procedure_presentation.snapshot.slice(:filters, :sort).to_s)
].join('--')
else
groupe_instructeurs_ids.sort.join('-')
end

View file

@ -6,7 +6,7 @@ FactoryBot.define do
groupe_instructeurs { [association(:groupe_instructeur)] }
after(:build) do |export, _evaluator|
export.key = Export.generate_cache_key(export.groupe_instructeurs.map(&:id), export.procedure_presentation&.id)
export.key = Export.generate_cache_key(export.groupe_instructeurs.map(&:id), export.procedure_presentation)
end
end
end

View file

@ -8,7 +8,7 @@ describe AssignTo, type: :model do
let(:errors) { procedure_presentation_and_errors.second }
context "without a procedure_presentation" do
it { expect(procedure_presentation_or_default).not_to be_persisted }
it { expect(procedure_presentation_or_default).to be_persisted }
it { expect(procedure_presentation_or_default).to be_valid }
it { expect(errors).to be_nil }
end
@ -28,7 +28,7 @@ describe AssignTo, type: :model do
pp
end
it { expect(procedure_presentation_or_default).not_to be_persisted }
it { expect(procedure_presentation_or_default).to be_persisted }
it { expect(procedure_presentation_or_default).to be_valid }
it { expect(errors).to be_present }
it do

View file

@ -66,6 +66,28 @@ RSpec.describe Export, type: :model do
end
end
describe '.find_or_create_export' do
let!(:procedure) { create(:procedure) }
let!(:gi_1) { create(:groupe_instructeur, procedure: procedure, instructeurs: [create(:instructeur)]) }
let!(:pp) { gi_1.instructeurs.first.procedure_presentation_and_errors_for_procedure_id(procedure.id).first }
before { pp.add_filter('tous', 'self/created_at', '10/12/2021') }
context 'with procedure_presentation having different filters' do
it 'works once' do
expect { Export.find_or_create_export(:zip, [gi_1], time_span_type: Export.time_span_types.fetch(:everything), statut: Export.statuts.fetch(:tous), procedure_presentation: pp) }
.to change { Export.count }.by(1)
end
it 'works once, changes procedure_presentation, recreate a new' do
expect { Export.find_or_create_export(:zip, [gi_1], time_span_type: Export.time_span_types.fetch(:everything), statut: Export.statuts.fetch(:tous), procedure_presentation: pp) }
.to change { Export.count }.by(1)
pp.add_filter('tous', 'self/updated_at', '10/12/2021')
expect { Export.find_or_create_export(:zip, [gi_1], time_span_type: Export.time_span_types.fetch(:everything), statut: Export.statuts.fetch(:tous), procedure_presentation: pp) }
.to change { Export.count }.by(1)
end
end
end
describe '.dossiers_for_export' do
let!(:procedure) { create(:procedure, :published, :with_instructeur) }

View file

@ -169,14 +169,17 @@ describe Instructeur, type: :model do
pp.save(:validate => false)
end
it { expect(procedure_presentation).not_to be_persisted }
it 'recreates a valid prsentation' do
expect(procedure_presentation).to be_persisted
end
it { expect(procedure_presentation).to be_valid }
it { expect(errors).to be_present }
end
context 'with default presentation' do
let(:procedure_id) { procedure_2.id }
it { expect(procedure_presentation).not_to be_persisted }
it { expect(procedure_presentation).to be_persisted }
it { expect(errors).to be_nil }
end
end