tech(tache.recovery): ajoute une tache pour re-importer des dossiers venant d'un backup

Update app/lib/recovery/exporter.rb

Co-authored-by: Colin Darie <colin@darie.eu>
This commit is contained in:
Martin 2023-05-15 17:41:53 +02:00 committed by LeSim
parent 9d1d523cf6
commit f76e52cc97
8 changed files with 82 additions and 63 deletions

View file

@ -1,12 +1,12 @@
describe Recovery::Exporter do
let(:dossier_ids) { [create(:dossier, :with_individual).id, create(:dossier, :with_individual).id] }
let(:fp) { Rails.root.join('spec', 'fixtures', 'recovery', 'export.dump') }
let(:fp) { Rails.root.join('spec', 'fixtures', 'export.dump') }
subject { Recovery::Exporter.new(dossier_ids:, file_path: fp).dump }
def cleanup_export_file
# if File.exist?(fp)
# FileUtils.rm(fp)
# end
if File.exist?(fp)
FileUtils.rm(fp)
end
end
before { cleanup_export_file }
@ -20,29 +20,4 @@ describe Recovery::Exporter do
expect { subject }.to change { File.exist?(fp) }
.from(false).to(true)
end
context 'exported' do
before { subject }
let(:exported_dossiers) { Marshal.load(File.read(fp)) }
it 'contains as much as dossiers as input' do
expect(exported_dossiers.size).to eq(dossier_ids.size)
end
it 'contains input dossier ids' do
expect(exported_dossiers.map(&:id)).to match_array(dossier_ids)
end
it 'contains procedure dossier ids' do
expect(exported_dossiers.first.procedure).to be_an_instance_of(Procedure)
end
it 'contains dossier.revision ids' do
expect(exported_dossiers.first.revision).to be_an_instance_of(ProcedureRevision)
end
it 'contains dossier.user' do
expect(exported_dossiers.first.user).to be_an_instance_of(User)
end
end
end

View file

@ -1,16 +0,0 @@
describe Recovery::Importer do
let(:file_path) { Rails.root.join('spec', 'fixtures', 'recovery', 'export.dump') }
let(:importer) { Recovery::Importer.new(file_path:) }
subject { importer.load }
context 'loaded_data' do
let(:loaded_dossiers) { importer.dossiers }
it 'contains user' do
expect(loaded_dossiers.first.user).to be_an_instance_of(User)
end
end
it 're-import dossiers from .dump' do
expect { subject }.to change { Dossier.count }.by(importer.dossiers.size)
end
end

View file

@ -11,7 +11,7 @@ describe 'Recovery::LifeCycle' do
let(:some_file) { Rack::Test::UploadedFile.new('spec/fixtures/files/white.png', 'image/png') }
let(:geo_area) { build(:geo_area, :selection_utilisateur, :polygon) }
let(:fp) { Rails.root.join('spec', 'fixtures', 'export.dump') }
let(:dossier) do
d = create(:dossier, procedure:)
@ -52,21 +52,28 @@ describe 'Recovery::LifeCycle' do
def carte(d) = d.champs.find_by(type: "Champs::CarteChamp")
def siret(d) = d.champs.find_by(type: "Champs::SiretChamp")
def cleanup_export_file
if File.exist?(fp)
FileUtils.rm(fp)
end
end
let(:instructeur) { create(:instructeur) }
before do
instructeur.followed_dossiers << dossier
cleanup_export_file
end
after { cleanup_export_file }
it 'reloads the full grappe' do
expect(Dossier.count).to eq(1)
expect(Dossier.first.champs.count).not_to be(0)
@dossier_ids = Dossier.ids
Recovery::Exporter.new(dossier_ids: @dossier_ids).dump
Recovery::Exporter.new(dossier_ids: @dossier_ids, file_path: fp).dump
Dossier.where(id: @dossier_ids).destroy_all
Recovery::Importer.new().load
Recovery::Importer.new(file_path: fp).load
expect(Dossier.count).to eq(1)