github.com/weaviate/weaviate@v1.24.6/test/acceptance/objects/crefs_without_waiting_for_refresh_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  	"fmt"
    16  	"testing"
    17  
    18  	"github.com/go-openapi/strfmt"
    19  	clschema "github.com/weaviate/weaviate/client/schema"
    20  	"github.com/weaviate/weaviate/entities/models"
    21  	"github.com/weaviate/weaviate/entities/schema"
    22  	"github.com/weaviate/weaviate/test/helper"
    23  	testhelper "github.com/weaviate/weaviate/test/helper"
    24  )
    25  
    26  // See https://github.com/weaviate/weaviate/issues/980
    27  func Test_AddingReferenceWithoutWaiting_UsingPostObjects(t *testing.T) {
    28  	defer func() {
    29  		// clean up so we can run this test multiple times in a row
    30  		delCityParams := clschema.NewSchemaObjectsDeleteParams().WithClassName("ReferenceWaitingTestCity")
    31  		dresp, err := helper.Client(t).Schema.SchemaObjectsDelete(delCityParams, nil)
    32  		t.Logf("clean up - delete city \n%v\n %v", dresp, err)
    33  
    34  		delPlaceParams := clschema.NewSchemaObjectsDeleteParams().WithClassName("ReferenceWaitingTestPlace")
    35  		dresp, err = helper.Client(t).Schema.SchemaObjectsDelete(delPlaceParams, nil)
    36  		t.Logf("clean up - delete place \n%v\n %v", dresp, err)
    37  	}()
    38  
    39  	t.Log("1. create ReferenceTestPlace class")
    40  	placeClass := &models.Class{
    41  		Class: "ReferenceWaitingTestPlace",
    42  		Properties: []*models.Property{
    43  			{
    44  				DataType:     schema.DataTypeText.PropString(),
    45  				Tokenization: models.PropertyTokenizationWhitespace,
    46  				Name:         "name",
    47  			},
    48  		},
    49  	}
    50  	params := clschema.NewSchemaObjectsCreateParams().WithObjectClass(placeClass)
    51  	resp, err := helper.Client(t).Schema.SchemaObjectsCreate(params, nil)
    52  	helper.AssertRequestOk(t, resp, err, nil)
    53  
    54  	t.Log("2. create ReferenceTestCity class with HasPlace cross-ref")
    55  	cityClass := &models.Class{
    56  		Class: "ReferenceWaitingTestCity",
    57  		Properties: []*models.Property{
    58  			{
    59  				DataType:     schema.DataTypeText.PropString(),
    60  				Tokenization: models.PropertyTokenizationWhitespace,
    61  				Name:         "name",
    62  			},
    63  			{
    64  				DataType: []string{"ReferenceWaitingTestPlace"},
    65  				Name:     "HasPlace",
    66  			},
    67  		},
    68  	}
    69  	params = clschema.NewSchemaObjectsCreateParams().WithObjectClass(cityClass)
    70  	resp, err = helper.Client(t).Schema.SchemaObjectsCreate(params, nil)
    71  	helper.AssertRequestOk(t, resp, err, nil)
    72  
    73  	t.Log("3. add a places and save the ID")
    74  	placeID := assertCreateObject(t, "ReferenceWaitingTestPlace", map[string]interface{}{
    75  		"name": "Place 1",
    76  	})
    77  
    78  	t.Log("4. add one city with ref to the place")
    79  	cityID := assertCreateObject(t, "ReferenceWaitingTestCity", map[string]interface{}{
    80  		"name": "My City",
    81  		"hasPlace": models.MultipleRef{
    82  			&models.SingleRef{
    83  				Beacon: strfmt.URI(fmt.Sprintf("weaviate://localhost/%s", placeID.String())),
    84  			},
    85  		},
    86  	})
    87  
    88  	assertGetObjectEventually(t, cityID)
    89  
    90  	actualThunk := func() interface{} {
    91  		city := assertGetObject(t, cityID)
    92  		return city.Properties
    93  	}
    94  	t.Log("7. verify first cross ref was added")
    95  	testhelper.AssertEventuallyEqual(t, map[string]interface{}{
    96  		"name": "My City",
    97  		"hasPlace": []interface{}{
    98  			map[string]interface{}{
    99  				"beacon": fmt.Sprintf("weaviate://localhost/%s/%s", "ReferenceWaitingTestPlace", placeID.String()),
   100  				"href":   fmt.Sprintf("/v1/objects/%s/%s", "ReferenceWaitingTestPlace", placeID.String()),
   101  			},
   102  		},
   103  	}, actualThunk)
   104  }