fetch_source_context_test.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 TestFetchSourceContextNoFetchSource(t *testing.T) {
  10. builder := NewFetchSourceContext(false)
  11. src, err := builder.Source()
  12. if err != nil {
  13. t.Fatal(err)
  14. }
  15. data, err := json.Marshal(src)
  16. if err != nil {
  17. t.Fatalf("marshaling to JSON failed: %v", err)
  18. }
  19. got := string(data)
  20. expected := `false`
  21. if got != expected {
  22. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  23. }
  24. }
  25. func TestFetchSourceContextNoFetchSourceIgnoreIncludesAndExcludes(t *testing.T) {
  26. builder := NewFetchSourceContext(false).Include("a", "b").Exclude("c")
  27. src, err := builder.Source()
  28. if err != nil {
  29. t.Fatal(err)
  30. }
  31. data, err := json.Marshal(src)
  32. if err != nil {
  33. t.Fatalf("marshaling to JSON failed: %v", err)
  34. }
  35. got := string(data)
  36. expected := `false`
  37. if got != expected {
  38. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  39. }
  40. }
  41. func TestFetchSourceContextFetchSource(t *testing.T) {
  42. builder := NewFetchSourceContext(true)
  43. src, err := builder.Source()
  44. if err != nil {
  45. t.Fatal(err)
  46. }
  47. data, err := json.Marshal(src)
  48. if err != nil {
  49. t.Fatalf("marshaling to JSON failed: %v", err)
  50. }
  51. got := string(data)
  52. expected := `true`
  53. if got != expected {
  54. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  55. }
  56. }
  57. func TestFetchSourceContextFetchSourceWithIncludesOnly(t *testing.T) {
  58. builder := NewFetchSourceContext(true).Include("a", "b")
  59. src, err := builder.Source()
  60. if err != nil {
  61. t.Fatal(err)
  62. }
  63. data, err := json.Marshal(src)
  64. if err != nil {
  65. t.Fatalf("marshaling to JSON failed: %v", err)
  66. }
  67. got := string(data)
  68. expected := `{"includes":["a","b"]}`
  69. if got != expected {
  70. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  71. }
  72. }
  73. func TestFetchSourceContextFetchSourceWithIncludesAndExcludes(t *testing.T) {
  74. builder := NewFetchSourceContext(true).Include("a", "b").Exclude("c")
  75. src, err := builder.Source()
  76. if err != nil {
  77. t.Fatal(err)
  78. }
  79. data, err := json.Marshal(src)
  80. if err != nil {
  81. t.Fatalf("marshaling to JSON failed: %v", err)
  82. }
  83. got := string(data)
  84. expected := `{"excludes":["c"],"includes":["a","b"]}`
  85. if got != expected {
  86. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  87. }
  88. }
  89. func TestFetchSourceContextQueryDefaults(t *testing.T) {
  90. builder := NewFetchSourceContext(true)
  91. values := builder.Query()
  92. got := values.Encode()
  93. expected := ""
  94. if got != expected {
  95. t.Errorf("expected %q; got: %q", expected, got)
  96. }
  97. }
  98. func TestFetchSourceContextQueryNoFetchSource(t *testing.T) {
  99. builder := NewFetchSourceContext(false)
  100. values := builder.Query()
  101. got := values.Encode()
  102. expected := "_source=false"
  103. if got != expected {
  104. t.Errorf("expected %q; got: %q", expected, got)
  105. }
  106. }
  107. func TestFetchSourceContextQueryFetchSourceWithIncludesAndExcludes(t *testing.T) {
  108. builder := NewFetchSourceContext(true).Include("a", "b").Exclude("c")
  109. values := builder.Query()
  110. got := values.Encode()
  111. expected := "_source_exclude=c&_source_include=a%2Cb"
  112. if got != expected {
  113. t.Errorf("expected %q; got: %q", expected, got)
  114. }
  115. }