advsql/db_test.go
2022-07-12 21:05:45 +02:00

53 lines
1.3 KiB
Go

package advsql
import (
"crypto/sha512"
"encoding/hex"
"fmt"
"testing"
_ "github.com/go-sql-driver/mysql"
)
var TestDatabase = new(Database)
type User struct {
Name string
Hash []byte
Salt string
}
func ScanUserPkFirst(u *User, encode ScanFunc) error {
return encode(&u.Name, &u.Hash, &u.Salt)
}
func ScanUserPkLast(u *User, encode ScanFunc) error {
return encode(&u.Hash, &u.Salt, &u.Name)
}
var (
InsertUser = Insert(TestDatabase, "INSERT INTO users VALUES (?, ?, ?)", ScanUserPkFirst)
UpdateUser = Update(TestDatabase, "UPDATE users SET hash = ?, salt = ? WHERE name = ?", ScanUserPkLast)
GetUserByName = QueryOne(TestDatabase, "SELECT * FROM users WHERE namea = ?", ScanUserPkFirst)
)
func TestDB(t *testing.T) {
InitMysqlDatabase(TestDatabase, "192.168.178.2", 3306, "root", "6MvtRA4iZJtbenR537otFnoHpzt9vYN7", "users")
//InitMysqlDatabase(TestDatabase, "ip", 3306, "username", "password", "database")
defer TestDatabase.Close()
pw := sha512.Sum512([]byte("weiter"))
timon := &User{
Name: "timon",
Hash: pw[:],
Salt: "salt",
}
fmt.Println("insert:", InsertUser(timon))
timon.Hash = []byte("asd")
fmt.Println("update:", UpdateUser(timon))
user := GetUserByName("tordarus")
fmt.Printf("name: \"%s\" | hash: \"%s\" | salt: \"%s\"\n", user.Name, hex.EncodeToString(user.Hash), user.Salt)
}