github.com/weaviate/weaviate@v1.24.6/modules/qna-openai/additional/answer/answer_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 answer
    13  
    14  import (
    15  	"context"
    16  	"testing"
    17  
    18  	"github.com/stretchr/testify/assert"
    19  	"github.com/stretchr/testify/require"
    20  	"github.com/weaviate/weaviate/entities/moduletools"
    21  	"github.com/weaviate/weaviate/entities/search"
    22  	qnamodels "github.com/weaviate/weaviate/modules/qna-openai/additional/models"
    23  	"github.com/weaviate/weaviate/modules/qna-openai/ent"
    24  )
    25  
    26  func TestAdditionalAnswerProvider(t *testing.T) {
    27  	t.Run("should fail with empty content", func(t *testing.T) {
    28  		// given
    29  		qnaClient := &fakeQnAClient{}
    30  		fakeHelper := &fakeParamsHelper{}
    31  		answerProvider := New(qnaClient, fakeHelper)
    32  		in := []search.Result{
    33  			{
    34  				ID: "some-uuid",
    35  			},
    36  		}
    37  		fakeParams := &Params{}
    38  		limit := 1
    39  		argumentModuleParams := map[string]interface{}{}
    40  
    41  		// when
    42  		out, err := answerProvider.AdditionalPropertyFn(context.Background(), in, fakeParams, &limit, argumentModuleParams, nil)
    43  
    44  		// then
    45  		require.NotNil(t, err)
    46  		require.NotEmpty(t, out)
    47  		assert.Error(t, err, "empty content")
    48  	})
    49  
    50  	t.Run("should fail with empty question", func(t *testing.T) {
    51  		// given
    52  		qnaClient := &fakeQnAClient{}
    53  		fakeHelper := &fakeParamsHelper{}
    54  		answerProvider := New(qnaClient, fakeHelper)
    55  		in := []search.Result{
    56  			{
    57  				ID: "some-uuid",
    58  				Schema: map[string]interface{}{
    59  					"content": "content",
    60  				},
    61  			},
    62  		}
    63  		fakeParams := &Params{}
    64  		limit := 1
    65  		argumentModuleParams := map[string]interface{}{}
    66  
    67  		// when
    68  		out, err := answerProvider.AdditionalPropertyFn(context.Background(), in, fakeParams, &limit, argumentModuleParams, nil)
    69  
    70  		// then
    71  		require.NotNil(t, err)
    72  		require.NotEmpty(t, out)
    73  		assert.Error(t, err, "empty content")
    74  	})
    75  
    76  	t.Run("should answer", func(t *testing.T) {
    77  		// given
    78  		qnaClient := &fakeQnAClient{}
    79  		fakeHelper := &fakeParamsHelper{}
    80  		answerProvider := New(qnaClient, fakeHelper)
    81  		in := []search.Result{
    82  			{
    83  				ID: "some-uuid",
    84  				Schema: map[string]interface{}{
    85  					"content": "content",
    86  				},
    87  			},
    88  		}
    89  		fakeParams := &Params{}
    90  		limit := 1
    91  		argumentModuleParams := map[string]interface{}{
    92  			"ask": map[string]interface{}{
    93  				"question": "question",
    94  			},
    95  		}
    96  
    97  		// when
    98  		out, err := answerProvider.AdditionalPropertyFn(context.Background(), in, fakeParams, &limit, argumentModuleParams, nil)
    99  
   100  		// then
   101  		require.Nil(t, err)
   102  		require.NotEmpty(t, out)
   103  		assert.Equal(t, 1, len(in))
   104  		answer, answerOK := in[0].AdditionalProperties["answer"]
   105  		assert.True(t, answerOK)
   106  		assert.NotNil(t, answer)
   107  		answerAdditional, answerAdditionalOK := answer.(*qnamodels.Answer)
   108  		assert.True(t, answerAdditionalOK)
   109  		assert.Equal(t, "answer", *answerAdditional.Result)
   110  	})
   111  
   112  	t.Run("should answer with property", func(t *testing.T) {
   113  		// given
   114  		qnaClient := &fakeQnAClient{}
   115  		fakeHelper := &fakeParamsHelper{}
   116  		answerProvider := New(qnaClient, fakeHelper)
   117  		in := []search.Result{
   118  			{
   119  				ID: "some-uuid",
   120  				Schema: map[string]interface{}{
   121  					"content":  "content with answer",
   122  					"content2": "this one is just a title",
   123  				},
   124  			},
   125  		}
   126  		fakeParams := &Params{}
   127  		limit := 1
   128  		argumentModuleParams := map[string]interface{}{
   129  			"ask": map[string]interface{}{
   130  				"question":   "question",
   131  				"properties": []string{"content", "content2"},
   132  			},
   133  		}
   134  
   135  		// when
   136  		out, err := answerProvider.AdditionalPropertyFn(context.Background(), in, fakeParams, &limit, argumentModuleParams, nil)
   137  
   138  		// then
   139  		require.Nil(t, err)
   140  		require.NotEmpty(t, out)
   141  		assert.Equal(t, 1, len(in))
   142  		answer, answerOK := in[0].AdditionalProperties["answer"]
   143  		assert.True(t, answerOK)
   144  		assert.NotNil(t, answer)
   145  		answerAdditional, answerAdditionalOK := answer.(*qnamodels.Answer)
   146  		assert.True(t, answerAdditionalOK)
   147  		assert.Equal(t, "answer", *answerAdditional.Result)
   148  		assert.Equal(t, "content", *answerAdditional.Property)
   149  		assert.Equal(t, 13, answerAdditional.StartPosition)
   150  		assert.Equal(t, 19, answerAdditional.EndPosition)
   151  		assert.Equal(t, true, answerAdditional.HasAnswer)
   152  	})
   153  }
   154  
   155  type fakeQnAClient struct{}
   156  
   157  func (c *fakeQnAClient) Answer(ctx context.Context, text, question string, cfg moduletools.ClassConfig) (*ent.AnswerResult, error) {
   158  	return c.getAnswer(question, "answer"), nil
   159  }
   160  
   161  func (c *fakeQnAClient) getAnswer(question, answer string) *ent.AnswerResult {
   162  	return &ent.AnswerResult{
   163  		Text:     question,
   164  		Question: question,
   165  		Answer:   &answer,
   166  	}
   167  }
   168  
   169  type fakeParamsHelper struct{}
   170  
   171  func (h *fakeParamsHelper) GetQuestion(params interface{}) string {
   172  	if fakeParamsMap, ok := params.(map[string]interface{}); ok {
   173  		if question, ok := fakeParamsMap["question"].(string); ok {
   174  			return question
   175  		}
   176  	}
   177  	return ""
   178  }
   179  
   180  func (h *fakeParamsHelper) GetProperties(params interface{}) []string {
   181  	if fakeParamsMap, ok := params.(map[string]interface{}); ok {
   182  		if properties, ok := fakeParamsMap["properties"].([]string); ok {
   183  			return properties
   184  		}
   185  	}
   186  	return nil
   187  }