github.com/weaviate/weaviate@v1.24.6/modules/sum-transformers/additional/summary/summary_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 summary 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/search" 21 "github.com/weaviate/weaviate/modules/sum-transformers/ent" 22 ) 23 24 func TestAdditionalAnswerProvider(t *testing.T) { 25 t.Run("should fail with empty content", func(t *testing.T) { 26 // given 27 sumClient := &fakeSUMClient{} 28 summaryProvider := New(sumClient) 29 in := []search.Result{ 30 { 31 ID: "some-uuid", 32 }, 33 } 34 fakeParams := &Params{} 35 limit := 1 36 argumentModuleParams := map[string]interface{}{} 37 38 // when 39 out, err := summaryProvider.AdditionalPropertyFn(context.Background(), in, fakeParams, &limit, argumentModuleParams, nil) 40 41 // then 42 require.NotNil(t, err) 43 require.NotEmpty(t, out) 44 assert.Error(t, err, "empty schema content") 45 }) 46 47 t.Run("should fail with empty params", func(t *testing.T) { 48 // given 49 sumClient := &fakeSUMClient{} 50 summaryProvider := New(sumClient) 51 in := []search.Result{ 52 { 53 ID: "some-uuid", 54 Schema: map[string]interface{}{ 55 "content": "content", 56 }, 57 }, 58 } 59 fakeParams := &Params{} 60 limit := 1 61 argumentModuleParams := map[string]interface{}{} 62 63 // when 64 out, err := summaryProvider.AdditionalPropertyFn(context.Background(), in, fakeParams, &limit, argumentModuleParams, nil) 65 66 // then 67 require.NotNil(t, err) 68 require.NotEmpty(t, out) 69 assert.Error(t, err, "empty params") 70 }) 71 72 t.Run("should summarize", func(t *testing.T) { 73 sumClient := &fakeSUMClient{} 74 summaryProvider := New(sumClient) 75 in := []search.Result{ 76 { 77 ID: "some-uuid", 78 Schema: map[string]interface{}{ 79 "content": "this is the content", 80 }, 81 }, 82 } 83 fakeParams := &Params{Properties: []string{"content"}} 84 limit := 1 85 argumentModuleParams := map[string]interface{}{} 86 87 // when 88 out, err := summaryProvider.AdditionalPropertyFn(context.Background(), in, fakeParams, &limit, argumentModuleParams, nil) 89 // then 90 require.Nil(t, err) 91 require.NotEmpty(t, out) 92 assert.Equal(t, 1, len(in)) 93 answer, answerOK := in[0].AdditionalProperties["summary"] 94 assert.True(t, answerOK) 95 assert.NotNil(t, answer) 96 answerAdditional, answerAdditionalOK := answer.([]ent.SummaryResult) 97 assert.True(t, answerAdditionalOK) 98 assert.Equal(t, "this is the summary", answerAdditional[0].Result) 99 assert.Equal(t, "content", answerAdditional[0].Property) 100 }) 101 } 102 103 type fakeSUMClient struct{} 104 105 func (c *fakeSUMClient) GetSummary(ctx context.Context, property, text string, 106 ) ([]ent.SummaryResult, error) { 107 return c.getSummary(property), nil 108 } 109 110 func (c *fakeSUMClient) getSummary(property string) []ent.SummaryResult { 111 return []ent.SummaryResult{{ 112 Property: property, 113 Result: "this is the summary", 114 }} 115 }