Skip to content

Commit

Permalink
Merge pull request #48 from arktos-venture/develop
Browse files Browse the repository at this point in the history
Add FindCountriesByCurrency
  • Loading branch information
pariz authored Aug 12, 2022
2 parents 2fa96f5 + 2668ac8 commit a108e89
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 0 deletions.
15 changes: 15 additions & 0 deletions query.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type Query struct {
Alpha3ToAlpha2 map[string]string
NativeNameToAlpha2 map[string]string
CallingCodeToAlpha2 map[string]string
CurrencyToAlpha2 map[string][]Country
}

// FindCountryByName finds a country by given name
Expand Down Expand Up @@ -67,6 +68,20 @@ func (q *Query) FindCountryByCallingCode(callingCode string) (result Country, er
return q.Countries[alpha2], nil
}

// FindCountriesByCurrency finds a Country based on the given struct data
func (q *Query) FindCountriesByCurrency(currency string) (results []Country, err error) {
if len(currency) != 3 {
return nil, makeError("Invalid currency format", currency)
}

alpha2, exists := q.CurrencyToAlpha2[currency]
if !exists {
return []Country{}, makeError("Could not find countries with currency name", currency)
}

return alpha2, nil
}

// FindAllCountries returns a list of all countries
func (q *Query) FindAllCountries() (countries map[string]Country) {
return q.Countries
Expand Down
37 changes: 37 additions & 0 deletions query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -397,3 +397,40 @@ func TestFindCountryByCallingCode(t *testing.T) {
})
}
}

func TestFindCountriesByCurrency(t *testing.T) {
tests := []struct {
name string
currency string
expectedError error
expectedCountry int
}{
{
name: "Find countries by valid currency should return countries when currency is present for countries",
currency: "EUR",
expectedError: nil,
expectedCountry: 34,
},
{
name: "Find countries by unknown country code should return error when searching countries",
currency: "ERT",
expectedError: fmt.Errorf("gountries error. Could not find countries with currency name: ERT"),
expectedCountry: 0,
},
{
name: "Find countries by invalid currency length code should return error when searching countries",
currency: "CNYE",
expectedError: fmt.Errorf("gountries error. Invalid currency format: CNYE"),
expectedCountry: 0,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
actualCountry, actualError := query.FindCountriesByCurrency(test.currency)
if !(reflect.DeepEqual(test.expectedCountry, len(actualCountry)) &&
reflect.DeepEqual(test.expectedError, actualError)) {
t.Fail()
}
})
}
}
12 changes: 12 additions & 0 deletions setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func NewFromPath(dataPath string) *Query {
queryInstance.Alpha3ToAlpha2 = populateAlphaIndex(queryInstance.Countries)
queryInstance.NativeNameToAlpha2 = populateNativeNameIndex(queryInstance.Countries)
queryInstance.CallingCodeToAlpha2 = populateCallingCodeIndex(queryInstance.Countries)
queryInstance.CurrencyToAlpha2 = populateCallingCurrencyIndex(queryInstance.Countries)

subdivisions := populateSubdivisions(dataPath)
for k := range queryInstance.Countries {
Expand Down Expand Up @@ -220,3 +221,14 @@ func populateCallingCodeIndex(countries map[string]Country) map[string]string {
}
return index
}

func populateCallingCurrencyIndex(countries map[string]Country) map[string][]Country {
index := make(map[string][]Country)

for _, country := range countries {
for _, currency := range country.Currencies {
index[currency] = append(index[currency], country)
}
}
return index
}

0 comments on commit a108e89

Please sign in to comment.