Debian Codegraph @

search_queries_geo_distance_test.go

raw
1 // Copyright 2012-present Oliver Eilhard. All rights reserved.
2 // Use of this source code is governed by a MIT-license.
3 // See http://olivere.mit-license.org/license.txt for details.
4
5 package elastic
6
7 import (
8 "encoding/json"
9 "testing"
10 )
11
12 func TestGeoDistanceQuery(t *testing.T) {
13 q := NewGeoDistanceQuery("pin.location")
14 q = q.Lat(40)
15 q = q.Lon(-70)
16 q = q.Distance("200km")
17 q = q.DistanceType("plane")
18 q = q.OptimizeBbox("memory")
19 src, err := q.Source()
20 if err != nil {
21 t.Fatal(err)
22 }
23 data, err := json.Marshal(src)
24 if err != nil {
25 t.Fatalf("marshaling to JSON failed: %v", err)
26 }
27 got := string(data)
28 expected := `{"geo_distance":{"distance":"200km","distance_type":"plane","optimize_bbox":"memory","pin.location":{"lat":40,"lon":-70}}}`
29 if got != expected {
30 t.Errorf("expected\n%s\n,got:\n%s", expected, got)
31 }
32 }
33
34 func TestGeoDistanceQueryWithGeoPoint(t *testing.T) {
35 q := NewGeoDistanceQuery("pin.location")
36 q = q.GeoPoint(GeoPointFromLatLon(40, -70))
37 q = q.Distance("200km")
38 src, err := q.Source()
39 if err != nil {
40 t.Fatal(err)
41 }
42 data, err := json.Marshal(src)
43 if err != nil {
44 t.Fatalf("marshaling to JSON failed: %v", err)
45 }
46 got := string(data)
47 expected := `{"geo_distance":{"distance":"200km","pin.location":{"lat":40,"lon":-70}}}`
48 if got != expected {
49 t.Errorf("expected\n%s\n,got:\n%s", expected, got)
50 }
51 }
52
53 func TestGeoDistanceQueryWithGeoHash(t *testing.T) {
54 q := NewGeoDistanceQuery("pin.location")
55 q = q.GeoHash("drm3btev3e86")
56 q = q.Distance("12km")
57 src, err := q.Source()
58 if err != nil {
59 t.Fatal(err)
60 }
61 data, err := json.Marshal(src)
62 if err != nil {
63 t.Fatalf("marshaling to JSON failed: %v", err)
64 }
65 got := string(data)
66 expected := `{"geo_distance":{"distance":"12km","pin.location":"drm3btev3e86"}}`
67 if got != expected {
68 t.Errorf("expected\n%s\n,got:\n%s", expected, got)
69 }
70 }