github.com/weaviate/weaviate@v1.24.6/test/modules/multi2vec-clip/clip_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  	"os"
    18  	"testing"
    19  
    20  	"github.com/stretchr/testify/assert"
    21  	"github.com/stretchr/testify/require"
    22  	"github.com/weaviate/weaviate/test/helper"
    23  	graphqlhelper "github.com/weaviate/weaviate/test/helper/graphql"
    24  	"github.com/weaviate/weaviate/test/helper/sample-schema/books"
    25  )
    26  
    27  func Test_CLIP(t *testing.T) {
    28  	helper.SetupClient(os.Getenv(weaviateEndpoint))
    29  	booksClass := books.ClassCLIPVectorizer()
    30  	helper.CreateClass(t, booksClass)
    31  	defer helper.DeleteClass(t, booksClass.Class)
    32  
    33  	t.Run("add data to Books schema", func(t *testing.T) {
    34  		for _, book := range books.Objects() {
    35  			helper.CreateObject(t, book)
    36  			helper.AssertGetObjectEventually(t, book.Class, book.ID)
    37  		}
    38  	})
    39  
    40  	tests := []struct {
    41  		concept       string
    42  		expectedTitle string
    43  	}{
    44  		{
    45  			concept:       "Dune",
    46  			expectedTitle: "Dune",
    47  		},
    48  		{
    49  			concept:       "three",
    50  			expectedTitle: "The Lord of the Ice Garden",
    51  		},
    52  	}
    53  	for _, tt := range tests {
    54  		t.Run("query Books data with nearText", func(t *testing.T) {
    55  			result := graphqlhelper.AssertGraphQL(t, helper.RootAuth, fmt.Sprintf(`
    56  				{
    57  					Get {
    58  						Books(
    59  							limit: 1
    60  							nearText: {
    61  								concepts: ["%v"]
    62  								distance: 0.5
    63  							}
    64  						){
    65  							title
    66  							_additional {
    67  								distance
    68  							}
    69  						}
    70  					}
    71  				}
    72  			`, tt.concept))
    73  			books := result.Get("Get", "Books").AsSlice()
    74  			require.Len(t, books, 1)
    75  			title := books[0].(map[string]interface{})["title"]
    76  			assert.Equal(t, tt.expectedTitle, title)
    77  			distance := books[0].(map[string]interface{})["_additional"].(map[string]interface{})["distance"].(json.Number)
    78  			assert.NotNil(t, distance)
    79  			dist, err := distance.Float64()
    80  			require.Nil(t, err)
    81  			assert.Greater(t, dist, 0.0)
    82  			assert.LessOrEqual(t, dist, 0.03)
    83  		})
    84  	}
    85  }