code.gitea.io/gitea@v1.22.3/modules/indexer/issues/meilisearch/meilisearch_test.go (about)

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