github.com/weaviate/weaviate@v1.24.6/test/acceptance/batch_request_endpoints/graphql_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 batch_request_endpoints
    13  
    14  // TODO: These tests add little value, they only test one specific error case,
    15  // but don't test any happy path at all. This should probably be removed or
    16  // fixed. However, they do at least assure that the order of return values matches
    17  // the order of input values.
    18  
    19  // Acceptance tests for the batch GraphQL endpoint
    20  
    21  // There is a helper struct called GraphQLResult that helps to navigate through the output,
    22  // a query generator and a few helper functions to access the GraphQL endpoint.
    23  // See the end of this file for more details on how those work.
    24  
    25  import (
    26  	"fmt"
    27  	"testing"
    28  
    29  	"github.com/stretchr/testify/assert"
    30  	graphql_client "github.com/weaviate/weaviate/client/graphql"
    31  	"github.com/weaviate/weaviate/entities/models"
    32  	"github.com/weaviate/weaviate/test/helper"
    33  )
    34  
    35  // TODO: change this test to simulate a successful query response when the test dataset is implemented.
    36  
    37  // Check if batch results are returned in the correct order by comparing result equality to predefined outcomes.
    38  // This includes testing whether individual requests and the batch request are handled correctly
    39  func gqlResultsOrder(t *testing.T) {
    40  	queryOneName := "testQuery"
    41  	queryTwoName := "testQuery2"
    42  	expectedResult := "Syntax Error GraphQL request (1:1) Unexpected Name \"%s\"\n\n1: %s\n   ^\n"
    43  
    44  	// perform the query
    45  	gqlResponse, err := queryBatchEndpoint(t, nil)
    46  	if err != nil {
    47  		t.Fatalf("The returned schema is not an JSON object: %v", err)
    48  	}
    49  	// check if the batch response contains two batched responses
    50  	assert.Equal(t, 2, len(gqlResponse))
    51  
    52  	// check if the error message matches the expected outcome (and are therefore returned in the correct order)
    53  	if len(gqlResponse) == 2 {
    54  		responseOne := gqlResponse[0].Errors[0].Message
    55  		responseTwo := gqlResponse[1].Errors[0].Message
    56  
    57  		fullExpectedOutcomeOne := fmt.Sprintf(expectedResult, queryOneName, queryOneName)
    58  		assert.Equal(t, fullExpectedOutcomeOne, responseOne)
    59  
    60  		fullExpectedOutcomeTwo := fmt.Sprintf(expectedResult, queryTwoName, queryTwoName)
    61  		assert.Equal(t, fullExpectedOutcomeTwo, responseTwo)
    62  	}
    63  }
    64  
    65  func gqlMalformedRequest(t *testing.T) {
    66  	vars := []int{1, 2, 3}
    67  	expectedResult := "422: expected map[string]interface{}, received %v"
    68  
    69  	// perform the query
    70  	gqlResponse, err := queryBatchEndpoint(t, vars)
    71  	if err != nil {
    72  		t.Fatalf("The returned schema is not an JSON object: %v", err)
    73  	}
    74  	// check if the batch response contains two batched responses
    75  	assert.Equal(t, 2, len(gqlResponse))
    76  
    77  	fullExpectedOutcome := fmt.Sprintf(expectedResult, vars)
    78  	assert.Equal(t, fullExpectedOutcome, gqlResponse[0].Errors[0].Message)
    79  	assert.Equal(t, fullExpectedOutcome, gqlResponse[1].Errors[0].Message)
    80  }
    81  
    82  // Helper functions
    83  // TODO: change this to a successful query when the test dataset is implemented. Make sure to implement a query returning 3 or more elements.
    84  // Perform a batch GraphQL query
    85  func queryBatchEndpoint(t *testing.T, vars interface{}) (models.GraphQLResponses, error) {
    86  	query1 := &models.GraphQLQuery{OperationName: "testQuery", Query: "testQuery", Variables: vars}
    87  	query2 := &models.GraphQLQuery{OperationName: "testQuery2", Query: "testQuery2", Variables: vars}
    88  
    89  	queries := models.GraphQLQueries{query1, query2}
    90  
    91  	params := graphql_client.NewGraphqlBatchParams().WithBody(queries)
    92  	response, err := helper.Client(t).Graphql.GraphqlBatch(params, nil)
    93  	if err != nil {
    94  		return nil, err
    95  	}
    96  
    97  	return response.Payload, nil
    98  }