From 476c7df160980ea25799b5f224afde291049c87e Mon Sep 17 00:00:00 2001 From: mfo Date: Tue, 25 Jun 2024 17:05:33 +0200 Subject: [PATCH 1/2] bug(Champs::DecimalNumberChamp.for_export): should be able to cast a number wrapped with a string having leading/trailing spaces --- spec/models/champs/decimal_number_champ_spec.rb | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/spec/models/champs/decimal_number_champ_spec.rb b/spec/models/champs/decimal_number_champ_spec.rb index 39ead6506..fc5be9d75 100644 --- a/spec/models/champs/decimal_number_champ_spec.rb +++ b/spec/models/champs/decimal_number_champ_spec.rb @@ -51,4 +51,21 @@ describe Champs::DecimalNumberChamp do it { is_expected.to be_truthy } end end + + describe 'for_export' do + let(:champ) { create(:champ_decimal_number, value:) } + subject { champ.for_export } + context 'with nil' do + let(:value) { 0 } + it { is_expected.to be_nil } + end + context 'with simple number' do + let(:value) { "120" } + it { is_expected.to eq(120) } + end + context 'with nuber with space' do + let(:value) { " 120 " } + it { is_expected.to eq(120) } + end + end end From cfda807b667f506e9f5a582ecc5ed6863e009294 Mon Sep 17 00:00:00 2001 From: mfo Date: Tue, 25 Jun 2024 17:38:34 +0200 Subject: [PATCH 2/2] fix(data): decimal number having spaces are no more exportable --- .../fix_decimal_number_with_spaces_task.rb | 20 +++++++++ .../champs/decimal_number_champ_spec.rb | 6 +-- ...ix_decimal_number_with_spaces_task_spec.rb | 42 +++++++++++++++++++ 3 files changed, 65 insertions(+), 3 deletions(-) create mode 100644 app/tasks/maintenance/fix_decimal_number_with_spaces_task.rb create mode 100644 spec/tasks/maintenance/fix_decimal_number_with_spaces_task_spec.rb diff --git a/app/tasks/maintenance/fix_decimal_number_with_spaces_task.rb b/app/tasks/maintenance/fix_decimal_number_with_spaces_task.rb new file mode 100644 index 000000000..7e6fe41b8 --- /dev/null +++ b/app/tasks/maintenance/fix_decimal_number_with_spaces_task.rb @@ -0,0 +1,20 @@ +# frozen_string_literal: true + +module Maintenance + class FixDecimalNumberWithSpacesTask < MaintenanceTasks::Task + ANY_SPACES = /[[:space:]]/ + def collection + Champs::DecimalNumberChamp.where.not(value: nil) + end + + def process(element) + if element.value.present? && ANY_SPACES.match?(element.value) + element.update_column(:value, element.value.gsub(ANY_SPACES, '')) + end + end + + def count + # not really interested in counting because it raises PG Statement timeout + end + end +end diff --git a/spec/models/champs/decimal_number_champ_spec.rb b/spec/models/champs/decimal_number_champ_spec.rb index fc5be9d75..3a5674f73 100644 --- a/spec/models/champs/decimal_number_champ_spec.rb +++ b/spec/models/champs/decimal_number_champ_spec.rb @@ -57,15 +57,15 @@ describe Champs::DecimalNumberChamp do subject { champ.for_export } context 'with nil' do let(:value) { 0 } - it { is_expected.to be_nil } + it { is_expected.to eq(0.0) } end context 'with simple number' do let(:value) { "120" } it { is_expected.to eq(120) } end - context 'with nuber with space' do + context 'with number having spaces' do let(:value) { " 120 " } - it { is_expected.to eq(120) } + it { is_expected.to be_nil } end end end diff --git a/spec/tasks/maintenance/fix_decimal_number_with_spaces_task_spec.rb b/spec/tasks/maintenance/fix_decimal_number_with_spaces_task_spec.rb new file mode 100644 index 000000000..1e257144f --- /dev/null +++ b/spec/tasks/maintenance/fix_decimal_number_with_spaces_task_spec.rb @@ -0,0 +1,42 @@ +# frozen_string_literal: true + +require "rails_helper" + +module Maintenance + RSpec.describe FixDecimalNumberWithSpacesTask do + describe "#process" do + subject(:process) { described_class.process(element) } + let(:champ) { create(:champ_decimal_number, value:) } + let(:element) { champ } + + context 'with nil' do + let(:value) { 0 } + it { expect { process }.not_to change { champ.reload.valid_value } } + end + context 'with simple number' do + let(:value) { "120" } + it { expect { process }.not_to change { champ.reload.valid_value } } + end + context 'with number having leading spaces' do + let(:value) { " 120" } + it { expect { process }.to change { champ.reload.valid_value }.from(nil).to("120") } + end + context 'with number having trailing spaces' do + let(:value) { "120 " } + it { expect { process }.to change { champ.reload.valid_value }.from(nil).to("120") } + end + context 'with number having leading and trailing spaces' do + let(:value) { " 120 " } + it { expect { process }.to change { champ.reload.valid_value }.from(nil).to("120") } + end + context 'with number having in between spaces' do + let(:value) { "1 2 0" } + it { expect { process }.to change { champ.reload.valid_value }.from(nil).to("120") } + end + context 'with number having in between tab' do + let(:value) { "\t120\t" } + it { expect { process }.to change { champ.reload.valid_value }.from(nil).to("120") } + end + end + end +end