suggester_context_test.go 1.6 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. "encoding/json"
  7. "testing"
  8. )
  9. func TestContextSuggesterSource(t *testing.T) {
  10. s := NewContextSuggester("place_suggestion").
  11. Prefix("tim").
  12. Field("suggest")
  13. src, err := s.Source(true)
  14. if err != nil {
  15. t.Fatal(err)
  16. }
  17. data, err := json.Marshal(src)
  18. if err != nil {
  19. t.Fatalf("marshaling to JSON failed: %v", err)
  20. }
  21. got := string(data)
  22. expected := `{"place_suggestion":{"prefix":"tim","completion":{"field":"suggest"}}}`
  23. if got != expected {
  24. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  25. }
  26. }
  27. func TestContextSuggesterSourceWithMultipleContexts(t *testing.T) {
  28. s := NewContextSuggester("place_suggestion").
  29. Prefix("tim").
  30. Field("suggest").
  31. ContextQueries(
  32. NewSuggesterCategoryQuery("place_type", "cafe", "restaurants"),
  33. )
  34. src, err := s.Source(true)
  35. if err != nil {
  36. t.Fatal(err)
  37. }
  38. data, err := json.Marshal(src)
  39. if err != nil {
  40. t.Fatalf("marshaling to JSON failed: %v", err)
  41. }
  42. got := string(data)
  43. // Due to the randomization of dictionary key, we could actually have two different valid expected outcomes
  44. expected := `{"place_suggestion":{"prefix":"tim","completion":{"context":{"place_type":[{"context":"cafe"},{"context":"restaurants"}]},"field":"suggest"}}}`
  45. if got != expected {
  46. expected := `{"place_suggestion":{"prefix":"tim","completion":{"context":{"place_type":[{"context":"restaurants"},{"context":"cafe"}]},"field":"suggest"}}}`
  47. if got != expected {
  48. t.Errorf("expected %s\n,got:\n%s", expected, got)
  49. }
  50. }
  51. }