-
Notifications
You must be signed in to change notification settings - Fork 0
/
id_test.go
70 lines (60 loc) · 1.88 KB
/
id_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
70
package mongo_test
import (
"regexp"
"testing"
"go.mongodb.org/mongo-driver/bson/primitive"
mongo "github.com/Dragomir-Ivanov/rest-layer-mongo-driver"
)
const (
validObjectID = "59a40602952dbd0001c3ffc9"
invalidObjectID = "59a40602952dbd0001c3ffc9f"
)
func TestObjectIDValidate(t *testing.T) {
v := &mongo.ObjectID{}
t.Run("validObjectID", func(t *testing.T) {
expect, _ := primitive.ObjectIDFromHex(validObjectID)
id, err := v.Validate(validObjectID)
if expect != id {
t.Errorf("v.Validate(validObjectID):\n %v (expect) != %v (actual)", expect, id)
}
if err != nil {
t.Error("v.Validate(validObjectID):\n unexpected error:", err)
}
})
t.Run("invalidObjectID", func(t *testing.T) {
id, err := v.Validate(invalidObjectID)
if nil != id {
t.Errorf("v.Validate(invalidObjectID):\n %v (expect) != %v (actual)", nil, id)
}
if err == nil {
t.Error("v.Validate(invalidObjectID):\n expected error, got nil")
}
})
}
func TestObjectIDJSONSchmea(t *testing.T) {
v := &mongo.ObjectID{}
m, err := v.BuildJSONSchema()
if err != nil {
t.Error("_, err := v.BuildJSONSchema():\n unexpected error:", err)
}
if m == nil {
t.Fatal("m, _ := v.BuildJSONSchema():\n expected m not to be nil")
}
if s := m["type"]; s != "string" {
t.Fatalf("m, _ := v.BuildJSONSchema(); m[\"type\"]\n %v (expected) != %v (actual)", "string", s)
}
re, err := regexp.Compile(m["pattern"].(string))
if err != nil {
t.Fatal("_, err := regexp.Compile(m[\"type\"]);\n unexpected error:", m, err)
}
t.Run("validObjectID", func(t *testing.T) {
if match := re.MatchString(validObjectID); !match {
t.Errorf("re.MatchString(validObjectID)\n %v (expected) != %v (actual)", true, match)
}
})
t.Run("invalidObjectID", func(t *testing.T) {
if match := re.MatchString(invalidObjectID); match {
t.Errorf("re.MatchString(invalidObjectID)\n %v (expected) != %v (actual)", false, match)
}
})
}