Debian Codegraph @

search_aggs_bucket_date_histogram_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 TestDateHistogramAggregation(t *testing.T) {
13 agg := NewDateHistogramAggregation().
14 Field("date").
15 Interval("month").
16 Format("YYYY-MM").
17 TimeZone("UTC").
18 Offset("+6h")
19 src, err := agg.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 := `{"date_histogram":{"field":"date","format":"YYYY-MM","interval":"month","offset":"+6h","time_zone":"UTC"}}`
29 if got != expected {
30 t.Errorf("expected\n%s\n,got:\n%s", expected, got)
31 }
32 }
33
34 func TestDateHistogramAggregationWithKeyedResponse(t *testing.T) {
35 agg := NewDateHistogramAggregation().Field("date").Interval("year").Missing("1900").Keyed(true)
36 src, err := agg.Source()
37 if err != nil {
38 t.Fatal(err)
39 }
40 data, err := json.Marshal(src)
41 if err != nil {
42 t.Fatalf("marshaling to JSON failed: %v", err)
43 }
44 got := string(data)
45 expected := `{"date_histogram":{"field":"date","interval":"year","keyed":true,"missing":"1900"}}`
46 if got != expected {
47 t.Errorf("expected\n%s\n,got:\n%s", expected, got)
48 }
49 }
50
51 func TestDateHistogramAggregationWithMissing(t *testing.T) {
52 agg := NewDateHistogramAggregation().Field("date").Interval("year").Missing("1900")
53 src, err := agg.Source()
54 if err != nil {
55 t.Fatal(err)
56 }
57 data, err := json.Marshal(src)
58 if err != nil {
59 t.Fatalf("marshaling to JSON failed: %v", err)
60 }
61 got := string(data)
62 expected := `{"date_histogram":{"field":"date","interval":"year","missing":"1900"}}`
63 if got != expected {
64 t.Errorf("expected\n%s\n,got:\n%s", expected, got)
65 }
66 }