Skip to content

Commit

Permalink
Add test for federal tax calculator.
Browse files Browse the repository at this point in the history
  • Loading branch information
neb417 committed Jul 14, 2024
1 parent d8fcdbe commit c4f5ed4
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 14 deletions.
4 changes: 2 additions & 2 deletions app/services/federal_tax_calculator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ def call
attr_accessor :income

def calculate
bracket = FederalTaxBracket.where("bottom_range_cents <= ?", income.cents).order(:bottom_range_cents).last
taxable_at_bracket_rate = Money.new(income - bracket.bottom_range)
bracket = FederalTaxBracket.where("bottom_range_cents <= ?", income.rate_cents).order(:bottom_range_cents).last
taxable_at_bracket_rate = Money.new(income.rate - bracket.bottom_range)
rated = bracket.rate * taxable_at_bracket_rate
rated + bracket.cumulative
end
Expand Down
14 changes: 7 additions & 7 deletions spec/factories/federal_tax_brackets.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,24 @@
factory :federal_tax_bracket do
tier { "Tier 1" }
bottom_range_cents { 0 }
top_range_cents { 100_000 }
top_range_cents { 100_000 } #$1,000.00
rate { 0.1 }
cumulative_cents { 0 }

trait :tier_2 do
tier { "Tier 2" }
bottom_range_cents { 1_000_100 }
top_range_cents { 10_000_000 }
bottom_range_cents { 100_100 } #$1,001.00
top_range_cents { 10_000_000 } #$100,000.00
rate { 0.15 }
cumulative_cents { 200_000 }
cumulative_cents { 10_000 } #$100.00
end

trait :tier_3 do
tier { "Tier 2" }
bottom_range_cents { 10_000_100 }
top_range_cents { 50_000_000 }
bottom_range_cents { 10_000_100 } #$100,001.00
top_range_cents { 50_000_000 } #$500,000.00
rate { 0.25 }
cumulative_cents { 500_000 }
cumulative_cents { 1_485_000 } # $14,850.00
end

trait :with_all_tiers do
Expand Down
10 changes: 5 additions & 5 deletions spec/factories/incomes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@
weekly_income_cents { 300_000 * 40 }
end

# trait :with_all_types do
# after :create do |_record|
# create(:income, :hourly)
# end
# end
trait :with_all_types do
after :create do |_record|
create(:income, :hourly)
end
end

# to_create do |instance|
# instance.id = Income.find_or_create_by(income_type: instance.income_type).id
Expand Down
23 changes: 23 additions & 0 deletions spec/services/federal_tax_calculator_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# frozen_string_literal: true

require("rails_helper")

RSpec.describe FederalTaxCalculator, type: :service do
subject(:service) do
described_class.call(
income: salary_income)
end

let!(:salary_income) { create(:income) }
let!(:tax_brackets) { create(:federal_tax_bracket, :with_all_tiers) }

it { expect(service).to be_a Money }

it "calculates federal tax" do
# salary_income = $50,000
# tax_brackets = 10% on first $1,000, 15% from $1,001 to $100,000, 25% from $100,001 to $500,000

expect(service.format).to eq("$7,449.85")
expect((salary_income.rate - service).format).to eq("$42,550.15")
end
end

0 comments on commit c4f5ed4

Please sign in to comment.