From 41228e22958dcb2ab5585398409a7d3726553642 Mon Sep 17 00:00:00 2001 From: natdeanlewissoftwire <94526761+natdeanlewissoftwire@users.noreply.github.com> Date: Fri, 8 Sep 2023 16:02:03 +0100 Subject: [PATCH] Fix hundredth error on migration of very small scharge values (#1898) * feat: round all decimal values to 2dp in safe_string_as_decimal * refactor: lint --- app/services/imports/logs_import_service.rb | 2 +- .../lettings_logs_import_service_spec.rb | 23 +++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/app/services/imports/logs_import_service.rb b/app/services/imports/logs_import_service.rb index 930956f16..a82208bbe 100644 --- a/app/services/imports/logs_import_service.rb +++ b/app/services/imports/logs_import_service.rb @@ -72,7 +72,7 @@ module Imports if str.nil? nil else - BigDecimal(str, exception: false) + BigDecimal(str, exception: false).round(2) end end diff --git a/spec/services/imports/lettings_logs_import_service_spec.rb b/spec/services/imports/lettings_logs_import_service_spec.rb index 8367c7d71..6bfe53571 100644 --- a/spec/services/imports/lettings_logs_import_service_spec.rb +++ b/spec/services/imports/lettings_logs_import_service_spec.rb @@ -796,6 +796,29 @@ RSpec.describe Imports::LettingsLogsImportService do end end + context "and scharge is ever so slightly positive" do + let(:lettings_log_id) { "0b4a68df-30cc-474a-93c0-a56ce8fdad3b" } + + before do + lettings_log_xml.at_xpath("//xmlns:Q18aii").content = "1.66533E-16" + end + + it "does not raise an error" do + expect { lettings_log_service.send(:create_log, lettings_log_xml) } + .not_to raise_error + end + + it "sets scharge to 0" do + allow(logger).to receive(:warn) + + lettings_log_service.send(:create_log, lettings_log_xml) + lettings_log = LettingsLog.find_by(old_id: lettings_log_id) + + expect(lettings_log).not_to be_nil + expect(lettings_log.scharge).to eq(0) + end + end + context "and tshortfall is not positive" do let(:lettings_log_id) { "0b4a68df-30cc-474a-93c0-a56ce8fdad3b" }