search_queries_script_test.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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 TestScriptQuery(t *testing.T) {
  10. q := NewScriptQuery(NewScript("doc['num1'.value > 1"))
  11. src, err := q.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 := `{"script":{"script":{"inline":"doc['num1'.value \u003e 1"}}}`
  21. if got != expected {
  22. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  23. }
  24. }
  25. func TestScriptQueryWithParams(t *testing.T) {
  26. q := NewScriptQuery(NewScript("doc['num1'.value > 1"))
  27. q = q.QueryName("MyQueryName")
  28. src, err := q.Source()
  29. if err != nil {
  30. t.Fatal(err)
  31. }
  32. data, err := json.Marshal(src)
  33. if err != nil {
  34. t.Fatalf("marshaling to JSON failed: %v", err)
  35. }
  36. got := string(data)
  37. expected := `{"script":{"_name":"MyQueryName","script":{"inline":"doc['num1'.value \u003e 1"}}}`
  38. if got != expected {
  39. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  40. }
  41. }