1
0
Fork 0

Skip individual tests intstead of whole suite

This commit is contained in:
Gregory Eremin 2018-07-09 00:48:38 +02:00
parent 55fcca661e
commit 2070776cf7
5 changed files with 69 additions and 30 deletions

View File

@ -8,6 +8,9 @@ test:
@go test ./...
dbtest:
@go test ./sqldb -dsn="root:@(127.0.0.1:3306)/sqldb_pkg_test"
@go test ./dbc -dsn="root:@(127.0.0.1:3306)/sqldb_pkg_test"
fulltest: test dbtest
bench:
cd ./dbc && go test -bench=.

13
context2/test_context.go Normal file
View File

@ -0,0 +1,13 @@
package context2
import (
"context"
"testing"
"github.com/localhots/gobelt/log"
)
// TestContext returns a new context with test name added.
func TestContext(t *testing.T) context.Context {
return log.ContextWithFields(context.Background(), log.F{"test": t.Name()})
}

View File

@ -6,6 +6,7 @@ import (
)
func TestCallChain(t *testing.T) {
requireConn(t)
ctx := context.Background()
mustExec(t, conn.
Exec(ctx, "INSERT INTO sqldb_test (id, name) VALUES (3, 'Fred')").Then().

View File

@ -1,14 +1,15 @@
package dbc
import (
"context"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/localhots/gobelt/context2"
)
func TestLoadSingleValue(t *testing.T) {
ctx := context.Background()
requireConn(t)
ctx := context2.TestContext(t)
exp := int(1)
var out int
mustQuery(t, conn.Query(ctx, "SELECT 1").Load(&out))
@ -18,7 +19,8 @@ func TestLoadSingleValue(t *testing.T) {
}
func TestLoadSlice(t *testing.T) {
ctx := context.Background()
requireConn(t)
ctx := context2.TestContext(t)
exp := []int{1, 2}
var out []int
mustQuery(t, conn.Query(ctx, "SELECT id FROM sqldb_test").Load(&out))
@ -28,7 +30,8 @@ func TestLoadSlice(t *testing.T) {
}
func TestLoadMap(t *testing.T) {
ctx := context.Background()
requireConn(t)
ctx := context2.TestContext(t)
exp := map[string]interface{}{"id": int64(1), "name": "Alice"}
var out map[string]interface{}
mustQuery(t, conn.Query(ctx, "SELECT * FROM sqldb_test WHERE id = 1").Load(&out))
@ -38,7 +41,8 @@ func TestLoadMap(t *testing.T) {
}
func TestLoadSliceOfMaps(t *testing.T) {
ctx := context.Background()
requireConn(t)
ctx := context2.TestContext(t)
exp := []map[string]interface{}{
{"id": int64(1), "name": "Alice"},
{"id": int64(2), "name": "Bob"},
@ -51,7 +55,8 @@ func TestLoadSliceOfMaps(t *testing.T) {
}
func TestLoadStruct(t *testing.T) {
ctx := context.Background()
requireConn(t)
ctx := context2.TestContext(t)
exp := record{ID: 1, Name: "Alice"}
var out record
mustQuery(t, conn.Query(ctx, "SELECT * FROM sqldb_test WHERE id = 1").Load(&out))
@ -61,7 +66,8 @@ func TestLoadStruct(t *testing.T) {
}
func TestLoadSliceOfStructs(t *testing.T) {
ctx := context.Background()
requireConn(t)
ctx := context2.TestContext(t)
exp := []record{
{ID: 1, Name: "Alice"},
{ID: 2, Name: "Bob"},

View File

@ -3,12 +3,11 @@ package dbc
import (
"context"
"flag"
"fmt"
"log"
"os"
"testing"
_ "github.com/go-sql-driver/mysql" // MySQL driver
"github.com/localhots/gobelt/log"
)
type record struct {
@ -19,25 +18,43 @@ type record struct {
var conn Conn
func TestMain(m *testing.M) {
ctx := context.Background()
dsn := flag.String("dsn", "", "Database source name")
flag.Parse()
if *dsn == "" {
log.Println("Database source name is not provided, skipping package tests")
os.Exit(0)
log.Warn(ctx, "Database source name is not provided, some tests would be skipped")
} else {
connect(ctx, *dsn)
seed(ctx)
}
log.Println("Establishing connection to the test database")
ctx := context.Background()
log.Info(ctx, "Starting test suite")
exitCode := m.Run()
if exitCode == 0 {
log.Info(ctx, "Test suite completed successfully")
} else {
log.Error(ctx, "Test suite failed")
}
if conn != nil {
if err := conn.Close(); err != nil {
log.Errorf(ctx, "Failed to close connection: %v\n", err)
}
}
os.Exit(exitCode)
}
func connect(ctx context.Context, dsn string) {
log.Info(ctx, "Establishing connection to the test database")
var err error
conn, err = Connect(ctx, MySQL, *dsn)
conn, err = Connect(ctx, MySQL, dsn)
if err != nil {
log.Fatalf("Failed to connect: %v\n", err)
log.Fatalf(ctx, "Failed to connect: %v\n", err)
}
}
log.Println("Seeding database")
mustExecMain(conn.Exec(ctx, `
DROP TABLE IF EXISTS sqldb_test
`))
func seed(ctx context.Context) {
log.Info(ctx, "Seeding database")
mustExecMain(conn.Exec(ctx, `DROP TABLE IF EXISTS sqldb_test`))
mustExecMain(conn.Exec(ctx, `
CREATE TABLE sqldb_test (
id int(11) UNSIGNED NOT NULL,
@ -51,14 +68,6 @@ func TestMain(m *testing.M) {
(1, "Alice"),
(2, "Bob")
`))
fmt.Println("Starting test suite")
exitCode := m.Run()
log.Println("Test suite finished")
if err := conn.Close(); err != nil {
log.Printf("Failed to close connection: %v\n", err)
}
os.Exit(exitCode)
}
func mustExec(t *testing.T, r ExecResult) ExecResult {
@ -71,7 +80,7 @@ func mustExec(t *testing.T, r ExecResult) ExecResult {
func mustExecMain(r ExecResult) ExecResult {
if r.Error() != nil {
log.Fatalf("Query failed: %v\n", r.Error())
log.Fatalf(context.Background(), "Query failed: %v\n", r.Error())
}
return r
}
@ -82,3 +91,10 @@ func mustQuery(t *testing.T, err error) {
t.Fatalf("Query failed: %v", err)
}
}
func requireConn(t *testing.T) {
t.Helper()
if conn == nil {
t.Skip("Connection required")
}
}