db/scan_test.go

103 lines
2.0 KiB
Go
Raw Normal View History

2023-04-12 15:58:25 +08:00
package db
import (
"testing"
)
var myConn *Connection
2023-04-12 15:58:25 +08:00
func init() {
database := Open(map[string]DBConfig{
"mysql": {
2023-04-12 15:58:25 +08:00
Driver: "mysql",
Host: "localhost",
Port: "3366",
Database: "demo",
Username: "demo",
Password: "ded86bf25d661bb723f3898b2440dd678382e2dd",
Charset: "utf8mb4",
MultiStatements: true,
// ParseTime: true,
},
})
myConn = database.Connection("mysql")
2023-04-12 15:58:25 +08:00
}
func TestScanMapSlice(t *testing.T) {
dest := []map[string]any{}
myConn.Select("select * from fsm", []any{}, &dest)
2023-04-12 15:58:25 +08:00
t.Log(dest)
}
func TestScanValues(t *testing.T) {
dest := []int{}
myConn.Select("select 1 as id", []any{}, &dest)
2023-04-12 15:58:25 +08:00
t.Log(dest)
}
func TestScanMap(t *testing.T) {
dest := map[string]any{}
myConn.Select("select 0 as id, 'test' as code", []any{}, &dest)
2023-04-12 15:58:25 +08:00
t.Log(dest)
}
func TestScanStruct(t *testing.T) {
dest := struct {
Id int `db:"id"`
Code string `db:"code"`
}{}
myConn.Select(`select 0 as id, 'test' as code`, []any{}, &dest)
t.Log(dest)
}
func TestScanJsonFieldStruct(t *testing.T) {
dest := struct {
Id int `db:"id"`
Code string `db:"code"`
Map map[string]any `db:"map"`
Arr []int64 `db:"arr"`
}{}
myConn.Select(`select 0 as id, NULL as code, '{"a":1}' as map, '[3, 4]' as arr`, []any{}, &dest)
2023-04-12 15:58:25 +08:00
t.Log(dest)
}
func TestScanStructSlice(t *testing.T) {
dest := []struct {
Id int `db:"id"`
Code string `db:"code"`
}{}
myConn.Select("select * from ((select 0 as id, 'test' as code) union (select 1 as id, 'test1' as code)) as t", []any{}, &dest)
t.Log(dest)
}
func TestScanJsonFieldStructSlice(t *testing.T) {
dest := []struct {
Id int `db:"id"`
Code string `db:"code"`
Map map[string]any `db:"map"`
Arr []any `db:"arr"`
}{}
myConn.Select(`select * from ((select 0 as id, 'test' as code, '{"a":1}' as map, '[1, 2]' as arr) union (select 1 as id, 'test1' as code, '{"a":2}' as map, '[1, 2]' as arr)) as t`, []any{}, &dest)
2023-04-12 15:58:25 +08:00
t.Log(dest)
}