github.com/weaviate/weaviate@v1.24.6/test/acceptance/multi_tenancy/tenant_objects_reference_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  	"testing"
    16  
    17  	"github.com/stretchr/testify/assert"
    18  	"github.com/stretchr/testify/require"
    19  	"github.com/weaviate/weaviate/entities/models"
    20  	"github.com/weaviate/weaviate/entities/schema"
    21  	"github.com/weaviate/weaviate/test/helper"
    22  )
    23  
    24  func TestTenantObjectsReference(t *testing.T) {
    25  	className := "MultiTenantClass"
    26  	mutableProp := "mutableProp"
    27  	refProp := "refProp"
    28  	testClass := models.Class{
    29  		Class: className,
    30  		MultiTenancyConfig: &models.MultiTenancyConfig{
    31  			Enabled: true,
    32  		},
    33  		Properties: []*models.Property{
    34  			{
    35  				Name:     "name",
    36  				DataType: schema.DataTypeText.PropString(),
    37  			},
    38  			{
    39  				Name:     mutableProp,
    40  				DataType: schema.DataTypeText.PropString(),
    41  			},
    42  			{
    43  				Name:     refProp,
    44  				DataType: []string{className},
    45  			},
    46  		},
    47  	}
    48  	tenantNames := []string{
    49  		"Tenant1", "Tenant2", "Tenant3",
    50  	}
    51  	tenantObjects := []*models.Object{
    52  		{
    53  			ID:    "0927a1e0-398e-4e76-91fb-04a7a8f0405c",
    54  			Class: className,
    55  			Properties: map[string]interface{}{
    56  				"name":      tenantNames[0],
    57  				mutableProp: "obj#0",
    58  			},
    59  			Tenant: tenantNames[0],
    60  		},
    61  		{
    62  			ID:    "831ae1d0-f441-44b1-bb2a-46548048e26f",
    63  			Class: className,
    64  			Properties: map[string]interface{}{
    65  				"name":      tenantNames[1],
    66  				mutableProp: "obj#1",
    67  			},
    68  			Tenant: tenantNames[1],
    69  		},
    70  		{
    71  			ID:    "6f3363e0-c0a0-4618-bf1f-b6cad9cdff59",
    72  			Class: className,
    73  			Properties: map[string]interface{}{
    74  				"name":      tenantNames[2],
    75  				mutableProp: "obj#2",
    76  			},
    77  			Tenant: tenantNames[2],
    78  		},
    79  	}
    80  	tenantRefs := []*models.Object{
    81  		{
    82  			ID:    "169b62a7-ef1c-481d-8fb0-27f11716bde7",
    83  			Class: className,
    84  			Properties: map[string]interface{}{
    85  				"name":      tenantNames[0],
    86  				mutableProp: "ref#0",
    87  			},
    88  			Tenant: tenantNames[0],
    89  		},
    90  		{
    91  			ID:    "4d78424d-f7bd-479b-bd8a-52510e2db0fd",
    92  			Class: className,
    93  			Properties: map[string]interface{}{
    94  				"name":      tenantNames[1],
    95  				mutableProp: "ref#1",
    96  			},
    97  			Tenant: tenantNames[1],
    98  		},
    99  		{
   100  			ID:    "c1db0a06-d5f9-4f77-aa3c-08a44f16e358",
   101  			Class: className,
   102  			Properties: map[string]interface{}{
   103  				"name":      tenantNames[2],
   104  				mutableProp: "ref#2",
   105  			},
   106  			Tenant: tenantNames[2],
   107  		},
   108  	}
   109  
   110  	defer func() {
   111  		helper.DeleteClass(t, className)
   112  	}()
   113  
   114  	t.Run("create class with multi-tenancy enabled", func(t *testing.T) {
   115  		helper.CreateClass(t, &testClass)
   116  	})
   117  
   118  	t.Run("create tenants", func(t *testing.T) {
   119  		tenants := make([]*models.Tenant, len(tenantNames))
   120  		for i := range tenants {
   121  			tenants[i] = &models.Tenant{Name: tenantNames[i]}
   122  		}
   123  		helper.CreateTenants(t, className, tenants)
   124  	})
   125  
   126  	t.Run("add tenant objects", func(t *testing.T) {
   127  		for i, obj := range tenantObjects {
   128  			helper.CreateObject(t, obj)
   129  			helper.CreateObject(t, tenantRefs[i])
   130  		}
   131  
   132  		t.Run("verify tenant object creation", func(t *testing.T) {
   133  			for i, obj := range tenantObjects {
   134  				resp, err := helper.TenantObject(t, obj.Class, obj.ID, tenantNames[i])
   135  				require.Nil(t, err)
   136  				require.Equal(t, obj.ID, resp.ID)
   137  				require.Equal(t, obj.Class, resp.Class)
   138  				require.Equal(t, obj.Properties, resp.Properties)
   139  			}
   140  		})
   141  	})
   142  
   143  	t.Run("add tenant object references", func(t *testing.T) {
   144  		for i, obj := range tenantObjects {
   145  			ref := &models.SingleRef{Beacon: helper.NewBeacon(className, tenantRefs[i].ID)}
   146  			helper.AddReferenceTenant(t, obj, ref, refProp, tenantNames[i])
   147  		}
   148  
   149  		t.Run("assert tenant object references", func(t *testing.T) {
   150  			for i, obj := range tenantObjects {
   151  				resp, err := helper.TenantObject(t, obj.Class, obj.ID, tenantNames[i])
   152  				require.Nil(t, err)
   153  				require.Equal(t, obj.ID, resp.ID)
   154  				require.Equal(t, obj.Class, resp.Class)
   155  				refs := resp.Properties.(map[string]interface{})[refProp].([]interface{})
   156  				require.Len(t, refs, 1)
   157  				expectedBeacon := helper.NewBeacon(className, tenantRefs[i].ID).String()
   158  				assert.Equal(t, expectedBeacon, refs[0].(map[string]interface{})["beacon"])
   159  			}
   160  		})
   161  	})
   162  
   163  	t.Run("delete tenant object references", func(Z *testing.T) {
   164  		for i, obj := range tenantObjects {
   165  			ref := &models.SingleRef{Beacon: helper.NewBeacon(className, tenantRefs[i].ID)}
   166  			helper.DeleteReferenceTenant(t, obj, ref, refProp, tenantNames[i])
   167  		}
   168  
   169  		t.Run("assert tenant object references", func(t *testing.T) {
   170  			for i, obj := range tenantObjects {
   171  				resp, err := helper.TenantObject(t, obj.Class, obj.ID, tenantNames[i])
   172  				require.Nil(t, err)
   173  				require.Equal(t, obj.ID, resp.ID)
   174  				require.Equal(t, obj.Class, resp.Class)
   175  				refs := resp.Properties.(map[string]interface{})[refProp].([]interface{})
   176  				require.Len(t, refs, 0)
   177  			}
   178  		})
   179  	})
   180  }