github.com/weaviate/weaviate@v1.24.6/test/acceptance/actions/update_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  	"encoding/json"
    18  	"fmt"
    19  	"testing"
    20  
    21  	"github.com/davecgh/go-spew/spew"
    22  	"github.com/weaviate/weaviate/client/objects"
    23  	"github.com/weaviate/weaviate/entities/models"
    24  	"github.com/weaviate/weaviate/test/helper"
    25  )
    26  
    27  // run from setup_test.go
    28  func updateObjectsDeprecated(t *testing.T) {
    29  	t.Run("update and set number", func(t *testing.T) {
    30  		uuid := helper.AssertCreateObject(t, "TestObject", map[string]interface{}{})
    31  		helper.AssertGetObjectEventually(t, "TestObject", uuid)
    32  
    33  		schema := models.PropertySchema(map[string]interface{}{
    34  			"testNumber": 41.0,
    35  		})
    36  
    37  		update := models.Object{}
    38  		update.Properties = schema
    39  		update.Class = "TestObject"
    40  		update.ID = uuid
    41  
    42  		params := objects.NewObjectsUpdateParams().WithID(uuid).WithBody(&update)
    43  		updateResp, err := helper.Client(t).Objects.ObjectsUpdate(params, nil)
    44  		helper.AssertRequestOk(t, updateResp, err, nil)
    45  
    46  		actualThunk := func() interface{} {
    47  			updatedObject := helper.AssertGetObject(t, update.Class, uuid)
    48  			updatedSchema := updatedObject.Properties.(map[string]interface{})
    49  			if updatedSchema["testNumber"] == nil {
    50  				return nil
    51  			}
    52  			num, _ := updatedSchema["testNumber"].(json.Number).Float64()
    53  			return num
    54  		}
    55  		helper.AssertEventuallyEqual(t, 41.0, actualThunk)
    56  	})
    57  
    58  	t.Run("update and set string", func(t *testing.T) {
    59  		uuid := helper.AssertCreateObject(t, "TestObject", map[string]interface{}{})
    60  		helper.AssertGetObjectEventually(t, "TestObject", uuid)
    61  
    62  		schema := models.PropertySchema(map[string]interface{}{
    63  			"testString": "wibbly wobbly",
    64  		})
    65  
    66  		update := models.Object{}
    67  		update.Properties = schema
    68  		update.Class = "TestObject"
    69  		update.ID = uuid
    70  
    71  		params := objects.NewObjectsUpdateParams().WithID(uuid).WithBody(&update)
    72  		updateResp, err := helper.Client(t).Objects.ObjectsUpdate(params, nil)
    73  		helper.AssertRequestOk(t, updateResp, err, nil)
    74  
    75  		actualThunk := func() interface{} {
    76  			updatedObject := helper.AssertGetObject(t, update.Class, uuid)
    77  			updatedSchema := updatedObject.Properties.(map[string]interface{})
    78  			return updatedSchema["testString"]
    79  		}
    80  		helper.AssertEventuallyEqual(t, "wibbly wobbly", actualThunk)
    81  	})
    82  
    83  	t.Run("update and set bool", func(t *testing.T) {
    84  		t.Parallel()
    85  		uuid := helper.AssertCreateObject(t, "TestObject", map[string]interface{}{})
    86  		helper.AssertGetObjectEventually(t, "TestObject", uuid)
    87  
    88  		schema := models.PropertySchema(map[string]interface{}{
    89  			"testTrueFalse": true,
    90  		})
    91  
    92  		update := models.Object{}
    93  		update.Properties = schema
    94  		update.Class = "TestObject"
    95  		update.ID = uuid
    96  
    97  		params := objects.NewObjectsUpdateParams().WithID(uuid).WithBody(&update)
    98  		updateResp, err := helper.Client(t).Objects.ObjectsUpdate(params, nil)
    99  
   100  		helper.AssertRequestOk(t, updateResp, err, nil)
   101  
   102  		actualThunk := func() interface{} {
   103  			updatedObject := helper.AssertGetObject(t, update.Class, uuid)
   104  			updatedSchema := updatedObject.Properties.(map[string]interface{})
   105  			return updatedSchema["testTrueFalse"]
   106  		}
   107  		helper.AssertEventuallyEqual(t, true, actualThunk)
   108  	})
   109  
   110  	t.Run("can patch object with cref", func(t *testing.T) {
   111  		thingToRefID := helper.AssertCreateObject(t, "ObjectTestThing", nil)
   112  		helper.AssertGetObjectEventually(t, "ObjectTestThing", thingToRefID)
   113  		objectID := helper.AssertCreateObject(t, "TestObject", nil)
   114  		helper.AssertGetObjectEventually(t, "TestObject", objectID)
   115  
   116  		merge := &models.Object{
   117  			Class: "TestObject",
   118  			Properties: map[string]interface{}{
   119  				"testReference": []interface{}{
   120  					map[string]interface{}{
   121  						"beacon": fmt.Sprintf("weaviate://localhost/%s", thingToRefID),
   122  					},
   123  				},
   124  			},
   125  		}
   126  
   127  		// Now to try to link
   128  		params := objects.NewObjectsPatchParams().
   129  			WithBody(merge).
   130  			WithID(objectID)
   131  		patchResp, err := helper.Client(t).Objects.ObjectsPatch(params, nil)
   132  		spew.Dump(err)
   133  		helper.AssertRequestOk(t, patchResp, err, nil)
   134  
   135  		actualThunk := func() interface{} {
   136  			patchedObject := helper.AssertGetObject(t, merge.Class, objectID)
   137  
   138  			rawRef, ok := patchedObject.Properties.(map[string]interface{})["testReference"]
   139  			if !ok {
   140  				return nil
   141  			}
   142  
   143  			refsSlice, ok := rawRef.([]interface{})
   144  			if !ok {
   145  				t.Logf("found the ref prop, but it was not a slice, but %T", refsSlice)
   146  				t.Fail()
   147  			}
   148  
   149  			if len(refsSlice) != 1 {
   150  				t.Logf("expected ref slice to have one element, but got: %d", len(refsSlice))
   151  				t.Fail()
   152  			}
   153  
   154  			refMap, ok := refsSlice[0].(map[string]interface{})
   155  			if !ok {
   156  				t.Logf("found the ref element, but it was not a map, but %T", refsSlice[0])
   157  				t.Fail()
   158  			}
   159  
   160  			return refMap["beacon"]
   161  		}
   162  
   163  		helper.AssertEventuallyEqual(t, fmt.Sprintf("weaviate://localhost/ObjectTestThing/%s", thingToRefID), actualThunk)
   164  	})
   165  }