-
Notifications
You must be signed in to change notification settings - Fork 177
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add test suite for storage interface
- Loading branch information
1 parent
7127616
commit 6da9de6
Showing
4 changed files
with
164 additions
and
126 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
package storagetest | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
"github.com/stretchr/testify/suite" | ||
|
||
"github.com/lovoo/goka/storage" | ||
) | ||
|
||
// TeardownFunc cleans up state after tests. | ||
type TeardownFunc func() error | ||
|
||
type StorageTestSuite struct { | ||
suite.Suite | ||
|
||
storageBuilder func(t *testing.T) (storage.Storage, TeardownFunc) | ||
storage storage.Storage | ||
teardownFn TeardownFunc | ||
} | ||
|
||
func NewStorageTestSuite(storageBuilder func(t *testing.T) (storage.Storage, TeardownFunc)) *StorageTestSuite { | ||
return &StorageTestSuite{ | ||
storageBuilder: storageBuilder, | ||
} | ||
} | ||
|
||
func (s *StorageTestSuite) SetupTest() { | ||
storage, teardownFn := s.storageBuilder(s.T()) | ||
s.storage = storage | ||
s.teardownFn = teardownFn | ||
} | ||
|
||
func (s *StorageTestSuite) TeardownTest() { | ||
err := s.teardownFn() | ||
require.NoError(s.T(), err) | ||
s.teardownFn = nil | ||
} | ||
|
||
func (s *StorageTestSuite) TestHas() { | ||
st := s.storage | ||
|
||
var ( | ||
err error | ||
hasKey bool | ||
) | ||
|
||
hasKey, err = st.Has("test-key") | ||
require.NoError(s.T(), err) | ||
require.False(s.T(), hasKey) | ||
|
||
err = st.Set("test-key", []byte("test")) | ||
require.NoError(s.T(), err) | ||
|
||
hasKey, err = st.Has("test-key") | ||
require.NoError(s.T(), err) | ||
require.True(s.T(), hasKey) | ||
} | ||
|
||
func (s *StorageTestSuite) TestSetGet() { | ||
st := s.storage | ||
|
||
value, err := st.Get("example1") | ||
require.NoError(s.T(), err) | ||
require.Nil(s.T(), value) | ||
|
||
err = st.Set("example1", []byte("example-message")) | ||
require.NoError(s.T(), err) | ||
|
||
value, err = st.Get("example1") | ||
require.NoError(s.T(), err) | ||
require.Equal(s.T(), []byte("example-message"), value) | ||
} | ||
|
||
func (s *StorageTestSuite) TestDelete() { | ||
st := s.storage | ||
|
||
err := st.Set("example1", []byte("example-message")) | ||
require.NoError(s.T(), err) | ||
|
||
require.NoError(s.T(), st.Delete("example1")) | ||
|
||
hasKey, err := st.Has("example1") | ||
require.NoError(s.T(), err) | ||
require.False(s.T(), hasKey) | ||
|
||
value, err := st.Get("example1") | ||
require.NoError(s.T(), err) | ||
require.Nil(s.T(), value) | ||
} | ||
|
||
func (s *StorageTestSuite) TestOffsetSetGet() { | ||
st := s.storage | ||
|
||
offset, err := st.GetOffset(0) | ||
require.NoError(s.T(), err) | ||
require.Equal(s.T(), int64(0), offset) | ||
|
||
require.NoError(s.T(), st.SetOffset(100)) | ||
|
||
offset, err = st.GetOffset(0) | ||
require.NoError(s.T(), err) | ||
require.Equal(s.T(), int64(100), offset) | ||
} | ||
|
||
func (s *StorageTestSuite) TestIterator() { | ||
st := s.storage | ||
|
||
kv := map[string]string{ | ||
"key-1": "val-1", | ||
"key-2": "val-2", | ||
"key-3": "val-3", | ||
} | ||
|
||
for k, v := range kv { | ||
require.Nil(s.T(), st.Set(k, []byte(v))) | ||
} | ||
|
||
require.Nil(s.T(), st.SetOffset(777)) | ||
|
||
iter, err := st.Iterator() | ||
require.Nil(s.T(), err) | ||
defer iter.Release() | ||
count := 0 | ||
|
||
// accessing iterator before Next should only return nils | ||
val, err := iter.Value() | ||
require.True(s.T(), val == nil) | ||
require.Nil(s.T(), err) | ||
|
||
for iter.Next() { | ||
count++ | ||
key := string(iter.Key()) | ||
expected, ok := kv[key] | ||
if !ok { | ||
s.T().Fatalf("unexpected key from iterator: %s", key) | ||
} | ||
|
||
val, err := iter.Value() | ||
require.Nil(s.T(), err) | ||
require.Equal(s.T(), expected, string(val)) | ||
} | ||
require.Equal(s.T(), count, len(kv)) | ||
} |