Skip to content

Commit

Permalink
kv: migrate test framework to testify (tikv#5111)
Browse files Browse the repository at this point in the history
ref tikv#4813

Signed-off-by: LLThomas <[email protected]>

Co-authored-by: Ti Chi Robot <[email protected]>
  • Loading branch information
2 people authored and CabinfeverB committed Jul 14, 2022
1 parent 219fa0e commit 44ed1eb
Showing 1 changed file with 32 additions and 37 deletions.
69 changes: 32 additions & 37 deletions server/storage/kv/kv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,83 +23,78 @@ import (
"strconv"
"testing"

. "github.com/pingcap/check"
"github.com/stretchr/testify/require"
"github.com/tikv/pd/pkg/tempurl"
"go.etcd.io/etcd/clientv3"
"go.etcd.io/etcd/embed"
)

func TestKV(t *testing.T) {
TestingT(t)
}

type testKVSuite struct{}

var _ = Suite(&testKVSuite{})

func (s *testKVSuite) TestEtcd(c *C) {
func TestEtcd(t *testing.T) {
re := require.New(t)
cfg := newTestSingleConfig()
defer cleanConfig(cfg)
etcd, err := embed.StartEtcd(cfg)
c.Assert(err, IsNil)
re.NoError(err)
defer etcd.Close()

ep := cfg.LCUrls[0].String()
client, err := clientv3.New(clientv3.Config{
Endpoints: []string{ep},
})
c.Assert(err, IsNil)
re.NoError(err)
rootPath := path.Join("/pd", strconv.FormatUint(100, 10))

kv := NewEtcdKVBase(client, rootPath)
s.testReadWrite(c, kv)
s.testRange(c, kv)
testReadWrite(re, kv)
testRange(re, kv)
}

func (s *testKVSuite) TestLevelDB(c *C) {
func TestLevelDB(t *testing.T) {
re := require.New(t)
dir, err := os.MkdirTemp("/tmp", "leveldb_kv")
c.Assert(err, IsNil)
re.NoError(err)
defer os.RemoveAll(dir)
kv, err := NewLevelDBKV(dir)
c.Assert(err, IsNil)
re.NoError(err)

s.testReadWrite(c, kv)
s.testRange(c, kv)
testReadWrite(re, kv)
testRange(re, kv)
}

func (s *testKVSuite) TestMemKV(c *C) {
func TestMemKV(t *testing.T) {
re := require.New(t)
kv := NewMemoryKV()
s.testReadWrite(c, kv)
s.testRange(c, kv)
testReadWrite(re, kv)
testRange(re, kv)
}

func (s *testKVSuite) testReadWrite(c *C, kv Base) {
func testReadWrite(re *require.Assertions, kv Base) {
v, err := kv.Load("key")
c.Assert(err, IsNil)
c.Assert(v, Equals, "")
re.NoError(err)
re.Equal("", v)
err = kv.Save("key", "value")
c.Assert(err, IsNil)
re.NoError(err)
v, err = kv.Load("key")
c.Assert(err, IsNil)
c.Assert(v, Equals, "value")
re.NoError(err)
re.Equal("value", v)
err = kv.Remove("key")
c.Assert(err, IsNil)
re.NoError(err)
v, err = kv.Load("key")
c.Assert(err, IsNil)
c.Assert(v, Equals, "")
re.NoError(err)
re.Equal("", v)
err = kv.Remove("key")
c.Assert(err, IsNil)
re.NoError(err)
}

func (s *testKVSuite) testRange(c *C, kv Base) {
func testRange(re *require.Assertions, kv Base) {
keys := []string{
"test-a", "test-a/a", "test-a/ab",
"test", "test/a", "test/ab",
"testa", "testa/a", "testa/ab",
}
for _, k := range keys {
err := kv.Save(k, k)
c.Assert(err, IsNil)
re.NoError(err)
}
sortedKeys := append(keys[:0:0], keys...)
sort.Strings(sortedKeys)
Expand All @@ -120,9 +115,9 @@ func (s *testKVSuite) testRange(c *C, kv Base) {

for _, tc := range testCases {
ks, vs, err := kv.LoadRange(tc.start, tc.end, tc.limit)
c.Assert(err, IsNil)
c.Assert(ks, DeepEquals, tc.expect)
c.Assert(vs, DeepEquals, tc.expect)
re.NoError(err)
re.Equal(tc.expect, ks)
re.Equal(tc.expect, vs)
}
}

Expand Down

0 comments on commit 44ed1eb

Please sign in to comment.