github.com/weaviate/weaviate@v1.24.6/usecases/modulecomponents/arguments/nearText/searcher_movements_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 nearText 13 14 import ( 15 "fmt" 16 "testing" 17 18 "github.com/stretchr/testify/assert" 19 ) 20 21 func TestMoveVectorToAnother(t *testing.T) { 22 t.Run("moving towards one another", func(t *testing.T) { 23 type testCase struct { 24 name string 25 source []float32 26 target []float32 27 weight float32 28 expectedResult []float32 29 expectedError error 30 } 31 32 tests := []testCase{ 33 { 34 name: "no force", 35 source: []float32{0, 1, 2, 3, 4}, 36 target: []float32{0, 0, 0, 0, 0}, 37 weight: 0, 38 expectedResult: []float32{0, 1, 2, 3, 4}, 39 expectedError: nil, 40 }, 41 { 42 name: "wrong vector sizes", 43 source: []float32{0, 1, 2, 3, 4}, 44 target: []float32{0, 0, 0, 0}, 45 weight: 0, 46 expectedResult: nil, 47 expectedError: fmt.Errorf("movement: vector lengths don't match: got 5 and 4"), 48 }, 49 { 50 name: "force larger 1", 51 source: []float32{0, 1, 2, 3, 4}, 52 target: []float32{0, 0, 0, 0, 0}, 53 weight: 1.5, 54 expectedResult: nil, 55 expectedError: fmt.Errorf("movement: force must be between 0 and 1: got 1.500000"), 56 }, 57 { 58 name: "force smaller 0", 59 source: []float32{0, 1, 2, 3, 4}, 60 target: []float32{0, 0, 0, 0, 0}, 61 weight: -0.2, 62 expectedResult: nil, 63 expectedError: fmt.Errorf("movement: force must be between 0 and 1: got -0.200000"), 64 }, 65 { 66 name: "force equals 1", 67 source: []float32{0, 1, 2, 3, 4}, 68 target: []float32{1, 1, 1, 1, 1}, 69 weight: 1, 70 expectedResult: []float32{0.5, 1, 1.5, 2, 2.5}, 71 expectedError: nil, 72 }, 73 { 74 name: "force equals 0.25", 75 source: []float32{0, 1, 2, 3, 4}, 76 target: []float32{1, 1, 1, 1, 1}, 77 weight: 0.25, 78 expectedResult: []float32{0.125, 1, 1.875, 2.75, 3.625}, 79 expectedError: nil, 80 }, 81 } 82 83 for _, test := range tests { 84 t.Run(test.name, func(t *testing.T) { 85 v := newMovements() 86 res, err := v.MoveTo(test.source, test.target, test.weight) 87 assert.Equal(t, test.expectedError, err) 88 assert.Equal(t, test.expectedResult, res) 89 }) 90 } 91 }) 92 93 t.Run("moving away from one another", func(t *testing.T) { 94 type testCase struct { 95 name string 96 source []float32 97 target []float32 98 weight float32 99 expectedResult []float32 100 expectedError error 101 } 102 103 tests := []testCase{ 104 { 105 name: "no force", 106 source: []float32{1, 2, 3, 4}, 107 target: []float32{0, 0, 0, 0}, 108 weight: 0, 109 expectedResult: []float32{1, 2, 3, 4}, 110 expectedError: nil, 111 }, 112 { 113 name: "wrong vector sizes", 114 source: []float32{0, 1, 2, 3, 4}, 115 target: []float32{0, 0, 0, 0}, 116 weight: 0, 117 expectedResult: nil, 118 expectedError: fmt.Errorf("movement (moveAwayFrom): vector lengths don't match: got 5 and 4"), 119 }, 120 { 121 name: "force smaller 0", 122 source: []float32{0, 1, 2, 3, 4}, 123 target: []float32{0, 0, 0, 0, 0}, 124 weight: -0.2, 125 expectedResult: nil, 126 expectedError: fmt.Errorf("movement (moveAwayFrom): force must be 0 or positive: got -0.200000"), 127 }, 128 { 129 name: "reproducing example from investigation period", 130 source: []float32{1.0, 1.0}, 131 target: []float32{1.2, 0.8}, 132 weight: 1, 133 expectedResult: []float32{0.9, 1.1}, 134 expectedError: nil, 135 }, 136 137 { 138 name: "force equals 0.25", 139 source: []float32{0, 1, 2, 3, 4}, 140 target: []float32{1, 1, 1, 1, 1}, 141 weight: 0.25, 142 expectedResult: []float32{-0.125, 1, 2.125, 3.25, 4.375}, 143 expectedError: nil, 144 }, 145 } 146 147 for _, test := range tests { 148 t.Run(test.name, func(t *testing.T) { 149 v := newMovements() 150 res, err := v.MoveAwayFrom(test.source, test.target, test.weight) 151 assert.Equal(t, test.expectedError, err) 152 for i := range test.expectedResult { 153 assert.InEpsilon(t, test.expectedResult[i], res[i], 0.01) 154 } 155 }) 156 } 157 }) 158 }