package geo import ( "math" "testing" ) var testPoints = map[string]Point{ "eu-west-1": {53.35, -6.26}, "eu-west-2": {51.51, -0.13}, "ap-southeast-2": {-33.87, 151.21}, } func testPointOf(r string) (Point, bool) { p, ok := testPoints[r]; return p, ok } func TestDistanceKmDublinLondon(t *testing.T) { d := DistanceKm(testPoints["eu-west-1"], testPoints["eu-west-2"]) if math.Abs(d-464) > 5 { t.Fatalf("Dublin to London = %.1f km, want about 464", d) } } func TestNearest(t *testing.T) { london := Point{51.5, -0.12} sydney := Point{-33.9, 151.2} home := Candidate{Code: "", Home: true, AWSRegion: "eu-west-1"} uk := Candidate{Code: "uk", AWSRegion: "eu-west-2"} au := Candidate{Code: "au", AWSRegion: "ap-southeast-2"} cases := []struct { name string p Point cands []Candidate want string wantOK bool }{ {"single", london, []Candidate{home}, "", true}, {"clear winner", london, []Candidate{home, uk, au}, "uk", true}, {"far side", sydney, []Candidate{home, uk, au}, "au", true}, {"home with no point gives no answer", london, []Candidate{{Code: "", Home: true, AWSRegion: ""}, uk}, "", false}, {"non-home with no point is left out", london, []Candidate{home, {Code: "zz", AWSRegion: "xx-nowhere-1"}}, "", true}, {"empty", london, nil, "", false}, {"tie goes to the home", testPoints["eu-west-2"], []Candidate{{Code: "uk2", AWSRegion: "eu-west-2"}, {Code: "", Home: true, AWSRegion: "eu-west-2"}}, "", true}, {"non-home tie goes to the smaller code", testPoints["eu-west-2"], []Candidate{{Code: "uk2", AWSRegion: "eu-west-2"}, {Code: "uk", AWSRegion: "eu-west-2"}}, "uk", true}, } for _, c := range cases { got, ok := NearestWith(c.p, c.cands, testPointOf) if got != c.want || ok != c.wantOK { t.Errorf("%s: got (%q, %v), want (%q, %v)", c.name, got, ok, c.want, c.wantOK) } } }