Skip to content

Commit

Permalink
chore: add test coverage for missing branch conditions
Browse files Browse the repository at this point in the history
  • Loading branch information
matteoredz committed Oct 23, 2023
1 parent a5580d8 commit 3cbcc71
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 10 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ gem "rubocop-minitest"
gem "rubocop-performance"
gem "rubocop-rake"
gem "simplecov"
gem "timecop"
5 changes: 3 additions & 2 deletions lib/itax_code/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,9 @@ def gender
# value could be wrong. E.g. a person born on 1920 would have birthdate_year = 20 meaning
# that both 1920 and 2020 could be valid born years.
def year
val = (Date.today.year.to_s[0..1] + utils.omocodia_decode(raw[:birthdate_year])).to_i
val > Date.today.year ? val - 100 : val
current_year = Date.today.year
val = (current_year.to_s[0..1] + utils.omocodia_decode(raw[:birthdate_year])).to_i
val > current_year ? val - 100 : val
end

def month
Expand Down
28 changes: 20 additions & 8 deletions test/itax_code/parser_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,34 @@ class ParserTest < ActiveSupport::TestCase
assert_equal decoded_f, klass.new("RSSMRA80A41F205B").decode
end

test "#decode returns invalid code on invalid birthplace" do
test "#decode when foreign" do
assert_equal decoded_foreign, klass.new("BRRDRN70M41Z602D").decode
end

test "raises NoTaxCodeError when no tax code is given" do
assert_raises klass::NoTaxCodeError do
klass.new("")
# NOTE: This tests the branch where the computed date is before the current year.
test "decodes the birthdate without manipulating the original value" do
Timecop.freeze(Date.parse("1990-01-01")) do
assert_equal "1985-04-03", klass.new("CCCFBA85D03L219P").decode[:birthdate]
end
end

test "raises InvalidTaxCodeError when the tax code is invalid" do
# FIXME: This behaviour needs to be fixed, maybe by raising an InvalidTaxCodeError.
test "returns nil birthplace when the lookup on both cities and countries fails" do
assert_nil klass.new("BRRDRN70M41ZXXXE").decode[:birthplace]
end

test "raises NoTaxCodeError with an empty tax code" do
assert_raises(klass::NoTaxCodeError) { klass.new("") }
end

test "raises NoTaxCodeError with a nil tax code" do
assert_raises(klass::NoTaxCodeError) { klass.new(nil) }
end

test "raises InvalidTaxCodeError when the tax code has wrong length" do
wrong_length_tax_code = "CCCFBA85D03L219PXXX"

assert_raises klass::InvalidTaxCodeError do
klass.new(wrong_length_tax_code)
end
assert_raises(klass::InvalidTaxCodeError) { klass.new(wrong_length_tax_code) }
end

test "raises InvalidControlInternalNumberError when the cin differs from the computed one" do
Expand Down
1 change: 1 addition & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@
require "byebug"
require "minitest/autorun"
require "mocha/minitest"
require "timecop"

0 comments on commit 3cbcc71

Please sign in to comment.