Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix generating Faker::Bank#routing_number #1563

Merged
merged 1 commit into from Nov 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 13 additions & 7 deletions lib/faker/default/bank.rb
Expand Up @@ -50,8 +50,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 @@ -77,12 +80,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