Test
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

search_request_test.go 1.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. _ "net/http"
  8. "testing"
  9. )
  10. func TestSearchRequestIndex(t *testing.T) {
  11. builder := NewSearchRequest().Index("test")
  12. data, err := json.Marshal(builder.header())
  13. if err != nil {
  14. t.Fatalf("marshaling to JSON failed: %v", err)
  15. }
  16. got := string(data)
  17. expected := `{"index":"test"}`
  18. if got != expected {
  19. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  20. }
  21. }
  22. func TestSearchRequestIndices(t *testing.T) {
  23. builder := NewSearchRequest().Index("test", "test2")
  24. data, err := json.Marshal(builder.header())
  25. if err != nil {
  26. t.Fatalf("marshaling to JSON failed: %v", err)
  27. }
  28. got := string(data)
  29. expected := `{"indices":["test","test2"]}`
  30. if got != expected {
  31. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  32. }
  33. }
  34. func TestSearchRequestHasIndices(t *testing.T) {
  35. builder := NewSearchRequest()
  36. if builder.HasIndices() {
  37. t.Errorf("expected HasIndices to return true; got %v", builder.HasIndices())
  38. }
  39. builder = builder.Index("test", "test2")
  40. if !builder.HasIndices() {
  41. t.Errorf("expected HasIndices to return false; got %v", builder.HasIndices())
  42. }
  43. }
  44. func TestSearchRequestIgnoreUnavailable(t *testing.T) {
  45. builder := NewSearchRequest().Index("test").IgnoreUnavailable(true)
  46. data, err := json.Marshal(builder.header())
  47. if err != nil {
  48. t.Fatalf("marshaling to JSON failed: %v", err)
  49. }
  50. got := string(data)
  51. expected := `{"ignore_unavailable":true,"index":"test"}`
  52. if got != expected {
  53. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  54. }
  55. }