From 646c92ef5ff215301477c6ff22604cc7a4dbe26a Mon Sep 17 00:00:00 2001 From: natdeanlewissoftwire Date: Wed, 8 Nov 2023 11:28:42 +0000 Subject: [PATCH] feat: also allow user to input extra leading zeroes and test --- app/models/location.rb | 2 +- app/models/scheme.rb | 4 +-- .../lettings/year2022/row_parser_spec.rb | 26 ++++++++++++++++++ .../lettings/year2023/row_parser_spec.rb | 27 +++++++++++++++++++ 4 files changed, 56 insertions(+), 3 deletions(-) diff --git a/app/models/location.rb b/app/models/location.rb index a2c57a7bb..65e0466b3 100644 --- a/app/models/location.rb +++ b/app/models/location.rb @@ -110,7 +110,7 @@ class Location < ApplicationRecord def self.find_by_id_on_multiple_fields(id) return if id.nil? - where(id:).or(where("ltrim(old_visible_id, '0') = ?", id.to_s)).first + where(id:).or(where("ltrim(old_visible_id, '0') = ?", id.to_i.to_s)).first end def postcode=(postcode) diff --git a/app/models/scheme.rb b/app/models/scheme.rb index 58135dd76..254953c19 100644 --- a/app/models/scheme.rb +++ b/app/models/scheme.rb @@ -169,9 +169,9 @@ class Scheme < ApplicationRecord if scheme_id.start_with?("S") where(id: scheme_id[1..]).first elsif location_id.present? - joins(:locations).where("ltrim(schemes.old_visible_id, '0') = ? AND ltrim(locations.old_visible_id, '0') = ?", scheme_id.to_s, location_id.to_s).first || where("ltrim(schemes.old_visible_id, '0') = ?", scheme_id.to_s).first + joins(:locations).where("ltrim(schemes.old_visible_id, '0') = ? AND ltrim(locations.old_visible_id, '0') = ?", scheme_id.to_i.to_s, location_id.to_i.to_s).first || where("ltrim(schemes.old_visible_id, '0') = ?", scheme_id.to_i.to_s).first else - where("ltrim(old_visible_id, '0') = ?", scheme_id.to_s).first + where("ltrim(old_visible_id, '0') = ?", scheme_id.to_i.to_s).first end end diff --git a/spec/services/bulk_upload/lettings/year2022/row_parser_spec.rb b/spec/services/bulk_upload/lettings/year2022/row_parser_spec.rb index edd88d5a1..101a17b3b 100644 --- a/spec/services/bulk_upload/lettings/year2022/row_parser_spec.rb +++ b/spec/services/bulk_upload/lettings/year2022/row_parser_spec.rb @@ -1403,6 +1403,19 @@ RSpec.describe BulkUpload::Lettings::Year2022::RowParser do expect(parser.log.location).to eql(location) end end + + context "when the user provides an id with leading zeroes" do + let(:attributes) { { bulk_upload:, field_4: scheme.old_visible_id, field_5: "00123", field_111: owning_org.old_visible_id, field_113: owning_org.old_visible_id } } + + before do + location.old_visible_id = "123" + location.save! + end + + it "assigns the correct location" do + expect(parser.log.location).to eql(location) + end + end end end @@ -1434,6 +1447,19 @@ RSpec.describe BulkUpload::Lettings::Year2022::RowParser do expect(parser.log.scheme).to eql(scheme) end end + + context "when the user provides an id with leading zeroes" do + let(:attributes) { { bulk_upload:, field_4: "010", field_5: location.old_visible_id, field_111: owning_org.old_visible_id, field_113: owning_org.old_visible_id } } + + before do + scheme.old_visible_id = "10" + scheme.save! + end + + it "assigns the correct scheme" do + expect(parser.log.scheme).to eql(scheme) + end + end end end diff --git a/spec/services/bulk_upload/lettings/year2023/row_parser_spec.rb b/spec/services/bulk_upload/lettings/year2023/row_parser_spec.rb index ac331c51f..171007eec 100644 --- a/spec/services/bulk_upload/lettings/year2023/row_parser_spec.rb +++ b/spec/services/bulk_upload/lettings/year2023/row_parser_spec.rb @@ -1746,6 +1746,20 @@ RSpec.describe BulkUpload::Lettings::Year2023::RowParser do expect(parser.log.location).to eql(location) end end + + context "when the user provides an id with leading zeroes" do + let(:attributes) { { bulk_upload:, field_15: scheme.old_visible_id, field_16: "00123", field_1: owning_org.old_visible_id, field_2: owning_org.old_visible_id } } + + before do + location.old_visible_id = "123" + location.save! + end + + it "assigns the correct location" do + expect(parser.log.location).to eql(location) + end + end + end end @@ -1777,6 +1791,19 @@ RSpec.describe BulkUpload::Lettings::Year2023::RowParser do expect(parser.log.scheme).to eql(scheme) end end + + context "when the user provides an id with leading zeroes" do + let(:attributes) { { bulk_upload:, field_15: "010", field_16: location.old_visible_id, field_1: owning_org.old_visible_id, field_2: owning_org.old_visible_id } } + + before do + scheme.old_visible_id = "10" + scheme.save! + end + + it "assigns the correct scheme" do + expect(parser.log.scheme).to eql(scheme) + end + end end end