github.com/weaviate/weaviate@v1.24.6/adapters/repos/db/vector/geo/geo_test.go (about) 1 // _ _ 2 // __ _____ __ ___ ___ __ _| |_ ___ 3 // \ \ /\ / / _ \/ _` \ \ / / |/ _` | __/ _ \ 4 // \ V V / __/ (_| |\ V /| | (_| | || __/ 5 // \_/\_/ \___|\__,_| \_/ |_|\__,_|\__\___| 6 // 7 // Copyright © 2016 - 2024 Weaviate B.V. All rights reserved. 8 // 9 // CONTACT: hello@weaviate.io 10 // 11 12 package geo 13 14 import ( 15 "context" 16 "testing" 17 18 "github.com/stretchr/testify/assert" 19 "github.com/stretchr/testify/require" 20 "github.com/weaviate/weaviate/entities/cyclemanager" 21 "github.com/weaviate/weaviate/entities/filters" 22 "github.com/weaviate/weaviate/entities/models" 23 ) 24 25 func TestGeoJourney(t *testing.T) { 26 elements := []models.GeoCoordinates{ 27 { // coordinates of munich 28 Latitude: ptFloat32(48.13743), 29 Longitude: ptFloat32(11.57549), 30 }, 31 { // coordinates of stuttgart 32 Latitude: ptFloat32(48.78232), 33 Longitude: ptFloat32(9.17702), 34 }, 35 } 36 37 getCoordinates := func(ctx context.Context, id uint64) (*models.GeoCoordinates, error) { 38 return &elements[id], nil 39 } 40 41 geoIndex, err := NewIndex(Config{ 42 ID: "unit-test", 43 CoordinatesForID: getCoordinates, 44 DisablePersistence: true, 45 RootPath: "doesnt-matter-persistence-is-off", 46 }, 47 cyclemanager.NewCallbackGroupNoop(), cyclemanager.NewCallbackGroupNoop(), 48 cyclemanager.NewCallbackGroupNoop(), cyclemanager.NewCallbackGroupNoop()) 49 require.Nil(t, err) 50 51 t.Run("importing all", func(t *testing.T) { 52 for id, coordinates := range elements { 53 err := geoIndex.Add(uint64(id), &coordinates) 54 require.Nil(t, err) 55 } 56 }) 57 58 t.Run("importing an invalid object", func(t *testing.T) { 59 err := geoIndex.Add(9000, &models.GeoCoordinates{}) 60 assert.Equal(t, "invalid arguments: latitude must be set", err.Error()) 61 }) 62 63 km := float32(1000) 64 t.Run("searching missing longitude", func(t *testing.T) { 65 _, err := geoIndex.WithinRange(context.Background(), filters.GeoRange{ 66 GeoCoordinates: &models.GeoCoordinates{ 67 Latitude: ptFloat32(48.13743), 68 }, 69 Distance: 300 * km, 70 }) 71 assert.Equal(t, "invalid arguments: longitude must be set", err.Error()) 72 }) 73 74 t.Run("searching missing latitude", func(t *testing.T) { 75 _, err := geoIndex.WithinRange(context.Background(), filters.GeoRange{ 76 GeoCoordinates: &models.GeoCoordinates{ 77 Longitude: ptFloat32(11.57549), 78 }, 79 Distance: 300 * km, 80 }) 81 assert.Equal(t, "invalid arguments: latitude must be set", err.Error()) 82 }) 83 84 t.Run("searching within 500km of munich", func(t *testing.T) { 85 // should return both cities, with munich first and stuttgart second 86 results, err := geoIndex.WithinRange(context.Background(), filters.GeoRange{ 87 GeoCoordinates: &models.GeoCoordinates{ 88 Latitude: ptFloat32(48.13743), 89 Longitude: ptFloat32(11.57549), 90 }, 91 Distance: 500 * km, 92 }) 93 require.Nil(t, err) 94 95 expectedResults := []uint64{0, 1} 96 assert.Equal(t, expectedResults, results) 97 }) 98 99 t.Run("searching within 10km of munich", func(t *testing.T) { 100 // should return both cities, with munich first and stuttgart second 101 results, err := geoIndex.WithinRange(context.Background(), filters.GeoRange{ 102 GeoCoordinates: &models.GeoCoordinates{ 103 Latitude: ptFloat32(48.13743), 104 Longitude: ptFloat32(11.57549), 105 }, 106 Distance: 10 * km, 107 }) 108 require.Nil(t, err) 109 110 expectedResults := []uint64{0} 111 assert.Equal(t, expectedResults, results) 112 }) 113 } 114 115 func ptFloat32(in float32) *float32 { 116 return &in 117 }