2024-04-29 00:17:15 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-06-16 17:21:47 +02:00
|
|
|
describe Logic::ChampValue do
|
|
|
|
include Logic
|
|
|
|
|
2023-12-21 16:09:23 +01:00
|
|
|
describe '#compute' do
|
2024-07-01 15:31:32 +02:00
|
|
|
let(:procedure) { create(:procedure, types_de_champ_public: [{ type: tdc_type, drop_down_other: }]) }
|
|
|
|
let(:drop_down_other) { nil }
|
|
|
|
let(:tdc_type) { :text }
|
|
|
|
let(:tdc) { procedure.active_revision.types_de_champ.first }
|
|
|
|
let(:dossier) { create(:dossier, procedure:) }
|
|
|
|
|
2023-12-21 16:09:23 +01:00
|
|
|
subject { champ_value(champ.stable_id).compute([champ]) }
|
2022-06-16 17:21:47 +02:00
|
|
|
|
2023-12-21 16:09:23 +01:00
|
|
|
context 'yes_no tdc' do
|
2024-07-01 15:31:32 +02:00
|
|
|
let(:tdc_type) { :yes_no }
|
|
|
|
let(:champ) { Champs::YesNoChamp.new(value: value, stable_id: tdc.stable_id, dossier:) }
|
2023-12-21 16:09:23 +01:00
|
|
|
let(:value) { 'true' }
|
2022-06-16 17:21:47 +02:00
|
|
|
|
2023-12-21 16:09:23 +01:00
|
|
|
it { expect(champ_value(champ.stable_id).type([champ.type_de_champ])).to eq(:boolean) }
|
2022-06-16 17:21:47 +02:00
|
|
|
|
2023-12-21 16:09:23 +01:00
|
|
|
context 'with true value' do
|
|
|
|
it { is_expected.to be(true) }
|
|
|
|
end
|
2022-06-16 17:21:47 +02:00
|
|
|
|
2023-12-21 16:09:23 +01:00
|
|
|
context 'with false value' do
|
|
|
|
let(:value) { 'false' }
|
2022-06-16 17:21:47 +02:00
|
|
|
|
2023-12-21 16:09:23 +01:00
|
|
|
it { is_expected.to be(false) }
|
2022-07-19 17:40:50 +02:00
|
|
|
end
|
|
|
|
|
2023-12-21 16:09:23 +01:00
|
|
|
context 'with a value not visible' do
|
|
|
|
before do
|
|
|
|
expect(champ).to receive(:visible?).and_return(false)
|
|
|
|
end
|
|
|
|
|
|
|
|
it { is_expected.to be nil }
|
|
|
|
end
|
2022-07-19 17:40:50 +02:00
|
|
|
end
|
2022-06-16 17:21:47 +02:00
|
|
|
|
2023-12-21 16:09:23 +01:00
|
|
|
context 'integer tdc' do
|
2024-07-01 15:31:32 +02:00
|
|
|
let(:tdc_type) { :integer_number }
|
|
|
|
let(:champ) { Champs::IntegerNumberChamp.new(value:, stable_id: tdc.stable_id, dossier:) }
|
|
|
|
let(:value) { '42' }
|
2022-06-16 17:21:47 +02:00
|
|
|
|
2023-12-21 16:09:23 +01:00
|
|
|
it { expect(champ_value(champ.stable_id).type([champ.type_de_champ])).to eq(:number) }
|
|
|
|
it { is_expected.to eq(42) }
|
2022-07-21 15:48:37 +02:00
|
|
|
|
2023-12-21 16:09:23 +01:00
|
|
|
context 'with a blank value' do
|
2024-07-01 15:31:32 +02:00
|
|
|
let(:value) { '' }
|
2022-07-21 15:48:37 +02:00
|
|
|
|
2023-12-21 16:09:23 +01:00
|
|
|
it { is_expected.to be nil }
|
|
|
|
end
|
2023-09-26 10:38:38 +02:00
|
|
|
end
|
2022-06-16 17:21:47 +02:00
|
|
|
|
2023-12-21 16:09:23 +01:00
|
|
|
context 'decimal tdc' do
|
2024-07-01 15:31:32 +02:00
|
|
|
let(:tdc_type) { :decimal_number }
|
|
|
|
let(:champ) { Champs::DecimalNumberChamp.new(value:, stable_id: tdc.stable_id, dossier:) }
|
|
|
|
let(:value) { '42.01' }
|
2022-06-16 17:21:47 +02:00
|
|
|
|
2023-12-21 16:09:23 +01:00
|
|
|
it { expect(champ_value(champ.stable_id).type([champ.type_de_champ])).to eq(:number) }
|
|
|
|
it { is_expected.to eq(42.01) }
|
2023-09-26 10:38:38 +02:00
|
|
|
end
|
2022-06-16 17:21:47 +02:00
|
|
|
|
2023-12-21 16:09:23 +01:00
|
|
|
context 'dropdown tdc' do
|
2024-07-01 15:31:32 +02:00
|
|
|
let(:tdc_type) { :drop_down_list }
|
|
|
|
let(:champ) { Champs::DropDownListChamp.new(value:, other:, stable_id: tdc.stable_id, dossier:) }
|
|
|
|
let(:value) { 'val1' }
|
|
|
|
let(:other) { nil }
|
2022-06-09 14:20:06 +02:00
|
|
|
|
2023-12-21 16:09:23 +01:00
|
|
|
it { expect(champ_value(champ.stable_id).type([champ.type_de_champ])).to eq(:enum) }
|
|
|
|
it { is_expected.to eq('val1') }
|
|
|
|
it { expect(champ_value(champ.stable_id).options([champ.type_de_champ])).to match_array([["val1", "val1"], ["val2", "val2"], ["val3", "val3"]]) }
|
2022-08-02 12:57:41 +02:00
|
|
|
|
2023-12-21 16:09:23 +01:00
|
|
|
context 'with other enabled' do
|
2024-07-01 15:31:32 +02:00
|
|
|
let(:tdc_type) { :drop_down_list }
|
|
|
|
let(:drop_down_other) { true }
|
2022-08-02 12:57:41 +02:00
|
|
|
|
2023-12-21 16:09:23 +01:00
|
|
|
it { is_expected.to eq('val1') }
|
2024-04-20 03:33:46 +02:00
|
|
|
it { expect(champ_value(champ.stable_id).options([champ.type_de_champ])).to match_array([["val1", "val1"], ["val2", "val2"], ["val3", "val3"], [I18n.t('shared.champs.drop_down_list.other'), "__other__"]]) }
|
2022-08-02 12:57:41 +02:00
|
|
|
|
2024-07-01 15:31:32 +02:00
|
|
|
context 'with other filled' do
|
|
|
|
let(:other) { true }
|
2022-08-02 12:57:41 +02:00
|
|
|
|
2024-07-01 15:31:32 +02:00
|
|
|
it { is_expected.to eq(Champs::DropDownListChamp::OTHER) }
|
|
|
|
end
|
2023-12-21 16:09:23 +01:00
|
|
|
end
|
2022-08-02 12:57:41 +02:00
|
|
|
end
|
2022-06-09 14:20:06 +02:00
|
|
|
|
2023-12-21 16:09:23 +01:00
|
|
|
context 'checkbox tdc' do
|
2024-07-01 15:31:32 +02:00
|
|
|
let(:tdc_type) { :checkbox }
|
|
|
|
let(:champ) { Champs::CheckboxChamp.new(value:, stable_id: tdc.stable_id, dossier:) }
|
|
|
|
let(:value) { 'true' }
|
2022-06-16 17:21:47 +02:00
|
|
|
|
2023-12-21 16:09:23 +01:00
|
|
|
it { expect(champ_value(champ.stable_id).type([champ.type_de_champ])).to eq(:boolean) }
|
|
|
|
it { is_expected.to eq(true) }
|
|
|
|
end
|
2022-06-16 17:21:47 +02:00
|
|
|
|
2023-12-21 16:09:23 +01:00
|
|
|
context 'departement tdc' do
|
2024-07-01 15:31:32 +02:00
|
|
|
let(:tdc_type) { :departements }
|
|
|
|
let(:champ) { Champs::DepartementChamp.new(value:, stable_id: tdc.stable_id, dossier:) }
|
|
|
|
let(:value) { '02' }
|
2023-09-13 10:53:52 +02:00
|
|
|
|
2023-12-21 16:09:23 +01:00
|
|
|
it { expect(champ_value(champ.stable_id).type([champ.type_de_champ])).to eq(:departement_enum) }
|
2024-02-13 11:24:58 +01:00
|
|
|
it { is_expected.to eq({ value: '02', code_region: '32' }) }
|
2023-12-21 16:09:23 +01:00
|
|
|
end
|
2023-09-13 10:53:52 +02:00
|
|
|
|
2023-12-21 16:09:23 +01:00
|
|
|
context 'region tdc' do
|
2024-07-01 15:31:32 +02:00
|
|
|
let(:tdc_type) { :regions }
|
|
|
|
let(:champ) { Champs::RegionChamp.new(value:, stable_id: tdc.stable_id, dossier:) }
|
|
|
|
let(:value) { 'La Réunion' }
|
2023-09-15 12:19:30 +02:00
|
|
|
|
2023-12-21 16:09:23 +01:00
|
|
|
it { is_expected.to eq('04') }
|
2023-09-15 12:19:30 +02:00
|
|
|
end
|
|
|
|
|
2024-02-13 11:24:58 +01:00
|
|
|
context 'commune tdc' do
|
2024-07-01 15:31:32 +02:00
|
|
|
let(:tdc_type) { :communes }
|
|
|
|
let(:champ) do
|
|
|
|
Champs::CommuneChamp.new(code_postal:, external_id:, stable_id: tdc.stable_id, dossier:)
|
|
|
|
.tap { |c| c.send(:on_codes_change) } # private method called before save to fill value, which is required for compute
|
|
|
|
end
|
|
|
|
let(:code_postal) { '92500' }
|
|
|
|
let(:external_id) { '92063' }
|
2024-02-13 11:24:58 +01:00
|
|
|
|
2024-07-01 15:31:32 +02:00
|
|
|
it do
|
|
|
|
is_expected.to eq({ code_departement: '92', code_region: '11' })
|
|
|
|
end
|
2024-02-13 11:24:58 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'epci tdc' do
|
2024-07-01 15:31:32 +02:00
|
|
|
let(:tdc_type) { :epci }
|
|
|
|
let(:champ) do
|
|
|
|
Champs::EpciChamp.new(code_departement:, external_id:, stable_id: tdc.stable_id, dossier:)
|
|
|
|
.tap { |c| c.send(:on_epci_name_changes) } # private method called before save to fill value, which is required for compute
|
2024-02-13 11:24:58 +01:00
|
|
|
end
|
2024-07-01 15:31:32 +02:00
|
|
|
let(:code_departement) { '43' }
|
|
|
|
let(:external_id) { '244301016' }
|
2024-02-13 11:24:58 +01:00
|
|
|
|
|
|
|
it { is_expected.to eq({ code_departement: '43', code_region: '84' }) }
|
|
|
|
end
|
|
|
|
|
2023-12-21 16:09:23 +01:00
|
|
|
describe 'errors' do
|
2024-07-01 15:31:32 +02:00
|
|
|
let(:tdc_type) { :number }
|
|
|
|
let(:champ) { Champs::IntegerNumberChamp.new(value: nil, stable_id: tdc.stable_id, dossier:) }
|
2023-12-21 16:09:23 +01:00
|
|
|
|
|
|
|
it { expect(champ_value(champ.stable_id).errors([champ.type_de_champ])).to be_empty }
|
|
|
|
it { expect(champ_value(champ.stable_id).errors([])).to eq([{ type: :not_available }]) }
|
2023-09-15 12:19:30 +02:00
|
|
|
end
|
|
|
|
|
2023-12-21 16:09:23 +01:00
|
|
|
describe '#sources' do
|
2024-07-01 15:31:32 +02:00
|
|
|
let(:tdc_type) { :number }
|
|
|
|
let(:champ) { Champs::IntegerNumberChamp.new(value: nil, stable_id: tdc.stable_id, dossier:) }
|
2023-09-19 16:15:32 +02:00
|
|
|
|
2023-12-21 16:09:23 +01:00
|
|
|
it { expect(champ_value(champ.stable_id).sources).to eq([champ.stable_id]) }
|
|
|
|
end
|
2023-09-19 16:15:32 +02:00
|
|
|
|
2023-12-21 16:09:23 +01:00
|
|
|
context 'with multiple revision' do
|
|
|
|
let(:options) { ['revision_1'] }
|
|
|
|
let(:procedure) do
|
|
|
|
create(:procedure, :published, :for_individual, types_de_champ_public: [{ type: :drop_down_list, libelle: 'dropdown', options: options }])
|
|
|
|
end
|
|
|
|
let(:drop_down_r1) { procedure.published_revision.types_de_champ_public.first }
|
|
|
|
let(:stable_id) { drop_down_r1.stable_id }
|
2023-09-13 10:20:34 +02:00
|
|
|
|
2023-12-21 16:09:23 +01:00
|
|
|
it { expect(champ_value(stable_id).options([drop_down_r1])).to match_array([["revision_1", "revision_1"]]) }
|
2023-09-13 10:20:34 +02:00
|
|
|
|
2023-12-21 16:09:23 +01:00
|
|
|
context 'with a new revision' do
|
|
|
|
let(:drop_down_r2) { procedure.draft_revision.types_de_champ_public.first }
|
2022-06-16 17:21:47 +02:00
|
|
|
|
2023-12-21 16:09:23 +01:00
|
|
|
before do
|
|
|
|
tdc = procedure.draft_revision.find_and_ensure_exclusive_use(stable_id)
|
|
|
|
tdc.drop_down_options = ['revision_2']
|
|
|
|
tdc.save!
|
|
|
|
end
|
|
|
|
|
|
|
|
it do
|
|
|
|
expect(champ_value(stable_id).options([drop_down_r2])).to match_array([["revision_2", "revision_2"]])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2022-06-16 17:21:47 +02:00
|
|
|
end
|
|
|
|
end
|