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 TestIndicesExistsTypeBuildURL(t *testing.T) { 13 client := setupTestClientAndCreateIndex(t) 14 15 tests := []struct { 16 Indices []string 17 Types []string 18 Expected string 19 ExpectValidateFailure bool 20 }{ 21 { 22 []string{}, 23 []string{}, 24 "", 25 true, 26 }, 27 { 28 []string{"index1"}, 29 []string{}, 30 "", 31 true, 32 }, 33 { 34 []string{}, 35 []string{"type1"}, 36 "", 37 true, 38 }, 39 { 40 []string{"index1"}, 41 []string{"type1"}, 42 "/index1/_mapping/type1", 43 false, 44 }, 45 { 46 []string{"index1", "index2"}, 47 []string{"type1"}, 48 "/index1%2Cindex2/_mapping/type1", 49 false, 50 }, 51 { 52 []string{"index1", "index2"}, 53 []string{"type1", "type2"}, 54 "/index1%2Cindex2/_mapping/type1%2Ctype2", 55 false, 56 }, 57 } 58 59 for i, test := range tests { 60 err := client.TypeExists().Index(test.Indices...).Type(test.Types...).Validate() 61 if err == nil && test.ExpectValidateFailure { 62 t.Errorf("#%d: expected validate to fail", i+1) 63 continue 64 } 65 if err != nil && !test.ExpectValidateFailure { 66 t.Errorf("#%d: expected validate to succeed", i+1) 67 continue 68 } 69 if !test.ExpectValidateFailure { 70 path, _, err := client.TypeExists().Index(test.Indices...).Type(test.Types...).buildURL() 71 if err != nil { 72 t.Fatalf("#%d: %v", i+1, err) 73 } 74 if path != test.Expected { 75 t.Errorf("#%d: expected %q; got: %q", i+1, test.Expected, path) 76 } 77 } 78 } 79} 80 81func TestIndicesExistsType(t *testing.T) { 82 client := setupTestClient(t) 83 84 // Create index with tweet type 85 createIndex, err := client.CreateIndex(testIndexName).Body(testMapping).Do(context.TODO()) 86 if err != nil { 87 t.Fatal(err) 88 } 89 if createIndex == nil { 90 t.Errorf("expected result to be != nil; got: %v", createIndex) 91 } 92 if !createIndex.Acknowledged { 93 t.Errorf("expected CreateIndexResult.Acknowledged %v; got %v", true, createIndex.Acknowledged) 94 } 95 96 // Check if type exists 97 exists, err := client.TypeExists().Index(testIndexName).Type("tweet").Do(context.TODO()) 98 if err != nil { 99 t.Fatal(err) 100 } 101 if !exists { 102 t.Fatalf("type %s should exist in index %s, but doesn't\n", "tweet", testIndexName) 103 } 104 105 // Delete index 106 deleteIndex, err := client.DeleteIndex(testIndexName).Do(context.TODO()) 107 if err != nil { 108 t.Fatal(err) 109 } 110 if !deleteIndex.Acknowledged { 111 t.Errorf("expected DeleteIndexResult.Acknowledged %v; got %v", true, deleteIndex.Acknowledged) 112 } 113 114 // Check if type exists 115 exists, err = client.TypeExists().Index(testIndexName).Type("tweet").Do(context.TODO()) 116 if err != nil { 117 t.Fatal(err) 118 } 119 if exists { 120 t.Fatalf("type %s should not exist in index %s, but it does\n", "tweet", testIndexName) 121 } 122} 123 124func TestIndicesExistsTypeValidate(t *testing.T) { 125 client := setupTestClient(t) 126 127 // No index name -> fail with error 128 res, err := NewIndicesExistsTypeService(client).Do(context.TODO()) 129 if err == nil { 130 t.Fatalf("expected IndicesExistsType to fail without index name") 131 } 132 if res != false { 133 t.Fatalf("expected result to be false; got: %v", res) 134 } 135} 136