Debian Codegraph @

search_aggs_bucket_geohash_grid_test.go

raw
1 package elastic
2
3 import (
4 "encoding/json"
5 "testing"
6 )
7
8 func TestGeoHashGridAggregation(t *testing.T) {
9 agg := NewGeoHashGridAggregation().Field("location").Precision(5)
10 src, err := agg.Source()
11 if err != nil {
12 t.Fatal(err)
13 }
14
15 data, err := json.Marshal(src)
16 if err != nil {
17 t.Fatalf("Marshalling to JSON failed: %v", err)
18 }
19 got := string(data)
20 expected := `{"geohash_grid":{"field":"location","precision":5}}`
21 if got != expected {
22 t.Errorf("expected\n%s\n,got:\n%s", expected, got)
23 }
24 }
25
26 func TestGeoHashGridAggregationWithMetaData(t *testing.T) {
27 agg := NewGeoHashGridAggregation().Field("location").Precision(5)
28 agg = agg.Meta(map[string]interface{}{"name": "Oliver"})
29 src, err := agg.Source()
30 if err != nil {
31 t.Fatal(err)
32 }
33
34 data, err := json.Marshal(src)
35 if err != nil {
36 t.Fatalf("Marshalling to JSON failed: %v", err)
37 }
38 got := string(data)
39 expected := `{"geohash_grid":{"field":"location","precision":5},"meta":{"name":"Oliver"}}`
40
41 if got != expected {
42 t.Errorf("expected\n%s\n,got:\n%s", expected, got)
43 }
44 }
45
46 func TestGeoHashGridAggregationWithSize(t *testing.T) {
47 agg := NewGeoHashGridAggregation().Field("location").Precision(5).Size(5)
48 agg = agg.Meta(map[string]interface{}{"name": "Oliver"})
49 src, err := agg.Source()
50 if err != nil {
51 t.Fatal(err)
52 }
53
54 data, err := json.Marshal(src)
55 if err != nil {
56 t.Fatalf("Marshalling to JSON failed: %v", err)
57 }
58 got := string(data)
59 expected := `{"geohash_grid":{"field":"location","precision":5,"size":5},"meta":{"name":"Oliver"}}`
60
61 if got != expected {
62 t.Errorf("expected\n%s\n,got:\n%s", expected, got)
63 }
64 }
65
66 func TestGeoHashGridAggregationWithShardSize(t *testing.T) {
67 agg := NewGeoHashGridAggregation().Field("location").Precision(5).ShardSize(5)
68 agg = agg.Meta(map[string]interface{}{"name": "Oliver"})
69 src, err := agg.Source()
70 if err != nil {
71 t.Fatal(err)
72 }
73
74 data, err := json.Marshal(src)
75 if err != nil {
76 t.Fatalf("Marshalling to JSON failed: %v", err)
77 }
78 got := string(data)
79 expected := `{"geohash_grid":{"field":"location","precision":5,"shard_size":5},"meta":{"name":"Oliver"}}`
80
81 if got != expected {
82 t.Errorf("expected\n%s\n,got:\n%s", expected, got)
83 }
84 }