| 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 |
"testing" |
| 10 |
) |
| 11 |
|
| 12 |
func TestContextSuggesterSource(t *testing.T) { |
| 13 |
s := NewContextSuggester("place_suggestion"). |
| 14 |
Prefix("tim"). |
| 15 |
Field("suggest") |
| 16 |
src, err := s.Source(true) |
| 17 |
if err != nil { |
| 18 |
t.Fatal(err) |
| 19 |
} |
| 20 |
data, err := json.Marshal(src) |
| 21 |
if err != nil { |
| 22 |
t.Fatalf("marshaling to JSON failed: %v", err) |
| 23 |
} |
| 24 |
got := string(data) |
| 25 |
expected := `{"place_suggestion":{"prefix":"tim","completion":{"field":"suggest"}}}` |
| 26 |
if got != expected { |
| 27 |
t.Errorf("expected\n%s\n,got:\n%s", expected, got) |
| 28 |
} |
| 29 |
} |
| 30 |
|
| 31 |
func TestContextSuggesterSourceWithMultipleContexts(t *testing.T) { |
| 32 |
s := NewContextSuggester("place_suggestion"). |
| 33 |
Prefix("tim"). |
| 34 |
Field("suggest"). |
| 35 |
ContextQueries( |
| 36 |
NewSuggesterCategoryQuery("place_type", "cafe", "restaurants"), |
| 37 |
) |
| 38 |
src, err := s.Source(true) |
| 39 |
if err != nil { |
| 40 |
t.Fatal(err) |
| 41 |
} |
| 42 |
data, err := json.Marshal(src) |
| 43 |
if err != nil { |
| 44 |
t.Fatalf("marshaling to JSON failed: %v", err) |
| 45 |
} |
| 46 |
got := string(data) |
| 47 |
// Due to the randomization of dictionary key, we could actually have two different valid expected outcomes |
| 48 |
expected := `{"place_suggestion":{"prefix":"tim","completion":{"context":{"place_type":[{"context":"cafe"},{"context":"restaurants"}]},"field":"suggest"}}}` |
| 49 |
if got != expected { |
| 50 |
expected := `{"place_suggestion":{"prefix":"tim","completion":{"context":{"place_type":[{"context":"restaurants"},{"context":"cafe"}]},"field":"suggest"}}}` |
| 51 |
if got != expected { |
| 52 |
t.Errorf("expected %s\n,got:\n%s", expected, got) |
| 53 |
} |
| 54 |
} |
| 55 |
} |