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 ./... @go test ./...
dbtest: 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 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) { func TestCallChain(t *testing.T) {
requireConn(t)
ctx := context.Background() ctx := context.Background()
mustExec(t, conn. mustExec(t, conn.
Exec(ctx, "INSERT INTO sqldb_test (id, name) VALUES (3, 'Fred')").Then(). Exec(ctx, "INSERT INTO sqldb_test (id, name) VALUES (3, 'Fred')").Then().

View File

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

View File

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