github.com/weaviate/weaviate@v1.24.6/adapters/repos/db/inverted/config_update_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 inverted
    13  
    14  import (
    15  	"testing"
    16  
    17  	"github.com/stretchr/testify/assert"
    18  	"github.com/stretchr/testify/require"
    19  	"github.com/weaviate/weaviate/adapters/repos/db/inverted/stopwords"
    20  	"github.com/weaviate/weaviate/entities/models"
    21  	"github.com/weaviate/weaviate/usecases/config"
    22  )
    23  
    24  func TestValidateUserConfigUpdate(t *testing.T) {
    25  	validInitial := &models.InvertedIndexConfig{
    26  		CleanupIntervalSeconds: 1,
    27  		Bm25: &models.BM25Config{
    28  			K1: config.DefaultBM25k1,
    29  			B:  config.DefaultBM25b,
    30  		},
    31  		Stopwords: &models.StopwordConfig{
    32  			Preset: stopwords.EnglishPreset,
    33  		},
    34  	}
    35  
    36  	t.Run("with valid updated config all fields", func(t *testing.T) {
    37  		updated := &models.InvertedIndexConfig{
    38  			CleanupIntervalSeconds: 2,
    39  			Bm25: &models.BM25Config{
    40  				K1: 1.3,
    41  				B:  0.778,
    42  			},
    43  			Stopwords: &models.StopwordConfig{
    44  				Preset:    "en",
    45  				Additions: []string{"star", "nebula"},
    46  				Removals:  []string{"the", "a"},
    47  			},
    48  		}
    49  
    50  		err := ValidateUserConfigUpdate(validInitial, updated)
    51  		require.Nil(t, err)
    52  	})
    53  
    54  	t.Run("with valid updated config missing BM25", func(t *testing.T) {
    55  		updated := &models.InvertedIndexConfig{
    56  			CleanupIntervalSeconds: 2,
    57  		}
    58  
    59  		err := ValidateUserConfigUpdate(validInitial, updated)
    60  		require.Nil(t, err)
    61  		assert.Equal(t, validInitial.Bm25.K1, updated.Bm25.K1)
    62  		assert.Equal(t, validInitial.Bm25.B, updated.Bm25.B)
    63  	})
    64  
    65  	t.Run("with valid updated config missing Stopwords", func(t *testing.T) {
    66  		updated := &models.InvertedIndexConfig{
    67  			CleanupIntervalSeconds: 2,
    68  		}
    69  
    70  		err := ValidateUserConfigUpdate(validInitial, updated)
    71  		require.Nil(t, err)
    72  		assert.Equal(t, validInitial.Stopwords.Preset, updated.Stopwords.Preset)
    73  		assert.Equal(t, validInitial.Stopwords.Additions, updated.Stopwords.Additions)
    74  		assert.Equal(t, validInitial.Stopwords.Removals, updated.Stopwords.Removals)
    75  	})
    76  
    77  	t.Run("with invalid cleanup interval", func(t *testing.T) {
    78  		updated := &models.InvertedIndexConfig{
    79  			CleanupIntervalSeconds: -1,
    80  		}
    81  
    82  		err := ValidateUserConfigUpdate(validInitial, updated)
    83  		require.EqualError(t, err, "cleanup interval seconds must be > 0")
    84  	})
    85  
    86  	t.Run("with invalid updated Bm25 config", func(t *testing.T) {
    87  		updated := &models.InvertedIndexConfig{
    88  			CleanupIntervalSeconds: 1,
    89  			Bm25: &models.BM25Config{
    90  				K1: 1.2,
    91  				B:  1.2,
    92  			},
    93  		}
    94  
    95  		err := ValidateUserConfigUpdate(validInitial, updated)
    96  		require.EqualError(t, err, "BM25.b must be <= 0 and <= 1")
    97  	})
    98  
    99  	t.Run("with invalid updated Stopwords preset config", func(t *testing.T) {
   100  		updated := &models.InvertedIndexConfig{
   101  			CleanupIntervalSeconds: 1,
   102  			Stopwords: &models.StopwordConfig{
   103  				Preset: "mongolian",
   104  			},
   105  		}
   106  
   107  		err := ValidateUserConfigUpdate(validInitial, updated)
   108  		require.EqualError(t, err, "stopwordPreset 'mongolian' does not exist")
   109  	})
   110  
   111  	t.Run("with invalid updated Stopwords addition/removal config", func(t *testing.T) {
   112  		updated := &models.InvertedIndexConfig{
   113  			CleanupIntervalSeconds: 1,
   114  			Stopwords: &models.StopwordConfig{
   115  				Additions: []string{"duplicate"},
   116  				Removals:  []string{"duplicate"},
   117  			},
   118  		}
   119  
   120  		err := ValidateUserConfigUpdate(validInitial, updated)
   121  		require.EqualError(t, err, "found 'duplicate' in both stopwords.additions and stopwords.removals")
   122  	})
   123  
   124  	t.Run("with invalid updated inverted index null state change", func(t *testing.T) {
   125  		updated := &models.InvertedIndexConfig{
   126  			IndexNullState: true,
   127  		}
   128  
   129  		err := ValidateUserConfigUpdate(validInitial, updated)
   130  		require.EqualError(t, err, "IndexNullState cannot be changed when updating a schema")
   131  	})
   132  
   133  	t.Run("with invalid updated inverted index property length change", func(t *testing.T) {
   134  		updated := &models.InvertedIndexConfig{
   135  			IndexPropertyLength: true,
   136  		}
   137  
   138  		err := ValidateUserConfigUpdate(validInitial, updated)
   139  		require.EqualError(t, err, "IndexPropertyLength cannot be changed when updating a schema")
   140  	})
   141  }