github.com/weaviate/weaviate@v1.24.6/test/helper/objects_assertions.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  	"net/http"
    16  	"testing"
    17  
    18  	"github.com/go-openapi/strfmt"
    19  	"github.com/weaviate/weaviate/client/objects"
    20  	"github.com/weaviate/weaviate/client/schema"
    21  	"github.com/weaviate/weaviate/entities/models"
    22  	"github.com/weaviate/weaviate/usecases/replica"
    23  )
    24  
    25  func AssertCreateObject(t *testing.T, className string, schema map[string]interface{}) strfmt.UUID {
    26  	t.Helper()
    27  	params := objects.NewObjectsCreateParams().WithBody(
    28  		&models.Object{
    29  			Class:      className,
    30  			Properties: schema,
    31  		})
    32  
    33  	resp, err := Client(t).Objects.ObjectsCreate(params, nil)
    34  
    35  	var objectID strfmt.UUID
    36  
    37  	// Ensure that the response is OK
    38  	AssertRequestOk(t, resp, err, func() {
    39  		objectID = resp.Payload.ID
    40  	})
    41  
    42  	return objectID
    43  }
    44  
    45  func AssertGetObject(t *testing.T, class string, uuid strfmt.UUID, include ...string) *models.Object {
    46  	t.Helper()
    47  	obj, err := GetObject(t, class, uuid, include...)
    48  	AssertRequestOk(t, obj, err, nil)
    49  	return obj
    50  }
    51  
    52  func AssertGetObjectEventually(t *testing.T, class string, uuid strfmt.UUID) *models.Object {
    53  	var (
    54  		resp *objects.ObjectsClassGetOK
    55  		err  error
    56  	)
    57  
    58  	checkThunk := func() interface{} {
    59  		resp, err = Client(t).Objects.ObjectsClassGet(objects.NewObjectsClassGetParams().WithClassName(class).WithID(uuid), nil)
    60  		return err == nil
    61  	}
    62  
    63  	AssertEventuallyEqual(t, true, checkThunk)
    64  
    65  	var object *models.Object
    66  
    67  	AssertRequestOk(t, resp, err, func() {
    68  		object = resp.Payload
    69  	})
    70  
    71  	return object
    72  }
    73  
    74  func AssertGetObjectFailsEventually(t *testing.T, class string, uuid strfmt.UUID) error {
    75  	var err error
    76  
    77  	checkThunk := func() interface{} {
    78  		_, err = Client(t).Objects.ObjectsClassGet(objects.NewObjectsClassGetParams().WithClassName(class).WithID(uuid), nil)
    79  		return err != nil
    80  	}
    81  
    82  	AssertEventuallyEqual(t, true, checkThunk)
    83  
    84  	return err
    85  }
    86  
    87  func AssertCreateObjectClass(t *testing.T, class *models.Class) {
    88  	t.Helper()
    89  	params := schema.NewSchemaObjectsCreateParams().WithObjectClass(class)
    90  	resp, err := Client(t).Schema.SchemaObjectsCreate(params, nil)
    91  	AssertRequestOk(t, resp, err, nil)
    92  }
    93  
    94  func AssertDeleteObjectClass(t *testing.T, class string) {
    95  	delRes, err := DeleteClassObject(t, class)
    96  	AssertRequestOk(t, delRes, err, nil)
    97  }
    98  
    99  func GetObject(t *testing.T, class string, uuid strfmt.UUID, include ...string) (*models.Object, error) {
   100  	req := objects.NewObjectsClassGetParams().WithID(uuid)
   101  	if class != "" {
   102  		req.WithClassName(class)
   103  	}
   104  	if len(include) > 0 {
   105  		req.WithInclude(&include[0])
   106  	}
   107  	getResp, err := Client(t).Objects.ObjectsClassGet(req, nil)
   108  	if err != nil {
   109  		return nil, err
   110  	}
   111  	return getResp.Payload, nil
   112  }
   113  
   114  func TenantObject(t *testing.T, class string, id strfmt.UUID, tenant string) (*models.Object, error) {
   115  	req := objects.NewObjectsClassGetParams().
   116  		WithClassName(class).WithID(id).WithTenant(&tenant)
   117  	getResp, err := Client(t).Objects.ObjectsClassGet(req, nil)
   118  	if err != nil {
   119  		return nil, err
   120  	}
   121  	return getResp.Payload, nil
   122  }
   123  
   124  func TenantObjectWithInclude(t *testing.T, class string, id strfmt.UUID, tenant string, includes string) (*models.Object, error) {
   125  	req := objects.NewObjectsClassGetParams().
   126  		WithClassName(class).WithID(id).WithTenant(&tenant).WithInclude(&includes)
   127  	getResp, err := Client(t).Objects.ObjectsClassGet(req, nil)
   128  	if err != nil {
   129  		return nil, err
   130  	}
   131  	return getResp.Payload, nil
   132  }
   133  
   134  func GetObjectCL(t *testing.T, class string, uuid strfmt.UUID,
   135  	cl replica.ConsistencyLevel, include ...string,
   136  ) (*models.Object, error) {
   137  	req := objects.NewObjectsClassGetParams().WithID(uuid)
   138  	if class != "" {
   139  		req.WithClassName(class)
   140  	}
   141  	if len(include) > 0 {
   142  		req.WithInclude(&include[0])
   143  	}
   144  	cls := string(cl)
   145  	req.ConsistencyLevel = &cls
   146  	getResp, err := Client(t).Objects.ObjectsClassGet(req, nil)
   147  	if err != nil {
   148  		return nil, err
   149  	}
   150  	return getResp.Payload, nil
   151  }
   152  
   153  func ObjectExistsCL(t *testing.T, class string, id strfmt.UUID, cl replica.ConsistencyLevel) (bool, error) {
   154  	cls := string(cl)
   155  	req := objects.NewObjectsClassHeadParams().
   156  		WithClassName(class).WithID(id).WithConsistencyLevel(&cls)
   157  	resp, err := Client(t).Objects.ObjectsClassHead(req, nil)
   158  	if err != nil {
   159  		return false, err
   160  	}
   161  	return resp.IsCode(http.StatusNoContent), nil
   162  }
   163  
   164  func TenantObjectExists(t *testing.T, class string, id strfmt.UUID, tenant string) (bool, error) {
   165  	req := objects.NewObjectsClassHeadParams().
   166  		WithClassName(class).WithID(id).WithTenant(&tenant)
   167  	resp, err := Client(t).Objects.ObjectsClassHead(req, nil)
   168  	if err != nil {
   169  		return false, err
   170  	}
   171  	return resp.IsCode(http.StatusNoContent), nil
   172  }
   173  
   174  func GetObjectFromNode(t *testing.T, class string, uuid strfmt.UUID, nodename string) (*models.Object, error) {
   175  	req := objects.NewObjectsClassGetParams().WithID(uuid)
   176  	if class != "" {
   177  		req.WithClassName(class)
   178  	}
   179  	if nodename != "" {
   180  		req.WithNodeName(&nodename)
   181  	}
   182  	getResp, err := Client(t).Objects.ObjectsClassGet(req, nil)
   183  	if err != nil {
   184  		return nil, err
   185  	}
   186  	return getResp.Payload, nil
   187  }
   188  
   189  func GetTenantObjectFromNode(t *testing.T, class string, uuid strfmt.UUID, nodename, tenant string) (*models.Object, error) {
   190  	req := objects.NewObjectsClassGetParams().WithID(uuid).
   191  		WithClassName(class).
   192  		WithNodeName(&nodename).
   193  		WithTenant(&tenant)
   194  	getResp, err := Client(t).Objects.ObjectsClassGet(req, nil)
   195  	if err != nil {
   196  		return nil, err
   197  	}
   198  	return getResp.Payload, nil
   199  }
   200  
   201  func DeleteClassObject(t *testing.T, class string) (*schema.SchemaObjectsDeleteOK, error) {
   202  	delParams := schema.NewSchemaObjectsDeleteParams().WithClassName(class)
   203  	return Client(t).Schema.SchemaObjectsDelete(delParams, nil)
   204  }
   205  
   206  func DeleteTenantObject(t *testing.T, class string, id strfmt.UUID, tenant string) {
   207  	params := objects.NewObjectsClassDeleteParams().
   208  		WithClassName(class).WithID(id).WithTenant(&tenant)
   209  	resp, err := Client(t).Objects.ObjectsClassDelete(params, nil)
   210  	AssertRequestOk(t, resp, err, nil)
   211  }
   212  
   213  func ListObjects(t *testing.T, class string) (*models.ObjectsListResponse, error) {
   214  	params := objects.NewObjectsListParams()
   215  	if class != "" {
   216  		params.WithClass(&class)
   217  	}
   218  
   219  	resp, err := Client(t).Objects.ObjectsList(params, nil)
   220  	if err != nil {
   221  		return nil, err
   222  	}
   223  	return resp.Payload, nil
   224  }
   225  
   226  func TenantListObjects(t *testing.T, class string, tenant string) (*models.ObjectsListResponse, error) {
   227  	params := objects.NewObjectsListParams().WithTenant(&tenant)
   228  	if class != "" {
   229  		params.WithClass(&class)
   230  	}
   231  
   232  	resp, err := Client(t).Objects.ObjectsList(params, nil)
   233  	if err != nil {
   234  		return nil, err
   235  	}
   236  	return resp.Payload, nil
   237  }