8
8
"github.com/stretchr/testify/assert"
9
9
)
10
10
11
- // Ensure MockEmbedder satisfies the Embedder interface
12
11
var _ Embedder = (* MockEmbedder )(nil )
13
12
14
13
type MockEmbedder struct {
@@ -20,28 +19,57 @@ func (m *MockEmbedder) Embed(ctx context.Context, input string) ([]float32, erro
20
19
return m .Response , m .Err
21
20
}
22
21
23
- func TestMockEmbedder_Success (t * testing.T ) {
24
- mock := & MockEmbedder {
25
- Response : make ([]float32 , 768 ),
26
- }
22
+ func TestMockEmbedder (t * testing.T ) {
23
+ t .Run ("Success" , func (t * testing.T ) {
24
+ mock := & MockEmbedder {
25
+ Response : make ([]float32 , 768 ),
26
+ }
27
27
28
- ctx := context .Background ()
29
- input := "test input"
30
- result , err := mock .Embed (ctx , input )
28
+ ctx := context .Background ()
29
+ input := "test input"
30
+
31
+ result , err := mock .Embed (ctx , input )
32
+
33
+ assert .NoError (t , err , "expected no error on successful embed" )
34
+ assert .NotNil (t , result , "expected a non-nil result" )
35
+ assert .Equal (t , 768 , len (result ), "expected result to have 768 dimensions" )
36
+ })
37
+
38
+ t .Run ("Error" , func (t * testing.T ) {
39
+ expectedErr := errors .New ("mock error" )
40
+ mock := & MockEmbedder {
41
+ Err : expectedErr ,
42
+ }
31
43
32
- assert .NoError (t , err , "expected no error on successful embed" )
33
- assert .NotNil (t , result , "expected a non-nil result" )
34
- assert .Equal (t , 768 , len (result ), "expected result to have 768 dimensions" )
44
+ ctx := context .Background ()
45
+ result , err := mock .Embed (ctx , "fail case" )
46
+
47
+ assert .Error (t , err , "expected an error on embed failure" )
48
+ assert .ErrorIs (t , err , expectedErr , "error should match mock error" )
49
+ assert .Nil (t , result , "expected result to be nil on error" )
50
+ })
51
+
52
+ t .Run ("Empty input" , func (t * testing.T ) {
53
+ mock := & MockEmbedder {
54
+ Response : make ([]float32 , 768 ),
55
+ }
56
+
57
+ ctx := context .Background ()
58
+ result , err := mock .Embed (ctx , "" )
59
+
60
+ assert .NoError (t , err , "expected no error on empty input" )
61
+ assert .NotNil (t , result , "expected result even with empty input" )
62
+ assert .Equal (t , 768 , len (result ), "expected result to have 768 dimensions" )
63
+ })
35
64
}
36
65
37
- func TestMockEmbedder_Error ( t * testing.T ) {
66
+ func BenchmarkMockEmbedder ( b * testing.B ) {
38
67
mock := & MockEmbedder {
39
- Err : errors . New ( "mock error" ),
68
+ Response : make ([] float32 , 768 ),
40
69
}
41
70
42
71
ctx := context .Background ()
43
- result , err := mock .Embed (ctx , "fail case" )
44
-
45
- assert .Error (t , err , "expected an error on embed failure" )
46
- assert .Nil (t , result , "expected result to be nil on error" )
72
+ for i := 0 ; i < b .N ; i ++ {
73
+ _ , _ = mock .Embed (ctx , "benchmark input" )
74
+ }
47
75
}
0 commit comments