search_aggs_bucket_date_range_test.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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 TestDateRangeAggregation(t *testing.T) {
  10. agg := NewDateRangeAggregation().Field("created_at")
  11. agg = agg.AddRange(nil, "2012-12-31")
  12. agg = agg.AddRange("2013-01-01", "2013-12-31")
  13. agg = agg.AddRange("2014-01-01", nil)
  14. src, err := agg.Source()
  15. if err != nil {
  16. t.Fatal(err)
  17. }
  18. data, err := json.Marshal(src)
  19. if err != nil {
  20. t.Fatalf("marshaling to JSON failed: %v", err)
  21. }
  22. got := string(data)
  23. expected := `{"date_range":{"field":"created_at","ranges":[{"to":"2012-12-31"},{"from":"2013-01-01","to":"2013-12-31"},{"from":"2014-01-01"}]}}`
  24. if got != expected {
  25. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  26. }
  27. }
  28. func TestDateRangeAggregationWithPointers(t *testing.T) {
  29. d1 := "2012-12-31"
  30. d2 := "2013-01-01"
  31. d3 := "2013-12-31"
  32. d4 := "2014-01-01"
  33. agg := NewDateRangeAggregation().Field("created_at")
  34. agg = agg.AddRange(nil, &d1)
  35. agg = agg.AddRange(d2, &d3)
  36. agg = agg.AddRange(d4, nil)
  37. src, err := agg.Source()
  38. if err != nil {
  39. t.Fatal(err)
  40. }
  41. data, err := json.Marshal(src)
  42. if err != nil {
  43. t.Fatalf("marshaling to JSON failed: %v", err)
  44. }
  45. got := string(data)
  46. expected := `{"date_range":{"field":"created_at","ranges":[{"to":"2012-12-31"},{"from":"2013-01-01","to":"2013-12-31"},{"from":"2014-01-01"}]}}`
  47. if got != expected {
  48. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  49. }
  50. }
  51. func TestDateRangeAggregationWithUnbounded(t *testing.T) {
  52. agg := NewDateRangeAggregation().Field("created_at").
  53. AddUnboundedFrom("2012-12-31").
  54. AddRange("2013-01-01", "2013-12-31").
  55. AddUnboundedTo("2014-01-01")
  56. src, err := agg.Source()
  57. if err != nil {
  58. t.Fatal(err)
  59. }
  60. data, err := json.Marshal(src)
  61. if err != nil {
  62. t.Fatalf("marshaling to JSON failed: %v", err)
  63. }
  64. got := string(data)
  65. expected := `{"date_range":{"field":"created_at","ranges":[{"to":"2012-12-31"},{"from":"2013-01-01","to":"2013-12-31"},{"from":"2014-01-01"}]}}`
  66. if got != expected {
  67. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  68. }
  69. }
  70. func TestDateRangeAggregationWithLtAndCo(t *testing.T) {
  71. agg := NewDateRangeAggregation().Field("created_at").
  72. Lt("2012-12-31").
  73. Between("2013-01-01", "2013-12-31").
  74. Gt("2014-01-01")
  75. src, err := agg.Source()
  76. if err != nil {
  77. t.Fatal(err)
  78. }
  79. data, err := json.Marshal(src)
  80. if err != nil {
  81. t.Fatalf("marshaling to JSON failed: %v", err)
  82. }
  83. got := string(data)
  84. expected := `{"date_range":{"field":"created_at","ranges":[{"to":"2012-12-31"},{"from":"2013-01-01","to":"2013-12-31"},{"from":"2014-01-01"}]}}`
  85. if got != expected {
  86. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  87. }
  88. }
  89. func TestDateRangeAggregationWithKeyedFlag(t *testing.T) {
  90. agg := NewDateRangeAggregation().Field("created_at").
  91. Keyed(true).
  92. Lt("2012-12-31").
  93. Between("2013-01-01", "2013-12-31").
  94. Gt("2014-01-01")
  95. src, err := agg.Source()
  96. if err != nil {
  97. t.Fatal(err)
  98. }
  99. data, err := json.Marshal(src)
  100. if err != nil {
  101. t.Fatalf("marshaling to JSON failed: %v", err)
  102. }
  103. got := string(data)
  104. expected := `{"date_range":{"field":"created_at","keyed":true,"ranges":[{"to":"2012-12-31"},{"from":"2013-01-01","to":"2013-12-31"},{"from":"2014-01-01"}]}}`
  105. if got != expected {
  106. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  107. }
  108. }
  109. func TestDateRangeAggregationWithKeys(t *testing.T) {
  110. agg := NewDateRangeAggregation().Field("created_at").
  111. Keyed(true).
  112. LtWithKey("pre-2012", "2012-12-31").
  113. BetweenWithKey("2013", "2013-01-01", "2013-12-31").
  114. GtWithKey("post-2013", "2014-01-01")
  115. src, err := agg.Source()
  116. if err != nil {
  117. t.Fatal(err)
  118. }
  119. data, err := json.Marshal(src)
  120. if err != nil {
  121. t.Fatalf("marshaling to JSON failed: %v", err)
  122. }
  123. got := string(data)
  124. expected := `{"date_range":{"field":"created_at","keyed":true,"ranges":[{"key":"pre-2012","to":"2012-12-31"},{"from":"2013-01-01","key":"2013","to":"2013-12-31"},{"from":"2014-01-01","key":"post-2013"}]}}`
  125. if got != expected {
  126. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  127. }
  128. }
  129. func TestDateRangeAggregationWithSpecialNames(t *testing.T) {
  130. agg := NewDateRangeAggregation().Field("created_at").
  131. AddRange("now-10M/M", "now+10M/M")
  132. src, err := agg.Source()
  133. if err != nil {
  134. t.Fatal(err)
  135. }
  136. data, err := json.Marshal(src)
  137. if err != nil {
  138. t.Fatalf("marshaling to JSON failed: %v", err)
  139. }
  140. got := string(data)
  141. expected := `{"date_range":{"field":"created_at","ranges":[{"from":"now-10M/M","to":"now+10M/M"}]}}`
  142. if got != expected {
  143. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  144. }
  145. }