2023-04-12 15:58:25 +08:00
package db
import (
"testing"
)
2023-06-21 10:03:51 +08:00
var myConn * Connection
2023-04-12 15:58:25 +08:00
func init ( ) {
2023-06-21 10:03:51 +08:00
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,
} ,
} )
2023-06-21 10:03:51 +08:00
myConn = database . Connection ( "mysql" )
2023-04-12 15:58:25 +08:00
}
func TestScanMapSlice ( t * testing . T ) {
dest := [ ] map [ string ] any { }
2023-06-21 10:03:51 +08:00
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 { }
2023-06-21 10:03:51 +08:00
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 { }
2023-06-21 10:03:51 +08:00
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" `
} { }
2023-06-21 10:03:51 +08:00
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" `
2023-06-21 11:07:25 +08:00
Arr [ ] int64 ` db:"arr" `
2023-06-21 10:03:51 +08:00
} { }
2023-06-21 13:12:09 +08:00
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" `
} { }
2023-06-21 10:03:51 +08:00
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" `
2023-06-21 11:07:25 +08:00
Arr [ ] any ` db:"arr" `
2023-06-21 10:03:51 +08:00
} { }
2023-06-21 11:07:25 +08:00
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 )
}