package places import ( "os" "testing" ) // The real list the node ships (web/dist is the one it embeds). func load(t *testing.T) []Place { t.Helper() ps, err := Load(os.DirFS("../../web/dist")) if err != nil { t.Fatal(err) } if len(ps) < 30000 { t.Fatalf("only %d places", len(ps)) } return ps } func first(t *testing.T, ps []Place, q string) Match { t.Helper() m := Search(ps, q, 1) if len(m) == 0 { t.Fatalf("%q: nothing found", q) } return m[0] } func TestNamesAreFoundHoweverTheyAreSpelled(t *testing.T) { ps := load(t) for q, name := range map[string]string{ "Bonn": "Bonn", "bonn": "Bonn", "köln": "Köln", "koeln": "Köln", "KOELN": "Köln", "zurich": "Zürich", "Zürich": "Zürich", } { if got := first(t, ps, q).Place.Name; got != name { t.Errorf("%q: found %q, want %q", q, got, name) } } } func TestOtherLanguagesLeadToThePlaceAndSayWhatMatched(t *testing.T) { ps := load(t) m := first(t, ps, "cologne") if m.Place.Name != "Köln" || m.Matched != "Cologne" { t.Fatalf("cologne: %s (%s)", m.Place.Name, m.Matched) } for q, cc := range map[string]string{"münchen": "DE", "muenchen": "DE", "nuernberg": "DE", "prag": "CZ", "wien": "AT"} { if got := first(t, ps, q).Place.Country; got != cc { t.Errorf("%q: country %s, want %s", q, got, cc) } } if first(t, ps, "Köln").Matched != "" { t.Error("the main name is not repeated") } } func TestTheBigCityWinsOverASmallNamesake(t *testing.T) { ps := load(t) v := Search(ps, "vienna", 5) if v[0].Place.Country != "AT" { t.Fatalf("first is %s", v[0].Place.Country) } if Search(ps, "b", 5) != nil || Search(ps, "qqqzzzxxx", 5) != nil || Search(ps, " ", 5) != nil { t.Error("too short or unknown queries must find nothing") } } func TestGeohashesAndCells(t *testing.T) { // Known values: the geohash of Hamburg's centre starts u1x, Bonn is in u1j. if got := Encode(57.64911, 10.40744, 11); got != "u4pruydqqvj" { t.Fatalf("Encode = %s (the standard test vector)", got) } ps := load(t) bonn := first(t, ps, "Bonn").Place cells := CellsNear(bonn.Lat, bonn.Lon, 40) if cells[0] != "u1j" { t.Fatalf("Bonn's own cell comes first: %v", cells) } seen := map[string]bool{} for _, c := range cells { if seen[c] || len(c) != 3 { t.Fatalf("bad or repeated cell in %v", cells) } seen[c] = true } // The middle of a cell is far from every edge; at 80 km its neighbours are in reach. if got := CellsNear(50.9, 7.2, 1); len(got) != 1 { t.Fatalf("a tiny reach stays in one cell: %v", got) } if got := CellsNear(50.9, 7.2, 80); len(got) < 2 { t.Fatalf("a wide reach crosses a border: %v", got) } for _, p := range [][2]float64{{89.99, 0}, {-89.99, 10}, {0, 179.99}, {0, -179.99}, {64.1, -21.9}} { for _, c := range CellsNear(p[0], p[1], 40) { if len(c) != 3 { t.Fatalf("%v: %v", p, c) } } } }