github.com/weaviate/weaviate@v1.24.6/test/acceptance/vector_distances/graphql_helper_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/runtime" 20 "github.com/weaviate/weaviate/client/graphql" 21 graphql_client "github.com/weaviate/weaviate/client/graphql" 22 "github.com/weaviate/weaviate/entities/models" 23 "github.com/weaviate/weaviate/test/helper" 24 ) 25 26 type GraphQLResult struct { 27 Result interface{} 28 } 29 30 // Perform a GraphQL request 31 func QueryGraphQL(t *testing.T, auth runtime.ClientAuthInfoWriterFunc, operation string, query string, variables map[string]interface{}) (*models.GraphQLResponse, error) { 32 var vars interface{} = variables 33 params := graphql_client.NewGraphqlPostParams().WithBody(&models.GraphQLQuery{OperationName: operation, Query: query, Variables: vars}) 34 response, err := helper.Client(t).Graphql.GraphqlPost(params, nil) 35 if err != nil { 36 return nil, err 37 } 38 39 return response.Payload, nil 40 } 41 42 // Perform a GraphQL request and call fatal on failure 43 func QueryGraphQLOrFatal(t *testing.T, auth runtime.ClientAuthInfoWriterFunc, operation string, query string, variables map[string]interface{}) *models.GraphQLResponse { 44 response, err := QueryGraphQL(t, auth, operation, query, variables) 45 if err != nil { 46 parsedErr, ok := err.(*graphql.GraphqlPostUnprocessableEntity) 47 if !ok { 48 t.Fatalf("Expected the query to succeed, but failed due to: %#v", err) 49 } 50 t.Fatalf("Expected the query to succeed, but failed with unprocessable entity: %v", parsedErr.Payload.Error[0]) 51 } 52 return response 53 } 54 55 // Perform a query and assert that it is successful 56 func AssertGraphQL(t *testing.T, auth runtime.ClientAuthInfoWriterFunc, query string) *GraphQLResult { 57 response := QueryGraphQLOrFatal(t, auth, "", query, nil) 58 59 if len(response.Errors) != 0 { 60 j, _ := json.Marshal(response.Errors) 61 t.Fatal("GraphQL resolved to an error:", string(j)) 62 } 63 64 data := make(map[string]interface{}) 65 66 // get rid of models.JSONData 67 for key, value := range response.Data { 68 data[key] = value 69 } 70 71 return &GraphQLResult{Result: data} 72 } 73 74 // Perform a query and assert that it has errors 75 func ErrorGraphQL(t *testing.T, auth runtime.ClientAuthInfoWriterFunc, query string) []*models.GraphQLError { 76 response := QueryGraphQLOrFatal(t, auth, "", query, nil) 77 78 if len(response.Errors) == 0 { 79 j, _ := json.Marshal(response.Errors) 80 t.Fatal("GraphQL resolved to data:", string(j)) 81 } 82 83 return response.Errors 84 } 85 86 // Drill down in the result 87 func (g GraphQLResult) Get(paths ...string) *GraphQLResult { 88 current := g.Result 89 for _, path := range paths { 90 var ok bool 91 currentAsMap := (current.(map[string]interface{})) 92 current, ok = currentAsMap[path] 93 if !ok { 94 panic(fmt.Sprintf("Cannot get element %s in %#v; result: %#v", path, paths, g.Result)) 95 } 96 } 97 98 return &GraphQLResult{ 99 Result: current, 100 } 101 } 102 103 // Cast the result to a slice 104 func (g *GraphQLResult) AsSlice() []interface{} { 105 return g.Result.([]interface{}) 106 }