advsql/db_test.go

52 lines
1.2 KiB
Go
Raw Permalink Normal View History

2022-07-05 12:38:39 +02:00
package advsql
import (
"crypto/sha512"
"encoding/hex"
"fmt"
"testing"
_ "github.com/go-sql-driver/mysql"
)
var TestDatabase *Database //= new(Database)
2022-07-05 12:38:39 +02:00
type User struct {
Name string
Hash []byte
Salt string
}
func ScanUserPkFirst(u *User, encode ScanFunc) error {
return encode(&u.Name, &u.Hash, &u.Salt)
2022-07-05 12:38:39 +02:00
}
func ScanUserPkLast(u *User, encode ScanFunc) error {
return encode(&u.Hash, &u.Salt, &u.Name)
2022-07-05 12:38:39 +02:00
}
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)
)
2022-07-05 12:38:39 +02:00
func TestDB(t *testing.T) {
InitMysqlDatabase(TestDatabase, "ip", 3306, "username", "password", "database")
defer TestDatabase.Close()
2022-07-05 12:38:39 +02:00
pw := sha512.Sum512([]byte("weiter"))
timon := &User{
Name: "timon",
Hash: pw[:],
Salt: "salt",
}
fmt.Println("insert:", InsertUser(timon))
2022-07-05 12:38:39 +02:00
timon.Hash = []byte("asd")
fmt.Println("update:", UpdateUser(timon))
2022-07-05 12:38:39 +02:00
user := GetUserByName("tordarus")
fmt.Printf("name: \"%s\" | hash: \"%s\" | salt: \"%s\"\n", user.Name, hex.EncodeToString(user.Hash), user.Salt)
2022-07-05 12:38:39 +02:00
}