demarches-normaliennes/spec/services/instructeurs_import_service_spec.rb
François Vantomme 99086a63be Fix (Instructeur): deal nicely with non-guaranteed elements order in arrays
Use of one-dimension arrays comparison & `contain_exactly` RSpec matcher
to avoid this behaviour:

Failures:
  1) InstructeursImportService#import when an email is malformed ignores or corrects
     Failure/Error:
       expect(procedure_groupes).to match_array([
         ["Occitanie", ["paul@mccartney.uk", "ringo@starr.uk"]],
         ["défaut", []]
       ])
       expected collection contained:  [["Occitanie", ["paul@mccartney.uk", "ringo@starr.uk"]], ["défaut", []]]
       actual collection contained:    [["Occitanie", ["ringo@starr.uk", "paul@mccartney.uk"]], ["défaut", []]]
       the missing elements were:      [["Occitanie", ["paul@mccartney.uk", "ringo@starr.uk"]]]
       the extra elements were:        [["Occitanie", ["ringo@starr.uk", "paul@mccartney.uk"]]]
     # ./spec/services/instructeurs_import_service_spec.rb:70:in `block (4 levels) in <main>'
2021-05-27 08:33:52 +02:00

130 lines
4.1 KiB
Ruby
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

describe InstructeursImportService do
describe '#import' do
let(:service) { InstructeursImportService.new }
let(:procedure) { create(:procedure) }
let(:procedure_groupes) do
procedure
.groupe_instructeurs
.map { |gi| [gi.label, gi.instructeurs.map(&:email)] }
.to_h
end
subject { service.import(procedure, lines) }
context 'nominal case' do
let(:lines) do
[
{ "groupe" => "Auvergne Rhone-Alpes", "email" => "john@lennon.fr" },
{ "groupe" => " Occitanie ", "email" => "paul@mccartney.uk" },
{ "groupe" => "Occitanie", "email" => "ringo@starr.uk" }
]
end
it 'imports' do
errors = subject
expect(procedure_groupes.keys).to contain_exactly("Auvergne Rhone-Alpes", "Occitanie", "défaut")
expect(procedure_groupes["Auvergne Rhone-Alpes"]).to contain_exactly("john@lennon.fr")
expect(procedure_groupes["Occitanie"]).to contain_exactly("paul@mccartney.uk", "ringo@starr.uk")
expect(procedure_groupes["défaut"]).to be_empty
expect(errors).to match_array([])
end
end
context 'when group already exists' do
let!(:gi) { create(:groupe_instructeur, label: 'Occitanie', procedure: procedure) }
let(:lines) do
[
{ "groupe" => "Occitanie", "email" => "ringo@starr.uk" }
]
end
before do
gi.instructeurs << create(:instructeur, email: 'george@harisson.uk')
end
it 'adds instructeur to existing groupe' do
subject
expect(procedure_groupes.keys).to contain_exactly("Occitanie", "défaut")
expect(procedure_groupes["Occitanie"]).to contain_exactly("george@harisson.uk", "ringo@starr.uk")
expect(procedure_groupes["défaut"]).to be_empty
end
end
context 'when an email is malformed' do
let(:lines) do
[
{ "groupe" => "Occitanie", "email" => "paul" },
{ "groupe" => "Occitanie", "email" => "  Paul@mccartney.uk " },
{ "groupe" => "Occitanie", "email" => "ringo@starr.uk" }
]
end
it 'ignores or corrects' do
errors = subject
expect(procedure_groupes.keys).to contain_exactly("Occitanie", "défaut")
expect(procedure_groupes["Occitanie"]).to contain_exactly("paul@mccartney.uk", "ringo@starr.uk")
expect(procedure_groupes["défaut"]).to be_empty
expect(errors).to contain_exactly("paul")
end
end
context 'when an instructeur already exists' do
let!(:instructeur) { create(:instructeur) }
let(:lines) do
[
{ "groupe" => "Occitanie", "email" => instructeur.email },
{ "groupe" => "Occitanie", "email" => "ringo@starr.uk" }
]
end
it 'reuses instructeur' do
subject
expect(procedure_groupes.keys).to contain_exactly("Occitanie", "défaut")
expect(procedure_groupes["Occitanie"]).to contain_exactly(instructeur.email, "ringo@starr.uk")
expect(procedure_groupes["défaut"]).to be_empty
end
end
context 'when there are 2 emails of same instructeur to be imported' do
let(:lines) do
[
{ "groupe" => "Occitanie", "email" => "ringo@starr.uk" },
{ "groupe" => "Occitanie", "email" => "ringo@starr.uk" }
]
end
it 'ignores duplicated instructeur' do
subject
expect(procedure_groupes.keys).to contain_exactly("Occitanie", "défaut")
expect(procedure_groupes["Occitanie"]).to contain_exactly("ringo@starr.uk")
expect(procedure_groupes["défaut"]).to be_empty
end
end
context 'when label of group is empty' do
let(:lines) do
[
{ "groupe" => "", "email" => "ringo@starr.uk" },
{ "groupe" => " ", "email" => "paul@starr.uk" }
]
end
it 'ignores instructeur' do
errors = subject
expect(procedure_groupes.keys).to contain_exactly("défaut")
expect(procedure_groupes["défaut"]).to be_empty
expect(errors).to contain_exactly("ringo@starr.uk", "paul@starr.uk")
end
end
end
end