put_template_test.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. "context"
  7. "testing"
  8. )
  9. func TestSearchTemplatesLifecycle(t *testing.T) {
  10. client := setupTestClientAndCreateIndex(t)
  11. // Template
  12. tmpl := `{"template":{"query":{"match":{"title":"{{query_string}}"}}}}`
  13. // Create template
  14. cresp, err := client.PutTemplate().Id("elastic-test").BodyString(tmpl).Do(context.TODO())
  15. if err != nil {
  16. t.Fatal(err)
  17. }
  18. if cresp == nil {
  19. t.Fatalf("expected response != nil; got: %v", cresp)
  20. }
  21. if !cresp.Acknowledged {
  22. t.Errorf("expected acknowledged = %v; got: %v", true, cresp.Acknowledged)
  23. }
  24. // Get template
  25. resp, err := client.GetTemplate().Id("elastic-test").Do(context.TODO())
  26. if err != nil {
  27. t.Fatal(err)
  28. }
  29. if resp == nil {
  30. t.Fatalf("expected response != nil; got: %v", resp)
  31. }
  32. if resp.Template == "" {
  33. t.Errorf("expected template != %q; got: %q", "", resp.Template)
  34. }
  35. // Delete template
  36. dresp, err := client.DeleteTemplate().Id("elastic-test").Do(context.TODO())
  37. if err != nil {
  38. t.Fatal(err)
  39. }
  40. if dresp == nil {
  41. t.Fatalf("expected response != nil; got: %v", dresp)
  42. }
  43. if !dresp.Acknowledged {
  44. t.Fatalf("expected acknowledged = %v; got: %v", true, dresp.Acknowledged)
  45. }
  46. }