github.com/weaviate/weaviate@v1.24.6/modules/multi2vec-clip/clients/vectorizer_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 "context" 16 "encoding/json" 17 "net/http" 18 "net/http/httptest" 19 "testing" 20 21 "github.com/stretchr/testify/assert" 22 "github.com/stretchr/testify/require" 23 "github.com/weaviate/weaviate/modules/multi2vec-clip/ent" 24 ) 25 26 func TestVectorize(t *testing.T) { 27 t.Run("when the response is successful", func(t *testing.T) { 28 server := httptest.NewServer(&testVectorizeHandler{ 29 t: t, 30 res: vecResponse{ 31 TextVectors: [][]float32{ 32 {0, 1, 2}, 33 }, 34 ImageVectors: [][]float32{ 35 {1, 2, 3}, 36 }, 37 }, 38 }) 39 defer server.Close() 40 c := New(server.URL, 0, nullLogger()) 41 res, err := c.Vectorize(context.Background(), []string{"hello"}, 42 []string{"image-encoding"}, ent.VectorizationConfig{}) 43 44 assert.Nil(t, err) 45 assert.Equal(t, &ent.VectorizationResult{ 46 TextVectors: [][]float32{ 47 {0, 1, 2}, 48 }, 49 ImageVectors: [][]float32{ 50 {1, 2, 3}, 51 }, 52 }, res) 53 }) 54 55 t.Run("when the server has a an error", func(t *testing.T) { 56 server := httptest.NewServer(&testVectorizeHandler{ 57 t: t, 58 res: vecResponse{ 59 Error: "some error from the server", 60 }, 61 }) 62 defer server.Close() 63 c := New(server.URL, 0, nullLogger()) 64 _, err := c.Vectorize(context.Background(), []string{"hello"}, 65 []string{"image-encoding"}, ent.VectorizationConfig{}) 66 67 require.NotNil(t, err) 68 assert.Contains(t, err.Error(), "some error from the server") 69 }) 70 } 71 72 type testVectorizeHandler struct { 73 t *testing.T 74 // the test handler will report as not ready before the time has passed 75 res vecResponse 76 } 77 78 func (f *testVectorizeHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { 79 assert.Equal(f.t, "/vectorize", r.URL.String()) 80 assert.Equal(f.t, http.MethodPost, r.Method) 81 82 if f.res.Error != "" { 83 w.WriteHeader(500) 84 } 85 jsonBytes, _ := json.Marshal(f.res) 86 w.Write(jsonBytes) 87 }