123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125 |
- // Copyright 2012-present Oliver Eilhard. All rights reserved.
- // Use of this source code is governed by a MIT-license.
- // See http://olivere.mit-license.org/license.txt for details.
- package elastic
- import (
- "encoding/json"
- "testing"
- )
- func TestFetchSourceContextNoFetchSource(t *testing.T) {
- builder := NewFetchSourceContext(false)
- src, err := builder.Source()
- if err != nil {
- t.Fatal(err)
- }
- data, err := json.Marshal(src)
- if err != nil {
- t.Fatalf("marshaling to JSON failed: %v", err)
- }
- got := string(data)
- expected := `false`
- if got != expected {
- t.Errorf("expected\n%s\n,got:\n%s", expected, got)
- }
- }
- func TestFetchSourceContextNoFetchSourceIgnoreIncludesAndExcludes(t *testing.T) {
- builder := NewFetchSourceContext(false).Include("a", "b").Exclude("c")
- src, err := builder.Source()
- if err != nil {
- t.Fatal(err)
- }
- data, err := json.Marshal(src)
- if err != nil {
- t.Fatalf("marshaling to JSON failed: %v", err)
- }
- got := string(data)
- expected := `false`
- if got != expected {
- t.Errorf("expected\n%s\n,got:\n%s", expected, got)
- }
- }
- func TestFetchSourceContextFetchSource(t *testing.T) {
- builder := NewFetchSourceContext(true)
- src, err := builder.Source()
- if err != nil {
- t.Fatal(err)
- }
- data, err := json.Marshal(src)
- if err != nil {
- t.Fatalf("marshaling to JSON failed: %v", err)
- }
- got := string(data)
- expected := `true`
- if got != expected {
- t.Errorf("expected\n%s\n,got:\n%s", expected, got)
- }
- }
- func TestFetchSourceContextFetchSourceWithIncludesOnly(t *testing.T) {
- builder := NewFetchSourceContext(true).Include("a", "b")
- src, err := builder.Source()
- if err != nil {
- t.Fatal(err)
- }
- data, err := json.Marshal(src)
- if err != nil {
- t.Fatalf("marshaling to JSON failed: %v", err)
- }
- got := string(data)
- expected := `{"includes":["a","b"]}`
- if got != expected {
- t.Errorf("expected\n%s\n,got:\n%s", expected, got)
- }
- }
- func TestFetchSourceContextFetchSourceWithIncludesAndExcludes(t *testing.T) {
- builder := NewFetchSourceContext(true).Include("a", "b").Exclude("c")
- src, err := builder.Source()
- if err != nil {
- t.Fatal(err)
- }
- data, err := json.Marshal(src)
- if err != nil {
- t.Fatalf("marshaling to JSON failed: %v", err)
- }
- got := string(data)
- expected := `{"excludes":["c"],"includes":["a","b"]}`
- if got != expected {
- t.Errorf("expected\n%s\n,got:\n%s", expected, got)
- }
- }
- func TestFetchSourceContextQueryDefaults(t *testing.T) {
- builder := NewFetchSourceContext(true)
- values := builder.Query()
- got := values.Encode()
- expected := ""
- if got != expected {
- t.Errorf("expected %q; got: %q", expected, got)
- }
- }
- func TestFetchSourceContextQueryNoFetchSource(t *testing.T) {
- builder := NewFetchSourceContext(false)
- values := builder.Query()
- got := values.Encode()
- expected := "_source=false"
- if got != expected {
- t.Errorf("expected %q; got: %q", expected, got)
- }
- }
- func TestFetchSourceContextQueryFetchSourceWithIncludesAndExcludes(t *testing.T) {
- builder := NewFetchSourceContext(true).Include("a", "b").Exclude("c")
- values := builder.Query()
- got := values.Encode()
- expected := "_source_exclude=c&_source_include=a%2Cb"
- if got != expected {
- t.Errorf("expected %q; got: %q", expected, got)
- }
- }
|