| 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 TestGeoDistanceAggregation(t *testing.T) { |
| 13 |
agg := NewGeoDistanceAggregation().Field("location").Point("52.3760, 4.894") |
| 14 |
agg = agg.AddRange(nil, 100) |
| 15 |
agg = agg.AddRange(100, 300) |
| 16 |
agg = agg.AddRange(300, nil) |
| 17 |
src, err := agg.Source() |
| 18 |
if err != nil { |
| 19 |
t.Fatal(err) |
| 20 |
} |
| 21 |
data, err := json.Marshal(src) |
| 22 |
if err != nil { |
| 23 |
t.Fatalf("marshaling to JSON failed: %v", err) |
| 24 |
} |
| 25 |
got := string(data) |
| 26 |
expected := `{"geo_distance":{"field":"location","origin":"52.3760, 4.894","ranges":[{"to":100},{"from":100,"to":300},{"from":300}]}}` |
| 27 |
if got != expected { |
| 28 |
t.Errorf("expected\n%s\n,got:\n%s", expected, got) |
| 29 |
} |
| 30 |
} |
| 31 |
|
| 32 |
func TestGeoDistanceAggregationWithPointers(t *testing.T) { |
| 33 |
hundred := 100 |
| 34 |
threeHundred := 300 |
| 35 |
agg := NewGeoDistanceAggregation().Field("location").Point("52.3760, 4.894") |
| 36 |
agg = agg.AddRange(nil, &hundred) |
| 37 |
agg = agg.AddRange(hundred, &threeHundred) |
| 38 |
agg = agg.AddRange(threeHundred, nil) |
| 39 |
src, err := agg.Source() |
| 40 |
if err != nil { |
| 41 |
t.Fatal(err) |
| 42 |
} |
| 43 |
data, err := json.Marshal(src) |
| 44 |
if err != nil { |
| 45 |
t.Fatalf("marshaling to JSON failed: %v", err) |
| 46 |
} |
| 47 |
got := string(data) |
| 48 |
expected := `{"geo_distance":{"field":"location","origin":"52.3760, 4.894","ranges":[{"to":100},{"from":100,"to":300},{"from":300}]}}` |
| 49 |
if got != expected { |
| 50 |
t.Errorf("expected\n%s\n,got:\n%s", expected, got) |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
func TestGeoDistanceAggregationWithUnbounded(t *testing.T) { |
| 55 |
agg := NewGeoDistanceAggregation().Field("location").Point("52.3760, 4.894") |
| 56 |
agg = agg.AddUnboundedFrom(100) |
| 57 |
agg = agg.AddRange(100, 300) |
| 58 |
agg = agg.AddUnboundedTo(300) |
| 59 |
src, err := agg.Source() |
| 60 |
if err != nil { |
| 61 |
t.Fatal(err) |
| 62 |
} |
| 63 |
data, err := json.Marshal(src) |
| 64 |
if err != nil { |
| 65 |
t.Fatalf("marshaling to JSON failed: %v", err) |
| 66 |
} |
| 67 |
got := string(data) |
| 68 |
expected := `{"geo_distance":{"field":"location","origin":"52.3760, 4.894","ranges":[{"to":100},{"from":100,"to":300},{"from":300}]}}` |
| 69 |
if got != expected { |
| 70 |
t.Errorf("expected\n%s\n,got:\n%s", expected, got) |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
func TestGeoDistanceAggregationWithMetaData(t *testing.T) { |
| 75 |
agg := NewGeoDistanceAggregation().Field("location").Point("52.3760, 4.894") |
| 76 |
agg = agg.AddRange(nil, 100) |
| 77 |
agg = agg.AddRange(100, 300) |
| 78 |
agg = agg.AddRange(300, nil) |
| 79 |
agg = agg.Meta(map[string]interface{}{"name": "Oliver"}) |
| 80 |
src, err := agg.Source() |
| 81 |
if err != nil { |
| 82 |
t.Fatal(err) |
| 83 |
} |
| 84 |
data, err := json.Marshal(src) |
| 85 |
if err != nil { |
| 86 |
t.Fatalf("marshaling to JSON failed: %v", err) |
| 87 |
} |
| 88 |
got := string(data) |
| 89 |
expected := `{"geo_distance":{"field":"location","origin":"52.3760, 4.894","ranges":[{"to":100},{"from":100,"to":300},{"from":300}]},"meta":{"name":"Oliver"}}` |
| 90 |
if got != expected { |
| 91 |
t.Errorf("expected\n%s\n,got:\n%s", expected, got) |
| 92 |
} |
| 93 |
} |