generated from milosgajdos/go-repo-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
embeddings_test.go
69 lines (62 loc) · 1.21 KB
/
embeddings_test.go
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
package embeddings
import (
"reflect"
"testing"
)
func TestToFloat32(t *testing.T) {
e := Embedding{
Vector: []float64{1.0, 2.0, 3.0},
}
exp := []float32{1.0, 2.0, 3.0}
got := e.ToFloat32()
if len(got) != len(exp) {
t.Fatalf("expected %d vals, got %v", len(exp), len(got))
}
for i, f := range got {
if exp[i] != f {
t.Fatalf("expected %v, got %v", exp, got)
}
}
}
func TestBase64Decode(t *testing.T) {
t.Parallel()
testCases := []struct {
name string
given string
exp []float64
wantErr bool
}{
{
name: "valid",
given: "H4XrUbgeCUAAAAAAAABFwESLbOf7QI9A",
exp: []float64{3.14, -42.0, 1000.123},
wantErr: false,
},
{
name: "invalid",
given: "garbage",
wantErr: true,
},
}
for _, tc := range testCases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
embBase64 := Base64(tc.given)
got, err := embBase64.Decode()
if err != nil {
if !tc.wantErr {
t.Fatalf("unexpected error: %v", err)
}
return
}
// no error, but we expect one
if tc.wantErr {
t.Fatal("expected error")
}
if !reflect.DeepEqual(got.Vector, tc.exp) {
t.Fatalf("expected: %v, got: %v", tc.exp, got.Vector)
}
})
}
}