Skip to content

Commit

Permalink
Adds FFaker::Bank.routing_number and account_number
Browse files Browse the repository at this point in the history
  • Loading branch information
professor committed Sep 19, 2023
1 parent 08f3d13 commit cebd12c
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
## development

- Add your change HERE
- Add FFaker::BankUS.accounting_number [@professor]
- Add FFaker::BankUS.routing_number [@professor]
- Resolve a lot of RuboCop offenses [@AlexWayfer]
- Change `Image.url`, `Image.file`, `Book.cover` and `Avatar.image` arguments to keywords [@AlexWayfer]
- Adds FFaker::Date.birthday [@professor]
Expand Down
33 changes: 33 additions & 0 deletions lib/ffaker/bank_us.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# 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_ninth_digit(partial_routing_number)

"#{partial_routing_number}#{ninth_digit}"
end

private

def generate_ninth_digit(num_string)
# This leverages the `Modules 10, Straight Summation` used for routing_numbers
# See http://www.sxlist.com/techref/ecommerce/bank/routingnumber/index.htm
# for more details
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
35 changes: 35 additions & 0 deletions test/test_bank_us.rb
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::BankUS,
: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) +

Check failure on line 28 in test/test_bank_us.rb

View workflow job for this annotation

GitHub Actions / RuboCop lint

Wrap expressions with varying precedence with parentheses to avoid ambiguity.
3 * (routing_number[1].to_i + routing_number[4].to_i + routing_number[7].to_i) +

Check failure on line 29 in test/test_bank_us.rb

View workflow job for this annotation

GitHub Actions / RuboCop lint

Wrap expressions with varying precedence with parentheses to avoid ambiguity.
9 * (routing_number[2].to_i + routing_number[5].to_i + routing_number[8].to_i)

Check failure on line 30 in test/test_bank_us.rb

View workflow job for this annotation

GitHub Actions / RuboCop lint

Wrap expressions with varying precedence with parentheses to avoid ambiguity.
)

assert_equal(0, checksum % 10, 'The routing number''s checksum is not a multiple of ten')

Check failure on line 33 in test/test_bank_us.rb

View workflow job for this annotation

GitHub Actions / RuboCop lint

Combine 'The routing number' and 's checksum is not a multiple of ten' into a single string literal, rather than using implicit string concatenation. Or, if they were intended to be separate method arguments, separate them with a comma.
end
end

0 comments on commit cebd12c

Please sign in to comment.