diff --git a/app/models/lettings_log.rb b/app/models/lettings_log.rb index 0041f3200..69769b782 100644 --- a/app/models/lettings_log.rb +++ b/app/models/lettings_log.rb @@ -8,6 +8,7 @@ class LettingsLogValidator < ActiveModel::Validator include Validations::TenancyValidations include Validations::DateValidations include Validations::LocalAuthorityValidations + def validate(record) validation_methods = public_methods.select { |method| method.starts_with?("validate_") } validation_methods.each { |meth| public_send(meth, record) } diff --git a/app/services/bulk_upload/lettings/row_parser.rb b/app/services/bulk_upload/lettings/row_parser.rb index e2831b799..1eae1d639 100644 --- a/app/services/bulk_upload/lettings/row_parser.rb +++ b/app/services/bulk_upload/lettings/row_parser.rb @@ -137,10 +137,27 @@ class BulkUpload::Lettings::RowParser attribute :field_133, :integer attribute :field_134, :integer + validates :field_1, presence: true, inclusion: { in: (1..12).to_a } + + def attribute_set + @attribute_set ||= instance_variable_get(:@attributes) + end + + def validate_data_types + unless attribute_set["field_1"].value_before_type_cast&.match?(/\A\d+\z/) + errors.add(:field_1, :invalid) + end + end + def valid? + errors.clear + + super + + validate_data_types + log.valid? - errors.clear log.errors.each do |error| field = field_for_attribute(error.attribute) @@ -160,6 +177,7 @@ private def field_mapping { + field_1: :lettype, field_134: :renewal, } end diff --git a/spec/services/bulk_upload/lettings/row_parser_spec.rb b/spec/services/bulk_upload/lettings/row_parser_spec.rb index 50a9e8772..7a5490c2a 100644 --- a/spec/services/bulk_upload/lettings/row_parser_spec.rb +++ b/spec/services/bulk_upload/lettings/row_parser_spec.rb @@ -20,7 +20,41 @@ RSpec.describe BulkUpload::Lettings::RowParser do end end - describe "field_134" do + describe "#field_1" do + context "when null" do + let(:attributes) { { field_1: nil } } + + it "returns an error" do + expect(parser.errors[:field_1]).to be_present + end + end + + context "when incorrect data type" do + let(:attributes) { { field_1: "foo" } } + + it "returns an error" do + expect(parser.errors[:field_1]).to be_present + end + end + + context "when unpermitted value" do + let(:attributes) { { field_1: "101" } } + + it "returns an error" do + expect(parser.errors[:field_1]).to be_present + end + end + + context "when valid" do + let(:attributes) { { field_1: "1" } } + + it "does not return any errors" do + expect(parser.errors[:field_1]).to be_blank + end + end + end + + describe "#field_134" do context "when an unpermitted value" do let(:attributes) { { field_134: 3 } }