Skip to content

Commit

Permalink
Refactor total method to use sum instead of reduce
Browse files Browse the repository at this point in the history
Plus some extra specs to cover missing scenarios
  • Loading branch information
rioug committed Jan 29, 2024
1 parent 04db8db commit e129ef3
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 14 deletions.
12 changes: 2 additions & 10 deletions app/models/enterprise_fee_adjustments.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,10 @@ def initialize(adjustments)
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
44 changes: 40 additions & 4 deletions spec/models/enterprise_fee_adjustments_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@
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
Expand All @@ -20,10 +24,26 @@
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, fee_adjustment2])

enterprise_fee_adjustments = EnterpriseFeeAdjustments.new([fee_adjustment])
expect(enterprise_fee_adjustments.total_additional_tax).to eq(15.0)
end

expect(enterprise_fee_adjustments.total_additional_tax).to eq(10.0)
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
Expand Down Expand Up @@ -54,10 +74,26 @@
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
Expand Down

0 comments on commit e129ef3

Please sign in to comment.