2019-12-17 18:27:08 +01:00
|
|
|
package entity
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
2019-12-21 17:24:29 +01:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2019-12-17 18:27:08 +01:00
|
|
|
)
|
|
|
|
|
2020-05-08 11:46:09 +02:00
|
|
|
func TestNewLocation(t *testing.T) {
|
|
|
|
t.Run("new label", func(t *testing.T) {
|
2020-07-11 23:43:29 +02:00
|
|
|
l := NewGeo(1, 1)
|
|
|
|
l.GeoCategory = "restaurant"
|
|
|
|
l.GeoName = "LocationName"
|
2020-05-10 19:43:49 +02:00
|
|
|
l.Place = PlaceFixtures.Pointer("zinkwazi")
|
2019-12-17 18:27:08 +01:00
|
|
|
|
2020-05-08 11:46:09 +02:00
|
|
|
assert.Equal(t, "restaurant", l.Category())
|
2020-05-08 14:18:11 +02:00
|
|
|
assert.Equal(t, false, l.NoCategory())
|
2020-05-08 11:46:09 +02:00
|
|
|
assert.Equal(t, false, l.Unknown())
|
|
|
|
assert.Equal(t, "LocationName", l.Name())
|
|
|
|
assert.Equal(t, false, l.NoName())
|
|
|
|
assert.Equal(t, "KwaDukuza, KwaZulu-Natal, South Africa", l.Label())
|
|
|
|
assert.Equal(t, "KwaDukuza", l.City())
|
|
|
|
assert.Equal(t, false, l.LongCity())
|
|
|
|
assert.Equal(t, false, l.CityContains("xxx"))
|
|
|
|
assert.Equal(t, false, l.NoCity())
|
|
|
|
assert.Equal(t, "KwaZulu-Natal", l.State())
|
|
|
|
assert.Equal(t, false, l.NoState())
|
|
|
|
assert.Equal(t, "za", l.CountryCode())
|
|
|
|
assert.Equal(t, "South Africa", l.CountryName())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLocation_Keywords(t *testing.T) {
|
2020-05-10 19:43:49 +02:00
|
|
|
t.Run("mexico", func(t *testing.T) {
|
2020-07-11 23:43:29 +02:00
|
|
|
m := GeoFixtures["mexico"]
|
2020-05-10 19:43:49 +02:00
|
|
|
r := m.Keywords()
|
2020-05-29 12:21:17 +02:00
|
|
|
assert.Equal(t, []string{"adosada", "ancient", "botanical", "garden", "mexico", "platform", "pyramid", "state-of-mexico", "teotihuacán"}, r)
|
2020-05-08 11:46:09 +02:00
|
|
|
})
|
2020-05-10 19:43:49 +02:00
|
|
|
t.Run("caravan park", func(t *testing.T) {
|
2020-07-11 23:43:29 +02:00
|
|
|
m := GeoFixtures["caravan park"]
|
2020-05-10 19:43:49 +02:00
|
|
|
r := m.Keywords()
|
|
|
|
assert.Equal(t, []string{"camping", "caravan", "kwazulu-natal", "lobotes", "mandeni", "park", "south-africa"}, r)
|
2020-05-08 11:46:09 +02:00
|
|
|
})
|
2020-07-10 10:29:56 +02:00
|
|
|
t.Run("place id empty", func(t *testing.T) {
|
2020-07-11 23:43:29 +02:00
|
|
|
m := &Geo{}
|
2020-07-10 10:29:56 +02:00
|
|
|
r := m.Keywords()
|
|
|
|
assert.Empty(t, r)
|
|
|
|
})
|
2020-05-08 11:46:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestLocation_Find(t *testing.T) {
|
|
|
|
t.Run("place in db", func(t *testing.T) {
|
2020-07-11 23:43:29 +02:00
|
|
|
m := GeoFixtures["mexico"]
|
2020-05-10 19:43:49 +02:00
|
|
|
r := m.Find("")
|
2020-05-08 11:46:09 +02:00
|
|
|
assert.Nil(t, r)
|
|
|
|
})
|
|
|
|
t.Run("invalid api", func(t *testing.T) {
|
2020-07-11 23:43:29 +02:00
|
|
|
l := NewGeo(2, 1)
|
2020-05-08 11:46:09 +02:00
|
|
|
err := l.Find("")
|
2020-05-29 12:21:17 +02:00
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("error expected")
|
|
|
|
}
|
|
|
|
|
2020-05-08 11:46:09 +02:00
|
|
|
assert.Equal(t, "maps: reverse lookup disabled", err.Error())
|
|
|
|
})
|
2019-12-17 18:27:08 +01:00
|
|
|
}
|
2020-07-10 10:29:56 +02:00
|
|
|
|
|
|
|
func TestFirstOrCreateLocation(t *testing.T) {
|
|
|
|
t.Run("id empty", func(t *testing.T) {
|
2020-07-11 23:43:29 +02:00
|
|
|
loc := &Geo{}
|
2020-07-10 10:29:56 +02:00
|
|
|
|
2020-07-11 23:43:29 +02:00
|
|
|
assert.Nil(t, FirstOrCreateGeo(loc))
|
2020-07-10 10:29:56 +02:00
|
|
|
})
|
|
|
|
t.Run("place id empty", func(t *testing.T) {
|
2020-07-11 23:43:29 +02:00
|
|
|
loc := &Geo{ID: "1234jhy"}
|
2020-07-10 10:29:56 +02:00
|
|
|
|
2020-07-11 23:43:29 +02:00
|
|
|
assert.Nil(t, FirstOrCreateGeo(loc))
|
2020-07-10 10:29:56 +02:00
|
|
|
})
|
|
|
|
t.Run("success", func(t *testing.T) {
|
2020-07-11 23:43:29 +02:00
|
|
|
loc := GeoFixtures.Pointer("caravan park")
|
2020-07-10 10:29:56 +02:00
|
|
|
|
2020-07-11 23:43:29 +02:00
|
|
|
result := FirstOrCreateGeo(loc)
|
2020-07-10 10:29:56 +02:00
|
|
|
|
|
|
|
if result == nil {
|
|
|
|
t.Fatal("result should not be nil")
|
|
|
|
}
|
|
|
|
assert.NotEmpty(t, result.ID)
|
|
|
|
})
|
|
|
|
}
|