github.com/weaviate/weaviate@v1.24.6/test/helper/objects.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 helper 13 14 import ( 15 "strings" 16 "testing" 17 18 "github.com/go-openapi/strfmt" 19 "github.com/stretchr/testify/assert" 20 "github.com/weaviate/weaviate/client/batch" 21 "github.com/weaviate/weaviate/client/meta" 22 "github.com/weaviate/weaviate/client/objects" 23 "github.com/weaviate/weaviate/client/schema" 24 "github.com/weaviate/weaviate/entities/models" 25 "github.com/weaviate/weaviate/entities/schema/crossref" 26 "github.com/weaviate/weaviate/usecases/replica" 27 ) 28 29 func SetupClient(uri string) { 30 host, port := "", "" 31 res := strings.Split(uri, ":") 32 if len(res) == 2 { 33 host, port = res[0], res[1] 34 } 35 ServerHost = host 36 ServerPort = port 37 } 38 39 func CreateClass(t *testing.T, class *models.Class) { 40 params := schema.NewSchemaObjectsCreateParams().WithObjectClass(class) 41 resp, err := Client(t).Schema.SchemaObjectsCreate(params, nil) 42 AssertRequestOk(t, resp, err, nil) 43 } 44 45 func GetClass(t *testing.T, class string) *models.Class { 46 params := schema.NewSchemaObjectsGetParams().WithClassName(class) 47 resp, err := Client(t).Schema.SchemaObjectsGet(params, nil) 48 AssertRequestOk(t, resp, err, nil) 49 return resp.Payload 50 } 51 52 func UpdateClass(t *testing.T, class *models.Class) { 53 params := schema.NewSchemaObjectsUpdateParams(). 54 WithObjectClass(class).WithClassName(class.Class) 55 resp, err := Client(t).Schema.SchemaObjectsUpdate(params, nil) 56 AssertRequestOk(t, resp, err, nil) 57 } 58 59 func CreateObject(t *testing.T, object *models.Object) { 60 params := objects.NewObjectsCreateParams().WithBody(object) 61 resp, err := Client(t).Objects.ObjectsCreate(params, nil) 62 AssertRequestOk(t, resp, err, nil) 63 } 64 65 func CreateObjectCL(t *testing.T, object *models.Object, cl replica.ConsistencyLevel) { 66 cls := string(cl) 67 params := objects.NewObjectsCreateParams().WithBody(object).WithConsistencyLevel(&cls) 68 resp, err := Client(t).Objects.ObjectsCreate(params, nil) 69 AssertRequestOk(t, resp, err, nil) 70 } 71 72 func CreateObjectsBatch(t *testing.T, objects []*models.Object) { 73 params := batch.NewBatchObjectsCreateParams(). 74 WithBody(batch.BatchObjectsCreateBody{ 75 Objects: objects, 76 }) 77 resp, err := Client(t).Batch.BatchObjectsCreate(params, nil) 78 AssertRequestOk(t, resp, err, nil) 79 CheckObjectsBatchResponse(t, resp.Payload, err) 80 } 81 82 func CheckObjectsBatchResponse(t *testing.T, resp []*models.ObjectsGetResponse, err error) { 83 AssertRequestOk(t, resp, err, nil) 84 for _, elem := range resp { 85 if !assert.Nil(t, elem.Result.Errors) { 86 t.Logf("expected nil, got: %v", 87 elem.Result.Errors.Error[0].Message) 88 } 89 } 90 } 91 92 func UpdateObject(t *testing.T, object *models.Object) { 93 params := objects.NewObjectsUpdateParams().WithID(object.ID).WithBody(object) 94 resp, err := Client(t).Objects.ObjectsUpdate(params, nil) 95 AssertRequestOk(t, resp, err, nil) 96 } 97 98 func UpdateObjectCL(t *testing.T, object *models.Object, cl replica.ConsistencyLevel) { 99 cls := string(cl) 100 params := objects.NewObjectsClassPutParams().WithClassName(object.Class). 101 WithID(object.ID).WithBody(object).WithConsistencyLevel(&cls) 102 resp, err := Client(t).Objects.ObjectsClassPut(params, nil) 103 AssertRequestOk(t, resp, err, nil) 104 } 105 106 func PatchObject(t *testing.T, object *models.Object) { 107 params := objects.NewObjectsPatchParams().WithID(object.ID).WithBody(object) 108 resp, err := Client(t).Objects.ObjectsPatch(params, nil) 109 AssertRequestOk(t, resp, err, nil) 110 } 111 112 func DeleteClass(t *testing.T, class string) { 113 delParams := schema.NewSchemaObjectsDeleteParams().WithClassName(class) 114 delRes, err := Client(t).Schema.SchemaObjectsDelete(delParams, nil) 115 AssertRequestOk(t, delRes, err, nil) 116 } 117 118 func DeleteObject(t *testing.T, object *models.Object) { 119 params := objects.NewObjectsClassDeleteParams(). 120 WithClassName(object.Class).WithID(object.ID) 121 resp, err := Client(t).Objects.ObjectsClassDelete(params, nil) 122 AssertRequestOk(t, resp, err, nil) 123 } 124 125 func DeleteObjectsBatch(t *testing.T, body *models.BatchDelete) { 126 params := batch.NewBatchObjectsDeleteParams().WithBody(body) 127 resp, err := Client(t).Batch.BatchObjectsDelete(params, nil) 128 AssertRequestOk(t, resp, err, nil) 129 } 130 131 func DeleteTenantObjectsBatch(t *testing.T, body *models.BatchDelete, 132 tenant string, 133 ) (*models.BatchDeleteResponse, error) { 134 params := batch.NewBatchObjectsDeleteParams(). 135 WithBody(body).WithTenant(&tenant) 136 resp, err := Client(t).Batch.BatchObjectsDelete(params, nil) 137 if err != nil { 138 return nil, err 139 } 140 return resp.Payload, nil 141 } 142 143 func AddReferences(t *testing.T, refs []*models.BatchReference) ([]*models.BatchReferenceResponse, error) { 144 params := batch.NewBatchReferencesCreateParams().WithBody(refs) 145 resp, err := Client(t).Batch.BatchReferencesCreate(params, nil) 146 if err != nil { 147 return nil, err 148 } 149 return resp.Payload, nil 150 } 151 152 func CheckReferencesBatchResponse(t *testing.T, resp []*models.BatchReferenceResponse, err error) { 153 AssertRequestOk(t, resp, err, nil) 154 for _, elem := range resp { 155 if !assert.Nil(t, elem.Result.Errors) { 156 t.Logf("expected nil, got: %v", 157 elem.Result.Errors.Error[0].Message) 158 } 159 } 160 } 161 162 func AddReference(t *testing.T, object *models.Object, ref *models.SingleRef, prop string) { 163 params := objects.NewObjectsClassReferencesCreateParams(). 164 WithClassName(object.Class).WithID(object.ID).WithBody(ref).WithPropertyName(prop) 165 resp, err := Client(t).Objects.ObjectsClassReferencesCreate(params, nil) 166 AssertRequestOk(t, resp, err, nil) 167 } 168 169 func AddReferenceTenant(t *testing.T, object *models.Object, ref *models.SingleRef, prop string, tenant string) { 170 params := objects.NewObjectsClassReferencesCreateParams(). 171 WithClassName(object.Class).WithID(object.ID).WithBody(ref).WithPropertyName(prop).WithTenant(&tenant) 172 resp, err := Client(t).Objects.ObjectsClassReferencesCreate(params, nil) 173 AssertRequestOk(t, resp, err, nil) 174 } 175 176 func DeleteReference(t *testing.T, object *models.Object, ref *models.SingleRef, prop string) { 177 params := objects.NewObjectsClassReferencesDeleteParams(). 178 WithClassName(object.Class).WithID(object.ID).WithBody(ref).WithPropertyName(prop) 179 resp, err := Client(t).Objects.ObjectsClassReferencesDelete(params, nil) 180 AssertRequestOk(t, resp, err, nil) 181 } 182 183 func DeleteReferenceTenant(t *testing.T, object *models.Object, ref *models.SingleRef, prop string, tenant string) { 184 params := objects.NewObjectsClassReferencesDeleteParams(). 185 WithClassName(object.Class).WithID(object.ID).WithBody(ref).WithPropertyName(prop).WithTenant(&tenant) 186 resp, err := Client(t).Objects.ObjectsClassReferencesDelete(params, nil) 187 AssertRequestOk(t, resp, err, nil) 188 } 189 190 func UpdateReferenceTenant(t *testing.T, object *models.Object, ref models.MultipleRef, prop string, tenant string) { 191 params := objects.NewObjectsClassReferencesPutParams(). 192 WithClassName(object.Class).WithID(object.ID).WithBody(ref).WithPropertyName(prop).WithTenant(&tenant) 193 resp, err := Client(t).Objects.ObjectsClassReferencesPut(params, nil) 194 AssertRequestOk(t, resp, err, nil) 195 } 196 197 func CreateTenants(t *testing.T, class string, tenants []*models.Tenant) { 198 params := schema.NewTenantsCreateParams().WithClassName(class).WithBody(tenants) 199 resp, err := Client(t).Schema.TenantsCreate(params, nil) 200 AssertRequestOk(t, resp, err, nil) 201 } 202 203 func CreateTenantsReturnError(t *testing.T, class string, tenants []*models.Tenant) error { 204 params := schema.NewTenantsCreateParams().WithClassName(class).WithBody(tenants) 205 _, err := Client(t).Schema.TenantsCreate(params, nil) 206 return err 207 } 208 209 func GetTenants(t *testing.T, class string) (*schema.TenantsGetOK, error) { 210 params := schema.NewTenantsGetParams().WithClassName(class) 211 resp, err := Client(t).Schema.TenantsGet(params, nil) 212 return resp, err 213 } 214 215 func DeleteTenants(t *testing.T, class string, tenants []string) error { 216 params := schema.NewTenantsDeleteParams().WithClassName(class).WithTenants(tenants) 217 _, err := Client(t).Schema.TenantsDelete(params, nil) 218 return err 219 } 220 221 func NewBeacon(className string, id strfmt.UUID) strfmt.URI { 222 return crossref.New("localhost", className, id).SingleRef().Beacon 223 } 224 225 func GetMeta(t *testing.T) *models.Meta { 226 params := meta.NewMetaGetParams() 227 resp, err := Client(t).Meta.MetaGet(params, nil) 228 AssertRequestOk(t, resp, err, nil) 229 return resp.Payload 230 }