github.com/weaviate/weaviate@v1.24.6/test/acceptance/batch_request_endpoints/batch_journey_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 batch_request_endpoints
    13  
    14  import (
    15  	"fmt"
    16  	"testing"
    17  
    18  	"github.com/go-openapi/strfmt"
    19  	"github.com/google/uuid"
    20  	"github.com/stretchr/testify/assert"
    21  	"github.com/stretchr/testify/require"
    22  	"github.com/weaviate/weaviate/client/batch"
    23  	"github.com/weaviate/weaviate/entities/models"
    24  	"github.com/weaviate/weaviate/entities/schema"
    25  	"github.com/weaviate/weaviate/test/helper"
    26  )
    27  
    28  func batchJourney(t *testing.T) {
    29  	sourcesSize := 10
    30  	targetsSize := 3
    31  	var sources []*models.Object
    32  	var targets []*models.Object
    33  
    34  	t.Run("create some data", func(t *testing.T) {
    35  		sources = make([]*models.Object, sourcesSize)
    36  		for i := range sources {
    37  			sources[i] = &models.Object{
    38  				Class: "BulkTestSource",
    39  				ID:    mustNewUUID(),
    40  				Properties: map[string]interface{}{
    41  					"name": fmt.Sprintf("source%d", i),
    42  				},
    43  			}
    44  		}
    45  
    46  		targets = make([]*models.Object, targetsSize)
    47  		for i := range targets {
    48  			targets[i] = &models.Object{
    49  				Class: "BulkTest",
    50  				ID:    mustNewUUID(),
    51  				Properties: map[string]interface{}{
    52  					"name": fmt.Sprintf("target%d", i),
    53  				},
    54  			}
    55  		}
    56  	})
    57  
    58  	t.Run("import all data in batch", func(t *testing.T) {
    59  		params := batch.NewBatchObjectsCreateParams().WithBody(
    60  			batch.BatchObjectsCreateBody{
    61  				Objects: append(sources, targets...),
    62  			},
    63  		)
    64  		res, err := helper.Client(t).Batch.BatchObjectsCreate(params, nil)
    65  		require.Nil(t, err)
    66  
    67  		for _, elem := range res.Payload {
    68  			assert.Nil(t, elem.Result.Errors)
    69  		}
    70  	})
    71  
    72  	t.Run("set one cref each from each source to all targets", func(t *testing.T) {
    73  		body := make([]*models.BatchReference, sourcesSize*targetsSize)
    74  		for i := range sources {
    75  			for j := range targets {
    76  				index := i*targetsSize + j
    77  				body[index] = &models.BatchReference{
    78  					From: strfmt.URI(
    79  						fmt.Sprintf("weaviate://localhost/BulkTestSource/%s/ref", sources[i].ID)),
    80  					To: strfmt.URI(fmt.Sprintf("weaviate://localhost/%s", targets[j].ID)),
    81  				}
    82  			}
    83  		}
    84  		params := batch.NewBatchReferencesCreateParams().WithBody(body)
    85  		res, err := helper.Client(t).Batch.BatchReferencesCreate(params, nil)
    86  		require.Nil(t, err)
    87  
    88  		for _, elem := range res.Payload {
    89  			assert.Nil(t, elem.Result.Errors)
    90  		}
    91  	})
    92  
    93  	t.Run("verify using GraphQL", func(t *testing.T) {
    94  		result := AssertGraphQL(t, helper.RootAuth, `
    95  		{  Get { BulkTestSource { ref { ... on BulkTest { name }  } } } }
    96  		`)
    97  		items := result.Get("Get", "BulkTestSource").AsSlice()
    98  		assert.Len(t, items, sourcesSize)
    99  		for _, obj := range items {
   100  			refs := obj.(map[string]interface{})["ref"].([]interface{})
   101  			assert.Len(t, refs, targetsSize)
   102  		}
   103  	})
   104  }
   105  
   106  func mustNewUUID() strfmt.UUID {
   107  	return strfmt.UUID(uuid.New().String())
   108  }
   109  
   110  func Test_BugFlakyResultCountWithVectorSearch(t *testing.T) {
   111  	className := "FlakyBugTestClass"
   112  
   113  	// since this bug occurs only in around 1 in 25 cases, we run the test
   114  	// multiple times to increase the chance we're running into it
   115  	amount := 50
   116  	for i := 0; i < amount; i++ {
   117  		t.Run("create schema", func(t *testing.T) {
   118  			createObjectClass(t, &models.Class{
   119  				Class: className,
   120  				Properties: []*models.Property{
   121  					{
   122  						Name:         "title",
   123  						DataType:     schema.DataTypeText.PropString(),
   124  						Tokenization: models.PropertyTokenizationWhitespace,
   125  					},
   126  					{
   127  						Name:         "url",
   128  						DataType:     schema.DataTypeText.PropString(),
   129  						Tokenization: models.PropertyTokenizationWhitespace,
   130  					},
   131  					{
   132  						Name:     "wordCount",
   133  						DataType: []string{"int"},
   134  					},
   135  				},
   136  			})
   137  		})
   138  
   139  		t.Run("create and import some data", func(t *testing.T) {
   140  			objects := []*models.Object{
   141  				{
   142  					Class: className,
   143  					Properties: map[string]interface{}{
   144  						"title":     "article 1",
   145  						"url":       "http://articles.local/my-article-1",
   146  						"wordCount": 60,
   147  					},
   148  				},
   149  				{
   150  					Class: className,
   151  					Properties: map[string]interface{}{
   152  						"title":     "article 2",
   153  						"url":       "http://articles.local/my-article-2",
   154  						"wordCount": 40,
   155  					},
   156  				},
   157  				{
   158  					Class: className,
   159  					Properties: map[string]interface{}{
   160  						"title":     "article 3",
   161  						"url":       "http://articles.local/my-article-3",
   162  						"wordCount": 600,
   163  					},
   164  				},
   165  			}
   166  
   167  			params := batch.NewBatchObjectsCreateParams().WithBody(
   168  				batch.BatchObjectsCreateBody{
   169  					Objects: objects,
   170  				},
   171  			)
   172  			res, err := helper.Client(t).Batch.BatchObjectsCreate(params, nil)
   173  			require.Nil(t, err)
   174  
   175  			for _, elem := range res.Payload {
   176  				assert.Nil(t, elem.Result.Errors)
   177  			}
   178  		})
   179  
   180  		t.Run("verify using GraphQL", func(t *testing.T) {
   181  			result := AssertGraphQL(t, helper.RootAuth, fmt.Sprintf(`
   182  		{  Get { %s(nearText: {concepts: ["news"]}) {  
   183  				wordCount title url 
   184  		} } }
   185  		`, className))
   186  			items := result.Get("Get", className).AsSlice()
   187  			assert.Len(t, items, 3)
   188  		})
   189  
   190  		t.Run("cleanup", func(t *testing.T) {
   191  			deleteObjectClass(t, className)
   192  		})
   193  	}
   194  }