@@ -3,6 +3,7 @@ package gountries
33import (
44 "fmt"
55 "math/rand"
6+ "reflect"
67 "sort"
78 "testing"
89
@@ -359,8 +360,40 @@ func TestFindSubdivisionCountryByNameError(t *testing.T) {
359360
360361 _ , err := query .FindSubdivisionCountryByName (subdivisionName )
361362 if err != nil {
362- assert .Equal (t , "gountries error. Invalid subdivision name: " + subdivisionName , err .Error ())
363+ assert .Equal (t , "gountries error. Invalid subdivision name: " + subdivisionName , err .Error ())
363364 } else {
364365 t .Fail ()
365366 }
366367}
368+
369+ func TestFindCountryByCallingCode (t * testing.T ) {
370+ country , _ := query .FindCountryByName ("India" )
371+ tests := []struct {
372+ name string
373+ callingCode string
374+ expectedError error
375+ expectedCountry Country
376+ }{
377+ {
378+ name : "Find country by valid calling code should return country when calling code is present for countries" ,
379+ callingCode : "91" ,
380+ expectedError : nil ,
381+ expectedCountry : country ,
382+ },
383+ {
384+ name : "Find country by invalid country code should return error when searching for country" ,
385+ callingCode : "abc" ,
386+ expectedError : fmt .Errorf ("gountries error. Could not find country with callingCode: abc" ),
387+ expectedCountry : Country {},
388+ },
389+ }
390+ for _ , test := range tests {
391+ t .Run (test .name , func (t * testing.T ) {
392+ actualCountry , actualError := query .FindCountryByCallingCode (test .callingCode )
393+ if ! (reflect .DeepEqual (test .expectedCountry , actualCountry ) &&
394+ reflect .DeepEqual (test .expectedError , actualError )) {
395+ t .Fail ()
396+ }
397+ })
398+ }
399+ }
0 commit comments