field_caps_test.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. "encoding/json"
  8. "net/url"
  9. "reflect"
  10. "sort"
  11. "testing"
  12. )
  13. func TestFieldCapsURLs(t *testing.T) {
  14. tests := []struct {
  15. Service *FieldCapsService
  16. ExpectedPath string
  17. ExpectedParams url.Values
  18. }{
  19. {
  20. Service: &FieldCapsService{},
  21. ExpectedPath: "/_field_caps",
  22. ExpectedParams: url.Values{},
  23. },
  24. {
  25. Service: &FieldCapsService{
  26. index: []string{"index1", "index2"},
  27. },
  28. ExpectedPath: "/index1%2Cindex2/_field_caps",
  29. ExpectedParams: url.Values{},
  30. },
  31. {
  32. Service: &FieldCapsService{
  33. index: []string{"index_*"},
  34. pretty: true,
  35. },
  36. ExpectedPath: "/index_%2A/_field_caps",
  37. ExpectedParams: url.Values{"pretty": []string{"true"}},
  38. },
  39. }
  40. for _, test := range tests {
  41. gotPath, gotParams, err := test.Service.buildURL()
  42. if err != nil {
  43. t.Fatalf("expected no error; got: %v", err)
  44. }
  45. if gotPath != test.ExpectedPath {
  46. t.Errorf("expected URL path = %q; got: %q", test.ExpectedPath, gotPath)
  47. }
  48. if gotParams.Encode() != test.ExpectedParams.Encode() {
  49. t.Errorf("expected URL params = %v; got: %v", test.ExpectedParams, gotParams)
  50. }
  51. }
  52. }
  53. func TestFieldCapsRequestSerialize(t *testing.T) {
  54. req := &FieldCapsRequest{
  55. Fields: []string{"creation_date", "answer_count"},
  56. }
  57. data, err := json.Marshal(req)
  58. if err != nil {
  59. t.Fatalf("marshaling to JSON failed: %v", err)
  60. }
  61. got := string(data)
  62. expected := `{"fields":["creation_date","answer_count"]}`
  63. if got != expected {
  64. t.Fatalf("expected\n%s\n,got:\n%s", expected, got)
  65. }
  66. }
  67. func TestFieldCapsRequestDeserialize(t *testing.T) {
  68. body := `{
  69. "fields" : ["creation_date", "answer_count"]
  70. }`
  71. var request FieldCapsRequest
  72. if err := json.Unmarshal([]byte(body), &request); err != nil {
  73. t.Fatalf("unexpected error during unmarshalling: %v", err)
  74. }
  75. sort.Sort(lexicographically{request.Fields})
  76. expectedFields := []string{"answer_count", "creation_date"}
  77. if !reflect.DeepEqual(request.Fields, expectedFields) {
  78. t.Fatalf("expected fields to be %v, got %v", expectedFields, request.Fields)
  79. }
  80. }
  81. func TestFieldCapsResponseUnmarshalling(t *testing.T) {
  82. clusterStats := `{
  83. "_shards": {
  84. "total": 1,
  85. "successful": 1,
  86. "failed": 0
  87. },
  88. "fields": {
  89. "creation_date": {
  90. "type": "date",
  91. "searchable": true,
  92. "aggregatable": true,
  93. "indices": ["index1", "index2"],
  94. "non_searchable_indices": null,
  95. "non_aggregatable_indices": null
  96. },
  97. "answer": {
  98. "type": "keyword",
  99. "searchable": true,
  100. "aggregatable": true
  101. }
  102. }
  103. }`
  104. var resp FieldCapsResponse
  105. if err := json.Unmarshal([]byte(clusterStats), &resp); err != nil {
  106. t.Errorf("unexpected error during unmarshalling: %v", err)
  107. }
  108. caps, ok := resp.Fields["creation_date"]
  109. if !ok {
  110. t.Errorf("expected creation_date to be in the fields map, didn't find it")
  111. }
  112. if want, have := true, caps.Searchable; want != have {
  113. t.Errorf("expected creation_date searchable to be %v, got %v", want, have)
  114. }
  115. if want, have := true, caps.Aggregatable; want != have {
  116. t.Errorf("expected creation_date aggregatable to be %v, got %v", want, have)
  117. }
  118. if want, have := []string{"index1", "index2"}, caps.Indices; !reflect.DeepEqual(want, have) {
  119. t.Errorf("expected creation_date indices to be %v, got %v", want, have)
  120. }
  121. }
  122. func TestFieldCaps123(t *testing.T) {
  123. client := setupTestClientAndCreateIndexAndAddDocs(t)
  124. // client := setupTestClientAndCreateIndexAndAddDocs(t, SetTraceLog(log.New(os.Stdout, "", 0)))
  125. res, err := client.FieldCaps("_all").Fields("user", "message", "retweets", "created").Pretty(true).Do(context.TODO())
  126. if err != nil {
  127. t.Fatalf("expected no error; got: %v", err)
  128. }
  129. if res == nil {
  130. t.Fatalf("expected response; got: %v", res)
  131. }
  132. }