| 1 | package geo |
| 2 | |
| 3 | import ( |
| 4 | "math" |
| 5 | "testing" |
| 6 | ) |
| 7 | |
| 8 | var testPoints = map[string]Point{ |
| 9 | "eu-west-1": {53.35, -6.26}, |
| 10 | "eu-west-2": {51.51, -0.13}, |
| 11 | "ap-southeast-2": {-33.87, 151.21}, |
| 12 | } |
| 13 | |
| 14 | func testPointOf(r string) (Point, bool) { p, ok := testPoints[r]; return p, ok } |
| 15 | |
| 16 | func TestDistanceKmDublinLondon(t *testing.T) { |
| 17 | d := DistanceKm(testPoints["eu-west-1"], testPoints["eu-west-2"]) |
| 18 | if math.Abs(d-464) > 5 { |
| 19 | t.Fatalf("Dublin to London = %.1f km, want about 464", d) |
| 20 | } |
| 21 | } |
| 22 | |
| 23 | func TestNearest(t *testing.T) { |
| 24 | london := Point{51.5, -0.12} |
| 25 | sydney := Point{-33.9, 151.2} |
| 26 | home := Candidate{Code: "", Home: true, AWSRegion: "eu-west-1"} |
| 27 | uk := Candidate{Code: "uk", AWSRegion: "eu-west-2"} |
| 28 | au := Candidate{Code: "au", AWSRegion: "ap-southeast-2"} |
| 29 | |
| 30 | cases := []struct { |
| 31 | name string |
| 32 | p Point |
| 33 | cands []Candidate |
| 34 | want string |
| 35 | wantOK bool |
| 36 | }{ |
| 37 | {"single", london, []Candidate{home}, "", true}, |
| 38 | {"clear winner", london, []Candidate{home, uk, au}, "uk", true}, |
| 39 | {"far side", sydney, []Candidate{home, uk, au}, "au", true}, |
| 40 | {"home with no point gives no answer", london, |
| 41 | []Candidate{{Code: "", Home: true, AWSRegion: ""}, uk}, "", false}, |
| 42 | {"non-home with no point is left out", london, |
| 43 | []Candidate{home, {Code: "zz", AWSRegion: "xx-nowhere-1"}}, "", true}, |
| 44 | {"empty", london, nil, "", false}, |
| 45 | {"tie goes to the home", testPoints["eu-west-2"], |
| 46 | []Candidate{{Code: "uk2", AWSRegion: "eu-west-2"}, {Code: "", Home: true, AWSRegion: "eu-west-2"}}, "", true}, |
| 47 | {"non-home tie goes to the smaller code", testPoints["eu-west-2"], |
| 48 | []Candidate{{Code: "uk2", AWSRegion: "eu-west-2"}, {Code: "uk", AWSRegion: "eu-west-2"}}, "uk", true}, |
| 49 | } |
| 50 | for _, c := range cases { |
| 51 | got, ok := NearestWith(c.p, c.cands, testPointOf) |
| 52 | if got != c.want || ok != c.wantOK { |
| 53 | t.Errorf("%s: got (%q, %v), want (%q, %v)", c.name, got, ok, c.want, c.wantOK) |
| 54 | } |
| 55 | } |
| 56 | } |
| 57 | |