update zone to procedures

This commit is contained in:
Christophe Robillard 2021-12-15 19:36:02 +01:00
parent 818e0472dd
commit 882f92268c
3 changed files with 101 additions and 0 deletions

View file

@ -0,0 +1,21 @@
class UpdateZoneToProceduresService
def self.call(lines)
errors = []
lines.each do |line|
zone_label = line["POL_PUB_MINISTERE RATTACHEMENT"]
zone = Zone.find_by(acronym: zone_label)
if zone.nil?
errors << "Zone #{zone_label} introuvable"
else
id = line["id"]
procedure = Procedure.find_by(id: id)
if procedure
procedure.update(zone: zone)
else
errors << "Procedure #{id} introuvable"
end
end
end
errors
end
end

View file

@ -0,0 +1,23 @@
require Rails.root.join("lib", "tasks", "task_helper")
namespace :zones do
desc <<~EOD
Update zone to all procedures
rails zones:update_zone_to_procedures\[csv_path\]
EOD
task :update_zone_to_procedures, [:csv] => :environment do |_t, args|
csv = args[:csv]
lines = CSV.readlines(csv, headers: true)
rake_puts "Mise à jour des procédures en cours..."
errors =
UpdateZoneToProceduresService.call(lines)
if errors.present?
errors.each { |error| rake_puts error }
end
rake_puts "Mise à jour terminée"
end
end

View file

@ -0,0 +1,57 @@
describe UpdateZoneToProceduresService do
before(:all) do
Rake::Task['populate_zones'].invoke
end
after(:all) do
Zone.destroy_all
end
describe '#call' do
let(:procedure1) { create(:procedure, zone: nil) }
let(:procedure2) { create(:procedure, zone: nil) }
subject { described_class.call(lines) }
context 'nominal case' do
let(:lines) do
[
{ "id" => procedure1.id, "POL_PUB_MINISTERE RATTACHEMENT" => "PM" },
{ "id" => procedure2.id, "POL_PUB_MINISTERE RATTACHEMENT" => "MI" }
]
end
it 'updates zone to procedures' do
errors = subject
expect(errors).to eq []
expect(procedure1.reload.zone.acronym).to eq("PM")
expect(procedure2.reload.zone.acronym).to eq("MI")
end
end
context 'with unknown procedure' do
let(:lines) do
[
{ "id" => procedure1.id + procedure2.id, "POL_PUB_MINISTERE RATTACHEMENT" => "PM" }
]
end
it 'returns errors' do
errors = subject
expect(errors).to eq ["Procedure #{procedure1.id + procedure2.id} introuvable"]
end
end
context 'with unknown zone' do
let(:lines) do
[
{ "id" => procedure1.id, "POL_PUB_MINISTERE RATTACHEMENT" => "YOUPI" }
]
end
it 'returns errors' do
errors = subject
expect(errors).to eq ["Zone YOUPI introuvable"]
end
end
end
end