Skip to content

Commit

Permalink
Test sqlx.NewCluster for invalid input
Browse files Browse the repository at this point in the history
  • Loading branch information
Ilya Sinelnikov committed Nov 21, 2020
1 parent bed25ff commit fffa446
Showing 1 changed file with 25 additions and 15 deletions.
40 changes: 25 additions & 15 deletions sqlx/cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
}

0 comments on commit fffa446

Please sign in to comment.