github.com/weaviate/weaviate@v1.24.6/modules/qna-transformers/ask/grapqhl_extract_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 ask
    13  
    14  import (
    15  	"reflect"
    16  	"testing"
    17  )
    18  
    19  func Test_extractAskFn(t *testing.T) {
    20  	type args struct {
    21  		source map[string]interface{}
    22  	}
    23  	tests := []struct {
    24  		name string
    25  		args args
    26  		want interface{}
    27  	}{
    28  		{
    29  			name: "should parse properly with only question",
    30  			args: args{
    31  				source: map[string]interface{}{
    32  					"question": "some question",
    33  				},
    34  			},
    35  			want: &AskParams{
    36  				Question: "some question",
    37  			},
    38  		},
    39  		{
    40  			name: "should parse properly with question and distance",
    41  			args: args{
    42  				source: map[string]interface{}{
    43  					"question": "some question",
    44  					"distance": 0.8,
    45  				},
    46  			},
    47  			want: &AskParams{
    48  				Question:     "some question",
    49  				Distance:     0.8,
    50  				WithDistance: true,
    51  			},
    52  		},
    53  		{
    54  			name: "should parse properly with question and certainty",
    55  			args: args{
    56  				source: map[string]interface{}{
    57  					"question":  "some question",
    58  					"certainty": 0.8,
    59  				},
    60  			},
    61  			want: &AskParams{
    62  				Question:  "some question",
    63  				Certainty: 0.8,
    64  			},
    65  		},
    66  		{
    67  			name: "should parse properly without params",
    68  			args: args{
    69  				source: map[string]interface{}{},
    70  			},
    71  			want: &AskParams{},
    72  		},
    73  		{
    74  			name: "should parse properly with question, distance, and properties",
    75  			args: args{
    76  				source: map[string]interface{}{
    77  					"question":   "some question",
    78  					"distance":   0.8,
    79  					"properties": []interface{}{"prop1", "prop2"},
    80  				},
    81  			},
    82  			want: &AskParams{
    83  				Question:     "some question",
    84  				Distance:     0.8,
    85  				WithDistance: true,
    86  				Properties:   []string{"prop1", "prop2"},
    87  			},
    88  		},
    89  		{
    90  			name: "should parse properly with question and certainty and properties",
    91  			args: args{
    92  				source: map[string]interface{}{
    93  					"question":   "some question",
    94  					"certainty":  0.8,
    95  					"properties": []interface{}{"prop1", "prop2"},
    96  				},
    97  			},
    98  			want: &AskParams{
    99  				Question:   "some question",
   100  				Certainty:  0.8,
   101  				Properties: []string{"prop1", "prop2"},
   102  			},
   103  		},
   104  		{
   105  			name: "should parse properly with question, distance, properties, and rerank",
   106  			args: args{
   107  				source: map[string]interface{}{
   108  					"question":   "some question",
   109  					"distance":   0.8,
   110  					"properties": []interface{}{"prop1", "prop2"},
   111  					"rerank":     true,
   112  				},
   113  			},
   114  			want: &AskParams{
   115  				Question:     "some question",
   116  				Distance:     0.8,
   117  				WithDistance: true,
   118  				Properties:   []string{"prop1", "prop2"},
   119  				Rerank:       true,
   120  			},
   121  		},
   122  		{
   123  			name: "should parse properly with question and certainty and properties and rerank",
   124  			args: args{
   125  				source: map[string]interface{}{
   126  					"question":   "some question",
   127  					"certainty":  0.8,
   128  					"properties": []interface{}{"prop1", "prop2"},
   129  					"rerank":     true,
   130  				},
   131  			},
   132  			want: &AskParams{
   133  				Question:   "some question",
   134  				Certainty:  0.8,
   135  				Properties: []string{"prop1", "prop2"},
   136  				Rerank:     true,
   137  			},
   138  		},
   139  	}
   140  
   141  	testsWithAutocorrect := []struct {
   142  		name string
   143  		args args
   144  		want interface{}
   145  	}{
   146  		{
   147  			name: "should parse properly with only question and autocorrect",
   148  			args: args{
   149  				source: map[string]interface{}{
   150  					"question":    "some question",
   151  					"autocorrect": true,
   152  				},
   153  			},
   154  			want: &AskParams{
   155  				Question:    "some question",
   156  				Autocorrect: true,
   157  			},
   158  		},
   159  		{
   160  			name: "should parse properly and transform text in question",
   161  			args: args{
   162  				source: map[string]interface{}{
   163  					"question":    "transform this",
   164  					"autocorrect": true,
   165  				},
   166  			},
   167  			want: &AskParams{
   168  				Question:    "transformed text",
   169  				Autocorrect: true,
   170  			},
   171  		},
   172  		{
   173  			name: "should parse properly and not transform text in question",
   174  			args: args{
   175  				source: map[string]interface{}{
   176  					"question":    "transform this",
   177  					"autocorrect": false,
   178  				},
   179  			},
   180  			want: &AskParams{
   181  				Question:    "transform this",
   182  				Autocorrect: false,
   183  			},
   184  		},
   185  		{
   186  			name: "should parse properly with question, distance, properties, and autocorrect",
   187  			args: args{
   188  				source: map[string]interface{}{
   189  					"question":    "transform this",
   190  					"distance":    0.8,
   191  					"properties":  []interface{}{"prop1", "prop2"},
   192  					"autocorrect": true,
   193  				},
   194  			},
   195  			want: &AskParams{
   196  				Question:     "transformed text",
   197  				Distance:     0.8,
   198  				WithDistance: true,
   199  				Properties:   []string{"prop1", "prop2"},
   200  				Autocorrect:  true,
   201  			},
   202  		},
   203  		{
   204  			name: "should parse properly with question and certainty and properties and autocorrect",
   205  			args: args{
   206  				source: map[string]interface{}{
   207  					"question":    "transform this",
   208  					"certainty":   0.8,
   209  					"properties":  []interface{}{"prop1", "prop2"},
   210  					"autocorrect": true,
   211  				},
   212  			},
   213  			want: &AskParams{
   214  				Question:    "transformed text",
   215  				Certainty:   0.8,
   216  				Properties:  []string{"prop1", "prop2"},
   217  				Autocorrect: true,
   218  			},
   219  		},
   220  		{
   221  			name: "should parse properly with question, distance, properties, autocorrect, and rerank",
   222  			args: args{
   223  				source: map[string]interface{}{
   224  					"question":    "transform this",
   225  					"distance":    0.8,
   226  					"properties":  []interface{}{"prop1", "prop2"},
   227  					"autocorrect": true,
   228  					"rerank":      true,
   229  				},
   230  			},
   231  			want: &AskParams{
   232  				Question:     "transformed text",
   233  				Distance:     0.8,
   234  				WithDistance: true,
   235  				Properties:   []string{"prop1", "prop2"},
   236  				Autocorrect:  true,
   237  				Rerank:       true,
   238  			},
   239  		},
   240  		{
   241  			name: "should parse properly with question and certainty and properties and autocorrect and rerank",
   242  			args: args{
   243  				source: map[string]interface{}{
   244  					"question":    "transform this",
   245  					"certainty":   0.8,
   246  					"properties":  []interface{}{"prop1", "prop2"},
   247  					"autocorrect": true,
   248  					"rerank":      true,
   249  				},
   250  			},
   251  			want: &AskParams{
   252  				Question:    "transformed text",
   253  				Certainty:   0.8,
   254  				Properties:  []string{"prop1", "prop2"},
   255  				Autocorrect: true,
   256  				Rerank:      true,
   257  			},
   258  		},
   259  	}
   260  
   261  	testsWithAutocorrect = append(testsWithAutocorrect, tests...)
   262  
   263  	t.Run("should extract without text transformer", func(t *testing.T) {
   264  		provider := New(nil)
   265  		for _, tt := range tests {
   266  			t.Run(tt.name, func(t *testing.T) {
   267  				if got := provider.extractAskFn(tt.args.source); !reflect.DeepEqual(got, tt.want) {
   268  					t.Errorf("extractAskFn() = %v, want %v", got, tt.want)
   269  				}
   270  			})
   271  		}
   272  	})
   273  	t.Run("should extract with text transformer", func(t *testing.T) {
   274  		provider := New(&fakeTransformer{})
   275  		for _, tt := range testsWithAutocorrect {
   276  			t.Run(tt.name, func(t *testing.T) {
   277  				if got := provider.extractAskFn(tt.args.source); !reflect.DeepEqual(got, tt.want) {
   278  					t.Errorf("extractAskFn() = %v, want %v", got, tt.want)
   279  				}
   280  			})
   281  		}
   282  	})
   283  }