gf/util/gconv/gconv_z_unit_scan_test.go
2021-01-17 21:46:25 +08:00

126 lines
2.3 KiB
Go

// Copyright GoFrame Author(https://goframe.org). 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.
package gconv_test
import (
"github.com/gogf/gf/frame/g"
"github.com/gogf/gf/test/gtest"
"github.com/gogf/gf/util/gconv"
"testing"
)
func Test_Scan(t *testing.T) {
type User struct {
Uid int
Name string
Pass1 string `gconv:"password1"`
Pass2 string `gconv:"password2"`
}
gtest.C(t, func(t *gtest.T) {
var (
user = new(User)
params = g.Map{
"uid": 1,
"name": "john",
"PASS1": "123",
"PASS2": "456",
}
)
err := gconv.Scan(params, user)
t.Assert(err, nil)
t.Assert(user, &User{
Uid: 1,
Name: "john",
Pass1: "123",
Pass2: "456",
})
})
gtest.C(t, func(t *gtest.T) {
var (
users []User
params = g.Slice{
g.Map{
"uid": 1,
"name": "john1",
"PASS1": "111",
"PASS2": "222",
},
g.Map{
"uid": 2,
"name": "john2",
"PASS1": "333",
"PASS2": "444",
},
}
)
err := gconv.Scan(params, &users)
t.Assert(err, nil)
t.Assert(users, g.Slice{
&User{
Uid: 1,
Name: "john1",
Pass1: "111",
Pass2: "222",
},
&User{
Uid: 2,
Name: "john2",
Pass1: "333",
Pass2: "444",
},
})
})
}
func Test_ScanStr(t *testing.T) {
type User struct {
Uid int
Name string
Pass1 string `gconv:"password1"`
Pass2 string `gconv:"password2"`
}
gtest.C(t, func(t *gtest.T) {
var (
user = new(User)
params = `{"uid":1,"name":"john", "pass1":"123","pass2":"456"}`
)
err := gconv.Scan(params, user)
t.Assert(err, nil)
t.Assert(user, &User{
Uid: 1,
Name: "john",
Pass1: "123",
Pass2: "456",
})
})
gtest.C(t, func(t *gtest.T) {
var (
users []User
params = `[
{"uid":1,"name":"john1", "pass1":"111","pass2":"222"},
{"uid":2,"name":"john2", "pass1":"333","pass2":"444"}
]`
)
err := gconv.Scan(params, &users)
t.Assert(err, nil)
t.Assert(users, g.Slice{
&User{
Uid: 1,
Name: "john1",
Pass1: "111",
Pass2: "222",
},
&User{
Uid: 2,
Name: "john2",
Pass1: "333",
Pass2: "444",
},
})
})
}