github.com/weaviate/weaviate@v1.24.6/test/acceptance/batch_request_endpoints/thingscreate_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  // TODO: change this test to simulate a successful query response when the test dataset is implemented.
    13  
    14  // Acceptance tests for the batch ThingsCreate endpoint
    15  package batch_request_endpoints
    16  
    17  import (
    18  	"fmt"
    19  	"testing"
    20  
    21  	"github.com/stretchr/testify/assert"
    22  	"github.com/weaviate/weaviate/client/batch"
    23  	"github.com/weaviate/weaviate/entities/models"
    24  	"github.com/weaviate/weaviate/test/helper"
    25  )
    26  
    27  // Test if batching is working correctly. Sends an OK batch containing two batched requests that refer to non-existing classes.
    28  // The expected outcome is a 200 batch response containing two batched responses. These batched responses should both contain errors.
    29  func TestBatchThingsCreateResultsOrder(t *testing.T) {
    30  	t.Parallel()
    31  
    32  	classOneName := "ItIsExtremelyUnlikelyThatThisClassActuallyExistsButJustToBeSureHereAreSomeRandomNumbers12987825624398509861298409782539802434516542"
    33  	classTwoName := "ItIsExtremelyUnlikelyThatThisClassActuallyExistsButJustToBeSureHereAreSomeRandomNumbers12987825624398509861298409782539802434516541"
    34  	expectedResult := "class '%s' not present in schema"
    35  
    36  	// generate actioncreate content
    37  	object1 := &models.Object{
    38  		Class: classOneName,
    39  		Properties: map[string]interface{}{
    40  			"testString": "Test string",
    41  		},
    42  	}
    43  	object2 := &models.Object{
    44  		Class: classTwoName,
    45  		Properties: map[string]interface{}{
    46  			"testWholeNumber": 1,
    47  		},
    48  	}
    49  
    50  	testFields := "ALL"
    51  
    52  	// generate request body
    53  	params := batch.NewBatchObjectsCreateParams().WithBody(batch.BatchObjectsCreateBody{
    54  		Objects: []*models.Object{object1, object2},
    55  		Fields:  []*string{&testFields},
    56  	})
    57  
    58  	// perform the request
    59  	resp, err := helper.BatchClient(t).BatchObjectsCreate(params, nil)
    60  
    61  	// ensure that the response is OK
    62  	helper.AssertRequestOk(t, resp, err, func() {
    63  		thingsCreateResponse := resp.Payload
    64  
    65  		// check if the batch response contains two batched responses
    66  		assert.Equal(t, 2, len(thingsCreateResponse))
    67  
    68  		// check if the error message matches the expected outcome (and are therefore returned in the correct order)
    69  		if len(thingsCreateResponse) == 2 {
    70  			responseOne := thingsCreateResponse[0].Result.Errors.Error[0].Message
    71  			responseTwo := thingsCreateResponse[1].Result.Errors.Error[0].Message
    72  
    73  			fullExpectedOutcomeOne := fmt.Sprintf(expectedResult, classOneName)
    74  			assert.Contains(t, responseOne, fullExpectedOutcomeOne)
    75  
    76  			fullExpectedOutcomeTwo := fmt.Sprintf(expectedResult, classTwoName)
    77  			assert.Contains(t, responseTwo, fullExpectedOutcomeTwo)
    78  		}
    79  	})
    80  }