github.com/weaviate/weaviate@v1.24.6/usecases/schema/helpers_for_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  	"fmt"
    17  
    18  	"github.com/weaviate/weaviate/entities/models"
    19  	"github.com/weaviate/weaviate/entities/schema"
    20  	"github.com/weaviate/weaviate/entities/vectorindex/common"
    21  	"github.com/weaviate/weaviate/usecases/cluster"
    22  )
    23  
    24  type fakeRepo struct {
    25  	schema State
    26  }
    27  
    28  func newFakeRepo() *fakeRepo {
    29  	return &fakeRepo{schema: NewState(2)}
    30  }
    31  
    32  func (f *fakeRepo) Save(ctx context.Context, schema State) error {
    33  	f.schema = schema
    34  	return nil
    35  }
    36  
    37  func (f *fakeRepo) Load(context.Context) (State, error) {
    38  	return f.schema, nil
    39  }
    40  
    41  func (f *fakeRepo) NewClass(context.Context, ClassPayload) error {
    42  	return nil
    43  }
    44  
    45  func (f *fakeRepo) UpdateClass(context.Context, ClassPayload) error {
    46  	return nil
    47  }
    48  
    49  func (f *fakeRepo) DeleteClass(ctx context.Context, class string) error {
    50  	return nil
    51  }
    52  
    53  func (f *fakeRepo) NewShards(ctx context.Context, class string, shards []KeyValuePair) error {
    54  	return nil
    55  }
    56  
    57  func (f *fakeRepo) UpdateShards(ctx context.Context, class string, shards []KeyValuePair) error {
    58  	return nil
    59  }
    60  
    61  func (f *fakeRepo) DeleteShards(_ context.Context, class string, _ []string) error {
    62  	return nil
    63  }
    64  
    65  type fakeAuthorizer struct{}
    66  
    67  func (f *fakeAuthorizer) Authorize(principal *models.Principal, verb, resource string) error {
    68  	return nil
    69  }
    70  
    71  type fakeVectorConfig struct {
    72  	raw interface{}
    73  }
    74  
    75  func (f fakeVectorConfig) IndexType() string {
    76  	return "fake"
    77  }
    78  
    79  func (f fakeVectorConfig) DistanceName() string {
    80  	return common.DistanceCosine
    81  }
    82  
    83  func dummyParseVectorConfig(in interface{}, vectorIndexType string) (schema.VectorIndexConfig, error) {
    84  	return fakeVectorConfig{raw: in}, nil
    85  }
    86  
    87  func dummyValidateInvertedConfig(in *models.InvertedIndexConfig) error {
    88  	return nil
    89  }
    90  
    91  type fakeVectorizerValidator struct {
    92  	valid []string
    93  }
    94  
    95  func (f *fakeVectorizerValidator) ValidateVectorizer(moduleName string) error {
    96  	for _, valid := range f.valid {
    97  		if moduleName == valid {
    98  			return nil
    99  		}
   100  	}
   101  
   102  	return fmt.Errorf("invalid vectorizer %q", moduleName)
   103  }
   104  
   105  type fakeModuleConfig struct{}
   106  
   107  func (f *fakeModuleConfig) SetClassDefaults(class *models.Class) {
   108  	defaultConfig := map[string]interface{}{
   109  		"my-module1": map[string]interface{}{
   110  			"my-setting": "default-value",
   111  		},
   112  	}
   113  
   114  	asMap, ok := class.ModuleConfig.(map[string]interface{})
   115  	if !ok {
   116  		class.ModuleConfig = defaultConfig
   117  		return
   118  	}
   119  
   120  	module, ok := asMap["my-module1"]
   121  	if !ok {
   122  		class.ModuleConfig = defaultConfig
   123  		return
   124  	}
   125  
   126  	asMap, ok = module.(map[string]interface{})
   127  	if !ok {
   128  		class.ModuleConfig = defaultConfig
   129  		return
   130  	}
   131  
   132  	if _, ok := asMap["my-setting"]; !ok {
   133  		asMap["my-setting"] = "default-value"
   134  		defaultConfig["my-module1"] = asMap
   135  		class.ModuleConfig = defaultConfig
   136  	}
   137  }
   138  
   139  func (f *fakeModuleConfig) SetSinglePropertyDefaults(class *models.Class,
   140  	prop *models.Property) {
   141  }
   142  
   143  func (f *fakeModuleConfig) ValidateClass(ctx context.Context, class *models.Class) error {
   144  	return nil
   145  }
   146  
   147  type fakeClusterState struct {
   148  	hosts       []string
   149  	syncIgnored bool
   150  	skipRepair  bool
   151  }
   152  
   153  func (f *fakeClusterState) SchemaSyncIgnored() bool {
   154  	return f.syncIgnored
   155  }
   156  
   157  func (f *fakeClusterState) SkipSchemaRepair() bool {
   158  	return f.skipRepair
   159  }
   160  
   161  func (f *fakeClusterState) Hostnames() []string {
   162  	return f.hosts
   163  }
   164  
   165  func (f *fakeClusterState) AllNames() []string {
   166  	return f.hosts
   167  }
   168  
   169  func (f *fakeClusterState) Candidates() []string {
   170  	return f.hosts
   171  }
   172  
   173  func (f *fakeClusterState) LocalName() string {
   174  	return "node1"
   175  }
   176  
   177  func (f *fakeClusterState) NodeCount() int {
   178  	return 1
   179  }
   180  
   181  func (f *fakeClusterState) ClusterHealthScore() int {
   182  	return 0
   183  }
   184  
   185  func (f *fakeClusterState) ResolveParentNodes(string, string,
   186  ) (map[string]string, error) {
   187  	return nil, nil
   188  }
   189  
   190  func (f *fakeClusterState) NodeHostname(string) (string, bool) {
   191  	return "", false
   192  }
   193  
   194  type fakeTxClient struct {
   195  	openInjectPayload interface{}
   196  	openErr           error
   197  	abortErr          error
   198  	commitErr         error
   199  }
   200  
   201  func (f *fakeTxClient) OpenTransaction(ctx context.Context, host string, tx *cluster.Transaction) error {
   202  	if f.openInjectPayload != nil {
   203  		tx.Payload = f.openInjectPayload
   204  	}
   205  
   206  	return f.openErr
   207  }
   208  
   209  func (f *fakeTxClient) AbortTransaction(ctx context.Context, host string, tx *cluster.Transaction) error {
   210  	return f.abortErr
   211  }
   212  
   213  func (f *fakeTxClient) CommitTransaction(ctx context.Context, host string, tx *cluster.Transaction) error {
   214  	return f.commitErr
   215  }