github.com/weaviate/weaviate@v1.24.6/test/acceptance/objects/additional_props_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  	"testing"
    17  
    18  	"github.com/stretchr/testify/require"
    19  	"github.com/weaviate/weaviate/client/objects"
    20  	"github.com/weaviate/weaviate/entities/models"
    21  	"github.com/weaviate/weaviate/test/helper"
    22  )
    23  
    24  func searchNeighbors(t *testing.T) {
    25  	listParams := objects.NewObjectsListParams().WithInclude(ptString("nearestNeighbors"))
    26  	res, err := helper.Client(t).Objects.ObjectsList(listParams, nil)
    27  	require.Nil(t, err, "should not error")
    28  
    29  	extractNeighbor := func(in *models.Object) []interface{} {
    30  		// marshalling to JSON and back into an untyped map to make sure we assert
    31  		// on the actual JSON structure. This way if we accidentally change the
    32  		// goswagger generation so it affects both the client and the server in the
    33  		// same way, this test should catch it
    34  		b, err := json.Marshal(in)
    35  		require.Nil(t, err)
    36  
    37  		var untyped map[string]interface{}
    38  		err = json.Unmarshal(b, &untyped)
    39  		require.Nil(t, err)
    40  
    41  		return untyped["additional"].(map[string]interface{})["nearestNeighbors"].(map[string]interface{})["neighbors"].([]interface{})
    42  	}
    43  
    44  	validateNeighbors(t, extractNeighbor(res.Payload.Objects[0]), extractNeighbor(res.Payload.Objects[1]))
    45  }
    46  
    47  func featureProjection(t *testing.T) {
    48  	listParams := objects.NewObjectsListParams().WithInclude(ptString("featureProjection"))
    49  	res, err := helper.Client(t).Objects.ObjectsList(listParams, nil)
    50  	require.Nil(t, err, "should not error")
    51  
    52  	extractProjection := func(in *models.Object) []interface{} {
    53  		// marshalling to JSON and back into an untyped map to make sure we assert
    54  		// on the actual JSON structure. This way if we accidentally change the
    55  		// goswagger generation so it affects both the client and the server in the
    56  		// same way, this test should catch it
    57  		b, err := json.Marshal(in)
    58  		require.Nil(t, err)
    59  
    60  		var untyped map[string]interface{}
    61  		err = json.Unmarshal(b, &untyped)
    62  		require.Nil(t, err)
    63  
    64  		return untyped["additional"].(map[string]interface{})["featureProjection"].(map[string]interface{})["vector"].([]interface{})
    65  	}
    66  
    67  	validateProjections(t, 2, extractProjection(res.Payload.Objects[0]), extractProjection(res.Payload.Objects[1]))
    68  }
    69  
    70  func ptString(in string) *string {
    71  	return &in
    72  }
    73  
    74  func validateNeighbors(t *testing.T, neighborsGroups ...[]interface{}) {
    75  	for i, group := range neighborsGroups {
    76  		if len(group) == 0 {
    77  			t.Fatalf("group %d: length of neighbors is 0", i)
    78  		}
    79  
    80  		for j, neighbor := range group {
    81  			asMap := neighbor.(map[string]interface{})
    82  			if len(asMap["concept"].(string)) == 0 {
    83  				t.Fatalf("group %d: element %d: concept has length 0", i, j)
    84  			}
    85  		}
    86  	}
    87  }
    88  
    89  func validateProjections(t *testing.T, dims int, vectors ...[]interface{}) {
    90  	for _, vector := range vectors {
    91  		if len(vector) != dims {
    92  			t.Fatalf("expected feature projection vector to have length 3, got: %d", len(vector))
    93  		}
    94  	}
    95  }