Browse Source

move validation from parser to model

pull/1148/head
Phil Lee 3 years ago
parent
commit
f3a34fe223
  1. 36
      app/services/bulk_upload/lettings/row_parser.rb
  2. 72
      spec/services/bulk_upload/lettings/row_parser_spec.rb

36
app/services/bulk_upload/lettings/row_parser.rb

@ -137,19 +137,25 @@ class BulkUpload::Lettings::RowParser
attribute :field_133, :integer attribute :field_133, :integer
attribute :field_134, :integer attribute :field_134, :integer
validates :field_1, presence: true, numericality: { in: (1..12) } def valid?
validates :field_4, numericality: { in: (1..999), allow_blank: true } log.valid?
validates :field_4, presence: true, if: :field_4_presence_check
validate :validate_possible_answers errors.clear
# delegate :valid?, to: :native_object log.errors.each do |error|
# delegate :errors, to: :native_object field = field_for_attribute(error.attribute)
errors.add(field, error.type)
end
end
private private
def native_object def log
@native_object ||= LettingsLog.new(attributes_for_log) @log ||= LettingsLog.new(attributes_for_log)
end
def field_for_attribute(attribute)
field_mapping.invert[attribute]
end end
def field_mapping def field_mapping
@ -158,16 +164,6 @@ private
} }
end end
def validate_possible_answers
field_mapping.each do |field, attribute|
possible_answers = FormHandler.instance.current_lettings_form.questions.find { |q| q.id == attribute.to_s }.answer_options.keys
unless possible_answers.include?(public_send(field))
errors.add(field, "Value supplied is not one of the permitted values")
end
end
end
def attributes_for_log def attributes_for_log
hash = field_mapping.invert hash = field_mapping.invert
attributes = {} attributes = {}
@ -178,8 +174,4 @@ private
attributes attributes
end end
def field_4_presence_check
[1, 3, 5, 7, 9, 11].include?(field_1)
end
end end

72
spec/services/bulk_upload/lettings/row_parser_spec.rb

@ -3,77 +3,29 @@ require "rails_helper"
RSpec.describe BulkUpload::Lettings::RowParser do RSpec.describe BulkUpload::Lettings::RowParser do
subject(:parser) { described_class.new(attributes) } subject(:parser) { described_class.new(attributes) }
let(:attributes) { {} }
describe "validations" do describe "validations" do
before do before do
parser.valid? parser.valid?
end end
describe "field_1" do describe "#valid?" do
context "when null" do let(:attributes) { { field_134: 3 } }
let(:attributes) { { field_1: nil } }
it "returns an error" do
expect(parser.errors).to include(:field_1)
end
end
context "when outside permited range" do
let(:attributes) { { field_1: "13" } }
it "returns an error" do
expect(parser.errors).to include(:field_1)
end
end
context "when valid" do
let(:attributes) { { field_1: 1 } }
it "is valid" do
expect(parser.errors).not_to include(:field_1)
end
end
end
describe "field_4" do
context "when text" do
let(:attributes) { { field_4: "R" } }
it "is not valid" do
expect(parser.errors).to include(:field_4)
end
end
context "when valid" do
let(:attributes) { { field_4: "3" } }
it "is valid" do
expect(parser.errors).not_to include(:field_4)
end
end
context "when allowed to be null" do
let(:attributes) { { field_1: "2", field_4: "" } }
it "is valid" do
expect(parser.errors).not_to include(:field_4)
end
end
context "when not allowed to be null" do
let(:attributes) { { field_1: "3", field_4: "" } }
it "is not valid" do context "when calling the method multiple times" do
expect(parser.errors).to include(:field_4) it "does not add keep adding errors to the pile" do
expect { parser.valid? }.not_to change(parser.errors, :count)
end end
end end
end end
describe "#field_134" do describe "field_134" do
context "when not a possible value" do context "when an unpermitted value" do
let(:attributes) { { field_134: "3" } } let(:attributes) { { field_134: 3 } }
it "is not valid" do it "has errors on the field" do
expect(parser.errors).to include(:field_134) expect(parser.errors[:field_134]).to be_present
end end
end end
end end

Loading…
Cancel
Save