validate_test.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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 TestValidate(t *testing.T) {
  10. client := setupTestClientAndCreateIndex(t)
  11. tweet1 := tweet{User: "olivere", Message: "Welcome to Golang and Elasticsearch."}
  12. // Add a document
  13. indexResult, err := client.Index().
  14. Index(testIndexName).
  15. Type("tweet").
  16. BodyJson(&tweet1).
  17. Refresh("true").
  18. Do(context.TODO())
  19. if err != nil {
  20. t.Fatal(err)
  21. }
  22. if indexResult == nil {
  23. t.Errorf("expected result to be != nil; got: %v", indexResult)
  24. }
  25. query := NewTermQuery("user", "olivere")
  26. explain := true
  27. valid, err := client.Validate(testIndexName).Type("tweet").Explain(&explain).Query(query).Do(context.TODO())
  28. if err != nil {
  29. t.Fatal(err)
  30. }
  31. if valid == nil {
  32. t.Fatal("expected to return an validation")
  33. }
  34. if !valid.Valid {
  35. t.Errorf("expected valid to be %v; got: %v", true, valid.Valid)
  36. }
  37. invalidQuery := NewTermQuery("", false)
  38. valid, err = client.Validate(testIndexName).Type("tweet").Query(invalidQuery).Do(context.TODO())
  39. if err != nil {
  40. t.Fatal(err)
  41. }
  42. if valid == nil {
  43. t.Fatal("expected to return an validation")
  44. }
  45. if valid.Valid {
  46. t.Errorf("expected valid to be %v; got: %v", false, valid.Valid)
  47. }
  48. }