2019-12-20 11:30:58 +01:00
|
|
|
package maps
|
|
|
|
|
|
|
|
import (
|
2019-12-31 07:16:11 +01:00
|
|
|
"strings"
|
2019-12-20 11:30:58 +01:00
|
|
|
"testing"
|
|
|
|
|
2020-12-04 13:10:32 +01:00
|
|
|
"github.com/photoprism/photoprism/internal/hub/places"
|
2020-01-12 14:00:56 +01:00
|
|
|
"github.com/photoprism/photoprism/pkg/s2"
|
2019-12-20 11:30:58 +01:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2019-12-31 07:16:11 +01:00
|
|
|
func TestLocation_QueryPlaces(t *testing.T) {
|
|
|
|
t.Run("U Berliner Rathaus", func(t *testing.T) {
|
|
|
|
lat := 52.51961810676184
|
|
|
|
lng := 13.40806264572578
|
|
|
|
id := s2.Token(lat, lng)
|
|
|
|
|
2020-04-28 19:41:06 +02:00
|
|
|
l := NewLocation(id, "", "", "", "", "", "", "", []string{})
|
2019-12-31 07:16:11 +01:00
|
|
|
|
|
|
|
if err := l.QueryPlaces(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Equal(t, "Berlin, Germany", l.LocLabel)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-12-20 20:23:16 +01:00
|
|
|
func TestLocation_Assign(t *testing.T) {
|
2020-04-28 19:41:06 +02:00
|
|
|
t.Run("Italy", func(t *testing.T) {
|
|
|
|
id := "47786b2bed37"
|
|
|
|
|
|
|
|
o, err := places.FindLocation(id)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2020-04-28 21:52:15 +02:00
|
|
|
assert.Equal(t, "Comici I", o.Name())
|
2020-04-29 00:17:44 +02:00
|
|
|
assert.Equal(t, "Trentino-Alto Adige", o.State())
|
2020-04-28 19:41:06 +02:00
|
|
|
assert.Equal(t, "it", o.CountryCode())
|
|
|
|
|
|
|
|
var l Location
|
|
|
|
|
|
|
|
if err := l.Assign(o); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2020-04-28 21:52:15 +02:00
|
|
|
assert.Equal(t, "Comici I", l.LocName)
|
2020-12-02 00:18:56 +01:00
|
|
|
assert.Equal(t, "Santa Cristina Gherdëina, Trentino-Alto Adige, Italy", l.LocLabel)
|
2020-04-28 21:52:15 +02:00
|
|
|
assert.IsType(t, []string{}, l.Keywords())
|
2020-12-02 00:18:56 +01:00
|
|
|
assert.Equal(t, "christina, cristina, gröden, santa, südtirol, valgardena", l.KeywordString())
|
2020-04-28 19:41:06 +02:00
|
|
|
})
|
|
|
|
|
2019-12-20 11:30:58 +01:00
|
|
|
t.Run("BerlinFernsehturm", func(t *testing.T) {
|
|
|
|
lat := 52.5208
|
|
|
|
lng := 13.40953
|
2019-12-31 07:16:11 +01:00
|
|
|
id := s2.Token(lat, lng)
|
2019-12-20 11:30:58 +01:00
|
|
|
|
2020-01-13 11:07:09 +01:00
|
|
|
o, err := places.FindLocation(id)
|
2019-12-20 11:30:58 +01:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2020-12-02 00:18:56 +01:00
|
|
|
assert.Equal(t, "Berliner Fernsehturm", o.Name())
|
|
|
|
assert.Equal(t, "Berlin", o.City())
|
2020-01-13 11:07:09 +01:00
|
|
|
assert.Equal(t, "Berlin", o.State())
|
|
|
|
assert.Equal(t, "de", o.CountryCode())
|
2019-12-20 11:30:58 +01:00
|
|
|
|
|
|
|
var l Location
|
|
|
|
|
2019-12-20 12:20:35 +01:00
|
|
|
if err := l.Assign(o); err != nil {
|
2019-12-20 12:04:26 +01:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2019-12-20 11:30:58 +01:00
|
|
|
|
2020-12-02 00:18:56 +01:00
|
|
|
assert.Equal(t, "Berliner Fernsehturm", l.LocName)
|
2019-12-28 20:24:20 +01:00
|
|
|
assert.Equal(t, "Berlin, Germany", l.LocLabel)
|
2020-04-28 19:41:06 +02:00
|
|
|
assert.IsType(t, []string{}, l.Keywords())
|
|
|
|
assert.Equal(t, "", l.KeywordString())
|
2019-12-20 11:30:58 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("SantaMonica", func(t *testing.T) {
|
|
|
|
lat := 34.00909444444444
|
|
|
|
lng := -118.49700833333334
|
2019-12-31 07:16:11 +01:00
|
|
|
id := s2.Token(lat, lng)
|
2019-12-20 11:30:58 +01:00
|
|
|
|
2020-01-19 12:50:44 +01:00
|
|
|
o, err := places.FindLocation(id)
|
2019-12-20 11:30:58 +01:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.False(t, o.Cached)
|
2020-01-19 12:50:44 +01:00
|
|
|
assert.Equal(t, "California", o.State())
|
|
|
|
assert.Equal(t, "us", o.CountryCode())
|
2019-12-20 11:30:58 +01:00
|
|
|
|
|
|
|
var l Location
|
|
|
|
|
2019-12-20 12:20:35 +01:00
|
|
|
if err := l.Assign(o); err != nil {
|
2019-12-20 12:04:26 +01:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2019-12-20 11:30:58 +01:00
|
|
|
|
2019-12-28 20:24:20 +01:00
|
|
|
assert.Equal(t, "Santa Monica, California, USA", l.LocLabel)
|
2019-12-20 11:30:58 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("AirportZurich", func(t *testing.T) {
|
|
|
|
lat := 47.45401666666667
|
|
|
|
lng := 8.557494444444446
|
2019-12-31 07:16:11 +01:00
|
|
|
id := s2.Token(lat, lng)
|
2019-12-20 11:30:58 +01:00
|
|
|
|
2020-01-19 12:50:44 +01:00
|
|
|
o, err := places.FindLocation(id)
|
2019-12-20 11:30:58 +01:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var l Location
|
|
|
|
|
2019-12-20 12:20:35 +01:00
|
|
|
if err := l.Assign(o); err != nil {
|
2019-12-20 12:04:26 +01:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2019-12-20 11:30:58 +01:00
|
|
|
|
2019-12-27 05:18:52 +01:00
|
|
|
assert.Equal(t, "Airport", l.LocName)
|
2020-12-02 00:18:56 +01:00
|
|
|
assert.Equal(t, "Kloten, Zürich, Switzerland", l.LocLabel)
|
2019-12-20 11:30:58 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("AirportTegel", func(t *testing.T) {
|
|
|
|
lat := 52.559864397033024
|
|
|
|
lng := 13.28895092010498
|
2019-12-31 07:16:11 +01:00
|
|
|
id := s2.Token(lat, lng)
|
2019-12-20 11:30:58 +01:00
|
|
|
|
2020-01-19 12:50:44 +01:00
|
|
|
o, err := places.FindLocation(id)
|
2019-12-20 11:30:58 +01:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var l Location
|
|
|
|
|
2019-12-20 12:20:35 +01:00
|
|
|
if err := l.Assign(o); err != nil {
|
2019-12-20 12:04:26 +01:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2019-12-20 11:30:58 +01:00
|
|
|
|
2019-12-27 05:18:52 +01:00
|
|
|
assert.Equal(t, "Airport", l.LocName)
|
2020-12-02 00:18:56 +01:00
|
|
|
assert.Equal(t, "Tegel, Berlin, Germany", l.LocLabel)
|
2019-12-20 11:30:58 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("SouthAfrica", func(t *testing.T) {
|
|
|
|
lat := -31.976301666666668
|
|
|
|
lng := 29.148046666666666
|
2019-12-31 07:16:11 +01:00
|
|
|
id := s2.Token(lat, lng)
|
2019-12-20 11:30:58 +01:00
|
|
|
|
2020-01-13 11:07:09 +01:00
|
|
|
o, err := places.FindLocation(id)
|
2019-12-20 11:30:58 +01:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.False(t, o.Cached)
|
|
|
|
|
2020-01-13 11:07:09 +01:00
|
|
|
assert.Equal(t, "", o.Name())
|
|
|
|
assert.Equal(t, "Eastern Cape", o.State())
|
|
|
|
assert.Equal(t, "za", o.CountryCode())
|
2019-12-20 11:30:58 +01:00
|
|
|
|
|
|
|
var l Location
|
|
|
|
|
2019-12-20 12:20:35 +01:00
|
|
|
if err := l.Assign(o); err != nil {
|
2019-12-20 12:04:26 +01:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2019-12-20 11:30:58 +01:00
|
|
|
|
2019-12-31 07:16:11 +01:00
|
|
|
assert.True(t, strings.HasPrefix(l.ID, "1e5e4205"))
|
2020-01-13 11:07:09 +01:00
|
|
|
assert.Equal(t, "", l.LocName)
|
2019-12-28 20:24:20 +01:00
|
|
|
assert.Equal(t, "Eastern Cape, South Africa", l.LocLabel)
|
2019-12-20 11:30:58 +01:00
|
|
|
})
|
2019-12-22 21:39:53 +01:00
|
|
|
|
2020-01-13 11:07:09 +01:00
|
|
|
t.Run("ocean", func(t *testing.T) {
|
2019-12-22 21:39:53 +01:00
|
|
|
lat := -21.976301666666668
|
|
|
|
lng := 49.148046666666666
|
2019-12-31 07:16:11 +01:00
|
|
|
id := s2.Token(lat, lng)
|
2020-07-16 15:43:23 +02:00
|
|
|
// log.Printf("ID: %s", id)
|
2020-01-13 11:07:09 +01:00
|
|
|
o, err := places.FindLocation(id)
|
2019-12-22 21:39:53 +01:00
|
|
|
|
2020-07-16 15:43:23 +02:00
|
|
|
// log.Printf("Output: %+v", o)
|
2019-12-22 21:39:53 +01:00
|
|
|
|
2020-01-13 11:07:09 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
2019-12-22 21:39:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
var l Location
|
|
|
|
|
2020-01-13 11:07:09 +01:00
|
|
|
if err := l.Assign(o); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2020-12-02 07:53:44 +01:00
|
|
|
assert.Equal(t, "Indian Ocean", l.LocName)
|
2020-01-13 11:07:09 +01:00
|
|
|
assert.Equal(t, "", l.LocCategory)
|
2020-12-02 00:18:56 +01:00
|
|
|
assert.Equal(t, "Unknown", l.LocCity)
|
2020-07-12 06:14:22 +02:00
|
|
|
assert.Equal(t, "zz", l.LocCountry)
|
2019-12-22 21:39:53 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLocation_Unknown(t *testing.T) {
|
|
|
|
t.Run("true", func(t *testing.T) {
|
|
|
|
lat := 0.0
|
|
|
|
lng := 0.0
|
2019-12-31 07:16:11 +01:00
|
|
|
id := s2.Token(lat, lng)
|
2019-12-22 21:39:53 +01:00
|
|
|
|
2020-04-28 19:41:06 +02:00
|
|
|
l := NewLocation(id, "", "", "", "", "", "", "", []string{})
|
2019-12-22 21:39:53 +01:00
|
|
|
|
|
|
|
assert.Equal(t, true, l.Unknown())
|
|
|
|
})
|
|
|
|
t.Run("false", func(t *testing.T) {
|
|
|
|
lat := -31.976301666666668
|
|
|
|
lng := 29.148046666666666
|
2019-12-31 07:16:11 +01:00
|
|
|
id := s2.Token(lat, lng)
|
2019-12-22 21:39:53 +01:00
|
|
|
|
2020-04-28 19:41:06 +02:00
|
|
|
l := NewLocation(id, "", "", "", "", "", "", "", []string{})
|
2019-12-22 21:39:53 +01:00
|
|
|
|
|
|
|
assert.Equal(t, false, l.Unknown())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-12-27 05:18:52 +01:00
|
|
|
func TestLocation_place(t *testing.T) {
|
2019-12-22 21:39:53 +01:00
|
|
|
t.Run("unknown", func(t *testing.T) {
|
|
|
|
lat := 0.0
|
|
|
|
lng := 0.0
|
2019-12-31 07:16:11 +01:00
|
|
|
id := s2.Token(lat, lng)
|
2019-12-22 21:39:53 +01:00
|
|
|
|
2020-04-28 19:41:06 +02:00
|
|
|
l := NewLocation(id, "", "", "", "", "", "", "", []string{})
|
2019-12-22 21:39:53 +01:00
|
|
|
|
2019-12-28 20:24:20 +01:00
|
|
|
assert.Equal(t, "Unknown", l.label())
|
2019-12-22 21:39:53 +01:00
|
|
|
})
|
|
|
|
t.Run("Nürnberg, Bayern, Germany", func(t *testing.T) {
|
2020-04-28 19:41:06 +02:00
|
|
|
l := NewLocation("", "", "", "", "Nürnberg", "Bayern", "de", "", []string{})
|
2019-12-22 21:39:53 +01:00
|
|
|
|
2019-12-31 07:16:11 +01:00
|
|
|
assert.Equal(t, "Unknown", l.label())
|
2019-12-22 21:39:53 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-04-29 12:56:48 +02:00
|
|
|
func TestLocation_S2Token(t *testing.T) {
|
|
|
|
t.Run("123", func(t *testing.T) {
|
|
|
|
l := NewLocation("123", "Indian ocean", "", "", "Nürnberg", "Bayern", "de", "", []string{})
|
|
|
|
|
|
|
|
assert.Equal(t, "123", l.S2Token())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-06-05 16:49:32 +02:00
|
|
|
func TestLocation_PrefixedToken(t *testing.T) {
|
|
|
|
t.Run("123", func(t *testing.T) {
|
|
|
|
l := NewLocation("123", "Indian ocean", "", "", "Nürnberg", "Bayern", "de", "", []string{})
|
|
|
|
|
|
|
|
assert.Equal(t, s2.TokenPrefix+"123", l.PrefixedToken())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-12-27 05:18:52 +01:00
|
|
|
func TestLocation_Name(t *testing.T) {
|
|
|
|
t.Run("Christkindlesmarkt", func(t *testing.T) {
|
2020-04-28 19:41:06 +02:00
|
|
|
l := NewLocation("", "Christkindlesmarkt", "", "", "Nürnberg", "Bayern", "de", "", []string{})
|
2019-12-22 21:39:53 +01:00
|
|
|
|
2019-12-27 05:18:52 +01:00
|
|
|
assert.Equal(t, "Christkindlesmarkt", l.Name())
|
2019-12-22 21:39:53 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLocation_City(t *testing.T) {
|
|
|
|
t.Run("Nürnberg", func(t *testing.T) {
|
2020-04-28 19:41:06 +02:00
|
|
|
l := NewLocation("", "Christkindlesmarkt", "", "", "Nürnberg", "Bayern", "de", "", []string{})
|
2019-12-22 21:39:53 +01:00
|
|
|
|
|
|
|
assert.Equal(t, "Nürnberg", l.City())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLocation_State(t *testing.T) {
|
|
|
|
t.Run("Bayern", func(t *testing.T) {
|
2020-04-28 19:41:06 +02:00
|
|
|
l := NewLocation("", "Christkindlesmarkt", "", "", "Nürnberg", "Bayern", "de", "", []string{})
|
2019-12-22 21:39:53 +01:00
|
|
|
|
|
|
|
assert.Equal(t, "Bayern", l.State())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLocation_Category(t *testing.T) {
|
|
|
|
t.Run("test", func(t *testing.T) {
|
2020-04-28 19:41:06 +02:00
|
|
|
l := NewLocation("", "Christkindlesmarkt", "test", "", "Nürnberg", "Bayern", "de", "", []string{})
|
2019-12-22 21:39:53 +01:00
|
|
|
|
|
|
|
assert.Equal(t, "test", l.Category())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLocation_Source(t *testing.T) {
|
|
|
|
t.Run("source", func(t *testing.T) {
|
2020-04-28 19:41:06 +02:00
|
|
|
l := NewLocation("", "Christkindlesmarkt", "", "", "Nürnberg", "Bayern", "de", "source", []string{})
|
2019-12-22 21:39:53 +01:00
|
|
|
|
|
|
|
assert.Equal(t, "source", l.Source())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-12-27 05:18:52 +01:00
|
|
|
func TestLocation_Place(t *testing.T) {
|
2019-12-28 20:24:20 +01:00
|
|
|
t.Run("test-label", func(t *testing.T) {
|
2020-04-28 19:41:06 +02:00
|
|
|
l := NewLocation("", "Christkindlesmarkt", "", "test-label", "Nürnberg", "Bayern", "de", "", []string{})
|
2019-12-22 21:39:53 +01:00
|
|
|
|
2019-12-28 20:24:20 +01:00
|
|
|
assert.Equal(t, "test-label", l.Label())
|
2019-12-22 21:39:53 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLocation_CountryCode(t *testing.T) {
|
|
|
|
t.Run("de", func(t *testing.T) {
|
2020-04-28 19:41:06 +02:00
|
|
|
l := NewLocation("", "Christkindlesmarkt", "test", "test-label", "Nürnberg", "Bayern", "de", "", []string{})
|
2019-12-22 21:39:53 +01:00
|
|
|
|
|
|
|
assert.Equal(t, "de", l.CountryCode())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLocation_CountryName(t *testing.T) {
|
|
|
|
t.Run("Germany", func(t *testing.T) {
|
2020-04-28 19:41:06 +02:00
|
|
|
l := NewLocation("", "Christkindlesmarkt", "test", "test-label", "Nürnberg", "Bayern", "de", "", []string{})
|
2019-12-22 21:39:53 +01:00
|
|
|
|
|
|
|
assert.Equal(t, "Germany", l.CountryName())
|
|
|
|
})
|
2019-12-20 11:30:58 +01:00
|
|
|
}
|
2020-01-19 13:27:15 +01:00
|
|
|
|
|
|
|
func TestLocation_QueryApi(t *testing.T) {
|
2020-04-28 19:41:06 +02:00
|
|
|
l := NewLocation("3", "Christkindlesmarkt", "test", "test-label", "Nürnberg", "Bayern", "de", "", []string{})
|
2020-01-19 13:27:15 +01:00
|
|
|
t.Run("xxx", func(t *testing.T) {
|
|
|
|
api := l.QueryApi("xxx")
|
|
|
|
assert.Error(t, api, "maps: reverse lookup disabled")
|
|
|
|
})
|
|
|
|
t.Run("osm", func(t *testing.T) {
|
|
|
|
api := l.QueryApi("osm")
|
|
|
|
assert.Error(t, api)
|
|
|
|
})
|
|
|
|
t.Run("places", func(t *testing.T) {
|
|
|
|
api := l.QueryApi("places")
|
|
|
|
assert.Error(t, api)
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|