github.com/weaviate/weaviate@v1.24.6/test/acceptance/actions/add_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  	"testing"
    18  
    19  	"github.com/go-openapi/strfmt"
    20  	"github.com/weaviate/weaviate/client/objects"
    21  	"github.com/weaviate/weaviate/entities/models"
    22  
    23  	"github.com/stretchr/testify/assert"
    24  	"github.com/weaviate/weaviate/test/helper"
    25  )
    26  
    27  // executed in setup_test.go
    28  func addingObjects(t *testing.T) {
    29  	class := "TestObject"
    30  	t.Run("can create object", func(t *testing.T) {
    31  		// Set all object values to compare
    32  		objectTestString := "Test string"
    33  		objectTestInt := 1
    34  		objectTestBoolean := true
    35  		objectTestNumber := 1.337
    36  		objectTestDate := "2017-10-06T08:15:30+01:00"
    37  
    38  		params := objects.NewObjectsCreateParams().WithBody(
    39  			&models.Object{
    40  				Class: class,
    41  				Properties: map[string]interface{}{
    42  					"testString":      objectTestString,
    43  					"testWholeNumber": objectTestInt,
    44  					"testTrueFalse":   objectTestBoolean,
    45  					"testNumber":      objectTestNumber,
    46  					"testDateTime":    objectTestDate,
    47  				},
    48  			})
    49  
    50  		resp, err := helper.Client(t).Objects.ObjectsCreate(params, nil)
    51  
    52  		// Ensure that the response is OK
    53  		helper.AssertRequestOk(t, resp, err, func() {
    54  			object := resp.Payload
    55  			assert.Regexp(t, strfmt.UUIDPattern, object.ID)
    56  
    57  			schema, ok := object.Properties.(map[string]interface{})
    58  			if !ok {
    59  				t.Fatal("The returned schema is not an JSON object")
    60  			}
    61  
    62  			testWholeNumber, _ := schema["testWholeNumber"].(json.Number).Int64()
    63  			testNumber, _ := schema["testNumber"].(json.Number).Float64()
    64  
    65  			// Check whether the returned information is the same as the data added
    66  			assert.Equal(t, objectTestString, schema["testString"])
    67  			assert.Equal(t, objectTestInt, int(testWholeNumber))
    68  			assert.Equal(t, objectTestBoolean, schema["testTrueFalse"])
    69  			assert.Equal(t, objectTestNumber, testNumber)
    70  			assert.Equal(t, objectTestDate, schema["testDateTime"])
    71  		})
    72  	})
    73  
    74  	t.Run("can create and get object", func(t *testing.T) {
    75  		objectTestString := "Test string"
    76  		objectTestInt := 1
    77  		objectTestBoolean := true
    78  		objectTestNumber := 1.337
    79  		objectTestDate := "2017-10-06T08:15:30+01:00"
    80  
    81  		objectID := helper.AssertCreateObject(t, class, map[string]interface{}{
    82  			"testString":      objectTestString,
    83  			"testWholeNumber": objectTestInt,
    84  			"testTrueFalse":   objectTestBoolean,
    85  			"testNumber":      objectTestNumber,
    86  			"testDateTime":    objectTestDate,
    87  		})
    88  		helper.AssertGetObjectEventually(t, class, objectID)
    89  
    90  		// Now fetch the object
    91  		getResp, err := helper.Client(t).Objects.ObjectsGet(objects.NewObjectsGetParams().WithID(objectID), nil)
    92  
    93  		helper.AssertRequestOk(t, getResp, err, func() {
    94  			object := getResp.Payload
    95  
    96  			schema, ok := object.Properties.(map[string]interface{})
    97  			if !ok {
    98  				t.Fatal("The returned schema is not an JSON object")
    99  			}
   100  
   101  			testWholeNumber, _ := schema["testWholeNumber"].(json.Number).Int64()
   102  			testNumber, _ := schema["testNumber"].(json.Number).Float64()
   103  
   104  			// Check whether the returned information is the same as the data added
   105  			assert.Equal(t, objectTestString, schema["testString"])
   106  			assert.Equal(t, objectTestInt, int(testWholeNumber))
   107  			assert.Equal(t, objectTestBoolean, schema["testTrueFalse"])
   108  			assert.Equal(t, objectTestNumber, testNumber)
   109  			assert.Equal(t, objectTestDate, schema["testDateTime"])
   110  		})
   111  	})
   112  
   113  	t.Run("can add single ref", func(t *testing.T) {
   114  		firstID := helper.AssertCreateObject(t, class, map[string]interface{}{})
   115  		helper.AssertGetObjectEventually(t, class, firstID)
   116  
   117  		secondID := helper.AssertCreateObject(t, "TestObjectTwo", map[string]interface{}{
   118  			"testString": "stringy",
   119  			"testReference": []interface{}{
   120  				map[string]interface{}{
   121  					"beacon": fmt.Sprintf("weaviate://localhost/%s", firstID),
   122  				},
   123  			},
   124  		})
   125  
   126  		secondObject := helper.AssertGetObjectEventually(t, "TestObjectTwo", secondID)
   127  
   128  		singleRef := secondObject.Properties.(map[string]interface{})["testReference"].([]interface{})[0].(map[string]interface{})
   129  		assert.Equal(t, singleRef["beacon"].(string), fmt.Sprintf("weaviate://localhost/TestObject/%s", firstID))
   130  	})
   131  }