review: query the db only once
This commit is contained in:
parent
ab30af5fe5
commit
1e21a3d3e1
6 changed files with 86 additions and 42 deletions
|
@ -20,11 +20,11 @@ RSpec.describe DossierPrefillableConcern do
|
|||
context 'when the champs are valid' do
|
||||
let!(:type_de_champ_1) { create(:type_de_champ_text, procedure: procedure) }
|
||||
let(:value_1) { "any value" }
|
||||
let(:champ_id_1) { dossier.find_champ_by_stable_id(type_de_champ_1.stable_id).id }
|
||||
let(:champ_id_1) { find_champ_by_stable_id(dossier, type_de_champ_1.stable_id).id }
|
||||
|
||||
let!(:type_de_champ_2) { create(:type_de_champ_phone, procedure: procedure) }
|
||||
let(:value_2) { "33612345678" }
|
||||
let(:champ_id_2) { dossier.find_champ_by_stable_id(type_de_champ_2.stable_id).id }
|
||||
let(:champ_id_2) { find_champ_by_stable_id(dossier, type_de_champ_2.stable_id).id }
|
||||
|
||||
let(:values) { [{ id: champ_id_1, value: value_1 }, { id: champ_id_2, value: value_2 }] }
|
||||
|
||||
|
@ -38,7 +38,7 @@ RSpec.describe DossierPrefillableConcern do
|
|||
context 'when a champ is invalid' do
|
||||
let!(:type_de_champ) { create(:type_de_champ_phone, procedure: procedure) }
|
||||
let(:value) { "a non phone value" }
|
||||
let(:champ_id) { dossier.find_champ_by_stable_id(type_de_champ.stable_id).id }
|
||||
let(:champ_id) { find_champ_by_stable_id(dossier, type_de_champ.stable_id).id }
|
||||
|
||||
let(:values) { [{ id: champ_id, value: value }] }
|
||||
|
||||
|
@ -48,4 +48,10 @@ RSpec.describe DossierPrefillableConcern do
|
|||
end
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def find_champ_by_stable_id(dossier, stable_id)
|
||||
dossier.champs_public.joins(:type_de_champ).find_by(types_de_champ: { stable_id: stable_id })
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1855,36 +1855,37 @@ describe Dossier do
|
|||
end
|
||||
end
|
||||
|
||||
describe "#find_champ_by_stable_id" do
|
||||
describe '#find_champs_by_stable_ids' do
|
||||
let(:procedure) { create(:procedure, :published) }
|
||||
let(:dossier) { create(:dossier, :brouillon, procedure: procedure) }
|
||||
|
||||
subject { dossier.find_champ_by_stable_id(stable_id) }
|
||||
subject { dossier.find_champs_by_stable_ids(stable_ids) }
|
||||
|
||||
context 'when the stable id is missing' do
|
||||
let(:stable_id) { nil }
|
||||
context 'when stable_ids is empty' do
|
||||
let(:stable_ids) { [] }
|
||||
|
||||
it { expect(subject).to be_nil }
|
||||
it { expect(subject).to match([]) }
|
||||
end
|
||||
|
||||
context 'when the stable id is blank' do
|
||||
let(:stable_id) { "" }
|
||||
context 'when stable_ids contains nil or blank values' do
|
||||
let(:stable_ids) { [nil, ""] }
|
||||
|
||||
it { expect(subject).to be_nil }
|
||||
it { expect(subject).to match([]) }
|
||||
end
|
||||
|
||||
context 'when the stable id is present' do
|
||||
context 'when the dossier has no champ with the given stable id' do
|
||||
let(:stable_id) { 'My Neighbor Totoro is the best film ever' }
|
||||
context 'when stable_ids contains present values' do
|
||||
context 'when the dossier has no champ with the given stable ids' do
|
||||
let(:stable_ids) { ['My Neighbor Totoro', 'Miyazaki'] }
|
||||
|
||||
it { expect(subject).to be_nil }
|
||||
it { expect(subject).to match([]) }
|
||||
end
|
||||
|
||||
context 'when the dossier has a champ with the given stable id' do
|
||||
let!(:type_de_champ) { create(:type_de_champ_text, procedure: procedure) }
|
||||
let(:stable_id) { type_de_champ.stable_id }
|
||||
context 'when the dossier has champs with the given stable ids' do
|
||||
let!(:type_de_champ_1) { create(:type_de_champ_text, procedure: procedure) }
|
||||
let!(:type_de_champ_2) { create(:type_de_champ_textarea, procedure: procedure) }
|
||||
let(:stable_ids) { [type_de_champ_1.stable_id, type_de_champ_2.stable_id] }
|
||||
|
||||
it { expect(subject).to eq(dossier.champs_public.joins(:type_de_champ).find_by(types_de_champ: { stable_id: stable_id })) }
|
||||
it { expect(subject).to match(dossier.champs_public.joins(:type_de_champ).where(types_de_champ: { stable_id: stable_ids })) }
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -8,11 +8,11 @@ RSpec.describe PrefillParams do
|
|||
context "when the stable ids match the TypeDeChamp of the corresponding procedure" do
|
||||
let!(:type_de_champ_1) { create(:type_de_champ_text, procedure: procedure) }
|
||||
let(:value_1) { "any value" }
|
||||
let(:champ_id_1) { dossier.find_champ_by_stable_id(type_de_champ_1.stable_id).id }
|
||||
let(:champ_id_1) { find_champ_by_stable_id(dossier, type_de_champ_1.stable_id).id }
|
||||
|
||||
let!(:type_de_champ_2) { create(:type_de_champ_textarea, procedure: procedure) }
|
||||
let(:value_2) { "another value" }
|
||||
let(:champ_id_2) { dossier.find_champ_by_stable_id(type_de_champ_2.stable_id).id }
|
||||
let(:champ_id_2) { find_champ_by_stable_id(dossier, type_de_champ_2.stable_id).id }
|
||||
|
||||
let(:params) {
|
||||
{
|
||||
|
@ -50,7 +50,7 @@ RSpec.describe PrefillParams do
|
|||
shared_examples "a champ public value that is authorized" do |type_de_champ_name, value|
|
||||
context "when the type de champ is authorized (#{type_de_champ_name})" do
|
||||
let!(:type_de_champ) { create(type_de_champ_name, procedure: procedure) }
|
||||
let(:champ_id) { dossier.find_champ_by_stable_id(type_de_champ.stable_id).id }
|
||||
let(:champ_id) { find_champ_by_stable_id(dossier, type_de_champ.stable_id).id }
|
||||
|
||||
let(:params) { { type_de_champ.to_typed_id => value } }
|
||||
|
||||
|
@ -111,4 +111,10 @@ RSpec.describe PrefillParams do
|
|||
it_behaves_like "a champ public value that is unauthorized", :type_de_champ_mesri, "value"
|
||||
it_behaves_like "a champ public value that is unauthorized", :type_de_champ_carte, "value"
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def find_champ_by_stable_id(dossier, stable_id)
|
||||
dossier.champs_public.joins(:type_de_champ).find_by(types_de_champ: { stable_id: stable_id })
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue