| 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 |
"encoding/json" |
| 9 |
"net/url" |
| 10 |
"reflect" |
| 11 |
"sort" |
| 12 |
"testing" |
| 13 |
) |
| 14 |
|
| 15 |
func TestFieldStatsURLs(t *testing.T) { |
| 16 |
tests := []struct { |
| 17 |
Service *FieldStatsService |
| 18 |
ExpectedPath string |
| 19 |
ExpectedParams url.Values |
| 20 |
}{ |
| 21 |
{ |
| 22 |
Service: &FieldStatsService{}, |
| 23 |
ExpectedPath: "/_field_stats", |
| 24 |
ExpectedParams: url.Values{}, |
| 25 |
}, |
| 26 |
{ |
| 27 |
Service: &FieldStatsService{ |
| 28 |
level: FieldStatsClusterLevel, |
| 29 |
}, |
| 30 |
ExpectedPath: "/_field_stats", |
| 31 |
ExpectedParams: url.Values{"level": []string{FieldStatsClusterLevel}}, |
| 32 |
}, |
| 33 |
{ |
| 34 |
Service: &FieldStatsService{ |
| 35 |
level: FieldStatsIndicesLevel, |
| 36 |
}, |
| 37 |
ExpectedPath: "/_field_stats", |
| 38 |
ExpectedParams: url.Values{"level": []string{FieldStatsIndicesLevel}}, |
| 39 |
}, |
| 40 |
{ |
| 41 |
Service: &FieldStatsService{ |
| 42 |
level: FieldStatsClusterLevel, |
| 43 |
index: []string{"index1"}, |
| 44 |
}, |
| 45 |
ExpectedPath: "/index1/_field_stats", |
| 46 |
ExpectedParams: url.Values{"level": []string{FieldStatsClusterLevel}}, |
| 47 |
}, |
| 48 |
{ |
| 49 |
Service: &FieldStatsService{ |
| 50 |
level: FieldStatsIndicesLevel, |
| 51 |
index: []string{"index1", "index2"}, |
| 52 |
}, |
| 53 |
ExpectedPath: "/index1%2Cindex2/_field_stats", |
| 54 |
ExpectedParams: url.Values{"level": []string{FieldStatsIndicesLevel}}, |
| 55 |
}, |
| 56 |
{ |
| 57 |
Service: &FieldStatsService{ |
| 58 |
level: FieldStatsIndicesLevel, |
| 59 |
index: []string{"index_*"}, |
| 60 |
}, |
| 61 |
ExpectedPath: "/index_%2A/_field_stats", |
| 62 |
ExpectedParams: url.Values{"level": []string{FieldStatsIndicesLevel}}, |
| 63 |
}, |
| 64 |
} |
| 65 |
|
| 66 |
for _, test := range tests { |
| 67 |
gotPath, gotParams, err := test.Service.buildURL() |
| 68 |
if err != nil { |
| 69 |
t.Fatalf("expected no error; got: %v", err) |
| 70 |
} |
| 71 |
if gotPath != test.ExpectedPath { |
| 72 |
t.Errorf("expected URL path = %q; got: %q", test.ExpectedPath, gotPath) |
| 73 |
} |
| 74 |
if gotParams.Encode() != test.ExpectedParams.Encode() { |
| 75 |
t.Errorf("expected URL params = %v; got: %v", test.ExpectedParams, gotParams) |
| 76 |
} |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
func TestFieldStatsValidate(t *testing.T) { |
| 81 |
tests := []struct { |
| 82 |
Service *FieldStatsService |
| 83 |
Valid bool |
| 84 |
}{ |
| 85 |
{ |
| 86 |
Service: &FieldStatsService{}, |
| 87 |
Valid: true, |
| 88 |
}, |
| 89 |
{ |
| 90 |
Service: &FieldStatsService{ |
| 91 |
fields: []string{"field"}, |
| 92 |
}, |
| 93 |
Valid: true, |
| 94 |
}, |
| 95 |
{ |
| 96 |
Service: &FieldStatsService{ |
| 97 |
bodyJson: &FieldStatsRequest{ |
| 98 |
Fields: []string{"field"}, |
| 99 |
}, |
| 100 |
}, |
| 101 |
Valid: true, |
| 102 |
}, |
| 103 |
{ |
| 104 |
Service: &FieldStatsService{ |
| 105 |
level: FieldStatsClusterLevel, |
| 106 |
bodyJson: &FieldStatsRequest{ |
| 107 |
Fields: []string{"field"}, |
| 108 |
}, |
| 109 |
}, |
| 110 |
Valid: true, |
| 111 |
}, |
| 112 |
{ |
| 113 |
Service: &FieldStatsService{ |
| 114 |
level: FieldStatsIndicesLevel, |
| 115 |
bodyJson: &FieldStatsRequest{ |
| 116 |
Fields: []string{"field"}, |
| 117 |
}, |
| 118 |
}, |
| 119 |
Valid: true, |
| 120 |
}, |
| 121 |
{ |
| 122 |
Service: &FieldStatsService{ |
| 123 |
level: "random", |
| 124 |
}, |
| 125 |
Valid: false, |
| 126 |
}, |
| 127 |
} |
| 128 |
|
| 129 |
for _, test := range tests { |
| 130 |
err := test.Service.Validate() |
| 131 |
isValid := err == nil |
| 132 |
if isValid != test.Valid { |
| 133 |
t.Errorf("expected validity to be %v, got %v", test.Valid, isValid) |
| 134 |
} |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
func TestFieldStatsRequestSerialize(t *testing.T) { |
| 139 |
req := &FieldStatsRequest{ |
| 140 |
Fields: []string{"creation_date", "answer_count"}, |
| 141 |
IndexConstraints: map[string]*FieldStatsConstraints{ |
| 142 |
"creation_date": &FieldStatsConstraints{ |
| 143 |
Min: &FieldStatsComparison{Gte: "2014-01-01T00:00:00.000Z"}, |
| 144 |
Max: &FieldStatsComparison{Lt: "2015-01-01T10:00:00.000Z"}, |
| 145 |
}, |
| 146 |
}, |
| 147 |
} |
| 148 |
data, err := json.Marshal(req) |
| 149 |
if err != nil { |
| 150 |
t.Fatalf("marshaling to JSON failed: %v", err) |
| 151 |
} |
| 152 |
got := string(data) |
| 153 |
expected := `{"fields":["creation_date","answer_count"],"index_constraints":{"creation_date":{"min_value":{"gte":"2014-01-01T00:00:00.000Z"},"max_value":{"lt":"2015-01-01T10:00:00.000Z"}}}}` |
| 154 |
if got != expected { |
| 155 |
t.Errorf("expected\n%s\n,got:\n%s", expected, got) |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
func TestFieldStatsRequestDeserialize(t *testing.T) { |
| 160 |
body := `{ |
| 161 |
"fields" : ["creation_date", "answer_count"], |
| 162 |
"index_constraints" : { |
| 163 |
"creation_date" : { |
| 164 |
"min_value" : { |
| 165 |
"gte" : "2014-01-01T00:00:00.000Z" |
| 166 |
}, |
| 167 |
"max_value" : { |
| 168 |
"lt" : "2015-01-01T10:00:00.000Z" |
| 169 |
} |
| 170 |
} |
| 171 |
} |
| 172 |
}` |
| 173 |
|
| 174 |
var request FieldStatsRequest |
| 175 |
if err := json.Unmarshal([]byte(body), &request); err != nil { |
| 176 |
t.Errorf("unexpected error during unmarshalling: %v", err) |
| 177 |
} |
| 178 |
|
| 179 |
sort.Sort(lexicographically{request.Fields}) |
| 180 |
|
| 181 |
expectedFields := []string{"answer_count", "creation_date"} |
| 182 |
if !reflect.DeepEqual(request.Fields, expectedFields) { |
| 183 |
t.Errorf("expected fields to be %v, got %v", expectedFields, request.Fields) |
| 184 |
} |
| 185 |
|
| 186 |
constraints, ok := request.IndexConstraints["creation_date"] |
| 187 |
if !ok { |
| 188 |
t.Errorf("expected field creation_date, didn't find it!") |
| 189 |
} |
| 190 |
if constraints.Min.Lt != nil { |
| 191 |
t.Errorf("expected min value less than constraint to be empty, got %v", constraints.Min.Lt) |
| 192 |
} |
| 193 |
if constraints.Min.Gte != "2014-01-01T00:00:00.000Z" { |
| 194 |
t.Errorf("expected min value >= %v, found %v", "2014-01-01T00:00:00.000Z", constraints.Min.Gte) |
| 195 |
} |
| 196 |
if constraints.Max.Lt != "2015-01-01T10:00:00.000Z" { |
| 197 |
t.Errorf("expected max value < %v, found %v", "2015-01-01T10:00:00.000Z", constraints.Max.Lt) |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
func TestFieldStatsResponseUnmarshalling(t *testing.T) { |
| 202 |
clusterStats := `{ |
| 203 |
"_shards": { |
| 204 |
"total": 1, |
| 205 |
"successful": 1, |
| 206 |
"failed": 0 |
| 207 |
}, |
| 208 |
"indices": { |
| 209 |
"_all": { |
| 210 |
"fields": { |
| 211 |
"creation_date": { |
| 212 |
"type": "date", |
| 213 |
"max_doc": 1326564, |
| 214 |
"doc_count": 564633, |
| 215 |
"density": 42, |
| 216 |
"sum_doc_freq": 2258532, |
| 217 |
"sum_total_term_freq": -1, |
| 218 |
"searchable": true, |
| 219 |
"aggregatable": true, |
| 220 |
"min_value":1483016404000, |
| 221 |
"min_value_as_string": "2016-12-29T13:00:04.000Z", |
| 222 |
"max_value":1484152326000, |
| 223 |
"max_value_as_string": "2017-01-11T16:32:06.000Z" |
| 224 |
}, |
| 225 |
"answer_count": { |
| 226 |
"max_doc": 1326564, |
| 227 |
"doc_count": 139885, |
| 228 |
"density": 10, |
| 229 |
"sum_doc_freq": 559540, |
| 230 |
"sum_total_term_freq": -1, |
| 231 |
"searchable": true, |
| 232 |
"aggregatable": true, |
| 233 |
"min_value":1483016404000, |
| 234 |
"min_value_as_string": "2016-12-29T13:00:04.000Z", |
| 235 |
"max_value":1484152326000, |
| 236 |
"max_value_as_string": "2017-01-11T16:32:06.000Z" |
| 237 |
} |
| 238 |
} |
| 239 |
} |
| 240 |
} |
| 241 |
}` |
| 242 |
|
| 243 |
var response FieldStatsResponse |
| 244 |
if err := json.Unmarshal([]byte(clusterStats), &response); err != nil { |
| 245 |
t.Errorf("unexpected error during unmarshalling: %v", err) |
| 246 |
} |
| 247 |
|
| 248 |
stats, ok := response.Indices["_all"] |
| 249 |
if !ok { |
| 250 |
t.Errorf("expected _all to be in the indices map, didn't find it") |
| 251 |
} |
| 252 |
|
| 253 |
fieldStats, ok := stats.Fields["creation_date"] |
| 254 |
if !ok { |
| 255 |
t.Errorf("expected creation_date to be in the fields map, didn't find it") |
| 256 |
} |
| 257 |
if want, have := true, fieldStats.Searchable; want != have { |
| 258 |
t.Errorf("expected creation_date searchable to be %v, got %v", want, have) |
| 259 |
} |
| 260 |
if want, have := true, fieldStats.Aggregatable; want != have { |
| 261 |
t.Errorf("expected creation_date aggregatable to be %v, got %v", want, have) |
| 262 |
} |
| 263 |
if want, have := "2016-12-29T13:00:04.000Z", fieldStats.MinValueAsString; want != have { |
| 264 |
t.Errorf("expected creation_date min value string to be %q, got %q", want, have) |
| 265 |
} |
| 266 |
} |
| 267 |
|
| 268 |
type lexicographically struct { |
| 269 |
strings []string |
| 270 |
} |
| 271 |
|
| 272 |
func (l lexicographically) Len() int { |
| 273 |
return len(l.strings) |
| 274 |
} |
| 275 |
|
| 276 |
func (l lexicographically) Less(i, j int) bool { |
| 277 |
return l.strings[i] < l.strings[j] |
| 278 |
} |
| 279 |
|
| 280 |
func (l lexicographically) Swap(i, j int) { |
| 281 |
l.strings[i], l.strings[j] = l.strings[j], l.strings[i] |
| 282 |
} |