Refactor total method to use sum instead of reduce

Plus some extra specs to cover missing scenarios
This commit is contained in:
Gaetan Craig-Riou
2024-01-29 13:50:10 +11:00
parent 04db8dbd37
commit e129ef38c2
2 changed files with 42 additions and 14 deletions

View File

@@ -8,18 +8,10 @@ class EnterpriseFeeAdjustments
end
def total_additional_tax
@adjustments.reduce(0.0) do |sum, enterprise_fee|
sum += enterprise_fee.additional_tax_total if enterprise_fee&.adjustments
sum
end
@adjustments.sum { |enterprise_fee| enterprise_fee.additional_tax_total.to_f }
end
def total_included_tax
@adjustments.reduce(0.0) do |sum, enterprise_fee|
sum += enterprise_fee.included_tax_total if enterprise_fee&.adjustments
sum
end
@adjustments.sum { |enterprise_fee| enterprise_fee.included_tax_total.to_f }
end
end

View File

@@ -5,10 +5,14 @@ require 'spec_helper'
describe EnterpriseFeeAdjustments do
let(:tax_rate) { create(:tax_rate, amount: 0.1) }
let(:line_item) { create(:line_item) }
let(:line_item2) { create(:line_item) }
let(:enterprise_fee) { create(:enterprise_fee, tax_category: tax_rate.tax_category) }
let(:fee_adjustment) {
create( :adjustment, originator: enterprise_fee, adjustable: line_item, state: "closed")
}
let(:fee_adjustment2) {
create( :adjustment, originator: enterprise_fee, adjustable: line_item, state: "closed")
}
describe "#total_additional_tax" do
it "calculates total tax" do
@@ -20,10 +24,26 @@ describe EnterpriseFeeAdjustments do
state: "closed",
included: false
)
create(
:adjustment,
originator: tax_rate,
adjustable: fee_adjustment2,
amount: 5.0,
state: "closed",
included: false
)
enterprise_fee_adjustments = EnterpriseFeeAdjustments.new([fee_adjustment])
enterprise_fee_adjustments = EnterpriseFeeAdjustments.new([fee_adjustment, fee_adjustment2])
expect(enterprise_fee_adjustments.total_additional_tax).to eq(10.0)
expect(enterprise_fee_adjustments.total_additional_tax).to eq(15.0)
end
context "with no tax adjustment" do
it "returns 0.0" do
enterprise_fee_adjustments = EnterpriseFeeAdjustments.new([fee_adjustment])
expect(enterprise_fee_adjustments.total_additional_tax).to eq(0.0)
end
end
context "with tax included in price" do
@@ -54,10 +74,26 @@ describe EnterpriseFeeAdjustments do
state: "closed",
included: true
)
create(
:adjustment,
originator: tax_rate,
adjustable: fee_adjustment2,
amount: 5.0,
state: "closed",
included: true
)
enterprise_fee_adjustments = EnterpriseFeeAdjustments.new([fee_adjustment])
enterprise_fee_adjustments = EnterpriseFeeAdjustments.new([fee_adjustment, fee_adjustment2])
expect(enterprise_fee_adjustments.total_included_tax).to eq(10.0)
expect(enterprise_fee_adjustments.total_included_tax).to eq(15.0)
end
context "with no tax adjustment" do
it "returns 0.0" do
enterprise_fee_adjustments = EnterpriseFeeAdjustments.new([fee_adjustment])
expect(enterprise_fee_adjustments.total_additional_tax).to eq(0.0)
end
end
context "with tax excluded from price" do