Spaces:
Running
Running
File size: 5,498 Bytes
b110593 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
// _ _
// __ _____ __ ___ ___ __ _| |_ ___
// \ \ /\ / / _ \/ _` \ \ / / |/ _` | __/ _ \
// \ V V / __/ (_| |\ V /| | (_| | || __/
// \_/\_/ \___|\__,_| \_/ |_|\__,_|\__\___|
//
// Copyright © 2016 - 2024 Weaviate B.V. All rights reserved.
//
// CONTACT: [email protected]
//
package test
import (
"encoding/json"
"fmt"
"testing"
"time"
"github.com/go-openapi/strfmt"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/weaviate/weaviate/client/classifications"
"github.com/weaviate/weaviate/client/objects"
"github.com/weaviate/weaviate/client/schema"
"github.com/weaviate/weaviate/entities/models"
"github.com/weaviate/weaviate/test/helper"
testhelper "github.com/weaviate/weaviate/test/helper"
)
func knnClassification(t *testing.T) {
var id strfmt.UUID
t.Run("ensure class shard for classification is ready", func(t *testing.T) {
testhelper.AssertEventuallyEqualWithFrequencyAndTimeout(t, "READY",
func() interface{} {
shardStatus, err := helper.Client(t).Schema.SchemaObjectsShardsGet(schema.NewSchemaObjectsShardsGetParams().WithClassName("Recipe"), nil)
require.Nil(t, err)
require.GreaterOrEqual(t, len(shardStatus.Payload), 1)
return shardStatus.Payload[0].Status
}, 250*time.Millisecond, 15*time.Second)
})
t.Run("start the classification and wait for completion", func(t *testing.T) {
res, err := helper.Client(t).Classifications.ClassificationsPost(
classifications.NewClassificationsPostParams().WithParams(&models.Classification{
Class: "Recipe",
ClassifyProperties: []string{"ofType"},
BasedOnProperties: []string{"content"},
Type: "knn",
Settings: map[string]interface{}{
"k": 5,
},
}), nil)
require.Nil(t, err)
id = res.Payload.ID
// wait for classification to be completed
testhelper.AssertEventuallyEqualWithFrequencyAndTimeout(t, "completed",
func() interface{} {
res, err := helper.Client(t).Classifications.ClassificationsGet(
classifications.NewClassificationsGetParams().WithID(id.String()), nil)
require.Nil(t, err)
return res.Payload.Status
}, 100*time.Millisecond, 15*time.Second)
})
t.Run("assure changes present", func(t *testing.T) {
// wait for latest changes to be indexed / wait for consistency
testhelper.AssertEventuallyEqual(t, true, func() interface{} {
res, err := helper.Client(t).Objects.ObjectsGet(objects.NewObjectsGetParams().
WithID(unclassifiedSavory), nil)
require.Nil(t, err)
return res.Payload.Properties.(map[string]interface{})["ofType"] != nil
})
testhelper.AssertEventuallyEqual(t, true, func() interface{} {
res, err := helper.Client(t).Objects.ObjectsGet(objects.NewObjectsGetParams().
WithID(unclassifiedSweet), nil)
require.Nil(t, err)
return res.Payload.Properties.(map[string]interface{})["ofType"] != nil
})
})
t.Run("inspect unclassified savory", func(t *testing.T) {
res, err := helper.Client(t).Objects.
ObjectsGet(objects.NewObjectsGetParams().
WithID(unclassifiedSavory).
WithInclude(ptString("classification")), nil)
require.Nil(t, err)
schema, ok := res.Payload.Properties.(map[string]interface{})
require.True(t, ok)
expectedRefTarget := fmt.Sprintf("weaviate://localhost/RecipeType/%s",
recipeTypeSavory)
ref := schema["ofType"].([]interface{})[0].(map[string]interface{})
assert.Equal(t, ref["beacon"].(string), expectedRefTarget)
verifyMetaDistances(t, ref)
})
t.Run("inspect unclassified sweet", func(t *testing.T) {
res, err := helper.Client(t).Objects.
ObjectsGet(objects.NewObjectsGetParams().
WithID(unclassifiedSweet).
WithInclude(ptString("classification")), nil)
require.Nil(t, err)
schema, ok := res.Payload.Properties.(map[string]interface{})
require.True(t, ok)
expectedRefTarget := fmt.Sprintf("weaviate://localhost/RecipeType/%s",
recipeTypeSweet)
ref := schema["ofType"].([]interface{})[0].(map[string]interface{})
assert.Equal(t, ref["beacon"].(string), expectedRefTarget)
verifyMetaDistances(t, ref)
})
}
func verifyMetaDistances(t *testing.T, ref map[string]interface{}) {
classification, ok := ref["classification"].(map[string]interface{})
require.True(t, ok)
assert.Equal(t, json.Number("3"), classification["winningCount"])
assert.Equal(t, json.Number("2"), classification["losingCount"])
assert.Equal(t, json.Number("5"), classification["overallCount"])
closestWinning, err := classification["closestWinningDistance"].(json.Number).Float64()
require.Nil(t, err)
closestLosing, err := classification["closestLosingDistance"].(json.Number).Float64()
require.Nil(t, err)
closestOverall, err := classification["closestOverallDistance"].(json.Number).Float64()
require.Nil(t, err)
meanWinning, err := classification["meanWinningDistance"].(json.Number).Float64()
require.Nil(t, err)
meanLosing, err := classification["meanLosingDistance"].(json.Number).Float64()
require.Nil(t, err)
assert.True(t, closestWinning == closestOverall, "closestWinning == closestOverall")
assert.True(t, closestWinning < meanWinning, "closestWinning < meanWinning")
assert.True(t, closestWinning < closestLosing, "closestWinning < closestLosing")
assert.True(t, closestLosing < meanLosing, "closestLosing < meanLosing")
}
|