-
Notifications
You must be signed in to change notification settings - Fork 153
/
validator_test.go
68 lines (56 loc) · 1.98 KB
/
validator_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
package qmgo
import (
"context"
"go.mongodb.org/mongo-driver/bson"
"testing"
"time"
"github.com/stretchr/testify/require"
)
// User contains user information
type User struct {
FirstName string `bson:"fname"`
LastName string `bson:"lname"`
Age uint8 `bson:"age" validate:"gte=0,lte=130" ` // Age must in [0,130]
Email string `bson:"e-mail" validate:"required,email"` // Email can't be empty string, and must has email format
CreateAt time.Time `bson:"createAt" validate:"lte"` // CreateAt must lte than current time
Relations map[string]string `bson:"relations" validate:"max=2"` // Relations can't has more than 2 elements
}
func TestValidator(t *testing.T) {
ast := require.New(t)
cli := initClient("test")
ctx := context.Background()
defer cli.Close(ctx)
defer cli.DropCollection(ctx)
user := &User{
FirstName: "",
LastName: "",
Age: 45,
Email: "[email protected]",
}
_, err := cli.InsertOne(ctx, user)
ast.NoError(err)
user.Age = 200 // invalid age
_, err = cli.InsertOne(ctx, user)
ast.Error(err)
users := []*User{user, user, user}
_, err = cli.InsertMany(ctx, users)
ast.Error(err)
user.Age = 20
user.Email = "1234@gmail" // email tag, invalid email
err = cli.ReplaceOne(ctx, bson.M{"age": 45}, user)
ast.Error(err)
user.Email = "" // required tag, invalid empty string
_, err = cli.Upsert(ctx, bson.M{"age": 45}, user)
ast.Error(err)
user.Email = "[email protected]"
user.CreateAt = time.Now().Add(1 * time.Hour) // lte tag for time, time must lte current time
_, err = cli.Upsert(ctx, bson.M{"age": 45}, user)
ast.Error(err)
user.CreateAt = time.Now()
user.Relations = map[string]string{"Alex": "friend", "Joe": "friend"}
_, err = cli.Upsert(ctx, bson.M{"age": 45}, user)
ast.NoError(err)
user.Relations = map[string]string{"Alex": "friend", "Joe": "friend", "Bob": "sister"} // max tag, numbers of map
_, err = cli.Upsert(ctx, bson.M{"age": 45}, user)
ast.Error(err)
}