search_aggs_bucket_reverse_nested_test.go 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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 TestReverseNestedAggregation(t *testing.T) {
  10. agg := NewReverseNestedAggregation()
  11. src, err := agg.Source()
  12. if err != nil {
  13. t.Fatal(err)
  14. }
  15. data, err := json.Marshal(src)
  16. if err != nil {
  17. t.Fatalf("marshaling to JSON failed: %v", err)
  18. }
  19. got := string(data)
  20. expected := `{"reverse_nested":{}}`
  21. if got != expected {
  22. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  23. }
  24. }
  25. func TestReverseNestedAggregationWithPath(t *testing.T) {
  26. agg := NewReverseNestedAggregation().Path("comments")
  27. src, err := agg.Source()
  28. if err != nil {
  29. t.Fatal(err)
  30. }
  31. data, err := json.Marshal(src)
  32. if err != nil {
  33. t.Fatalf("marshaling to JSON failed: %v", err)
  34. }
  35. got := string(data)
  36. expected := `{"reverse_nested":{"path":"comments"}}`
  37. if got != expected {
  38. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  39. }
  40. }
  41. func TestReverseNestedAggregationWithSubAggregation(t *testing.T) {
  42. avgPriceAgg := NewAvgAggregation().Field("price")
  43. agg := NewReverseNestedAggregation().
  44. Path("a_path").
  45. SubAggregation("avg_price", avgPriceAgg)
  46. src, err := agg.Source()
  47. if err != nil {
  48. t.Fatal(err)
  49. }
  50. data, err := json.Marshal(src)
  51. if err != nil {
  52. t.Fatalf("marshaling to JSON failed: %v", err)
  53. }
  54. got := string(data)
  55. expected := `{"aggregations":{"avg_price":{"avg":{"field":"price"}}},"reverse_nested":{"path":"a_path"}}`
  56. if got != expected {
  57. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  58. }
  59. }
  60. func TestReverseNestedAggregationWithMeta(t *testing.T) {
  61. agg := NewReverseNestedAggregation().
  62. Path("a_path").
  63. Meta(map[string]interface{}{"name": "Oliver"})
  64. src, err := agg.Source()
  65. if err != nil {
  66. t.Fatal(err)
  67. }
  68. data, err := json.Marshal(src)
  69. if err != nil {
  70. t.Fatalf("marshaling to JSON failed: %v", err)
  71. }
  72. got := string(data)
  73. expected := `{"meta":{"name":"Oliver"},"reverse_nested":{"path":"a_path"}}`
  74. if got != expected {
  75. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  76. }
  77. }