From 2070776cf7d3f6fc3fb22bcc51390ee9cb6a828c Mon Sep 17 00:00:00 2001 From: Gregory Eremin Date: Mon, 9 Jul 2018 00:48:38 +0200 Subject: [PATCH] Skip individual tests intstead of whole suite --- Makefile | 7 +++- context2/test_context.go | 13 +++++++ dbc/chain_test.go | 1 + dbc/{result_test.go => rows_test.go} | 20 ++++++---- dbc/sqldb_test.go | 58 ++++++++++++++++++---------- 5 files changed, 69 insertions(+), 30 deletions(-) create mode 100644 context2/test_context.go rename dbc/{result_test.go => rows_test.go} (84%) diff --git a/Makefile b/Makefile index 4a39e92..d5debeb 100644 --- a/Makefile +++ b/Makefile @@ -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 \ No newline at end of file +fulltest: test dbtest + +bench: + cd ./dbc && go test -bench=. \ No newline at end of file diff --git a/context2/test_context.go b/context2/test_context.go new file mode 100644 index 0000000..acc63f8 --- /dev/null +++ b/context2/test_context.go @@ -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()}) +} diff --git a/dbc/chain_test.go b/dbc/chain_test.go index 5c147b8..b600e1b 100644 --- a/dbc/chain_test.go +++ b/dbc/chain_test.go @@ -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(). diff --git a/dbc/result_test.go b/dbc/rows_test.go similarity index 84% rename from dbc/result_test.go rename to dbc/rows_test.go index 6b5768b..32dc710 100644 --- a/dbc/result_test.go +++ b/dbc/rows_test.go @@ -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"}, diff --git a/dbc/sqldb_test.go b/dbc/sqldb_test.go index e8f7b60..fbdb479 100644 --- a/dbc/sqldb_test.go +++ b/dbc/sqldb_test.go @@ -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") + } +}