search_aggs_bucket_date_histogram_test.go 1.8 KB

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