github.com/weaviate/weaviate@v1.24.6/modules/text2vec-contextionary/concepts/rest_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 concepts 13 14 import ( 15 "context" 16 "io" 17 "net/http" 18 "net/http/httptest" 19 "testing" 20 21 "github.com/pkg/errors" 22 "github.com/stretchr/testify/assert" 23 "github.com/stretchr/testify/require" 24 "github.com/weaviate/weaviate/entities/models" 25 ) 26 27 func TestHandlers(t *testing.T) { 28 insp := newFakeInspector() 29 h := NewRESTHandlers(insp) 30 31 t.Run("without a concept", func(t *testing.T) { 32 insp.reset() 33 r := httptest.NewRequest("GET", "/", nil) 34 w := httptest.NewRecorder() 35 h.Handler().ServeHTTP(w, r) 36 37 res := w.Result() 38 defer res.Body.Close() 39 assert.Equal(t, http.StatusNotFound, res.StatusCode) 40 }) 41 42 t.Run("without any errors", func(t *testing.T) { 43 insp.reset() 44 r := httptest.NewRequest("GET", "/my-concept", nil) 45 w := httptest.NewRecorder() 46 h.Handler().ServeHTTP(w, r) 47 48 res := w.Result() 49 defer res.Body.Close() 50 json, err := io.ReadAll(res.Body) 51 require.Nil(t, err) 52 expected := `{"individualWords":[{` + 53 `"info":{"vector":[0.1,0.2]},"present":true,"word":"my-concept"}]}` 54 55 assert.Equal(t, http.StatusOK, res.StatusCode) 56 assert.Equal(t, expected, string(json)) 57 }) 58 59 t.Run("without an error from the UC", func(t *testing.T) { 60 insp.reset() 61 insp.err = errors.Errorf("invalid input") 62 r := httptest.NewRequest("GET", "/my-concept", nil) 63 w := httptest.NewRecorder() 64 h.Handler().ServeHTTP(w, r) 65 66 res := w.Result() 67 defer res.Body.Close() 68 json, err := io.ReadAll(res.Body) 69 require.Nil(t, err) 70 expected := `{"error":[{"message":"invalid input"}]}` 71 72 assert.Equal(t, http.StatusBadRequest, res.StatusCode) 73 assert.Equal(t, expected, string(json)) 74 }) 75 } 76 77 type fakeInspector struct { 78 err error 79 } 80 81 func (f *fakeInspector) reset() { 82 f.err = nil 83 } 84 85 func (f *fakeInspector) GetWords(ctx context.Context, 86 concept string, 87 ) (*models.C11yWordsResponse, error) { 88 return &models.C11yWordsResponse{ 89 IndividualWords: []*models.C11yWordsResponseIndividualWordsItems0{ 90 { 91 Present: true, 92 Word: concept, 93 Info: &models.C11yWordsResponseIndividualWordsItems0Info{ 94 Vector: []float32{0.1, 0.2}, 95 }, 96 }, 97 }, 98 }, f.err 99 } 100 101 func newFakeInspector() *fakeInspector { 102 return &fakeInspector{} 103 }