exists_test.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. "context"
  7. "testing"
  8. )
  9. func TestExists(t *testing.T) {
  10. client := setupTestClientAndCreateIndexAndAddDocs(t) //, SetTraceLog(log.New(os.Stdout, "", 0)))
  11. exists, err := client.Exists().Index(testIndexName).Type("comment").Id("1").Parent("tweet").Do(context.TODO())
  12. if err != nil {
  13. t.Fatal(err)
  14. }
  15. if !exists {
  16. t.Fatal("expected document to exist")
  17. }
  18. }
  19. func TestExistsValidate(t *testing.T) {
  20. client := setupTestClient(t)
  21. // No index -> fail with error
  22. res, err := NewExistsService(client).Type("tweet").Id("1").Do(context.TODO())
  23. if err == nil {
  24. t.Fatalf("expected Delete to fail without index name")
  25. }
  26. if res != false {
  27. t.Fatalf("expected result to be false; got: %v", res)
  28. }
  29. // No type -> fail with error
  30. res, err = NewExistsService(client).Index(testIndexName).Id("1").Do(context.TODO())
  31. if err == nil {
  32. t.Fatalf("expected Delete to fail without index name")
  33. }
  34. if res != false {
  35. t.Fatalf("expected result to be false; got: %v", res)
  36. }
  37. // No id -> fail with error
  38. res, err = NewExistsService(client).Index(testIndexName).Type("tweet").Do(context.TODO())
  39. if err == nil {
  40. t.Fatalf("expected Delete to fail without index name")
  41. }
  42. if res != false {
  43. t.Fatalf("expected result to be false; got: %v", res)
  44. }
  45. }