github.com/weaviate/weaviate@v1.24.6/usecases/objects/batch_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 objects
    13  
    14  import (
    15  	"context"
    16  	"testing"
    17  
    18  	"github.com/sirupsen/logrus/hooks/test"
    19  	"github.com/stretchr/testify/assert"
    20  	"github.com/weaviate/weaviate/entities/models"
    21  	"github.com/weaviate/weaviate/entities/schema"
    22  	"github.com/weaviate/weaviate/entities/vectorindex/hnsw"
    23  	"github.com/weaviate/weaviate/entities/verbosity"
    24  	"github.com/weaviate/weaviate/usecases/config"
    25  )
    26  
    27  func Test_BatchDelete_RequestValidation(t *testing.T) {
    28  	var (
    29  		vectorRepo *fakeVectorRepo
    30  		manager    *BatchManager
    31  	)
    32  
    33  	schema := schema.Schema{
    34  		Objects: &models.Schema{
    35  			Classes: []*models.Class{
    36  				{
    37  					Class: "Foo",
    38  					Properties: []*models.Property{
    39  						{
    40  							Name:         "name",
    41  							DataType:     schema.DataTypeText.PropString(),
    42  							Tokenization: models.PropertyTokenizationWhitespace,
    43  						},
    44  					},
    45  					VectorIndexConfig: hnsw.UserConfig{},
    46  					Vectorizer:        config.VectorizerModuleNone,
    47  				},
    48  			},
    49  		},
    50  	}
    51  
    52  	resetAutoSchema := func(autoSchema bool) {
    53  		vectorRepo = &fakeVectorRepo{}
    54  		config := &config.WeaviateConfig{
    55  			Config: config.Config{
    56  				AutoSchema: config.AutoSchema{
    57  					Enabled: autoSchema,
    58  				},
    59  			},
    60  		}
    61  		locks := &fakeLocks{}
    62  		schemaManager := &fakeSchemaManager{
    63  			GetSchemaResponse: schema,
    64  		}
    65  		logger, _ := test.NewNullLogger()
    66  		authorizer := &fakeAuthorizer{}
    67  		modulesProvider := getFakeModulesProvider()
    68  		manager = NewBatchManager(vectorRepo, modulesProvider, locks,
    69  			schemaManager, config, logger, authorizer, nil)
    70  	}
    71  
    72  	reset := func() {
    73  		resetAutoSchema(false)
    74  	}
    75  	ctx := context.Background()
    76  
    77  	reset()
    78  
    79  	t.Run("with invalid input", func(t *testing.T) {
    80  		tests := []struct {
    81  			input         *models.BatchDelete
    82  			expectedError string
    83  		}{
    84  			{
    85  				input: &models.BatchDelete{
    86  					DryRun: ptBool(false),
    87  					Output: ptString(verbosity.OutputVerbose),
    88  					Match: &models.BatchDeleteMatch{
    89  						Class: "SomeClass",
    90  						Where: &models.WhereFilter{
    91  							Path:      []string{"some", "path"},
    92  							Operator:  "Equal",
    93  							ValueText: ptString("value"),
    94  						},
    95  					},
    96  				},
    97  				expectedError: "validate: class: SomeClass doesn't exist",
    98  			},
    99  			{
   100  				input: &models.BatchDelete{
   101  					DryRun: ptBool(false),
   102  					Output: ptString(verbosity.OutputVerbose),
   103  					Match: &models.BatchDeleteMatch{
   104  						Class: "Foo",
   105  						Where: &models.WhereFilter{
   106  							Path:      []string{"some"},
   107  							Operator:  "Equal",
   108  							ValueText: ptString("value"),
   109  						},
   110  					},
   111  				},
   112  				expectedError: "validate: invalid where filter: no such prop with name 'some' found in class 'Foo' " +
   113  					"in the schema. Check your schema files for which properties in this class are available",
   114  			},
   115  			{
   116  				input: &models.BatchDelete{
   117  					DryRun: ptBool(false),
   118  					Output: ptString(verbosity.OutputVerbose),
   119  				},
   120  				expectedError: "validate: empty match clause",
   121  			},
   122  			{
   123  				input: &models.BatchDelete{
   124  					DryRun: ptBool(false),
   125  					Output: ptString(verbosity.OutputVerbose),
   126  					Match: &models.BatchDeleteMatch{
   127  						Class: "",
   128  					},
   129  				},
   130  				expectedError: "validate: empty match.class clause",
   131  			},
   132  			{
   133  				input: &models.BatchDelete{
   134  					DryRun: ptBool(false),
   135  					Output: ptString(verbosity.OutputVerbose),
   136  					Match: &models.BatchDeleteMatch{
   137  						Class: "Foo",
   138  					},
   139  				},
   140  				expectedError: "validate: empty match.where clause",
   141  			},
   142  			{
   143  				input: &models.BatchDelete{
   144  					DryRun: ptBool(false),
   145  					Output: ptString(verbosity.OutputVerbose),
   146  					Match: &models.BatchDeleteMatch{
   147  						Class: "Foo",
   148  						Where: &models.WhereFilter{
   149  							Path:      []string{},
   150  							Operator:  "Equal",
   151  							ValueText: ptString("name"),
   152  						},
   153  					},
   154  				},
   155  				expectedError: "validate: failed to parse where filter: invalid where filter: field 'path': must have at least one element",
   156  			},
   157  			{
   158  				input: &models.BatchDelete{
   159  					DryRun: ptBool(false),
   160  					Output: ptString("Simplified Chinese"),
   161  					Match: &models.BatchDeleteMatch{
   162  						Class: "Foo",
   163  						Where: &models.WhereFilter{
   164  							Path:      []string{"name"},
   165  							Operator:  "Equal",
   166  							ValueText: ptString("value"),
   167  						},
   168  					},
   169  				},
   170  				expectedError: "validate: invalid output: \"Simplified Chinese\", possible values are: \"minimal\", \"verbose\"",
   171  			},
   172  		}
   173  
   174  		for _, test := range tests {
   175  			_, err := manager.DeleteObjects(ctx, nil, test.input.Match, test.input.DryRun, test.input.Output, nil, "")
   176  			assert.Equal(t, test.expectedError, err.Error())
   177  		}
   178  	})
   179  }
   180  
   181  func ptBool(b bool) *bool {
   182  	return &b
   183  }
   184  
   185  func ptString(s string) *string {
   186  	return &s
   187  }