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

ft: added code for generating bank account numbers #150

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions bankaccount.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package faker

import (
"strings"
)

// BAccount is a faker struct for BAccount
type BAccount struct {
Faker *Faker
}

var accountLengths = map[string]int{
"US": 10, // United States
"UK": 14, // United Kingdom
"CA": 12, // Canada
"AU": 9, // Australia
"JP": 7, // Japan
"IN": 15, // India
"BR": 12, // Brazil
"CN": 19, // China
"MX": 18, // Mexico
"RU": 20, // Russia
"ZA": 14, // South Africa
"KR": 14, // South Korea
"ID": 16, // Indonesia
"NG": 10, // Nigeria
"SG": 10, // Singapore
"MY": 14, // Malaysia
"TH": 10, // Thailand
"": 15,
// Add more countries and their corresponding lengths here.
}

func (f BAccount) GenerateBankAccount(countryCode string) int {
cc := strings.ToUpper(countryCode)
if _, exists := accountLengths[cc]; exists {
AccountLength := accountLengths[cc]
return f.Faker.RandomNumber(AccountLength)
}
AccountLength := accountLengths[""]
return f.Faker.RandomNumber(AccountLength)

}
28 changes: 28 additions & 0 deletions bankaccount_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package faker

import (
"testing"
)

func TestBankAccountNumber(t *testing.T) {
v := New().BankAccount().GenerateBankAccount("")
NotExpect(t, "", v)
ExpectIntWithSize(t, 15, v)

v1 := New().BankAccount().GenerateBankAccount("uK")
NotExpect(t, "", v1)
ExpectIntWithSize(t, 14, v1)

v2 := New().BankAccount().GenerateBankAccount("ru")
NotExpect(t, "", v2)
ExpectIntWithSize(t, 20, v2)


v4 := New().BankAccount().GenerateBankAccount("SG")
NotExpect(t, "", v4)
ExpectIntWithSize(t, 10, v4)

v5 := New().BankAccount().GenerateBankAccount("de")
NotExpect(t, "", v1)
ExpectIntWithSize(t, 15, v5)
}
5 changes: 5 additions & 0 deletions faker.go
Original file line number Diff line number Diff line change
Expand Up @@ -600,3 +600,8 @@ func (f Faker) Blood() Blood {
func (f Faker) Json() Json {
return Json{&f}
}

// BankAccount returns a fake bank Account number for faker
func (f Faker) BankAccount() BAccount {
return BAccount{&f}
}
26 changes: 26 additions & 0 deletions faker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,32 @@ func Expect(t *testing.T, expected, got interface{}, values ...interface{}) {
}
}

// ExpectIntWithSize checks if a value is an integer and has a specific size.
func ExpectIntWithSize(t *testing.T, expectedSize int, v interface{}, values ...interface{}) {
t.Helper()

// Check if v is an integer.
intValue, ok := v.(int)
if !ok {
t.Errorf("Expected an integer, but got %T", v)
t.FailNow()
}

// Convert the integer to a string and check its length.
stringValue := fmt.Sprint(intValue)
if len(stringValue) != expectedSize {
t.Errorf("Expected an integer with %d digits, but got %s", expectedSize, stringValue)
t.FailNow()
}

// Optionally, you can print additional values.
if len(values) > 0 {
for _, val := range values {
t.Logf("%+v", val)
}
}
}

func NotExpect(t *testing.T, notExpected, got interface{}, values ...interface{}) {
t.Helper()
if notExpected == got {
Expand Down