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 TestPutMappingURL(t *testing.T) {
13	client := setupTestClientAndCreateIndex(t)
14
15	tests := []struct {
16		Indices  []string
17		Type     string
18		Expected string
19	}{
20		{
21			[]string{},
22			"tweet",
23			"/_mapping/tweet",
24		},
25		{
26			[]string{"*"},
27			"tweet",
28			"/%2A/_mapping/tweet",
29		},
30		{
31			[]string{"store-1", "store-2"},
32			"tweet",
33			"/store-1%2Cstore-2/_mapping/tweet",
34		},
35	}
36
37	for _, test := range tests {
38		path, _, err := client.PutMapping().Index(test.Indices...).Type(test.Type).buildURL()
39		if err != nil {
40			t.Fatal(err)
41		}
42		if path != test.Expected {
43			t.Errorf("expected %q; got: %q", test.Expected, path)
44		}
45	}
46}
47
48func TestMappingLifecycle(t *testing.T) {
49	client := setupTestClientAndCreateIndex(t)
50
51	mapping := `{
52		"tweetdoc":{
53			"properties":{
54				"field":{
55					"type":"keyword"
56				}
57			}
58		}
59	}`
60
61	putresp, err := client.PutMapping().Index(testIndexName2).Type("tweetdoc").BodyString(mapping).Do(context.TODO())
62	if err != nil {
63		t.Fatalf("expected put mapping to succeed; got: %v", err)
64	}
65	if putresp == nil {
66		t.Fatalf("expected put mapping response; got: %v", putresp)
67	}
68	if !putresp.Acknowledged {
69		t.Fatalf("expected put mapping ack; got: %v", putresp.Acknowledged)
70	}
71
72	getresp, err := client.GetMapping().Index(testIndexName2).Type("tweetdoc").Do(context.TODO())
73	if err != nil {
74		t.Fatalf("expected get mapping to succeed; got: %v", err)
75	}
76	if getresp == nil {
77		t.Fatalf("expected get mapping response; got: %v", getresp)
78	}
79	props, ok := getresp[testIndexName2]
80	if !ok {
81		t.Fatalf("expected JSON root to be of type map[string]interface{}; got: %#v", props)
82	}
83
84	// NOTE There is no Delete Mapping API in Elasticsearch 2.0
85}
86