github.com/weaviate/weaviate@v1.24.6/modules/text2vec-contextionary/additional/additional_arguments_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 additional
    13  
    14  import (
    15  	"testing"
    16  
    17  	"github.com/stretchr/testify/assert"
    18  	"github.com/tailor-inc/graphql"
    19  )
    20  
    21  func TestNearestNeighborsField(t *testing.T) {
    22  	t.Run("should generate nearestNeighbors argument properly", func(t *testing.T) {
    23  		// given
    24  		classname := "Class"
    25  
    26  		// when
    27  		nearestNeighbors := additionalNearestNeighborsField(classname)
    28  
    29  		// then
    30  		// the built graphQL field needs to support this structure:
    31  		// Type: {
    32  		//   neighbors: {
    33  		//     concept: "c1",
    34  		//     distance: 0.8
    35  		//   }
    36  		// }
    37  		assert.NotNil(t, nearestNeighbors)
    38  		assert.Equal(t, "ClassAdditionalNearestNeighbors", nearestNeighbors.Type.Name())
    39  		assert.NotNil(t, nearestNeighbors.Type)
    40  		nearestNeighborsObject, nearestNeighborsObjectOK := nearestNeighbors.Type.(*graphql.Object)
    41  		assert.True(t, nearestNeighborsObjectOK)
    42  		assert.Equal(t, 1, len(nearestNeighborsObject.Fields()))
    43  		neighbors, neighborsOK := nearestNeighborsObject.Fields()["neighbors"]
    44  		assert.True(t, neighborsOK)
    45  		neighborsList, neighborsListOK := neighbors.Type.(*graphql.List)
    46  		assert.True(t, neighborsListOK)
    47  		neighborsListObjects, neighborsListObjectsOK := neighborsList.OfType.(*graphql.Object)
    48  		assert.True(t, neighborsListObjectsOK)
    49  		assert.Equal(t, 2, len(neighborsListObjects.Fields()))
    50  		assert.NotNil(t, neighborsListObjects.Fields()["concept"])
    51  		assert.NotNil(t, neighborsListObjects.Fields()["distance"])
    52  	})
    53  }
    54  
    55  func TestFeatureProjectionField(t *testing.T) {
    56  	t.Run("should generate featureProjection argument properly", func(t *testing.T) {
    57  		// given
    58  		classname := "Class"
    59  
    60  		// when
    61  		featureProjection := additionalFeatureProjectionField(classname)
    62  
    63  		// then
    64  		// the built graphQL field needs to support this structure:
    65  		// Args: {
    66  		//   algorithm: "a",
    67  		//   dimensions: 1,
    68  		//   learningRate: 2,
    69  		//   iterations: 3,
    70  		//   perplexity: 4
    71  		// }
    72  		// Type: {
    73  		//   vector: [0, 1]
    74  		// }
    75  		assert.NotNil(t, featureProjection)
    76  		assert.Equal(t, "ClassAdditionalFeatureProjection", featureProjection.Type.Name())
    77  		assert.NotNil(t, featureProjection.Args)
    78  		assert.Equal(t, 5, len(featureProjection.Args))
    79  		assert.NotNil(t, featureProjection.Args["algorithm"])
    80  		assert.NotNil(t, featureProjection.Args["dimensions"])
    81  		assert.NotNil(t, featureProjection.Args["learningRate"])
    82  		assert.NotNil(t, featureProjection.Args["iterations"])
    83  		assert.NotNil(t, featureProjection.Args["perplexity"])
    84  		featureProjectionObject, featureProjectionObjectOK := featureProjection.Type.(*graphql.Object)
    85  		assert.True(t, featureProjectionObjectOK)
    86  		assert.Equal(t, 1, len(featureProjectionObject.Fields()))
    87  		assert.NotNil(t, featureProjectionObject.Fields()["vector"])
    88  	})
    89  }
    90  
    91  func TestSemanticPathField(t *testing.T) {
    92  	t.Run("should generate semanticPath argument properly", func(t *testing.T) {
    93  		// given
    94  		classname := "Class"
    95  
    96  		// when
    97  		semanticPath := additionalSemanticPathField(classname)
    98  
    99  		// then
   100  		// the built graphQL field needs to support this structure:
   101  		// Type: {
   102  		//   path: [
   103  		//     {
   104  		//       concept: "c1",
   105  		//       distanceToQuery: 0.1,
   106  		//       distanceToResult: 0.2,
   107  		//       distanceToNext: 0.3,
   108  		//       distanceToPrevious: 0.4,
   109  		//     }
   110  		// }
   111  		assert.NotNil(t, semanticPath)
   112  		assert.Equal(t, "ClassAdditionalSemanticPath", semanticPath.Type.Name())
   113  		semanticPathObject, semanticPathObjectOK := semanticPath.Type.(*graphql.Object)
   114  		assert.True(t, semanticPathObjectOK)
   115  		assert.Equal(t, 1, len(semanticPathObject.Fields()))
   116  		assert.NotNil(t, semanticPathObject.Fields()["path"])
   117  		semanticPathObjectList, semanticPathObjectListOK := semanticPathObject.Fields()["path"].Type.(*graphql.List)
   118  		assert.True(t, semanticPathObjectListOK)
   119  		semanticPathObjectListObjects, semanticPathObjectListOK := semanticPathObjectList.OfType.(*graphql.Object)
   120  		assert.True(t, semanticPathObjectListOK)
   121  		assert.Equal(t, 5, len(semanticPathObjectListObjects.Fields()))
   122  		assert.NotNil(t, semanticPathObjectListObjects.Fields()["concept"])
   123  		assert.NotNil(t, semanticPathObjectListObjects.Fields()["distanceToQuery"])
   124  		assert.NotNil(t, semanticPathObjectListObjects.Fields()["distanceToResult"])
   125  		assert.NotNil(t, semanticPathObjectListObjects.Fields()["distanceToNext"])
   126  		assert.NotNil(t, semanticPathObjectListObjects.Fields()["distanceToPrevious"])
   127  	})
   128  }
   129  
   130  func TestNearestInterpretationField(t *testing.T) {
   131  	t.Run("should generate interpretation argument properly", func(t *testing.T) {
   132  		// given
   133  		classname := "Class"
   134  
   135  		// when
   136  		interpretation := additionalInterpretationField(classname)
   137  
   138  		// then
   139  		// the built graphQL field needs to support this structure:
   140  		// Type: {
   141  		//   source: [
   142  		//     {
   143  		//       concept: "c1",
   144  		//       weight: 0.1,
   145  		//       occurrence: 0.2,
   146  		//     }
   147  		// }
   148  		assert.NotNil(t, interpretation)
   149  		assert.Equal(t, "ClassAdditionalInterpretation", interpretation.Type.Name())
   150  		interpretationObject, interpretationObjectOK := interpretation.Type.(*graphql.Object)
   151  		assert.True(t, interpretationObjectOK)
   152  		assert.Equal(t, 1, len(interpretationObject.Fields()))
   153  		assert.NotNil(t, interpretationObject.Fields()["source"])
   154  		interpretationObjectList, interpretationObjectListOK := interpretationObject.Fields()["source"].Type.(*graphql.List)
   155  		assert.True(t, interpretationObjectListOK)
   156  		interpretationObjectListObjects, interpretationObjectListObjectsOK := interpretationObjectList.OfType.(*graphql.Object)
   157  		assert.True(t, interpretationObjectListObjectsOK)
   158  		assert.Equal(t, 3, len(interpretationObjectListObjects.Fields()))
   159  		assert.NotNil(t, interpretationObjectListObjects.Fields()["concept"])
   160  		assert.NotNil(t, interpretationObjectListObjects.Fields()["weight"])
   161  		assert.NotNil(t, interpretationObjectListObjects.Fields()["occurrence"])
   162  	})
   163  }