github.com/weaviate/weaviate@v1.24.6/usecases/schema/delete_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 schema
    13  
    14  import (
    15  	"context"
    16  	"testing"
    17  
    18  	"github.com/stretchr/testify/assert"
    19  	"github.com/stretchr/testify/require"
    20  	"github.com/weaviate/weaviate/entities/models"
    21  	"github.com/weaviate/weaviate/usecases/sharding"
    22  )
    23  
    24  func TestForceDelete(t *testing.T) {
    25  	type test struct {
    26  		name           string
    27  		existingSchema []*models.Class
    28  		classToDelete  string
    29  		expErr         bool
    30  		expErrMsg      string
    31  		expSchema      []*models.Class
    32  	}
    33  
    34  	tests := []test{
    35  		{
    36  			name: "class exists",
    37  			existingSchema: []*models.Class{
    38  				{Class: "MyClass", VectorIndexType: "hnsw"},
    39  				{Class: "OtherClass", VectorIndexType: "hnsw"},
    40  			},
    41  			classToDelete: "MyClass",
    42  			expSchema: []*models.Class{
    43  				classWithDefaultsSet(t, "OtherClass"),
    44  			},
    45  			expErr: false,
    46  		},
    47  		{
    48  			name: "class does not exist",
    49  			existingSchema: []*models.Class{
    50  				{Class: "OtherClass", VectorIndexType: "hnsw"},
    51  			},
    52  			classToDelete: "MyClass",
    53  			expSchema: []*models.Class{
    54  				classWithDefaultsSet(t, "OtherClass"),
    55  			},
    56  			expErr: false,
    57  		},
    58  	}
    59  
    60  	for _, test := range tests {
    61  		t.Run(test.name, func(t *testing.T) {
    62  			clusterState := &fakeClusterState{
    63  				hosts: []string{"node1"},
    64  			}
    65  
    66  			txClient := &fakeTxClient{}
    67  
    68  			initialSchema := &State{
    69  				ObjectSchema: &models.Schema{
    70  					Classes: test.existingSchema,
    71  				},
    72  			}
    73  
    74  			sm, err := newManagerWithClusterAndTx(t, clusterState, txClient, initialSchema)
    75  			require.Nil(t, err)
    76  			err = sm.DeleteClass(context.Background(), nil, test.classToDelete)
    77  
    78  			if test.expErr {
    79  				require.NotNil(t, err)
    80  				assert.Contains(t, err.Error(), test.expErrMsg)
    81  			} else {
    82  				require.Nil(t, err)
    83  			}
    84  
    85  			assert.ElementsMatch(t, test.expSchema, sm.GetSchemaSkipAuth().Objects.Classes)
    86  
    87  			if len(sm.schemaCache.ShardingState) != len(test.expSchema) {
    88  				t.Errorf("sharding state entries != schema: %d vs %d",
    89  					len(sm.schemaCache.ShardingState), len(test.expSchema))
    90  			}
    91  		})
    92  	}
    93  }
    94  
    95  func classWithDefaultsSet(t *testing.T, name string) *models.Class {
    96  	class := &models.Class{Class: name, VectorIndexType: "hnsw"}
    97  
    98  	sc, err := sharding.ParseConfig(map[string]interface{}{}, 1)
    99  	require.Nil(t, err)
   100  
   101  	class.ShardingConfig = sc
   102  
   103  	class.VectorIndexConfig = fakeVectorConfig{}
   104  	class.ReplicationConfig = &models.ReplicationConfig{Factor: 1}
   105  
   106  	return class
   107  }