-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrag_test.go
More file actions
91 lines (85 loc) · 2.06 KB
/
rag_test.go
File metadata and controls
91 lines (85 loc) · 2.06 KB
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
package main
import (
"testing"
)
func TestSerializeDeserialize(t *testing.T) {
testCases := []struct {
doc Document
expected Document
}{
{
doc: Document{
embedding: []float32{1.0, 2.0, 3.0},
content: "This is a test document.",
metadata: map[string]string{"key1": "value1"},
},
expected: Document{
embedding: []float32{1.0, 2.0, 3.0},
content: "This is a test document.",
metadata: map[string]string{"key1": "value1"},
},
},
{
doc: Document{
embedding: []float32{1.0, 2.0, 3.0},
content: "This is a test document.",
metadata: map[string]string{},
},
expected: Document{
embedding: []float32{1.0, 2.0, 3.0},
content: "This is a test document.",
metadata: map[string]string{},
},
},
}
for _, tc := range testCases {
serialized, err := serializeDoc(tc.doc)
if err != nil {
t.Fatalf("serializeDoc failed: %v", err)
}
deserialized, err := deserializeDoc(serialized)
if err != nil {
t.Fatalf("deserializeDoc failed: %v", err)
}
if !float32SlicesEqual(deserialized.embedding, tc.expected.embedding) {
t.Errorf("Embedding mismatch: got %v, want %v", deserialized.embedding, tc.expected.embedding)
}
if deserialized.content != tc.expected.content {
t.Errorf("Content mismatch: got %q, want %q", deserialized.content, tc.expected.content)
}
if !mapsEqual(deserialized.metadata, tc.expected.metadata) {
t.Errorf("Metadata mismatch: got %v, want %v", deserialized.metadata, tc.expected.metadata)
}
}
}
func float32SlicesEqual(a, b []float32) bool {
if len(a) != len(b) {
return false
}
for i, v := range a {
if v != b[i] {
return false
}
}
return true
}
func mapsEqual(a, b map[string]string) bool {
if len(a) != len(b) {
return false
}
for k, v := range a {
if b[k] != v {
return false
}
}
return true
}
func TestDotProduct(t *testing.T) {
a := []float32{1, 2, 3}
b := []float32{4, 5, 6}
expected := float32(32)
got := dotProduct(a, b)
if got != expected {
t.Errorf("Dot product failed, got %f, expected %f", got, expected)
}
}