github.com/weaviate/weaviate@v1.24.6/test/acceptance/actions/individual_refs_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  // Acceptance tests for objects
    15  
    16  import (
    17  	"testing"
    18  
    19  	"github.com/stretchr/testify/assert"
    20  	"github.com/weaviate/weaviate/client/objects"
    21  	"github.com/weaviate/weaviate/entities/models"
    22  	"github.com/weaviate/weaviate/entities/schema/crossref"
    23  	"github.com/weaviate/weaviate/test/helper"
    24  )
    25  
    26  // run from setup_test.go
    27  func objectReferences(t *testing.T) {
    28  	var (
    29  		class1 = "TestObject"
    30  		class2 = "TestObjectTwo"
    31  	)
    32  	t.Run("can add reference individually", func(t *testing.T) {
    33  		t.Parallel()
    34  
    35  		toPointToUuid := helper.AssertCreateObject(t, "TestObject", map[string]interface{}{})
    36  		helper.AssertGetObjectEventually(t, class1, toPointToUuid)
    37  
    38  		uuid := helper.AssertCreateObject(t, class2, map[string]interface{}{})
    39  
    40  		// Verify that testReferences is empty
    41  		updatedObject := helper.AssertGetObjectEventually(t, class2, uuid)
    42  		updatedSchema := updatedObject.Properties.(map[string]interface{})
    43  		assert.Nil(t, updatedSchema["testReferences"])
    44  
    45  		// Append a property reference
    46  		params := objects.NewObjectsReferencesCreateParams().
    47  			WithID(uuid).
    48  			WithPropertyName("testReferences").
    49  			WithBody(crossref.NewLocalhost(class1, toPointToUuid).SingleRef())
    50  
    51  		updateResp, err := helper.Client(t).Objects.ObjectsReferencesCreate(params, nil)
    52  		helper.AssertRequestOk(t, updateResp, err, nil)
    53  
    54  		checkThunk := func() interface{} {
    55  			resp, err := helper.Client(t).Objects.ObjectsGet(objects.NewObjectsGetParams().WithID(uuid), nil)
    56  			if err != nil {
    57  				t.Log(err)
    58  				return false
    59  			}
    60  
    61  			updatedSchema = resp.Payload.Properties.(map[string]interface{})
    62  			return updatedSchema["testReferences"] != nil
    63  		}
    64  
    65  		helper.AssertEventuallyEqual(t, true, checkThunk)
    66  	})
    67  
    68  	t.Run("can replace all properties", func(t *testing.T) {
    69  		t.Parallel()
    70  
    71  		toPointToUuidFirst := helper.AssertCreateObject(t, "TestObject", map[string]interface{}{})
    72  		toPointToUuidLater := helper.AssertCreateObject(t, "TestObject", map[string]interface{}{})
    73  		helper.AssertGetObjectEventually(t, "TestObject", toPointToUuidFirst)
    74  		helper.AssertGetObjectEventually(t, "TestObject", toPointToUuidLater)
    75  
    76  		uuid := helper.AssertCreateObject(t, "TestObjectTwo", map[string]interface{}{
    77  			"testReferences": models.MultipleRef{
    78  				crossref.NewLocalhost("TestObject", toPointToUuidFirst).SingleRef(),
    79  			},
    80  		})
    81  
    82  		// Verify that testReferences is empty
    83  		updatedObject := helper.AssertGetObjectEventually(t, "TestObjectTwo", uuid)
    84  		updatedSchema := updatedObject.Properties.(map[string]interface{})
    85  		assert.NotNil(t, updatedSchema["testReferences"])
    86  
    87  		// Replace
    88  		params := objects.NewObjectsReferencesUpdateParams().
    89  			WithID(uuid).
    90  			WithPropertyName("testReferences").
    91  			WithBody(models.MultipleRef{
    92  				crossref.NewLocalhost("TestObject", toPointToUuidLater).SingleRef(),
    93  			})
    94  
    95  		updateResp, err := helper.Client(t).Objects.ObjectsReferencesUpdate(params, nil)
    96  		helper.AssertRequestOk(t, updateResp, err, nil)
    97  
    98  		checkThunk := func() interface{} {
    99  			resp, err := helper.Client(t).Objects.ObjectsGet(objects.NewObjectsGetParams().WithID(uuid), nil)
   100  			if err != nil {
   101  				t.Log(err)
   102  				return false
   103  			}
   104  
   105  			updatedSchema = resp.Payload.Properties.(map[string]interface{})
   106  			return updatedSchema["testReferences"] != nil
   107  		}
   108  
   109  		helper.AssertEventuallyEqual(t, true, checkThunk)
   110  	})
   111  
   112  	t.Run("remove property individually", func(t *testing.T) {
   113  		t.Parallel()
   114  
   115  		toPointToUuid := helper.AssertCreateObject(t, "TestObject", map[string]interface{}{})
   116  		helper.AssertGetObjectEventually(t, "TestObject", toPointToUuid)
   117  
   118  		uuid := helper.AssertCreateObject(t, "TestObjectTwo", map[string]interface{}{
   119  			"testReferences": models.MultipleRef{
   120  				crossref.NewLocalhost("TestObject", toPointToUuid).SingleRef(),
   121  			},
   122  		})
   123  
   124  		// Verify that testReferences is not empty
   125  		updatedObject := helper.AssertGetObjectEventually(t, "TestObjectTwo", uuid)
   126  		updatedSchema := updatedObject.Properties.(map[string]interface{})
   127  		assert.NotNil(t, updatedSchema["testReferences"])
   128  
   129  		// Delete a property reference
   130  		params := objects.NewObjectsReferencesDeleteParams().
   131  			WithID(uuid).
   132  			WithPropertyName("testReferences").
   133  			WithBody(
   134  				crossref.NewLocalhost("TestObject", toPointToUuid).SingleRef(),
   135  			)
   136  
   137  		updateResp, err := helper.Client(t).Objects.ObjectsReferencesDelete(params, nil)
   138  		helper.AssertRequestOk(t, updateResp, err, nil)
   139  
   140  		checkThunk := func() interface{} {
   141  			resp, err := helper.Client(t).Objects.ObjectsGet(objects.NewObjectsGetParams().WithID(uuid), nil)
   142  			if err != nil {
   143  				t.Log(err)
   144  				return false
   145  			}
   146  
   147  			refs := resp.Payload.Properties.(map[string]interface{})["testReferences"]
   148  
   149  			if refs == nil {
   150  				return true
   151  			}
   152  
   153  			refsSlice, ok := refs.([]interface{})
   154  			if ok {
   155  				return len(refsSlice) == 0
   156  			}
   157  
   158  			// neither nil, nor a list
   159  			t.Logf("prop %s was neither nil nor a list after deleting, instead we got %#v", "testReferences", refs)
   160  			t.Fail()
   161  
   162  			return false
   163  		}
   164  
   165  		helper.AssertEventuallyEqual(t, true, checkThunk)
   166  	})
   167  }