mirror of https://github.com/VinGarcia/ksql.git
Add tests for the .Close() method
parent
45d8ef4491
commit
d73528bd8b
|
@ -40,3 +40,12 @@ func (m mockTx) Rollback(ctx context.Context) error {
|
|||
func (m mockTx) Commit(ctx context.Context) error {
|
||||
return m.CommitFn(ctx)
|
||||
}
|
||||
|
||||
// mockCloser mocks the io.Closer interface
|
||||
type mockCloser struct {
|
||||
CloseFn func() error
|
||||
}
|
||||
|
||||
func (m mockCloser) Close() error {
|
||||
return m.CloseFn()
|
||||
}
|
||||
|
|
51
ksql_test.go
51
ksql_test.go
|
@ -1,6 +1,8 @@
|
|||
package ksql
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"testing"
|
||||
|
||||
tt "github.com/vingarcia/ksql/internal/testtools"
|
||||
|
@ -38,3 +40,52 @@ func TestNewAdapterWith(t *testing.T) {
|
|||
tt.AssertNotEqual(t, err, nil)
|
||||
})
|
||||
}
|
||||
|
||||
func TestClose(t *testing.T) {
|
||||
t.Run("should close the adapter if it implements the io.Closer interface", func(t *testing.T) {
|
||||
c := DB{
|
||||
db: struct {
|
||||
DBAdapter
|
||||
io.Closer
|
||||
}{
|
||||
DBAdapter: mockDBAdapter{},
|
||||
Closer: mockCloser{
|
||||
CloseFn: func() error {
|
||||
return nil
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
err := c.Close()
|
||||
tt.AssertNoErr(t, err)
|
||||
})
|
||||
|
||||
t.Run("should exit normally if the adapter does not implement the io.Closer interface", func(t *testing.T) {
|
||||
c := DB{
|
||||
db: mockDBAdapter{},
|
||||
}
|
||||
|
||||
err := c.Close()
|
||||
tt.AssertNoErr(t, err)
|
||||
})
|
||||
|
||||
t.Run("should report an error if the adapter.Close() returns one", func(t *testing.T) {
|
||||
c := DB{
|
||||
db: struct {
|
||||
DBAdapter
|
||||
io.Closer
|
||||
}{
|
||||
DBAdapter: mockDBAdapter{},
|
||||
Closer: mockCloser{
|
||||
CloseFn: func() error {
|
||||
return fmt.Errorf("fakeCloseErrMsg")
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
err := c.Close()
|
||||
tt.AssertErrContains(t, err, "fakeCloseErrMsg")
|
||||
})
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue