-
Notifications
You must be signed in to change notification settings - Fork 559
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds FFaker::Bank.routing_number and account_number
- Loading branch information
Showing
6 changed files
with
77 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# frozen_string_literal: true | ||
|
||
module FFaker | ||
module BankUS | ||
extend ModuleUtils | ||
extend self | ||
|
||
def account_number(min_digits: 9 , max_digits: 17) | ||
FFaker.numerify('#' * rand(min_digits..max_digits)) | ||
end | ||
|
||
def routing_number | ||
partial_routing_number = FFaker.numerify('########') | ||
ninth_digit = generate_checksum_digit(partial_routing_number) | ||
|
||
"#{partial_routing_number}#{ninth_digit}" | ||
end | ||
|
||
private | ||
|
||
def generate_checksum_digit(num_string) | ||
num_array = num_string.chars.map(&:to_i) | ||
( | ||
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 | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# frozen_string_literal: true | ||
|
||
require_relative 'helper' | ||
|
||
class TestBankUS < Test::Unit::TestCase | ||
include DeterministicHelper | ||
|
||
assert_methods_are_deterministic( | ||
FFaker::Bank, | ||
:account_number, :routing_number | ||
) | ||
|
||
def setup | ||
@tester = FFaker::BankUS | ||
end | ||
|
||
def test_account_number | ||
assert_instance_of String, @tester.account_number | ||
assert_match(/\A\d{8}\z/, @tester.account_number(min_digits: 8, max_digits: 8)) | ||
assert_match(/\A\d{12}\z/, @tester.account_number(min_digits: 12, max_digits: 12)) | ||
end | ||
|
||
def test_routing_number | ||
routing_number = @tester.routing_number | ||
assert_match(/\A\d{9}\z/, 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_equal(0, checksum) | ||
end | ||
end |