fix(data): decimal number having spaces are no more exportable

This commit is contained in:
mfo 2024-06-25 17:38:34 +02:00
parent 476c7df160
commit cfda807b66
No known key found for this signature in database
GPG key ID: 7CE3E1F5B794A8EC
3 changed files with 65 additions and 3 deletions

View file

@ -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

View file

@ -57,15 +57,15 @@ describe Champs::DecimalNumberChamp do
subject { champ.for_export } subject { champ.for_export }
context 'with nil' do context 'with nil' do
let(:value) { 0 } let(:value) { 0 }
it { is_expected.to be_nil } it { is_expected.to eq(0.0) }
end end
context 'with simple number' do context 'with simple number' do
let(:value) { "120" } let(:value) { "120" }
it { is_expected.to eq(120) } it { is_expected.to eq(120) }
end end
context 'with nuber with space' do context 'with number having spaces' do
let(:value) { " 120 " } let(:value) { " 120 " }
it { is_expected.to eq(120) } it { is_expected.to be_nil }
end end
end end
end end

View file

@ -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