-
Notifications
You must be signed in to change notification settings - Fork 2
/
primitives_test.go
54 lines (48 loc) · 1.05 KB
/
primitives_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
package mgs
import (
"errors"
"testing"
)
// PrimitivesImpl converts string to ObjectID.
type PrimitivesImpl struct{}
func (p PrimitivesImpl) ObjectID(val string) (interface{}, error) {
if val == "" {
return nil, errors.New("empty string is not a valid ObjectID")
}
// Mocking a valid ObjectID for demonstration purposes
return val + "_ObjectID", nil
}
func TestPrimitivesObjectID(t *testing.T) {
p := PrimitivesImpl{}
tests := []struct {
name string
input string
want interface{}
expectErr bool
}{
{
name: "Valid input",
input: "12345",
want: "12345_ObjectID",
expectErr: false,
},
{
name: "Empty input",
input: "",
want: nil,
expectErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := p.ObjectID(tt.input)
if (err != nil) != tt.expectErr {
t.Errorf("ObjectID() error = %v, expectErr %v", err, tt.expectErr)
return
}
if got != tt.want {
t.Errorf("ObjectID() got = %v, want %v", got, tt.want)
}
})
}
}