github.com/weaviate/weaviate@v1.24.6/test/modules/img2vec-neural/neural_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/base64"
    16  	"fmt"
    17  	"io"
    18  	"os"
    19  	"testing"
    20  
    21  	"github.com/stretchr/testify/assert"
    22  	"github.com/stretchr/testify/require"
    23  	"github.com/weaviate/weaviate/entities/models"
    24  	"github.com/weaviate/weaviate/entities/schema"
    25  	"github.com/weaviate/weaviate/test/helper"
    26  	graphqlhelper "github.com/weaviate/weaviate/test/helper/graphql"
    27  )
    28  
    29  func Test_Img2VecNeural(t *testing.T) {
    30  	helper.SetupClient(os.Getenv(weaviateEndpoint))
    31  
    32  	fashionItemClass := &models.Class{
    33  		Class:      "FashionItem",
    34  		Vectorizer: "img2vec-neural",
    35  		ModuleConfig: map[string]interface{}{
    36  			"img2vec-neural": map[string]interface{}{
    37  				"imageFields": []string{"image"},
    38  			},
    39  		},
    40  		Properties: []*models.Property{
    41  			{
    42  				Name:     "image",
    43  				DataType: schema.DataTypeBlob.PropString(),
    44  			},
    45  			{
    46  				Name:         "description",
    47  				DataType:     schema.DataTypeText.PropString(),
    48  				Tokenization: models.PropertyTokenizationWhitespace,
    49  			},
    50  		},
    51  	}
    52  
    53  	helper.CreateClass(t, fashionItemClass)
    54  	defer helper.DeleteClass(t, fashionItemClass.Class)
    55  
    56  	getBase64EncodedTestImage := func() string {
    57  		image, err := os.Open("./data/pixel.png")
    58  		require.Nil(t, err)
    59  		require.NotNil(t, image)
    60  		content, err := io.ReadAll(image)
    61  		require.Nil(t, err)
    62  		return base64.StdEncoding.EncodeToString(content)
    63  	}
    64  
    65  	t.Run("import data", func(t *testing.T) {
    66  		base64Image := getBase64EncodedTestImage()
    67  		obj := &models.Object{
    68  			Class: fashionItemClass.Class,
    69  			Properties: map[string]interface{}{
    70  				"image":       base64Image,
    71  				"description": "A single black pixel",
    72  			},
    73  		}
    74  		helper.CreateObject(t, obj)
    75  	})
    76  
    77  	t.Run("perform nearImage query", func(t *testing.T) {
    78  		queryTemplate := `
    79  		{
    80  			Get {
    81  				FashionItem(
    82  					nearImage: {
    83  						image: "%s"
    84  					}
    85  				){
    86  					image
    87  					description
    88  					_additional{vector}
    89  				}
    90  			}
    91  		}`
    92  		query := fmt.Sprintf(queryTemplate, getBase64EncodedTestImage())
    93  		result := graphqlhelper.AssertGraphQL(t, helper.RootAuth, query)
    94  		fashionItems := result.Get("Get", "FashionItem").AsSlice()
    95  		require.True(t, len(fashionItems) > 0)
    96  		item, ok := fashionItems[0].(map[string]interface{})
    97  		require.True(t, ok)
    98  		assert.NotNil(t, item["image"])
    99  		assert.NotNil(t, item["description"])
   100  		vector := item["_additional"].(map[string]interface{})["vector"]
   101  		assert.NotNil(t, vector)
   102  	})
   103  }