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	"context"
9	"testing"
10)
11
12func TestFlush(t *testing.T) {
13	client := setupTestClientAndCreateIndex(t)
14
15	// Flush all indices
16	res, err := client.Flush().Do(context.TODO())
17	if err != nil {
18		t.Fatal(err)
19	}
20	if res == nil {
21		t.Errorf("expected res to be != nil; got: %v", res)
22	}
23}
24
25func TestFlushBuildURL(t *testing.T) {
26	client := setupTestClientAndCreateIndex(t)
27
28	tests := []struct {
29		Indices               []string
30		Expected              string
31		ExpectValidateFailure bool
32	}{
33		{
34			[]string{},
35			"/_flush",
36			false,
37		},
38		{
39			[]string{"index1"},
40			"/index1/_flush",
41			false,
42		},
43		{
44			[]string{"index1", "index2"},
45			"/index1%2Cindex2/_flush",
46			false,
47		},
48	}
49
50	for i, test := range tests {
51		err := NewIndicesFlushService(client).Index(test.Indices...).Validate()
52		if err == nil && test.ExpectValidateFailure {
53			t.Errorf("case #%d: expected validate to fail", i+1)
54			continue
55		}
56		if err != nil && !test.ExpectValidateFailure {
57			t.Errorf("case #%d: expected validate to succeed", i+1)
58			continue
59		}
60		if !test.ExpectValidateFailure {
61			path, _, err := NewIndicesFlushService(client).Index(test.Indices...).buildURL()
62			if err != nil {
63				t.Fatalf("case #%d: %v", i+1, err)
64			}
65			if path != test.Expected {
66				t.Errorf("case #%d: expected %q; got: %q", i+1, test.Expected, path)
67			}
68		}
69	}
70}
71