github.com/weaviate/weaviate@v1.24.6/usecases/sharding/config_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 sharding
    13  
    14  import (
    15  	"encoding/json"
    16  	"errors"
    17  	"testing"
    18  
    19  	"github.com/stretchr/testify/assert"
    20  	"github.com/stretchr/testify/require"
    21  )
    22  
    23  func Test_Config(t *testing.T) {
    24  	type test struct {
    25  		name        string
    26  		input       interface{}
    27  		expected    Config
    28  		expectedErr error
    29  	}
    30  
    31  	tests := []test{
    32  		{
    33  			name:  "nothing specified, all defaults",
    34  			input: nil,
    35  			expected: Config{
    36  				VirtualPerPhysical:  DefaultVirtualPerPhysical,
    37  				DesiredCount:        7, // cluster size
    38  				DesiredVirtualCount: DefaultVirtualPerPhysical * 7,
    39  				ActualCount:         7, // cluster size
    40  				ActualVirtualCount:  DefaultVirtualPerPhysical * 7,
    41  				Key:                 DefaultKey,
    42  				Strategy:            DefaultStrategy,
    43  				Function:            DefaultFunction,
    44  			},
    45  		},
    46  
    47  		{
    48  			name: "everything specified, everything legal",
    49  			input: map[string]interface{}{
    50  				"virtualPerPhysical":  json.Number("64"),
    51  				"desiredCount":        json.Number("3"),
    52  				"desiredVirtualCount": json.Number("192"),
    53  				"replicas":            json.Number("3"),
    54  				"key":                 "_id",
    55  				"strategy":            "hash",
    56  				"function":            "murmur3",
    57  			},
    58  			expected: Config{
    59  				VirtualPerPhysical:  64,
    60  				DesiredCount:        3,
    61  				DesiredVirtualCount: 192,
    62  				ActualCount:         3,
    63  				ActualVirtualCount:  192,
    64  				Key:                 "_id",
    65  				Strategy:            "hash",
    66  				Function:            "murmur3",
    67  			},
    68  		},
    69  
    70  		{
    71  			name: "everything specified, everything legal, from disk using floats for numbers",
    72  			input: map[string]interface{}{
    73  				"virtualPerPhysical":  float64(64),
    74  				"desiredCount":        float64(3),
    75  				"desiredVirtualCount": float64(192),
    76  				"replicas":            float64(4),
    77  				"key":                 "_id",
    78  				"strategy":            "hash",
    79  				"function":            "murmur3",
    80  			},
    81  			expected: Config{
    82  				VirtualPerPhysical:  64,
    83  				DesiredCount:        3,
    84  				DesiredVirtualCount: 192,
    85  				ActualCount:         3,
    86  				ActualVirtualCount:  192,
    87  				Key:                 "_id",
    88  				Strategy:            "hash",
    89  				Function:            "murmur3",
    90  			},
    91  		},
    92  
    93  		{
    94  			name: "unsupported sharding key",
    95  			input: map[string]interface{}{
    96  				"key":      "myCustomField",
    97  				"strategy": "hash",
    98  				"function": "murmur3",
    99  			},
   100  			expectedErr: errors.New("sharding only supported on key '_id' " +
   101  				"for now, got: myCustomField"),
   102  		},
   103  
   104  		{
   105  			name: "unsupported sharding strategy",
   106  			input: map[string]interface{}{
   107  				"key":      "_id",
   108  				"strategy": "range",
   109  				"function": "murmur3",
   110  			},
   111  			expectedErr: errors.New("sharding only supported with strategy 'hash' " +
   112  				"for now, got: range"),
   113  		},
   114  
   115  		{
   116  			name: "unsupported sharding function",
   117  			input: map[string]interface{}{
   118  				"key":      "_id",
   119  				"strategy": "hash",
   120  				"function": "md5",
   121  			},
   122  			expectedErr: errors.New("sharding only supported with function 'murmur3' " +
   123  				"for now, got: md5"),
   124  		},
   125  	}
   126  
   127  	for _, test := range tests {
   128  		t.Run(test.name, func(t *testing.T) {
   129  			cfg, err := ParseConfig(test.input, 7) // pretend cluster size is 7
   130  
   131  			if test.expectedErr == nil {
   132  				assert.Nil(t, err)
   133  				assert.Equal(t, test.expected, cfg)
   134  			} else {
   135  				require.NotNil(t, err, "should have error'd")
   136  				assert.Equal(t, test.expectedErr.Error(), err.Error())
   137  			}
   138  		})
   139  	}
   140  }