fix(exports): display exact number of dossiers count at time generation

This commit is contained in:
Colin Darie 2023-10-09 10:47:06 +02:00
parent 640f729413
commit 428b869181
No known key found for this signature in database
GPG key ID: 8C76CADD40253590
3 changed files with 61 additions and 11 deletions

View file

@ -51,6 +51,7 @@ class Export < ApplicationRecord
end end
def compute def compute
self.dossiers_count = dossiers_for_export.count
load_snapshot! load_snapshot!
file.attach(blob.signed_id) # attaching a blob directly might run identify/virus scanner and wipe it file.attach(blob.signed_id) # attaching a blob directly might run identify/virus scanner and wipe it
@ -109,9 +110,10 @@ class Export < ApplicationRecord
end end
def count def count
if procedure_presentation_id.present? return dossiers_count if !dossiers_count.nil? # export generated
dossiers_for_export.count return dossiers_for_export.count if procedure_presentation_id.present?
end
nil
end end
def procedure def procedure

View file

@ -1,17 +1,19 @@
RSpec.describe Dossiers::ExportLinkComponent, type: :component do RSpec.describe Dossiers::ExportLinkComponent, type: :component do
let(:procedure) { create(:procedure) } let(:procedure) { create(:procedure) }
let(:groupe_instructeur) { create(:groupe_instructeur, procedure: procedure) } let(:groupe_instructeur) { create(:groupe_instructeur, procedure: procedure, instructeurs: [build(:instructeur)]) }
let(:export) { create(:export, groupe_instructeurs: [groupe_instructeur], updated_at: 5.minutes.ago, created_at: 10.minutes.ago) }
let(:export_url) { double("ExportUrl", call: "/some/fake/path") } let(:export_url) { double("ExportUrl", call: "/some/fake/path") }
let(:assign_to) { create(:assign_to, procedure: procedure, instructeur: groupe_instructeur.instructeurs.first) }
let(:procedure_presentation) { create(:procedure_presentation, procedure: procedure, assign_to: assign_to) }
let(:component) { described_class.new(procedure:, exports: [export], export_url:) } let(:component) { described_class.new(procedure:, exports: [export], export_url:) }
describe "rendering" do describe "rendering" do
subject { render_inline(component).to_html } subject { render_inline(component).to_html }
context "when the export is available" do context "when the export is available" do
let(:export) { create(:export, :generated, groupe_instructeurs: [groupe_instructeur], updated_at: 5.minutes.ago, created_at: 10.minutes.ago) }
before do before do
allow(export).to receive(:available?).and_return(true)
attachment = ActiveStorage::Attachment.new(name: "export", record: export, blob: ActiveStorage::Blob.new(byte_size: 10.kilobytes, content_type: "text/csv", filename: "export.csv")) attachment = ActiveStorage::Attachment.new(name: "export", record: export, blob: ActiveStorage::Blob.new(byte_size: 10.kilobytes, content_type: "text/csv", filename: "export.csv"))
allow(export).to receive(:file).and_return(attachment) allow(export).to receive(:file).and_return(attachment)
end end
@ -25,12 +27,29 @@ RSpec.describe Dossiers::ExportLinkComponent, type: :component do
expect(subject).to include("CSV") expect(subject).to include("CSV")
expect(subject).to include("10 ko") expect(subject).to include("10 ko")
end end
context 'when export is for everything' do
it 'not display the exact dossiers count' do
expect(subject).to include("tous les dossiers")
end
end
context 'when export is for a presentation' do
before do
export.update!(procedure_presentation: procedure_presentation)
end
it 'display the persisted dossiers count' do
expect(subject).to include("10 dossiers")
end
end
end end
context "when the export is not available" do context "when the export is not available" do
let(:export) { create(:export, :pending, groupe_instructeurs: [groupe_instructeur], procedure_presentation: procedure_presentation, created_at: 10.minutes.ago) }
before do before do
allow(export).to receive(:available?).and_return(false) create_list(:dossier, 3, :en_construction, procedure: procedure, groupe_instructeur: groupe_instructeur)
allow(export).to receive(:failed?).and_return(false)
end end
it "displays the pending label" do it "displays the pending label" do
@ -40,12 +59,14 @@ RSpec.describe Dossiers::ExportLinkComponent, type: :component do
it "displays a refresh page button" do it "displays a refresh page button" do
expect(subject).to include("Recharger") expect(subject).to include("Recharger")
end end
it 'displays the current dossiers count' do
expect(subject).to include("3 dossiers")
end
end end
context "when the export has failed" do context "when the export has failed" do
before do let(:export) { create(:export, :failed) }
allow(export).to receive(:failed?).and_return(true)
end
it "displays the refresh old export button" do it "displays the refresh old export button" do
expect(subject).to include("Regénérer") expect(subject).to include("Regénérer")

View file

@ -198,4 +198,31 @@ RSpec.describe Export, type: :model do
expect(results.count).to eq(1) expect(results.count).to eq(1)
end end
end end
describe '.dossiers_count' do
let(:export) { create(:export, :pending) }
before do
blob_double = instance_double("ActiveStorage::Blob", signed_id: "some_signed_id_value")
attachment_double = instance_double("ActiveStorage::Attached::One", attach: true)
allow(export).to receive(:blob).and_return(blob_double)
allow(export).to receive(:file).and_return(attachment_double)
create_list(:dossier, 3, :en_construction, groupe_instructeur: export.groupe_instructeurs.first)
end
it 'is not set until generation' do
expect(export.dossiers_count).to be_nil
end
it 'is persisted after generation' do
export.compute_with_safe_stale_for_purge do
export.compute
end
export.reload
expect(export.dossiers_count).to eq(3)
end
end
end end