Add tests for the .Close() method

pull/26/head
Vinícius Garcia 2022-08-03 21:21:31 -03:00
parent 45d8ef4491
commit d73528bd8b
2 changed files with 60 additions and 0 deletions

View File

@ -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()
}

View File

@ -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")
})
}