search_aggs_pipeline_bucket_script_test.go 911 B

123456789101112131415161718192021222324252627282930
  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 TestBucketScriptAggregation(t *testing.T) {
  10. agg := NewBucketScriptAggregation().
  11. AddBucketsPath("tShirtSales", "t-shirts>sales").
  12. AddBucketsPath("totalSales", "total_sales").
  13. Script(NewScript("tShirtSales / totalSales * 100"))
  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 := `{"bucket_script":{"buckets_path":{"tShirtSales":"t-shirts\u003esales","totalSales":"total_sales"},"script":{"inline":"tShirtSales / totalSales * 100"}}}`
  24. if got != expected {
  25. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  26. }
  27. }