gitea源码

meilisearch_test.go 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. // Copyright 2023 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package meilisearch
  4. import (
  5. "fmt"
  6. "net/http"
  7. "os"
  8. "testing"
  9. "time"
  10. "code.gitea.io/gitea/modules/indexer/issues/internal"
  11. "code.gitea.io/gitea/modules/indexer/issues/internal/tests"
  12. "code.gitea.io/gitea/modules/json"
  13. "github.com/meilisearch/meilisearch-go"
  14. "github.com/stretchr/testify/assert"
  15. "github.com/stretchr/testify/require"
  16. )
  17. func TestMeilisearchIndexer(t *testing.T) {
  18. // The meilisearch instance started by pull-db-tests.yml > test-unit > services > meilisearch
  19. url := "http://meilisearch:7700"
  20. key := "" // auth has been disabled in test environment
  21. if os.Getenv("CI") == "" {
  22. // Make it possible to run tests against a local meilisearch instance
  23. url = os.Getenv("TEST_MEILISEARCH_URL")
  24. if url == "" {
  25. t.Skip("TEST_MEILISEARCH_URL not set and not running in CI")
  26. return
  27. }
  28. key = os.Getenv("TEST_MEILISEARCH_KEY")
  29. }
  30. require.Eventually(t, func() bool {
  31. resp, err := http.Get(url)
  32. return err == nil && resp.StatusCode == http.StatusOK
  33. }, time.Minute, time.Second, "Expected meilisearch to be up")
  34. indexer := NewIndexer(url, key, fmt.Sprintf("test_meilisearch_indexer_%d", time.Now().Unix()))
  35. defer indexer.Close()
  36. tests.TestIndexer(t, indexer)
  37. }
  38. func TestConvertHits(t *testing.T) {
  39. convert := func(d any) []byte {
  40. b, _ := json.Marshal(d)
  41. return b
  42. }
  43. _, err := convertHits(&meilisearch.SearchResponse{
  44. Hits: []meilisearch.Hit{
  45. {
  46. "aa": convert(1),
  47. "bb": convert(2),
  48. "cc": convert(3),
  49. "dd": convert(4),
  50. },
  51. },
  52. })
  53. assert.ErrorIs(t, err, ErrMalformedResponse)
  54. validResponse := &meilisearch.SearchResponse{
  55. Hits: []meilisearch.Hit{
  56. {
  57. "id": convert(float64(11)),
  58. "title": convert("a title"),
  59. "content": convert("issue body with no match"),
  60. "comments": convert([]any{"hey whats up?", "I'm currently bowling", "nice"}),
  61. },
  62. {
  63. "id": convert(float64(22)),
  64. "title": convert("Bowling as title"),
  65. "content": convert(""),
  66. "comments": convert([]any{}),
  67. },
  68. {
  69. "id": convert(float64(33)),
  70. "title": convert("Bowl-ing as fuzzy match"),
  71. "content": convert(""),
  72. "comments": convert([]any{}),
  73. },
  74. },
  75. }
  76. hits, err := convertHits(validResponse)
  77. assert.NoError(t, err)
  78. assert.Equal(t, []internal.Match{{ID: 11}, {ID: 22}, {ID: 33}}, hits)
  79. }
  80. func TestDoubleQuoteKeyword(t *testing.T) {
  81. assert.Empty(t, doubleQuoteKeyword(""))
  82. assert.Equal(t, `"a" "b" "c"`, doubleQuoteKeyword("a b c"))
  83. assert.Equal(t, `"a" "d" "g"`, doubleQuoteKeyword("a d g"))
  84. assert.Equal(t, `"a" "d" "g"`, doubleQuoteKeyword("a d g"))
  85. assert.Equal(t, `"a" "d" "g"`, doubleQuoteKeyword(`a "" "d" """g`))
  86. }