Browse Source

feat: add validation to shared ownership type as well

pull/1404/head
Sam Seed 3 years ago
parent
commit
07e7d4f685
  1. 1
      app/models/validations/sales/financial_validations.rb
  2. 2
      config/locales/en.yml
  3. 20
      spec/models/validations/sales/financial_validations_spec.rb

1
app/models/validations/sales/financial_validations.rb

@ -63,6 +63,7 @@ module Validations::Sales::FinancialValidations
if threshold && record.stairbought < threshold
record.errors.add :stairbought, I18n.t("validations.financial.staircasing.percentage_bought_must_be_at_least_threshold", threshold:)
record.errors.add :type, I18n.t("validations.setup.type.percentage_bought_must_be_at_least_threshold", threshold:)
end
end

2
config/locales/en.yml

@ -159,6 +159,8 @@ en:
Enter a date within the %{current_start_year_short}/%{current_end_year_short} financial year, which is between %{current_start_year_long} and %{current_end_year_long}
previous_and_current_financial_year:
"Enter a date within the %{previous_start_year_short}/%{previous_end_year_short} or %{previous_end_year_short}/%{current_end_year_short} financial years, which is between %{previous_start_year_long} and %{current_end_year_long}"
type:
percentage_bought_must_be_at_least_threshold: "The minimum increase in equity while staircasing is %{threshold}% for this shared ownership type"
startdate:
later_than_14_days_after: "The tenancy start date must not be later than 14 days from today’s date"

20
spec/models/validations/sales/financial_validations_spec.rb

@ -163,27 +163,31 @@ RSpec.describe Validations::Sales::FinancialValidations do
describe "#validate_percentage_bought_at_least_threshold" do
let(:record) { FactoryBot.create(:sales_log) }
it "adds an error to stairbought if the percentage bought is less than the threshold (which depends on the shared ownership type)" do
it "adds an error to stairbought and type if the percentage bought is less than the threshold (which is 1% by default, but higher for some shared ownership types)" do
record.stairbought = 9
record.type = 2
financial_validator.validate_percentage_bought_at_least_threshold(record)
expect(record.errors["stairbought"]).to eq(["The minimum increase in equity while staircasing is 10%"])
expect(record.errors["type"]).to eq(["The minimum increase in equity while staircasing is 10% for this shared ownership type"])
record.errors.clear
record.type = 16
financial_validator.validate_percentage_bought_at_least_threshold(record)
expect(record.errors["stairbought"]).to eq(["The minimum increase in equity while staircasing is 10%"])
expect(record.errors["type"]).to eq(["The minimum increase in equity while staircasing is 10% for this shared ownership type"])
record.errors.clear
record.type = 18
financial_validator.validate_percentage_bought_at_least_threshold(record)
expect(record.errors["stairbought"]).to eq(["The minimum increase in equity while staircasing is 10%"])
expect(record.errors["type"]).to eq(["The minimum increase in equity while staircasing is 10% for this shared ownership type"])
record.errors.clear
record.type = 24
financial_validator.validate_percentage_bought_at_least_threshold(record)
expect(record.errors["stairbought"]).to eq(["The minimum increase in equity while staircasing is 10%"])
expect(record.errors["type"]).to eq(["The minimum increase in equity while staircasing is 10% for this shared ownership type"])
record.errors.clear
record.stairbought = 0
@ -191,45 +195,53 @@ RSpec.describe Validations::Sales::FinancialValidations do
record.type = 28
financial_validator.validate_percentage_bought_at_least_threshold(record)
expect(record.errors["stairbought"]).to eq(["The minimum increase in equity while staircasing is 1%"])
expect(record.errors["type"]).to eq(["The minimum increase in equity while staircasing is 1% for this shared ownership type"])
record.errors.clear
record.type = 30
financial_validator.validate_percentage_bought_at_least_threshold(record)
expect(record.errors["stairbought"]).to eq(["The minimum increase in equity while staircasing is 1%"])
expect(record.errors["type"]).to eq(["The minimum increase in equity while staircasing is 1% for this shared ownership type"])
record.errors.clear
record.type = 31
financial_validator.validate_percentage_bought_at_least_threshold(record)
expect(record.errors["stairbought"]).to eq(["The minimum increase in equity while staircasing is 1%"])
expect(record.errors["type"]).to eq(["The minimum increase in equity while staircasing is 1% for this shared ownership type"])
record.errors.clear
record.type = 32
financial_validator.validate_percentage_bought_at_least_threshold(record)
expect(record.errors["stairbought"]).to eq(["The minimum increase in equity while staircasing is 1%"])
expect(record.errors["type"]).to eq(["The minimum increase in equity while staircasing is 1% for this shared ownership type"])
record.errors.clear
end
it "doesn't add an error to stairbought if the percentage bought is greater than or equal to the threshold (which depends on the shared ownership type)" do
it "doesn't add an error to stairbought and type if the percentage bought is less than the threshold (which is 1% by default, but higher for some shared ownership types)" do
record.stairbought = 10
record.type = 2
financial_validator.validate_percentage_bought_at_least_threshold(record)
expect(record.errors["stairbought"]).to be_empty
expect(record.errors["type"]).to be_empty
record.errors.clear
record.type = 16
financial_validator.validate_percentage_bought_at_least_threshold(record)
expect(record.errors["stairbought"]).to be_empty
expect(record.errors["type"]).to be_empty
record.errors.clear
record.type = 18
financial_validator.validate_percentage_bought_at_least_threshold(record)
expect(record.errors["stairbought"]).to be_empty
expect(record.errors["type"]).to be_empty
record.errors.clear
record.type = 24
financial_validator.validate_percentage_bought_at_least_threshold(record)
expect(record.errors["stairbought"]).to be_empty
expect(record.errors["type"]).to be_empty
record.errors.clear
record.stairbought = 1
@ -237,21 +249,25 @@ RSpec.describe Validations::Sales::FinancialValidations do
record.type = 28
financial_validator.validate_percentage_bought_at_least_threshold(record)
expect(record.errors["stairbought"]).to be_empty
expect(record.errors["type"]).to be_empty
record.errors.clear
record.type = 30
financial_validator.validate_percentage_bought_at_least_threshold(record)
expect(record.errors["stairbought"]).to be_empty
expect(record.errors["type"]).to be_empty
record.errors.clear
record.type = 31
financial_validator.validate_percentage_bought_at_least_threshold(record)
expect(record.errors["stairbought"]).to be_empty
expect(record.errors["type"]).to be_empty
record.errors.clear
record.type = 32
financial_validator.validate_percentage_bought_at_least_threshold(record)
expect(record.errors["stairbought"]).to be_empty
expect(record.errors["type"]).to be_empty
record.errors.clear
end
end

Loading…
Cancel
Save