indices_create_test.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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 TestIndicesLifecycle(t *testing.T) {
  10. client := setupTestClient(t)
  11. // Create index
  12. createIndex, err := client.CreateIndex(testIndexName).Do(context.TODO())
  13. if err != nil {
  14. t.Fatal(err)
  15. }
  16. if !createIndex.Acknowledged {
  17. t.Errorf("expected IndicesCreateResult.Acknowledged %v; got %v", true, createIndex.Acknowledged)
  18. }
  19. // Check if index exists
  20. indexExists, err := client.IndexExists(testIndexName).Do(context.TODO())
  21. if err != nil {
  22. t.Fatal(err)
  23. }
  24. if !indexExists {
  25. t.Fatalf("index %s should exist, but doesn't\n", testIndexName)
  26. }
  27. // Delete index
  28. deleteIndex, err := client.DeleteIndex(testIndexName).Do(context.TODO())
  29. if err != nil {
  30. t.Fatal(err)
  31. }
  32. if !deleteIndex.Acknowledged {
  33. t.Errorf("expected DeleteIndexResult.Acknowledged %v; got %v", true, deleteIndex.Acknowledged)
  34. }
  35. // Check if index exists
  36. indexExists, err = client.IndexExists(testIndexName).Do(context.TODO())
  37. if err != nil {
  38. t.Fatal(err)
  39. }
  40. if indexExists {
  41. t.Fatalf("index %s should not exist, but does\n", testIndexName)
  42. }
  43. }
  44. func TestIndicesCreateValidate(t *testing.T) {
  45. client := setupTestClient(t)
  46. // No index name -> fail with error
  47. res, err := NewIndicesCreateService(client).Body(testMapping).Do(context.TODO())
  48. if err == nil {
  49. t.Fatalf("expected IndicesCreate to fail without index name")
  50. }
  51. if res != nil {
  52. t.Fatalf("expected result to be == nil; got: %v", res)
  53. }
  54. }