Skip to content

Commit

Permalink
Fix generating routing number (#1563)
Browse files Browse the repository at this point in the history
  • Loading branch information
psienko authored and vbrazo committed Nov 24, 2019
1 parent 8b93025 commit eaaeaf1
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
20 changes: 13 additions & 7 deletions lib/faker/default/bank.rb
Expand Up @@ -58,8 +58,11 @@ def swift_bic

def checksum(num_string)
num_array = num_string.split('').map(&:to_i)
digit = (7 * (num_array[0] + num_array[3] + num_array[6]) + 3 * (num_array[1] + num_array[4] + num_array[7]) + 9 * (num_array[2] + num_array[5])) % 10
digit == num_array[8]
(
7 * (num_array[0] + num_array[3] + num_array[6]) +
3 * (num_array[1] + num_array[4] + num_array[7]) +
9 * (num_array[2] + num_array[5])
) % 10
end

def compile_routing_number
Expand All @@ -85,12 +88,15 @@ def iban_checksum(country_code, account)
end

def valid_routing_number
for _ in 0..50
micr = compile_routing_number
routing_number = compile_routing_number
checksum = checksum(routing_number)
return routing_number if valid_checksum?(routing_number, checksum)

break if checksum(micr)
end
micr
routing_number[0..7] + checksum.to_s
end

def valid_checksum?(routing_number, checksum)
routing_number[8].to_i == checksum
end

def compile_fraction(routing_num)
Expand Down
10 changes: 9 additions & 1 deletion test/faker/default/test_faker_bank.rb
Expand Up @@ -10,7 +10,15 @@ def setup
end

def test_routing_number
assert Faker::Bank.routing_number.match(/\d{9}/)
routing_number = Faker::Bank.routing_number
checksum = (
7 * (routing_number[0].to_i + routing_number[3].to_i + routing_number[6].to_i) +
3 * (routing_number[1].to_i + routing_number[4].to_i + routing_number[7].to_i) +
9 * (routing_number[2].to_i + routing_number[5].to_i + routing_number[8].to_i)
) % 10

assert routing_number.match(/\d{9}/)
assert_equal(checksum, 0)
end

def test_routing_number_with_format
Expand Down

0 comments on commit eaaeaf1

Please sign in to comment.