Skip to content

Commit

Permalink
added exhaustive test cases for each continent currencies
Browse files Browse the repository at this point in the history
  • Loading branch information
isudhanshukrjha committed Dec 9, 2024
1 parent 36bfc9a commit c7a15eb
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 6 deletions.
4 changes: 1 addition & 3 deletions packages/i18nify-go/modules/converter/convert_to_major.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,8 @@ func ConvertToMajorUnit(code string, amount float64) (float64, error) {
// Calculate the minor unit multiplier to float64
minorUnitMultiplier := math.Pow(10, minorUnit)
if minorUnitMultiplier <= 0 {
minorUnitMultiplier = 100 // Default fallback value.
return 0, fmt.Errorf("invalid minor unit multiplier for currency code '%s'", code)
minorUnitMultiplier = 100 // Default fallback value = 100
}
// 100 = Default fallback value.

// Convert the amount from minor to major unit.
majorUnitAmount := amount / minorUnitMultiplier
Expand Down
1 change: 0 additions & 1 deletion packages/i18nify-go/modules/converter/convert_to_minor.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ func ConvertToMinorUnit(code string, amount float64) (float64, error) {
minorUnitMultiplier := math.Pow(10, minorUnit)
if minorUnitMultiplier <= 0 {
minorUnitMultiplier = 100 // Default fallback value.
return 0, fmt.Errorf("invalid minor unit multiplier for currency code '%s'", code)
}
// 100 = Default fallback value.

Expand Down
68 changes: 66 additions & 2 deletions packages/i18nify-go/modules/currency/currency_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,77 @@ func TestGetCurrencyInformation(t *testing.T) {
if err != nil {
return
}

// Validate specific details for USD as a sample
result, _ := GetCurrencyInformation("USD")

// Use assert.Equal for assertions with inline expected values
assert.Equal(t, "2", result.MinorUnit, "MinorUnit field mismatch")
assert.Equal(t, "US Dollar", result.Name, "Name field mismatch")
assert.Equal(t, "840", result.NumericCode, "NumericCode field mismatch")
assert.Equal(t, []string{"1", "5", "10", "25", "50", "100"}, result.PhysicalCurrencyDenominations, "PhysicalCurrencyDenominations field mismatch")
assert.Equal(t, "$", result.Symbol, "Symbol field mismatch")

// Test multiple countries with table-driven tests
countries := []struct {
code string
name string
symbol string
numeric string
}{
// Asia
{"CNY", "Yuan Renminbi", "CN¥", "156"},
{"JPY", "Yen", "¥", "392"},
{"INR", "Indian Rupee", "₹", "356"},
{"RUB", "Russian Ruble", "₽", "643"},
{"AED", "UAE Dirham", "د.إ", "784"},
// North America
{"USD", "US Dollar", "$", "840"},
// South America
{"BRL", "Brazilian Real", "R$", "986"},
// Australia
{"AUD", "Australian Dollar", "$", "36"},
// Europe
{"EUR", "Euro", "€", "978"},
// Africa
{"ZAR", "South African Rand", "R", "710"},
}

for _, country := range countries {
t.Run(country.code, func(t *testing.T) {
result, err := GetCurrencyInformation(country.code)
//fmt.Println("Here:", country.name, ":", result.Name)
assert.NoError(t, err, "Unexpected error retrieving currency information for %s", country.code)
assert.Equal(t, country.name, result.Name, "Name mismatch for %s", country.code)
assert.Equal(t, country.symbol, result.Symbol, "Symbol mismatch for %s", country.code)
assert.Equal(t, country.numeric, result.NumericCode, "Numeric code mismatch for %s", country.code)
})
}
}

func TestGetCurrencySymbol(t *testing.T) {
countries := []struct {
code string
symbol string
}{
{"CNY", "CN¥"},
{"JPY", "¥"},
{"INR", "₹"},
{"RUB", "₽"},
{"USD", "$"},
{"AED", "د.إ"},
{"BRL", "R$"},
{"AUD", "A$"},
{"SAR", "ر.س"},
{"EUR", "€"},
{"ZAR", "R"},
}

for _, country := range countries {
symbol, err := GetCurrencySymbol(country.code)

if err != nil {
t.Errorf("Error for code %s: %v", country.code, err)
} else {
assert.Equal(t, country.symbol, symbol, "Symbol mismatch for %s", country.code)
}
}
}

0 comments on commit c7a15eb

Please sign in to comment.