mirror of
https://github.com/CPunch/gopenfusion.git
synced 2026-02-08 03:40:03 +00:00
test: started internal/db test cases
- we use github.com/bitcomplete/sqltestutil to spin up a postgresql container to test against - minor error variable refactoring
This commit is contained in:
50
internal/db/db_test.go
Normal file
50
internal/db/db_test.go
Normal file
@@ -0,0 +1,50 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/bitcomplete/sqltestutil"
|
||||
)
|
||||
|
||||
var (
|
||||
testDB *DBHandler
|
||||
)
|
||||
|
||||
func TestMain(m *testing.M) {
|
||||
ctx := context.Background()
|
||||
psql, err := sqltestutil.StartPostgresContainer(ctx, "latest")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
defer psql.Shutdown(ctx)
|
||||
|
||||
testDB, err = OpenFromConnectionString("postgres", psql.ConnectionString()+"?sslmode=disable")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
defer testDB.Close()
|
||||
|
||||
if err := testDB.Setup(); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
os.Exit(m.Run())
|
||||
}
|
||||
|
||||
func TestDBAccount(t *testing.T) {
|
||||
if _, err := testDB.NewAccount("test", "test"); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
// now try to retrieve account data
|
||||
acc, err := testDB.TryLogin("test", "test")
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
if acc.Login != "test" {
|
||||
t.Error("account username is not test")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user