| 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 TestParentIdQueryTest(t *testing.T) { |
| 13 |
tests := []struct { |
| 14 |
Query Query |
| 15 |
Expected string |
| 16 |
}{ |
| 17 |
// #0 |
| 18 |
{ |
| 19 |
Query: NewParentIdQuery("blog_tag", "1"), |
| 20 |
Expected: `{"parent_id":{"id":"1","type":"blog_tag"}}`, |
| 21 |
}, |
| 22 |
// #1 |
| 23 |
{ |
| 24 |
Query: NewParentIdQuery("blog_tag", "1").IgnoreUnmapped(true), |
| 25 |
Expected: `{"parent_id":{"id":"1","ignore_unmapped":true,"type":"blog_tag"}}`, |
| 26 |
}, |
| 27 |
// #2 |
| 28 |
{ |
| 29 |
Query: NewParentIdQuery("blog_tag", "1").IgnoreUnmapped(false), |
| 30 |
Expected: `{"parent_id":{"id":"1","ignore_unmapped":false,"type":"blog_tag"}}`, |
| 31 |
}, |
| 32 |
// #3 |
| 33 |
{ |
| 34 |
Query: NewParentIdQuery("blog_tag", "1").IgnoreUnmapped(true).Boost(5).QueryName("my_parent_query"), |
| 35 |
Expected: `{"parent_id":{"_name":"my_parent_query","boost":5,"id":"1","ignore_unmapped":true,"type":"blog_tag"}}`, |
| 36 |
}, |
| 37 |
} |
| 38 |
|
| 39 |
for i, tt := range tests { |
| 40 |
src, err := tt.Query.Source() |
| 41 |
if err != nil { |
| 42 |
t.Fatalf("#%d: encoding Source failed: %v", i, err) |
| 43 |
} |
| 44 |
data, err := json.Marshal(src) |
| 45 |
if err != nil { |
| 46 |
t.Fatalf("#%d: marshaling to JSON failed: %v", i, err) |
| 47 |
} |
| 48 |
if want, got := tt.Expected, string(data); want != got { |
| 49 |
t.Fatalf("#%d: expected\n%s\ngot:\n%s", i, want, got) |
| 50 |
} |
| 51 |
} |
| 52 |
} |