review: update value with async fetch

1 - spec cover the job which fetches external data

2 - refactor the job with guard clauses

3 - delegate update operation to the champ itself

4 - annuaire education: override the update operation
to let the value be populated by the fetched data

5 - prefilling: don't fetch data synchronously
This commit is contained in:
sebastiencarceles 2023-02-28 13:55:52 +01:00
parent 8b25503f7e
commit f52554b5a3
11 changed files with 127 additions and 54 deletions

View file

@ -602,4 +602,12 @@ describe Champ do
end
end
end
describe '#update_with_external_data!' do
let(:champ) { create(:champ_siret) }
let(:data) { "data" }
subject { champ.update_with_external_data!(data: data) }
it { expect { subject }.to change { champ.reload.data }.to(data) }
end
end

View file

@ -0,0 +1,42 @@
# frozen_string_literal: true
require 'rails_helper'
RSpec.describe Champs::AnnuaireEducationChamp do
describe '#update_with_external_data!' do
let(:champ) { create(:champ_annuaire_education, data: "any data") }
subject { champ.update_with_external_data!(data: data) }
shared_examples "a data updater (without updating the value)" do |data|
it { expect { subject }.to change { champ.reload.data }.to(data) }
it { expect { subject }.not_to change { champ.reload.value } }
end
context 'when data is nil' do
let(:data) { nil }
it_behaves_like "a data updater (without updating the value)", nil
end
context 'when data is empty' do
let(:data) { '' }
it_behaves_like "a data updater (without updating the value)", ''
end
context 'when data is inconsistent' do
let(:data) { { 'yo' => 'lo' } }
it_behaves_like "a data updater (without updating the value)", { 'yo' => 'lo' }
end
context 'when data is consistent' do
let(:data) {
{
'nom_etablissement': "karrigel an ankou",
'nom_commune' => 'kumun',
'identifiant_de_l_etablissement' => '666667'
}.with_indifferent_access
}
it { expect { subject }.to change { champ.reload.data }.to(data) }
it { expect { subject }.to change { champ.reload.value }.to('karrigel an ankou, kumun (666667)') }
end
end
end

View file

@ -6,7 +6,6 @@ RSpec.describe PrefillParams do
let(:dossier) { create(:dossier, :brouillon, procedure: procedure) }
let(:types_de_champ_public) { [] }
let(:types_de_champ_private) { [] }
let(:api_annuaire_education_body) { File.read('spec/fixtures/files/api_education/annuaire_education.json') }
subject(:prefill_params_array) { described_class.new(dossier, params).to_a }
@ -18,11 +17,6 @@ RSpec.describe PrefillParams do
VCR.insert_cassette('api_geo_departements')
VCR.insert_cassette('api_geo_communes')
VCR.insert_cassette('api_geo_epcis')
stub_request(:get, /https:\/\/data.education.gouv.fr\/api\/records\/1.0/).to_return(
body: api_annuaire_education_body,
status: 200
)
end
after do
@ -256,12 +250,6 @@ RSpec.describe PrefillParams do
expect(prefill_params_array).to match([])
end
end
context "when the annuaire education can't find the school for the given value" do
let(:api_annuaire_education_body) { File.read('spec/fixtures/files/api_education/annuaire_education_empty.json') }
it_behaves_like "a champ public value that is unauthorized", :annuaire_education, "value"
end
end
private

View file

@ -16,41 +16,17 @@ RSpec.describe TypesDeChamp::PrefillAnnuaireEducationTypeDeChamp do
context 'when the value is nil' do
let(:value) { nil }
it { is_expected.to eq(nil) }
end
context 'when the value is empty' do
let(:value) { '' }
it { is_expected.to eq(nil) }
end
context 'when the value is present' do
let(:value) { '0050009H' }
before do
stub_request(:get, /https:\/\/data.education.gouv.fr\/api\/records\/1.0/)
.to_return(body: body, status: 200)
end
context 'when the annuaire education api responds with a valid schema' do
let(:body) { File.read('spec/fixtures/files/api_education/annuaire_education.json') }
it { is_expected.to match({ id: champ.id, external_id: '0050009H', value: 'Lycée professionnel Sévigné, Gap (0050009H)' }) }
end
context "when the annuaire education api responds with invalid schema" do
let(:body) { File.read('spec/fixtures/files/api_education/annuaire_education_invalid.json') }
it { is_expected.to eq(nil) }
end
context 'when the annuaire education api responds with empty schema' do
let(:body) { File.read('spec/fixtures/files/api_education/annuaire_education_empty.json') }
it { is_expected.to eq(nil) }
end
it { is_expected.to match({ id: champ.id, external_id: '0050009H', value: '0050009H' }) }
end
end
end