Skip to content

Commit

Permalink
feat: generating random tax code
Browse files Browse the repository at this point in the history
  • Loading branch information
domenicoprex committed Oct 20, 2024
1 parent ffac35c commit 3285c60
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 3 deletions.
7 changes: 7 additions & 0 deletions lib/itax_code.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,12 @@ def valid?(tax_code)
rescue Parser::Error
false
end

# Encodes random tax code.
#
# @return [String]
def random
Encoder.new(Utils.random_person_data).encode
end
end
end
34 changes: 34 additions & 0 deletions lib/itax_code/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,16 @@ def countries
CSV.foreach("#{__dir__}/data/countries.csv", headers: true)
end

def random_person_data
{
surname: random_string,
name: random_string,
gender: %w[F M].sample,
birthdate: random_birthday,
birthplace: %w[H501 F205 D612 E256].sample # Milan, Rome, Naples, Turin
}
end

private

def omocodia_letters
Expand Down Expand Up @@ -150,6 +160,30 @@ def omocodia
"5": "R", "6": "S", "7": "T", "8": "U", "9": "V"
}
end
def random_string
(0...6).map { (65 + rand(26)).chr }.join
end

def random_birthday
days_in_months = {1 => 31,
2 => 28,
3 => 31,
4 => 30,
5 => 31,
6 => 30,
7 => 31,
8 =>31,
9 =>30,
10 =>31,
11 =>30,
12 =>31,
}
year = rand(1930..2006)
month = rand(1..12)
day = rand(1..days_in_months[month])

"#{year}-#{month}-#{day}"
end
end
end
end
15 changes: 12 additions & 3 deletions test/itax_code/utils_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,19 @@ class UtilsTest < Minitest::Test
end
end

test "random person data structure contains expected keys" do
expected_keys = [:surname, :name, :gender, :birthdate, :birthplace]

expected_keys.each do |key|
assert klass.random_person_data.key?(key), "Expected key #{key} to be present in the hash"
end
end


private

def klass
Utils
end
def klass
Utils
end
end
end
4 changes: 4 additions & 0 deletions test/itax_code_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ class ItaxCodeTest < Minitest::Test
refute klass.valid?("WRONG")
end

test "#random" do
assert_equal 16, klass.random.length
end

private

def klass
Expand Down

0 comments on commit 3285c60

Please sign in to comment.