github.com/weaviate/weaviate@v1.24.6/modules/multi2vec-clip/vectorizer/class_settings_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 vectorizer
    13  
    14  import (
    15  	"encoding/json"
    16  	"testing"
    17  
    18  	"github.com/weaviate/weaviate/entities/moduletools"
    19  )
    20  
    21  func Test_classSettings_Validate(t *testing.T) {
    22  	type fields struct {
    23  		cfg moduletools.ClassConfig
    24  	}
    25  	tests := []struct {
    26  		name    string
    27  		fields  fields
    28  		wantErr bool
    29  	}{
    30  		{
    31  			name:    "should not pass with empty config",
    32  			wantErr: true,
    33  		},
    34  		{
    35  			name: "should not pass with nil config",
    36  			fields: fields{
    37  				cfg: nil,
    38  			},
    39  			wantErr: true,
    40  		},
    41  		{
    42  			name: "should not pass with nil imageFields",
    43  			fields: fields{
    44  				cfg: newConfigBuilder().addSetting("imageFields", nil).build(),
    45  			},
    46  			wantErr: true,
    47  		},
    48  		{
    49  			name: "should not pass with fault imageFields value",
    50  			fields: fields{
    51  				cfg: newConfigBuilder().addSetting("imageFields", []string{}).build(),
    52  			},
    53  			wantErr: true,
    54  		},
    55  		{
    56  			name: "should not pass with empty imageFields",
    57  			fields: fields{
    58  				cfg: newConfigBuilder().addSetting("imageFields", []interface{}{}).build(),
    59  			},
    60  			wantErr: true,
    61  		},
    62  		{
    63  			name: "should not pass with empty string in imageFields",
    64  			fields: fields{
    65  				cfg: newConfigBuilder().addSetting("imageFields", []interface{}{""}).build(),
    66  			},
    67  			wantErr: true,
    68  		},
    69  		{
    70  			name: "should not pass with int value in imageFields",
    71  			fields: fields{
    72  				cfg: newConfigBuilder().addSetting("imageFields", []interface{}{1.0}).build(),
    73  			},
    74  			wantErr: true,
    75  		},
    76  		{
    77  			name: "should pass with proper value in imageFields",
    78  			fields: fields{
    79  				cfg: newConfigBuilder().addSetting("imageFields", []interface{}{"field"}).build(),
    80  			},
    81  		},
    82  		{
    83  			name: "should pass with proper value in imageFields and inferenceUrl",
    84  			fields: fields{
    85  				cfg: newConfigBuilder().
    86  					addSetting("inferenceUrl", "http://inference.url").
    87  					addSetting("imageFields", []interface{}{"field"}).build(),
    88  			},
    89  		},
    90  		{
    91  			name: "should pass with proper value in imageFields and textFields",
    92  			fields: fields{
    93  				cfg: newConfigBuilder().
    94  					addSetting("imageFields", []interface{}{"imageField"}).
    95  					addSetting("textFields", []interface{}{"textField"}).
    96  					build(),
    97  			},
    98  		},
    99  		{
   100  			name: "should pass with proper value in 2 imageFields and 2 textFields",
   101  			fields: fields{
   102  				cfg: newConfigBuilder().
   103  					addSetting("textFields", []interface{}{"textField1", "textField2"}).
   104  					addSetting("imageFields", []interface{}{"imageField1", "imageField2"}).
   105  					build(),
   106  			},
   107  		},
   108  		{
   109  			name: "should pass with proper value in 2 imageFields and 2 textFields and weights",
   110  			fields: fields{
   111  				cfg: newConfigBuilder().
   112  					addSetting("textFields", []interface{}{"textField1", "textField2"}).
   113  					addSetting("imageFields", []interface{}{"imageField1", "imageField2"}).
   114  					addWeights([]interface{}{1, 2}, []interface{}{1, 2}).
   115  					build(),
   116  			},
   117  		},
   118  		{
   119  			name: "should pass with proper value in 1 imageFields and 2 textFields and weights",
   120  			fields: fields{
   121  				cfg: newConfigBuilder().
   122  					addSetting("textFields", []interface{}{"textField1", "textField2"}).
   123  					addSetting("imageFields", []interface{}{"imageField1"}).
   124  					addWeights([]interface{}{1, 2}, []interface{}{1}).
   125  					build(),
   126  			},
   127  		},
   128  		{
   129  			name: "should pass with proper value in 2 imageFields and 2 textFields and weights",
   130  			fields: fields{
   131  				cfg: newConfigBuilder().
   132  					addSetting("textFields", []interface{}{"textField1", "textField2"}).
   133  					addSetting("imageFields", []interface{}{"imageField1"}).
   134  					addWeights([]interface{}{1, 2}, []interface{}{1}).
   135  					build(),
   136  			},
   137  		},
   138  		{
   139  			name: "should not pass with proper value in 1 imageFields and 2 textFields and weights",
   140  			fields: fields{
   141  				cfg: newConfigBuilder().
   142  					addSetting("textFields", []interface{}{"textField1", "textField2"}).
   143  					addSetting("imageFields", []interface{}{"imageField1"}).
   144  					addWeights([]interface{}{1}, []interface{}{1}).
   145  					build(),
   146  			},
   147  			wantErr: true,
   148  		},
   149  		{
   150  			name: "should not pass with not proper weight value in 2 imageFields and 2 textFields and weights",
   151  			fields: fields{
   152  				cfg: newConfigBuilder().
   153  					addSetting("textFields", []interface{}{"textField1", "textField2"}).
   154  					addSetting("imageFields", []interface{}{"imageField1"}).
   155  					addWeights([]interface{}{1, "aaaa"}, []interface{}{1}).
   156  					build(),
   157  			},
   158  			wantErr: true,
   159  		},
   160  		{
   161  			name: "should not pass with not proper weight value in 2 imageFields and 2 textFields and weights",
   162  			fields: fields{
   163  				cfg: newConfigBuilder().
   164  					addSetting("textFields", []interface{}{"textField1", "textField2"}).
   165  					addSetting("imageFields", []interface{}{"imageField1"}).
   166  					addWeights([]interface{}{json.Number("1"), json.Number("2")}, []interface{}{json.Number("3")}).
   167  					build(),
   168  			},
   169  		},
   170  	}
   171  	for _, tt := range tests {
   172  		t.Run(tt.name, func(t *testing.T) {
   173  			ic := NewClassSettings(tt.fields.cfg)
   174  			if err := ic.Validate(); (err != nil) != tt.wantErr {
   175  				t.Errorf("classSettings.Validate() error = %v, wantErr %v", err, tt.wantErr)
   176  			}
   177  		})
   178  	}
   179  }