github.com/weaviate/weaviate@v1.24.6/modules/text2vec-contextionary/vectorizer/inspector_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 vectorizer 13 14 import ( 15 "context" 16 "fmt" 17 "testing" 18 19 "github.com/stretchr/testify/assert" 20 "github.com/stretchr/testify/require" 21 "github.com/weaviate/weaviate/entities/models" 22 ) 23 24 func TestInspector(t *testing.T) { 25 type test struct { 26 name string 27 input string 28 expectedErr error 29 expectedOutput *models.C11yWordsResponse 30 } 31 32 tests := []test{ 33 { 34 name: "invalid input", 35 input: "i don't like pizza", 36 expectedErr: fmt.Errorf("invalid word input: words must only contain unicode letters and digits"), 37 }, 38 { 39 name: "single valid word", 40 input: "pizza", 41 expectedOutput: &models.C11yWordsResponse{ 42 IndividualWords: []*models.C11yWordsResponseIndividualWordsItems0{ 43 { 44 Present: true, 45 Word: "pizza", 46 Info: &models.C11yWordsResponseIndividualWordsItems0Info{ 47 Vector: []float32{3, 2, 1, 0}, 48 NearestNeighbors: []*models.C11yNearestNeighborsItems0{ 49 { 50 Distance: 0.1, 51 Word: "word1", 52 }, 53 { 54 Distance: 0.2, 55 Word: "word2", 56 }, 57 }, 58 }, 59 }, 60 }, 61 }, 62 }, 63 { 64 name: "single valid word containing numbers", 65 input: "pi55a", 66 expectedOutput: &models.C11yWordsResponse{ 67 IndividualWords: []*models.C11yWordsResponseIndividualWordsItems0{ 68 { 69 Present: true, 70 Word: "pi55a", 71 Info: &models.C11yWordsResponseIndividualWordsItems0Info{ 72 Vector: []float32{3, 2, 1, 0}, 73 NearestNeighbors: []*models.C11yNearestNeighborsItems0{ 74 { 75 Distance: 0.1, 76 Word: "word1", 77 }, 78 { 79 Distance: 0.2, 80 Word: "word2", 81 }, 82 }, 83 }, 84 }, 85 }, 86 }, 87 }, 88 { 89 name: "concatenated words", 90 input: "pizzaBakerMakerShaker", 91 expectedOutput: &models.C11yWordsResponse{ 92 ConcatenatedWord: &models.C11yWordsResponseConcatenatedWord{ 93 ConcatenatedWord: "pizzaBakerMakerShaker", 94 SingleWords: []string{"pizza", "baker", "maker", "shaker"}, 95 ConcatenatedVector: []float32{0, 1, 2, 3}, 96 ConcatenatedNearestNeighbors: []*models.C11yNearestNeighborsItems0{ 97 { 98 Distance: 0.1, 99 Word: "word1", 100 }, 101 { 102 Distance: 0.2, 103 Word: "word2", 104 }, 105 }, 106 }, 107 IndividualWords: []*models.C11yWordsResponseIndividualWordsItems0{ 108 { 109 Present: true, 110 Word: "pizza", 111 Info: &models.C11yWordsResponseIndividualWordsItems0Info{ 112 Vector: []float32{3, 2, 1, 0}, 113 NearestNeighbors: []*models.C11yNearestNeighborsItems0{ 114 { 115 Distance: 0.1, 116 Word: "word1", 117 }, 118 { 119 Distance: 0.2, 120 Word: "word2", 121 }, 122 }, 123 }, 124 }, 125 { 126 Present: true, 127 Word: "baker", 128 Info: &models.C11yWordsResponseIndividualWordsItems0Info{ 129 Vector: []float32{3, 2, 1, 0}, 130 NearestNeighbors: []*models.C11yNearestNeighborsItems0{ 131 { 132 Distance: 0.1, 133 Word: "word1", 134 }, 135 { 136 Distance: 0.2, 137 Word: "word2", 138 }, 139 }, 140 }, 141 }, 142 { 143 Present: true, 144 Word: "maker", 145 Info: &models.C11yWordsResponseIndividualWordsItems0Info{ 146 Vector: []float32{3, 2, 1, 0}, 147 NearestNeighbors: []*models.C11yNearestNeighborsItems0{ 148 { 149 Distance: 0.1, 150 Word: "word1", 151 }, 152 { 153 Distance: 0.2, 154 Word: "word2", 155 }, 156 }, 157 }, 158 }, 159 { 160 Present: true, 161 Word: "shaker", 162 Info: &models.C11yWordsResponseIndividualWordsItems0Info{ 163 Vector: []float32{3, 2, 1, 0}, 164 NearestNeighbors: []*models.C11yNearestNeighborsItems0{ 165 { 166 Distance: 0.1, 167 Word: "word1", 168 }, 169 { 170 Distance: 0.2, 171 Word: "word2", 172 }, 173 }, 174 }, 175 }, 176 }, 177 }, 178 }, 179 } 180 181 for _, test := range tests { 182 t.Run(test.name, func(t *testing.T) { 183 client := &fakeClient{} 184 i := NewInspector(client) 185 res, err := i.GetWords(context.Background(), test.input) 186 require.Equal(t, err, test.expectedErr) 187 assert.Equal(t, res, test.expectedOutput) 188 }) 189 } 190 }