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