From fffa446cc6627ef41e0909c74ba2339f874d0696 Mon Sep 17 00:00:00 2001 From: Ilya Sinelnikov Date: Sat, 21 Nov 2020 17:37:37 +0300 Subject: [PATCH] Test sqlx.NewCluster for invalid input --- sqlx/cluster_test.go | 40 +++++++++++++++++++++++++--------------- 1 file changed, 25 insertions(+), 15 deletions(-) diff --git a/sqlx/cluster_test.go b/sqlx/cluster_test.go index d9fedf1..5aaac7c 100644 --- a/sqlx/cluster_test.go +++ b/sqlx/cluster_test.go @@ -28,19 +28,29 @@ import ( ) func TestNewCluster(t *testing.T) { - db, mock, err := sqlmock.New(sqlmock.MonitorPingsOption(true)) - require.NoError(t, err) - require.NotNil(t, db) - - mock.ExpectClose() - defer func() { assert.NoError(t, mock.ExpectationsWereMet()) }() - - node := NewNode("fake.addr", sqlx.NewDb(db, "sqlmock")) - cl, err := NewCluster([]Node{node}, func(_ context.Context, _ *sql.DB) (bool, error) { return false, nil }) - require.NoError(t, err) - require.NotNil(t, cl) - defer func() { require.NoError(t, cl.Close()) }() - - require.Len(t, cl.Nodes(), 1) - require.Equal(t, node, cl.Nodes()[0]) + checker := func(_ context.Context, _ *sql.DB) (bool, error) { return false, nil } + + t.Run("Works", func(t *testing.T) { + db, mock, err := sqlmock.New(sqlmock.MonitorPingsOption(true)) + require.NoError(t, err) + require.NotNil(t, db) + + mock.ExpectClose() + defer func() { assert.NoError(t, mock.ExpectationsWereMet()) }() + + node := NewNode("fake.addr", sqlx.NewDb(db, "sqlmock")) + cl, err := NewCluster([]Node{node}, checker) + require.NoError(t, err) + require.NotNil(t, cl) + defer func() { require.NoError(t, cl.Close()) }() + + require.Len(t, cl.Nodes(), 1) + require.Equal(t, node, cl.Nodes()[0]) + }) + + t.Run("Fails", func(t *testing.T) { + cl, err := NewCluster(nil, checker) + require.Error(t, err) + require.Nil(t, cl) + }) }