github.com/weaviate/weaviate@v1.24.6/test/modules/many-modules/many_modules_sanity_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 test 13 14 import ( 15 "encoding/json" 16 "fmt" 17 "testing" 18 19 "github.com/go-openapi/strfmt" 20 "github.com/stretchr/testify/assert" 21 "github.com/stretchr/testify/require" 22 "github.com/weaviate/weaviate/test/helper" 23 graphqlhelper "github.com/weaviate/weaviate/test/helper/graphql" 24 "github.com/weaviate/weaviate/test/helper/sample-schema/books" 25 "github.com/weaviate/weaviate/test/helper/sample-schema/multishard" 26 ) 27 28 func manyModulesTests(endpoint string) func(t *testing.T) { 29 return func(t *testing.T) { 30 helper.SetupClient(endpoint) 31 32 t.Run("check enabled modules", func(t *testing.T) { 33 meta := helper.GetMeta(t) 34 require.NotNil(t, meta) 35 36 expectedModuleNames := []string{ 37 "generative-cohere", "generative-palm", "generative-openai", "generative-aws", "generative-anyscale", 38 "text2vec-cohere", "text2vec-contextionary", "text2vec-openai", "text2vec-huggingface", 39 "text2vec-palm", "text2vec-aws", "text2vec-transformers", "qna-openai", "reranker-cohere", 40 "text2vec-voyageai", 41 } 42 43 modules, ok := meta.Modules.(map[string]interface{}) 44 require.True(t, ok) 45 assert.Len(t, modules, len(expectedModuleNames)) 46 47 moduleNames := []string{} 48 for name := range modules { 49 moduleNames = append(moduleNames, name) 50 } 51 assert.ElementsMatch(t, expectedModuleNames, moduleNames) 52 }) 53 54 booksClass := books.ClassContextionaryVectorizer() 55 multiShardClass := multishard.ClassContextionaryVectorizer() 56 helper.CreateClass(t, booksClass) 57 helper.CreateClass(t, multiShardClass) 58 defer helper.DeleteClass(t, booksClass.Class) 59 defer helper.DeleteClass(t, multiShardClass.Class) 60 61 t.Run("import data", func(t *testing.T) { 62 for _, book := range books.Objects() { 63 helper.CreateObject(t, book) 64 helper.AssertGetObjectEventually(t, book.Class, book.ID) 65 } 66 for _, multishard := range multishard.Objects() { 67 helper.CreateObject(t, multishard) 68 helper.AssertGetObjectEventually(t, multishard.Class, multishard.ID) 69 } 70 }) 71 72 t.Run("sanity checks", func(t *testing.T) { 73 concepts := []string{ 74 "Frank", "Herbert", "Dune", "Book", "Project", "Hail", "Mary", 75 "The Lord of the Ice Garden", "Ice Garden", "science", "fiction", 76 "fantasy novel", "novelist", 77 } 78 checkResults := func(query string) { 79 result := graphqlhelper.AssertGraphQL(t, helper.RootAuth, query) 80 books := result.Get("Get", "Books").AsSlice() 81 require.True(t, len(books) > 0) 82 results, ok := books[0].(map[string]interface{}) 83 require.True(t, ok) 84 assert.True(t, results["title"] != nil) 85 } 86 87 t.Run("nearText queries", func(t *testing.T) { 88 queryTemplate := ` 89 { 90 Get { 91 Books( 92 nearText: { 93 concepts: ["%s"] 94 } 95 ){ 96 title 97 } 98 } 99 }` 100 for _, concept := range concepts { 101 checkResults(fmt.Sprintf(queryTemplate, concept)) 102 } 103 }) 104 t.Run("nearObject queries", func(t *testing.T) { 105 queryTemplate := ` 106 { 107 Get { 108 Books( 109 nearObject: { 110 id: "%s" 111 } 112 ){ 113 title 114 } 115 } 116 }` 117 ids := []strfmt.UUID{books.Dune, books.ProjectHailMary, books.TheLordOfTheIceGarden} 118 for _, id := range ids { 119 checkResults(fmt.Sprintf(queryTemplate, id)) 120 } 121 }) 122 t.Run("nearVector queries", func(t *testing.T) { 123 getVectors := func() []string { 124 query := ` 125 { 126 Get { 127 Books(limit: 3){ _additional{ vector } } 128 } 129 }` 130 result := graphqlhelper.AssertGraphQL(t, helper.RootAuth, query) 131 books := result.Get("Get", "Books").AsSlice() 132 require.True(t, len(books) == 3) 133 vectors := make([]string, 3) 134 for i := 0; i < 3; i++ { 135 results, ok := books[i].(map[string]interface{}) 136 require.True(t, ok) 137 vector, ok := results["_additional"].(map[string]interface{})["vector"].([]interface{}) 138 require.True(t, ok) 139 vec, err := json.Marshal(vector) 140 require.Nil(t, err) 141 vectors[i] = string(vec) 142 } 143 return vectors 144 } 145 vectors := getVectors() 146 queryTemplate := ` 147 { 148 Get { 149 Books( 150 nearVector: { 151 vector: %s 152 } 153 ){ 154 title 155 } 156 } 157 }` 158 for _, vector := range vectors { 159 checkResults(fmt.Sprintf(queryTemplate, vector)) 160 } 161 }) 162 t.Run("hybrid queries", func(t *testing.T) { 163 queryTemplate := ` 164 { 165 Get { 166 Books( 167 hybrid: { 168 query: "%s" 169 } 170 ){ 171 title 172 } 173 } 174 }` 175 for _, concept := range concepts { 176 checkResults(fmt.Sprintf(queryTemplate, concept)) 177 } 178 }) 179 t.Run("bm25 queries", func(t *testing.T) { 180 queryTemplate := ` 181 { 182 Get { 183 Books( 184 bm25:{ 185 query: "%s" 186 } 187 ){ 188 title 189 } 190 } 191 }` 192 for _, concept := range []string{"Frank", "Project Hail Mary", "Dune", "Project", "Hail", "Mary"} { 193 checkResults(fmt.Sprintf(queryTemplate, concept)) 194 } 195 }) 196 }) 197 } 198 }