From b3387c5b038dfaa76b4d124da3e2099c8dd91954 Mon Sep 17 00:00:00 2001 From: natdeanlewissoftwire Date: Thu, 9 Mar 2023 16:12:50 +0000 Subject: [PATCH] feat: add tests --- .../sales/financial_validations_spec.rb | 88 +++++++++++++++++++ 1 file changed, 88 insertions(+) diff --git a/spec/models/validations/sales/financial_validations_spec.rb b/spec/models/validations/sales/financial_validations_spec.rb index fae0e4536..6b1ced864 100644 --- a/spec/models/validations/sales/financial_validations_spec.rb +++ b/spec/models/validations/sales/financial_validations_spec.rb @@ -224,4 +224,92 @@ RSpec.describe Validations::Sales::FinancialValidations do end end end + + describe "#validate_equity_in_range_for_year_and_type" do + let(:record) { FactoryBot.create(:sales_log) } + + context "with a log in the 22/23 collection year" do + before do + Timecop.freeze(Time.zone.local(2023, 1, 1)) + record.update!(saledate: Time.zone.local(2023, 1, 1)) + end + + after do + Timecop.unfreeze + end + + it "adds an error for type 2, equity below min with the correct percentage" do + record.type = 2 + record.equity = 1 + financial_validator.validate_equity_in_range_for_year_and_type(record) + expect(record.errors["equity"]).to include(match I18n.t("validations.financial.equity.under_min", min_equity: 25)) + expect(record.errors["type"]).to include(match I18n.t("validations.financial.equity.under_min", min_equity: 25)) + end + + it "adds an error for type 30, equity below min with the correct percentage" do + record.type = 30 + record.equity = 1 + financial_validator.validate_equity_in_range_for_year_and_type(record) + expect(record.errors["equity"]).to include(match I18n.t("validations.financial.equity.under_min", min_equity: 10)) + expect(record.errors["type"]).to include(match I18n.t("validations.financial.equity.under_min", min_equity: 10)) + end + + it "does not add an error for equity in range with the correct percentage" do + record.type = 2 + record.equity = 50 + financial_validator.validate_equity_in_range_for_year_and_type(record) + expect(record.errors).to be_empty + end + + it "adds an error for equity above max with the correct percentage" do + record.type = 2 + record.equity = 90 + financial_validator.validate_equity_in_range_for_year_and_type(record) + expect(record.errors["equity"]).to include(match I18n.t("validations.financial.equity.over_max")) + expect(record.errors["type"]).to include(match I18n.t("validations.financial.equity.over_max")) + end + end + + context "with a log in 23/24 collection year" do + before do + Timecop.freeze(Time.zone.local(2024, 1, 1)) + record.update!(saledate: Time.zone.local(2024, 1, 1)) + end + + after do + Timecop.unfreeze + end + + it "adds an error for type 2, equity below min with the correct percentage" do + record.type = 2 + record.equity = 1 + financial_validator.validate_equity_in_range_for_year_and_type(record) + expect(record.errors["equity"]).to include(match I18n.t("validations.financial.equity.under_min", min_equity: 10)) + expect(record.errors["type"]).to include(match I18n.t("validations.financial.equity.under_min", min_equity: 10)) + end + + it "adds an error for type 30, equity below min with the correct percentage" do + record.type = 30 + record.equity = 1 + financial_validator.validate_equity_in_range_for_year_and_type(record) + expect(record.errors["equity"]).to include(match I18n.t("validations.financial.equity.under_min", min_equity: 25)) + expect(record.errors["type"]).to include(match I18n.t("validations.financial.equity.under_min", min_equity: 25)) + end + + it "does not add an error for equity in range with the correct percentage" do + record.type = 2 + record.equity = 50 + financial_validator.validate_equity_in_range_for_year_and_type(record) + expect(record.errors).to be_empty + end + + it "adds an error for equity above max with the correct percentage" do + record.type = 2 + record.equity = 90 + financial_validator.validate_equity_in_range_for_year_and_type(record) + expect(record.errors["equity"]).to include(match I18n.t("validations.financial.equity.over_max")) + expect(record.errors["type"]).to include(match I18n.t("validations.financial.equity.over_max")) + end + end + end end