github.com/weaviate/weaviate@v1.24.6/modules/text-spellcheck/clients/spellcheck_meta_test.go (about)

     1  //                           _       _
     2  // __      _____  __ ___   ___  __ _| |_ ___
     3  // \ \ /\ / / _ \/ _` \ \ / / |/ _` | __/ _ \
     4  //  \ V  V /  __/ (_| |\ V /| | (_| | ||  __/
     5  //   \_/\_/ \___|\__,_| \_/ |_|\__,_|\__\___|
     6  //
     7  //  Copyright © 2016 - 2024 Weaviate B.V. All rights reserved.
     8  //
     9  //  CONTACT: hello@weaviate.io
    10  //
    11  
    12  package clients
    13  
    14  import (
    15  	"net/http"
    16  	"net/http/httptest"
    17  	"testing"
    18  	"time"
    19  
    20  	"github.com/stretchr/testify/assert"
    21  )
    22  
    23  func TestGetMeta(t *testing.T) {
    24  	t.Run("when the server is providing meta", func(t *testing.T) {
    25  		server := httptest.NewServer(&testMetaHandler{t: t})
    26  		defer server.Close()
    27  		c := New(server.URL, 0, nullLogger())
    28  		meta, err := c.MetaInfo()
    29  
    30  		assert.Nil(t, err)
    31  		assert.NotNil(t, meta)
    32  		metaModel := meta["model"]
    33  		assert.True(t, metaModel != nil)
    34  		model, modelOK := metaModel.(map[string]interface{})
    35  		assert.True(t, modelOK)
    36  		assert.True(t, model["name"] != nil)
    37  	})
    38  }
    39  
    40  type testMetaHandler struct {
    41  	t *testing.T
    42  	// the test handler will report as not ready before the time has passed
    43  	readyTime time.Time
    44  }
    45  
    46  func (f *testMetaHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
    47  	assert.Equal(f.t, "/meta", r.URL.String())
    48  	assert.Equal(f.t, http.MethodGet, r.Method)
    49  
    50  	if time.Since(f.readyTime) < 0 {
    51  		w.WriteHeader(http.StatusServiceUnavailable)
    52  	}
    53  
    54  	w.Write([]byte(f.metaInfo()))
    55  }
    56  
    57  func (f *testMetaHandler) metaInfo() string {
    58  	return `{
    59      "model": {
    60          "name": "pyspellchecker"
    61      }
    62  }`
    63  }