search_queries_parent_id_test.go 1.4 KB

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