| 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 |
|
| 5 |
package elastic |
| 6 |
|
| 7 |
import ( |
| 8 |
"context" |
| 9 |
"encoding/json" |
| 10 |
"net/url" |
| 11 |
"reflect" |
| 12 |
"sort" |
| 13 |
"testing" |
| 14 |
) |
| 15 |
|
| 16 |
func TestFieldCapsURLs(t *testing.T) { |
| 17 |
tests := []struct { |
| 18 |
Service *FieldCapsService |
| 19 |
ExpectedPath string |
| 20 |
ExpectedParams url.Values |
| 21 |
}{ |
| 22 |
{ |
| 23 |
Service: &FieldCapsService{}, |
| 24 |
ExpectedPath: "/_field_caps", |
| 25 |
ExpectedParams: url.Values{}, |
| 26 |
}, |
| 27 |
{ |
| 28 |
Service: &FieldCapsService{ |
| 29 |
index: []string{"index1", "index2"}, |
| 30 |
}, |
| 31 |
ExpectedPath: "/index1%2Cindex2/_field_caps", |
| 32 |
ExpectedParams: url.Values{}, |
| 33 |
}, |
| 34 |
{ |
| 35 |
Service: &FieldCapsService{ |
| 36 |
index: []string{"index_*"}, |
| 37 |
pretty: true, |
| 38 |
}, |
| 39 |
ExpectedPath: "/index_%2A/_field_caps", |
| 40 |
ExpectedParams: url.Values{"pretty": []string{"true"}}, |
| 41 |
}, |
| 42 |
} |
| 43 |
|
| 44 |
for _, test := range tests { |
| 45 |
gotPath, gotParams, err := test.Service.buildURL() |
| 46 |
if err != nil { |
| 47 |
t.Fatalf("expected no error; got: %v", err) |
| 48 |
} |
| 49 |
if gotPath != test.ExpectedPath { |
| 50 |
t.Errorf("expected URL path = %q; got: %q", test.ExpectedPath, gotPath) |
| 51 |
} |
| 52 |
if gotParams.Encode() != test.ExpectedParams.Encode() { |
| 53 |
t.Errorf("expected URL params = %v; got: %v", test.ExpectedParams, gotParams) |
| 54 |
} |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
func TestFieldCapsRequestSerialize(t *testing.T) { |
| 59 |
req := &FieldCapsRequest{ |
| 60 |
Fields: []string{"creation_date", "answer_count"}, |
| 61 |
} |
| 62 |
data, err := json.Marshal(req) |
| 63 |
if err != nil { |
| 64 |
t.Fatalf("marshaling to JSON failed: %v", err) |
| 65 |
} |
| 66 |
got := string(data) |
| 67 |
expected := `{"fields":["creation_date","answer_count"]}` |
| 68 |
if got != expected { |
| 69 |
t.Fatalf("expected\n%s\n,got:\n%s", expected, got) |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
func TestFieldCapsRequestDeserialize(t *testing.T) { |
| 74 |
body := `{ |
| 75 |
"fields" : ["creation_date", "answer_count"] |
| 76 |
}` |
| 77 |
|
| 78 |
var request FieldCapsRequest |
| 79 |
if err := json.Unmarshal([]byte(body), &request); err != nil { |
| 80 |
t.Fatalf("unexpected error during unmarshalling: %v", err) |
| 81 |
} |
| 82 |
|
| 83 |
sort.Sort(lexicographically{request.Fields}) |
| 84 |
|
| 85 |
expectedFields := []string{"answer_count", "creation_date"} |
| 86 |
if !reflect.DeepEqual(request.Fields, expectedFields) { |
| 87 |
t.Fatalf("expected fields to be %v, got %v", expectedFields, request.Fields) |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
func TestFieldCapsResponseUnmarshalling(t *testing.T) { |
| 92 |
clusterStats := `{ |
| 93 |
"_shards": { |
| 94 |
"total": 1, |
| 95 |
"successful": 1, |
| 96 |
"failed": 0 |
| 97 |
}, |
| 98 |
"fields": { |
| 99 |
"creation_date": { |
| 100 |
"type": "date", |
| 101 |
"searchable": true, |
| 102 |
"aggregatable": true, |
| 103 |
"indices": ["index1", "index2"], |
| 104 |
"non_searchable_indices": null, |
| 105 |
"non_aggregatable_indices": null |
| 106 |
}, |
| 107 |
"answer": { |
| 108 |
"type": "keyword", |
| 109 |
"searchable": true, |
| 110 |
"aggregatable": true |
| 111 |
} |
| 112 |
} |
| 113 |
}` |
| 114 |
|
| 115 |
var resp FieldCapsResponse |
| 116 |
if err := json.Unmarshal([]byte(clusterStats), &resp); err != nil { |
| 117 |
t.Errorf("unexpected error during unmarshalling: %v", err) |
| 118 |
} |
| 119 |
|
| 120 |
caps, ok := resp.Fields["creation_date"] |
| 121 |
if !ok { |
| 122 |
t.Errorf("expected creation_date to be in the fields map, didn't find it") |
| 123 |
} |
| 124 |
if want, have := true, caps.Searchable; want != have { |
| 125 |
t.Errorf("expected creation_date searchable to be %v, got %v", want, have) |
| 126 |
} |
| 127 |
if want, have := true, caps.Aggregatable; want != have { |
| 128 |
t.Errorf("expected creation_date aggregatable to be %v, got %v", want, have) |
| 129 |
} |
| 130 |
if want, have := []string{"index1", "index2"}, caps.Indices; !reflect.DeepEqual(want, have) { |
| 131 |
t.Errorf("expected creation_date indices to be %v, got %v", want, have) |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
func TestFieldCaps123(t *testing.T) { |
| 136 |
client := setupTestClientAndCreateIndexAndAddDocs(t) |
| 137 |
// client := setupTestClientAndCreateIndexAndAddDocs(t, SetTraceLog(log.New(os.Stdout, "", 0))) |
| 138 |
|
| 139 |
res, err := client.FieldCaps("_all").Fields("user", "message", "retweets", "created").Pretty(true).Do(context.TODO()) |
| 140 |
if err != nil { |
| 141 |
t.Fatalf("expected no error; got: %v", err) |
| 142 |
} |
| 143 |
if res == nil { |
| 144 |
t.Fatalf("expected response; got: %v", res) |
| 145 |
} |
| 146 |
} |