correctif(data.kc): re-import les données kc

This commit is contained in:
Martin 2023-05-15 11:41:38 +02:00 committed by LeSim
parent 174b0edaa1
commit e9115b10b5
7 changed files with 143 additions and 0 deletions

View file

@ -0,0 +1,21 @@
module Recovery
class Exporter
FILE_PATH = Rails.root.join('lib', 'data', 'export.dump')
attr_reader :dossiers
def initialize(dossier_ids:, file_path: FILE_PATH)
dossier_with_data = Dossier.where(id: dossier_ids)
.preload(:user,
:individual,
:etablissement,
revision: :procedure)
@dossiers = DossierPreloader.new(dossier_with_data).all
@file_path = file_path
end
def dump
File.open(@file_path, 'wb') { _1.write(Marshal.dump(@dossiers)) }
end
end
end

View file

@ -0,0 +1,17 @@
module Recovery
class Importer
attr_reader :dossiers
def initialize(file_path: Recovery::Exporter::FILE_PATH)
@dossiers = Marshal.load(File.read(file_path))
end
def load
@dossiers.map do |dossier|
dossier.instance_variable_set :@new_record, true
dossier.save!
end
end
end
end

View file

@ -0,0 +1,33 @@
module Recovery
class LifeCycle
def initialize(dossier_ids:)
@dossier_ids = dossier_ids
end
def load_export_destroy_and_import
export_dossiers
destroy_dossiers
import_dossiers
end
def exporter
@exporter ||= Recovery::Exporter.new(dossier_ids: @dossier_ids)
end
def importer
@importer ||= Importer.new()
end
def export_dossiers
exporter.dump
end
def destroy_dossiers
Dossier.where(id: @dossier_ids).destroy_all
end
def import_dossiers
importer.load
end
end
end

BIN
spec/fixtures/recovery/export.dump vendored Normal file

Binary file not shown.

View file

@ -0,0 +1,48 @@
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') }
subject { Recovery::Exporter.new(dossier_ids:, file_path: fp).dump }
def cleanup_export_file
# if File.exist?(fp)
# FileUtils.rm(fp)
# end
end
before { cleanup_export_file }
after { cleanup_export_file }
it 'exports dossiers to .dump' do
expect{ subject }.not_to raise_error
end
it 'exports dossiers local file .dump' 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

@ -0,0 +1,16 @@
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

@ -0,0 +1,8 @@
describe Recovery::LifeCycle do
describe '.load_export_destroy_and_import' do
it 'works with one dossier' do
dossier = create(:dossier, :with_individual)
expect { Recovery::LifeCycle.new(dossier_ids: [dossier.id]).load_export_destroy_and_import }.not_to change {Dossier.count}
end
end
end