Merge pull request #247 from 2892931976/gf-chj

This commit is contained in:
John Guo
2019-07-12 21:02:19 +08:00
committed by GitHub
5 changed files with 388 additions and 3 deletions

View File

@ -0,0 +1,92 @@
// Copyright 2017 gf Author(https://github.com/gogf/gf). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
// go test *.go -bench=".*" -benchmem
package gfcache_test
import (
"io/ioutil"
"os"
"testing"
"time"
"github.com/gogf/gf/g/os/gfcache"
"github.com/gogf/gf/g/os/gfile"
"github.com/gogf/gf/g/test/gtest"
)
func TestGetContents(t *testing.T) {
gtest.Case(t, func() {
var f *os.File
var err error
fileName := "test"
strTest := "123"
if !gfile.Exists(fileName) {
f, err = ioutil.TempFile("", fileName)
if err != nil {
t.Error("create file fail")
}
}
defer f.Close()
defer os.Remove(f.Name())
if gfile.Exists(f.Name()) {
f, err = gfile.OpenFile(f.Name(), os.O_APPEND|os.O_WRONLY, os.ModeAppend)
if err != nil {
t.Error("file open fail", err)
}
err = gfile.PutContents(f.Name(), strTest)
if err != nil {
t.Error("write error", err)
}
cache := gfcache.GetContents(f.Name(), 1)
gtest.Assert(cache, strTest)
}
})
gtest.Case(t, func() {
var f *os.File
var err error
fileName := "test2"
strTest := "123"
if !gfile.Exists(fileName) {
f, err = ioutil.TempFile("", fileName)
if err != nil {
t.Error("create file fail")
}
}
defer f.Close()
defer os.Remove(f.Name())
if gfile.Exists(f.Name()) {
cache := gfcache.GetContents(f.Name())
f, err = gfile.OpenFile(f.Name(), os.O_APPEND|os.O_WRONLY, os.ModeAppend)
if err != nil {
t.Error("file open fail", err)
}
err = gfile.PutContents(f.Name(), strTest)
if err != nil {
t.Error("write error", err)
}
gtest.Assert(cache, "")
time.Sleep(100 * time.Millisecond)
}
})
}

58
g/util/gvalid/gvalid_unit_basic_all_test.go Normal file → Executable file
View File

@ -7,12 +7,29 @@
package gvalid_test
import (
"testing"
"github.com/gogf/gf/g"
"github.com/gogf/gf/g/test/gtest"
"github.com/gogf/gf/g/util/gvalid"
"testing"
)
func Test_Check(t *testing.T) {
gtest.Case(t, func() {
rule := "abc:6,16"
val1 := 0
val2 := 7
val3 := 20
err1 := gvalid.Check(val1, rule, nil)
err2 := gvalid.Check(val2, rule, nil)
err3 := gvalid.Check(val3, rule, nil)
gtest.Assert(err1, "invalid rules:abc:6,16")
gtest.Assert(err2, "invalid rules:abc:6,16")
gtest.Assert(err3, "invalid rules:abc:6,16")
})
}
func Test_Required(t *testing.T) {
if m := gvalid.Check("1", "required", nil); m != nil {
t.Error(m)
@ -540,22 +557,44 @@ func Test_Length(t *testing.T) {
func Test_MinLength(t *testing.T) {
rule := "min-length:6"
msgs := map[string]string{
"min-length": "地址长度至少为:min位",
}
if m := gvalid.Check("123456", rule, nil); m != nil {
t.Error(m)
}
if m := gvalid.Check("12345", rule, nil); m == nil {
t.Error("长度校验失败")
}
if m := gvalid.Check("12345", rule, msgs); m == nil {
t.Error("长度校验失败")
}
rule2 := "min-length:abc"
if m := gvalid.Check("123456", rule2, nil); m == nil {
t.Error("长度校验失败")
}
}
func Test_MaxLength(t *testing.T) {
rule := "max-length:6"
msgs := map[string]string{
"max-length": "地址长度至大为:max位",
}
if m := gvalid.Check("12345", rule, nil); m != nil {
t.Error(m)
}
if m := gvalid.Check("1234567", rule, nil); m == nil {
t.Error("长度校验失败")
}
if m := gvalid.Check("1234567", rule, msgs); m == nil {
t.Error("长度校验失败")
}
rule2 := "max-length:abc"
if m := gvalid.Check("123456", rule2, nil); m == nil {
t.Error("长度校验失败")
}
}
func Test_Between(t *testing.T) {
@ -566,6 +605,9 @@ func Test_Between(t *testing.T) {
if m := gvalid.Check(10.02, rule, nil); m == nil {
t.Error("大小范围校验失败")
}
if m := gvalid.Check("a", rule, nil); m == nil {
t.Error("大小范围校验失败")
}
}
func Test_Min(t *testing.T) {
@ -575,14 +617,21 @@ func Test_Min(t *testing.T) {
val2 := "99"
val3 := "100"
val4 := "1000"
val5 := "a"
err1 := gvalid.Check(val1, rule, nil)
err2 := gvalid.Check(val2, rule, nil)
err3 := gvalid.Check(val3, rule, nil)
err4 := gvalid.Check(val4, rule, nil)
err5 := gvalid.Check(val5, rule, nil)
gtest.AssertNE(err1, nil)
gtest.AssertNE(err2, nil)
gtest.Assert(err3, nil)
gtest.Assert(err4, nil)
gtest.AssertNE(err5, nil)
rule2 := "min:a"
err6 := gvalid.Check(val1, rule2, nil)
gtest.AssertNE(err6, nil)
})
}
@ -593,14 +642,21 @@ func Test_Max(t *testing.T) {
val2 := "99"
val3 := "100"
val4 := "1000"
val5 := "a"
err1 := gvalid.Check(val1, rule, nil)
err2 := gvalid.Check(val2, rule, nil)
err3 := gvalid.Check(val3, rule, nil)
err4 := gvalid.Check(val4, rule, nil)
err5 := gvalid.Check(val5, rule, nil)
gtest.Assert(err1, nil)
gtest.Assert(err2, nil)
gtest.Assert(err3, nil)
gtest.AssertNE(err4, nil)
gtest.AssertNE(err5, nil)
rule2 := "max:a"
err6 := gvalid.Check(val1, rule2, nil)
gtest.AssertNE(err6, nil)
})
}

85
g/util/gvalid/gvalid_unit_checkmap_test.go Normal file → Executable file
View File

@ -7,12 +7,19 @@
package gvalid_test
import (
"testing"
"github.com/gogf/gf/g/test/gtest"
"github.com/gogf/gf/g/util/gvalid"
"testing"
)
func Test_CheckMap(t *testing.T) {
var params interface{}
if m := gvalid.CheckMap(params, nil, nil); m == nil {
t.Error("CheckMap校验失败")
}
kvmap := map[string]interface{}{
"id": "0",
"name": "john",
@ -50,6 +57,82 @@ func Test_CheckMap(t *testing.T) {
if m := gvalid.CheckMap(kvmap, rules, msgs); m != nil {
t.Error(m)
}
kvmap = map[string]interface{}{
"id": "1",
"name": "john",
}
rules = map[string]string{
"id": "",
"name": "",
}
msgs = map[string]interface{}{
"id": "ID不能为空|ID范围应当为:min到:max",
"name": map[string]string{
"required": "名称不能为空",
"length": "名称长度为:min到:max个字符",
},
}
if m := gvalid.CheckMap(kvmap, rules, msgs); m != nil {
t.Error(m)
}
kvmap = map[string]interface{}{
"id": "1",
"name": "john",
}
rules2 := []string{
"@required|between:1,100",
"@required|length:4,16",
}
msgs = map[string]interface{}{
"id": "ID不能为空|ID范围应当为:min到:max",
"name": map[string]string{
"required": "名称不能为空",
"length": "名称长度为:min到:max个字符",
},
}
if m := gvalid.CheckMap(kvmap, rules2, msgs); m != nil {
t.Error(m)
}
kvmap = map[string]interface{}{
"id": "1",
"name": "john",
}
rules2 = []string{
"id@required|between:1,100",
"name@required|length:4,16#名称不能为空|",
}
msgs = map[string]interface{}{
"id": "ID不能为空|ID范围应当为:min到:max",
"name": map[string]string{
"required": "名称不能为空",
"length": "名称长度为:min到:max个字符",
},
}
if m := gvalid.CheckMap(kvmap, rules2, msgs); m != nil {
t.Error(m)
}
kvmap = map[string]interface{}{
"id": "1",
"name": "john",
}
rules2 = []string{
"id@required|between:1,100",
"name@required|length:4,16#名称不能为空",
}
msgs = map[string]interface{}{
"id": "ID不能为空|ID范围应当为:min到:max",
"name": map[string]string{
"required": "名称不能为空",
"length": "名称长度为:min到:max个字符",
},
}
if m := gvalid.CheckMap(kvmap, rules2, msgs); m != nil {
t.Error(m)
}
}
// 如果值为nil并且不需要require*验证时,其他验证失效

132
g/util/gvalid/gvalid_unit_checkstruct_test.go Normal file → Executable file
View File

@ -16,6 +16,77 @@ import (
)
func Test_CheckStruct(t *testing.T) {
gtest.Case(t, func() {
type Object struct {
Name string
Age int
}
rules := []string{
"@required|length:6,16",
"@between:18,30",
}
msgs := map[string]interface{}{
"Name": map[string]string{
"required": "名称不能为空",
"length": "名称长度为:min到:max个字符",
},
"Age": "年龄为18到30周岁",
}
obj := &Object{"john", 16}
err := gvalid.CheckStruct(obj, rules, msgs)
gtest.Assert(err, nil)
})
gtest.Case(t, func() {
type Object struct {
Name string
Age int
}
rules := []string{
"Name@required|length:6,16#名称不能为空",
"Age@between:18,30",
}
msgs := map[string]interface{}{
"Name": map[string]string{
"required": "名称不能为空",
"length": "名称长度为:min到:max个字符",
},
"Age": "年龄为18到30周岁",
}
obj := &Object{"john", 16}
err := gvalid.CheckStruct(obj, rules, msgs)
gtest.AssertNE(err, nil)
gtest.Assert(len(err.Maps()), 2)
gtest.Assert(err.Maps()["Name"]["required"], "")
gtest.Assert(err.Maps()["Name"]["length"], "名称长度为6到16个字符")
gtest.Assert(err.Maps()["Age"]["between"], "年龄为18到30周岁")
})
gtest.Case(t, func() {
type Object struct {
Name string
Age int
}
rules := []string{
"Name@required|length:6,16#名称不能为空|",
"Age@between:18,30",
}
msgs := map[string]interface{}{
"Name": map[string]string{
"required": "名称不能为空",
"length": "名称长度为:min到:max个字符",
},
"Age": "年龄为18到30周岁",
}
obj := &Object{"john", 16}
err := gvalid.CheckStruct(obj, rules, msgs)
gtest.AssertNE(err, nil)
gtest.Assert(len(err.Maps()), 2)
gtest.Assert(err.Maps()["Name"]["required"], "")
gtest.Assert(err.Maps()["Name"]["length"], "名称长度为6到16个字符")
gtest.Assert(err.Maps()["Age"]["between"], "年龄为18到30周岁")
})
gtest.Case(t, func() {
type Object struct {
Name string
@ -54,6 +125,27 @@ func Test_CheckStruct(t *testing.T) {
gtest.Assert(err.Maps()["password"]["required"], "登录密码不能为空")
})
gtest.Case(t, func() {
type LoginRequest struct {
Username string `json:"username" gvalid:"@required#用户名不能为空"`
Password string `json:"password" gvalid:"@required#登录密码不能为空"`
}
var login LoginRequest
err := gvalid.CheckStruct(login, nil)
gtest.Assert(err, nil)
})
gtest.Case(t, func() {
type LoginRequest struct {
username string `json:"username" gvalid:"username@required#用户名不能为空"`
Password string `json:"password" gvalid:"password@required#登录密码不能为空"`
}
var login LoginRequest
err := gvalid.CheckStruct(login, nil)
gtest.AssertNE(err, nil)
gtest.Assert(err.Maps()["password"]["required"], "登录密码不能为空")
})
// gvalid tag
gtest.Case(t, func() {
type User struct {
@ -73,6 +165,46 @@ func Test_CheckStruct(t *testing.T) {
gtest.Assert(err.Maps()["uid"]["min"], "ID不能为空")
})
gtest.Case(t, func() {
type User struct {
Id int `gvalid:"uid@required|min:10#|ID不能为空"`
Age int `gvalid:"age@required#年龄不能为空"`
Username string `json:"username" gvalid:"username@required#用户名不能为空"`
Password string `json:"password" gvalid:"password@required#登录密码不能为空"`
}
user := &User{
Id: 1,
Username: "john",
Password: "123456",
}
rules := []string{
"username@required#用户名不能为空",
}
err := gvalid.CheckStruct(user, rules)
gtest.AssertNE(err, nil)
gtest.Assert(len(err.Maps()), 1)
gtest.Assert(err.Maps()["uid"]["min"], "ID不能为空")
})
gtest.Case(t, func() {
type User struct {
Id int `gvalid:"uid@required|min:10#ID不能为空"`
Age int `gvalid:"age@required#年龄不能为空"`
Username string `json:"username" gvalid:"username@required#用户名不能为空"`
Password string `json:"password" gvalid:"password@required#登录密码不能为空"`
}
user := &User{
Id: 1,
Username: "john",
Password: "123456",
}
err := gvalid.CheckStruct(user, nil)
gtest.AssertNE(err, nil)
gtest.Assert(len(err.Maps()), 1)
})
// valid tag
gtest.Case(t, func() {
type User struct {

24
g/util/gvalid/gvalid_unit_customerror_test.go Normal file → Executable file
View File

@ -7,11 +7,33 @@
package gvalid_test
import (
"github.com/gogf/gf/g/util/gvalid"
"strings"
"testing"
"github.com/gogf/gf/g/test/gtest"
"github.com/gogf/gf/g/util/gvalid"
)
func Test_Map(t *testing.T) {
rule := "ipv4"
val := "0.0.0"
msg := map[string]string{
"ipv4": "IPv4地址格式不正确",
}
err := gvalid.Check(val, rule, nil)
gtest.Assert(err.Map(), msg)
}
func Test_FirstString(t *testing.T) {
rule := "ipv4"
val := "0.0.0"
err := gvalid.Check(val, rule, nil)
n := err.FirstString()
gtest.Assert(n, "IPv4地址格式不正确")
}
func Test_SetDefaultErrorMsgs(t *testing.T) {
rule := "integer|length:6,16"
msgs := map[string]string{