Browse Source

feat: wip tests

pull/1971/head
natdeanlewissoftwire 3 years ago
parent
commit
6b2edf9882
  1. 65
      app/services/bulk_upload/lettings/year2023/row_parser.rb
  2. 181
      spec/services/bulk_upload/lettings/year2023/row_parser_spec.rb

65
app/services/bulk_upload/lettings/year2023/row_parser.rb

@ -332,6 +332,7 @@ class BulkUpload::Lettings::Year2023::RowParser
validates :field_16,
presence: {
if: proc { [2, 4, 6, 8, 10, 12].include?(field_5) },
message: I18n.t("validations.not_answered", question: "scheme code"),
category: :setup,
},
on: :after_log
@ -464,7 +465,7 @@ class BulkUpload::Lettings::Year2023::RowParser
"field_8", # startdate
"field_9", # startdate
"field_13", # tenancycode
field_4 != 1 ? "field_17" : nil, # location
field_4 != 1 ? location_field.to_s : nil, # location
field_4 != 2 ? "field_23" : nil, # postcode
field_4 != 2 ? "field_24" : nil, # postcode
"field_46", # age1
@ -734,43 +735,43 @@ private
unless location.scheme == scheme
block_log_creation!
errors.add(:field_17, "Scheme code must relate to a location that is owned by owning organisation or managing organisation", category: :setup)
errors.add(location_field, "#{scheme_or_management_group.capitalize} code must relate to a #{location_or_scheme} that is owned by owning organisation or managing organisation", category: :setup)
end
end
def validate_location_exists
if scheme && field_17.present? && location.nil?
errors.add(:field_17, "Location could not be found with the provided scheme code", category: :setup)
if scheme && location.nil?
errors.add(location_field, "#{location_or_scheme.capitalize} could not be found with the provided #{scheme_or_management_group} code", category: :setup)
end
end
def validate_location_data_given
if supported_housing? && field_17.blank?
errors.add(:field_17, I18n.t("validations.not_answered", question: "scheme code"), category: "setup")
if supported_housing? && location_id.blank?
errors.add(location_field, I18n.t("validations.not_answered", question: "#{location_or_scheme} code"), category: "setup")
end
end
def validate_scheme_related
return unless field_16.present? && scheme.present?
return unless scheme_id.present? && scheme.present?
owned_by_owning_org = owning_organisation && scheme.owning_organisation == owning_organisation
owned_by_managing_org = managing_organisation && scheme.owning_organisation == managing_organisation
unless owned_by_owning_org || owned_by_managing_org
block_log_creation!
errors.add(:field_16, "This management group code does not belong to your organisation, or any of your stock owners / managing agents", category: :setup)
errors.add(scheme_field, "This #{scheme_or_management_group} code does not belong to your organisation, or any of your stock owners / managing agents", category: :setup)
end
end
def validate_scheme_exists
if field_16.present? && scheme.nil?
errors.add(:field_16, "The management group code is not correct", category: :setup)
if scheme_id.present? && scheme.nil?
errors.add(scheme_field, "The #{scheme_or_management_group} code is not correct", category: :setup)
end
end
def validate_scheme_data_given
if supported_housing? && field_16.blank?
errors.add(:field_16, I18n.t("validations.not_answered", question: "management group code"), category: "setup")
if supported_housing? && scheme_id.blank?
errors.add(scheme_field, I18n.t("validations.not_answered", question: "#{scheme_or_management_group} code"), category: "setup")
end
end
@ -851,7 +852,7 @@ private
errors.add(:field_8, error_message) # startdate
errors.add(:field_9, error_message) # startdate
errors.add(:field_13, error_message) # tenancycode
errors.add(:field_17, error_message) if field_4 != 1 # location
errors.add(location_field, error_message) if field_4 != 1 # location
errors.add(:field_23, error_message) if field_4 != 2 # postcode_full
errors.add(:field_24, error_message) if field_4 != 2 # postcode_full
errors.add(:field_25, error_message) if field_4 != 2 # la
@ -876,8 +877,8 @@ private
owning_organisation_id: [:field_1],
managing_organisation_id: [:field_2],
renewal: [:field_6],
scheme: %i[field_16],
location: %i[field_17],
scheme: [scheme_field],
location: [location_field],
created_by: [:field_3],
needstype: [:field_4],
rent_type: %i[field_5 field_10 field_11],
@ -1258,13 +1259,43 @@ private
end
def scheme
@scheme ||= Scheme.find_by_id_on_multiple_fields(field_16)
return if field_16.nil?
@scheme ||= Scheme.find_by_id_on_multiple_fields(scheme_id)
end
def location
return if scheme.nil?
@location ||= scheme.locations.find_by_id_on_multiple_fields(field_17)
@location ||= scheme.locations.find_by_id_on_multiple_fields(location_id)
end
def log_uses_new_scheme_id?
field_16&.start_with?("S")
end
def scheme_field
log_uses_new_scheme_id? ? :field_16 : :field_15
end
def scheme_id
log_uses_new_scheme_id? ? field_16 : field_15
end
def location_field
log_uses_new_scheme_id? ? :field_17 : :field_16
end
def location_id
log_uses_new_scheme_id? ? field_17 : field_16
end
def scheme_or_management_group
log_uses_new_scheme_id? ? "scheme" : "management group"
end
def location_or_scheme
log_uses_new_scheme_id? ? "location" : "scheme"
end
def renttype

181
spec/services/bulk_upload/lettings/year2023/row_parser_spec.rb

@ -582,7 +582,7 @@ RSpec.describe BulkUpload::Lettings::Year2023::RowParser do
describe "#field_5" do
context "when null" do
let(:attributes) { { bulk_upload:, field_5: nil, field_15: "1" } }
let(:attributes) { { bulk_upload:, field_5: nil } }
it "returns an error" do
expect(parser.errors[:field_5]).to be_present
@ -652,12 +652,22 @@ RSpec.describe BulkUpload::Lettings::Year2023::RowParser do
end
end
describe "#field_15" do
context "when using New CORE ids" do
end
context "when using New CORE ids" do
end
end
describe "#field_16" do
context "when nullable not permitted" do
let(:attributes) { { bulk_upload:, field_5: "2", field_16: nil } }
it "cannot be nulled" do
expect(parser.errors[:field_16]).to be_present
expect(parser.errors[:field_16]).to include("You must answer scheme code")
end
end
@ -669,93 +679,138 @@ RSpec.describe BulkUpload::Lettings::Year2023::RowParser do
end
end
context "when matching scheme cannot be found" do
let(:attributes) { { bulk_upload:, field_5: "1", field_16: "123" } }
context "when using New CORE ids" do
context "when matching scheme cannot be found" do
let(:attributes) { { bulk_upload:, field_5: "1", field_16: "S123" } }
it "returns a setup error" do
expect(parser.errors.where(:field_16, category: :setup)).to be_present
it "returns a setup error" do
expect(parser.errors.where(:field_16, category: :setup).map(&:message)).to include("The scheme code is not correct")
end
end
end
context "when scheme belongs to someone else" do
let(:other_scheme) { create(:scheme, :with_old_visible_id) }
let(:attributes) { { bulk_upload:, field_5: "1", field_16: other_scheme.old_visible_id, field_1: owning_org.old_visible_id } }
context "when scheme belongs to someone else" do
let(:other_scheme) { create(:scheme, :with_old_visible_id) }
let(:attributes) { { bulk_upload:, field_5: "1", field_16: "S#{other_scheme.id}", field_1: owning_org.old_visible_id } }
it "returns a setup error" do
expect(parser.errors.where(:field_16, category: :setup).map(&:message)).to include("This management group code does not belong to your organisation, or any of your stock owners / managing agents")
it "returns a setup error" do
expect(parser.errors.where(:field_16, category: :setup).map(&:message)).to include("This scheme code does not belong to your organisation, or any of your stock owners / managing agents")
end
end
end
context "when scheme belongs to owning org" do
let(:scheme) { create(:scheme, :with_old_visible_id, owning_organisation: owning_org) }
let(:attributes) { { bulk_upload:, field_5: "1", field_16: scheme.old_visible_id, field_1: owning_org.old_visible_id } }
context "when scheme belongs to owning org" do
let(:scheme) { create(:scheme, :with_old_visible_id, owning_organisation: owning_org) }
let(:attributes) { { bulk_upload:, field_5: "1", field_16: "S#{scheme.id}", field_1: owning_org.old_visible_id } }
it "does not return an error" do
expect(parser.errors[:field_16]).to be_blank
it "does not return an error" do
expect(parser.errors[:field_16]).to be_blank
end
end
end
context "when scheme belongs to managing org" do
let(:scheme) { create(:scheme, :with_old_visible_id, owning_organisation: managing_org) }
let(:attributes) { { bulk_upload:, field_5: "1", field_16: scheme.old_visible_id, field_2: managing_org.old_visible_id } }
context "when scheme belongs to managing org" do
let(:scheme) { create(:scheme, :with_old_visible_id, owning_organisation: managing_org) }
let(:attributes) { { bulk_upload:, field_5: "1", field_16: "S#{scheme.id}", field_2: managing_org.old_visible_id } }
it "does not return an error" do
expect(parser.errors[:field_16]).to be_blank
it "does not return an error" do
expect(parser.errors[:field_16]).to be_blank
end
end
end
end
describe "#field_17" do
context "when location does not exist" do
let(:scheme) { create(:scheme, :with_old_visible_id, owning_organisation: owning_org) }
let(:attributes) do
{
bulk_upload:,
field_5: "1",
field_16: scheme.old_visible_id,
field_17: "dontexist",
field_1: owning_org.old_visible_id,
}
context "when using Old CORE ids" do
context "when matching scheme cannot be found" do
let(:attributes) { { bulk_upload:, field_5: "1", field_16: "123" } }
it "returns a setup error" do
expect(parser.errors.where(:field_16, category: :setup).map(&:message)).to include("The scheme code is not correct")
end
end
it "returns a setup error" do
expect(parser.errors.where(:field_17, category: :setup)).to be_present
context "when scheme belongs to someone else" do
let(:other_scheme) { create(:scheme, :with_old_visible_id) }
let(:attributes) { { bulk_upload:, field_5: "1", field_16: other_scheme.old_visible_id, field_1: owning_org.old_visible_id } }
it "returns a setup error" do
expect(parser.errors.where(:field_16, category: :setup).map(&:message)).to include("This scheme code does not belong to your organisation, or any of your stock owners / managing agents")
end
end
end
context "when location exists" do
let(:scheme) { create(:scheme, :with_old_visible_id, owning_organisation: owning_org) }
let(:attributes) do
{
bulk_upload:,
field_5: "1",
field_16: scheme.old_visible_id,
field_17: location.old_visible_id,
field_1: owning_org.old_visible_id,
}
context "when scheme belongs to owning org" do
let(:scheme) { create(:scheme, :with_old_visible_id, owning_organisation: owning_org) }
let(:attributes) { { bulk_upload:, field_5: "1", field_16: scheme.old_visible_id, field_1: owning_org.old_visible_id } }
it "does not return an error" do
expect(parser.errors[:field_16]).to be_blank
end
end
it "does not return an error" do
expect(parser.errors[:field_17]).to be_blank
context "when scheme belongs to managing org" do
let(:scheme) { create(:scheme, :with_old_visible_id, owning_organisation: managing_org) }
let(:attributes) { { bulk_upload:, field_5: "1", field_16: scheme.old_visible_id, field_2: managing_org.old_visible_id } }
it "does not return an error" do
expect(parser.errors[:field_16]).to be_blank
end
end
end
end
context "when location exists but not related" do
let(:location) { create(:scheme, :with_old_visible_id) }
let(:attributes) do
{
bulk_upload:,
field_5: "1",
field_16: scheme.old_visible_id,
field_17: location.old_visible_id,
field_1: owning_org.old_visible_id,
}
describe "#field_17" do
context "when using New CORE ids" do
context "when location does not exist" do
let(:scheme) { create(:scheme, :with_old_visible_id, owning_organisation: owning_org) }
let(:attributes) do
{
bulk_upload:,
field_5: "1",
field_16: scheme.old_visible_id,
field_17: "dontexist",
field_1: owning_org.old_visible_id,
}
end
it "returns a setup error" do
expect(parser.errors.where(:field_17, category: :setup)).to be_present
end
end
context "when location exists" do
let(:scheme) { create(:scheme, :with_old_visible_id, owning_organisation: owning_org) }
let(:attributes) do
{
bulk_upload:,
field_5: "1",
field_16: scheme.old_visible_id,
field_17: location.old_visible_id,
field_1: owning_org.old_visible_id,
}
end
it "does not return an error" do
expect(parser.errors[:field_17]).to be_blank
end
end
it "returns a setup error" do
expect(parser.errors.where(:field_17, category: :setup)).to be_present
context "when location exists but not related" do
let(:location) { create(:scheme, :with_old_visible_id) }
let(:attributes) do
{
bulk_upload:,
field_5: "1",
field_16: scheme.old_visible_id,
field_17: location.old_visible_id,
field_1: owning_org.old_visible_id,
}
end
it "returns a setup error" do
expect(parser.errors.where(:field_17, category: :setup)).to be_present
end
end
end
context "when using Old CORE ids" do
end
end
describe "#field_102" do # leaving reason

Loading…
Cancel
Save