Commit 6409c4d7 authored by Matthew Sykes's avatar Matthew Sykes
Browse files

[FAB-10686] testutil->testify common/ledger/util



Change-Id: I328eff7cb43431878afec7ec451d6398f1299542
Signed-off-by: default avatarMatthew Sykes <sykesmat@us.ibm.com>
parent ef92892c
......@@ -17,12 +17,10 @@ limitations under the License.
package util
import (
"fmt"
"os"
"path/filepath"
"testing"
"github.com/hyperledger/fabric/common/ledger/testutil"
"github.com/stretchr/testify/assert"
)
......@@ -37,8 +35,8 @@ func TestCreatingDBDirWithPathSeperator(t *testing.T) {
defer cleanup(dbPathTestWSeparator)
dirEmpty, err := CreateDirIfMissing(dbPathTestWSeparator)
testutil.AssertNoError(t, err, fmt.Sprintf("Error when trying to create a test db directory at [%s]", dbPathTestWSeparator))
testutil.AssertEquals(t, dirEmpty, true) //test directory is empty is returning true
assert.NoError(t, err, "Error when trying to create a test db directory at [%s]", dbPathTestWSeparator)
assert.True(t, dirEmpty) //test directory is empty is returning true
}
func TestCreatingDBDirWhenDirDoesAndDoesNotExists(t *testing.T) {
......@@ -48,13 +46,13 @@ func TestCreatingDBDirWhenDirDoesAndDoesNotExists(t *testing.T) {
//test creating a directory without path separator passed
dirEmpty, err := CreateDirIfMissing(dbPathTest)
testutil.AssertNoError(t, err, fmt.Sprintf("Error when trying to create a test db directory at [%s]", dbPathTest))
testutil.AssertEquals(t, dirEmpty, true)
assert.NoError(t, err, "Error when trying to create a test db directory at [%s]", dbPathTest)
assert.True(t, dirEmpty)
//test creating directory AGAIN, that is the directory already exists
dirEmpty2, err2 := CreateDirIfMissing(dbPathTest)
testutil.AssertNoError(t, err2, fmt.Sprintf("Error not handling existing directory when trying to create a test db directory at [%s]", dbPathTest))
testutil.AssertEquals(t, dirEmpty2, true)
assert.NoError(t, err2, "Error not handling existing directory when trying to create a test db directory at [%s]", dbPathTest)
assert.True(t, dirEmpty2)
}
func TestDirNotEmptyAndFileExists(t *testing.T) {
......@@ -64,31 +62,31 @@ func TestDirNotEmptyAndFileExists(t *testing.T) {
//create the directory
dirEmpty, err := CreateDirIfMissing(dbPathTest)
testutil.AssertNoError(t, err, fmt.Sprintf("Error when trying to create a test db directory at [%s]", dbPathTest))
testutil.AssertEquals(t, dirEmpty, true)
assert.NoError(t, err, "Error when trying to create a test db directory at [%s]", dbPathTest)
assert.True(t, dirEmpty)
//test file does not exists and size is returned correctly
exists2, size2, err2 := FileExists(dbFileTest)
testutil.AssertNoError(t, err2, fmt.Sprintf("Error when trying to determine if file exist when it does not at [%s]", dbFileTest))
testutil.AssertEquals(t, size2, int64(0))
testutil.AssertEquals(t, exists2, false) //test file that does not exists reports false
assert.NoError(t, err2, "Error when trying to determine if file exist when it does not at [%s]", dbFileTest)
assert.Equal(t, int64(0), size2)
assert.False(t, exists2) //test file that does not exists reports false
//create file
testStr := "This is some test data in a file"
sizeOfFileCreated, err3 := createAndWriteAFile(testStr)
testutil.AssertNoError(t, err3, fmt.Sprintf("Error when trying to create and write to file at [%s]", dbFileTest))
testutil.AssertEquals(t, sizeOfFileCreated, len(testStr)) //test file size returned is correct
assert.NoError(t, err3, "Error when trying to create and write to file at [%s]", dbFileTest)
assert.Equal(t, len(testStr), sizeOfFileCreated) //test file size returned is correct
//test that the file exists and size is returned correctly
exists, size, err4 := FileExists(dbFileTest)
testutil.AssertNoError(t, err4, fmt.Sprintf("Error when trying to determine if file exist at [%s]", dbFileTest))
testutil.AssertEquals(t, size, int64(sizeOfFileCreated))
testutil.AssertEquals(t, exists, true) //test file that does exists reports true
assert.NoError(t, err4, "Error when trying to determine if file exist at [%s]", dbFileTest)
assert.Equal(t, int64(sizeOfFileCreated), size)
assert.True(t, exists) //test file that does exists reports true
//test that if the directory is not empty
dirEmpty5, err5 := DirEmpty(dbPathTest)
testutil.AssertNoError(t, err5, fmt.Sprintf("Error when detecting if empty at db directory [%s]", dbPathTest))
testutil.AssertEquals(t, dirEmpty5, false) //test directory is empty is returning false
assert.NoError(t, err5, "Error when detecting if empty at db directory [%s]", dbPathTest)
assert.False(t, dirEmpty5) //test directory is empty is returning false
}
func TestListSubdirs(t *testing.T) {
......@@ -100,7 +98,7 @@ func TestListSubdirs(t *testing.T) {
}
subFolders, err := ListSubdirs(dbPathTest)
assert.NoError(t, err)
assert.Equal(t, childFolders, subFolders)
assert.Equal(t, subFolders, childFolders)
}
func createAndWriteAFile(sentence string) (int, error) {
......
......@@ -21,7 +21,7 @@ import (
"path/filepath"
"testing"
"github.com/hyperledger/fabric/common/ledger/testutil"
"github.com/stretchr/testify/assert"
"github.com/syndtr/goleveldb/leveldb"
)
......@@ -62,25 +62,25 @@ func TestLevelDBHelper(t *testing.T) {
db.Put([]byte("key3"), []byte("value3"), true)
val, _ := db.Get([]byte("key2"))
testutil.AssertEquals(t, string(val), "value2")
assert.Equal(t, "value2", string(val))
db.Delete([]byte("key1"), false)
db.Delete([]byte("key2"), true)
val1, err1 := db.Get([]byte("key1"))
testutil.AssertNoError(t, err1, "")
testutil.AssertEquals(t, string(val1), "")
assert.NoError(t, err1, "")
assert.Equal(t, "", string(val1))
val2, err2 := db.Get([]byte("key2"))
testutil.AssertNoError(t, err2, "")
testutil.AssertEquals(t, string(val2), "")
assert.NoError(t, err2, "")
assert.Equal(t, "", string(val2))
db.Close()
// second time open should not have any side effect
db.Close()
val3, err3 := db.Get([]byte("key3"))
testutil.AssertError(t, err3, "")
assert.Error(t, err3)
db.Open()
batch := &leveldb.Batch{}
......@@ -90,28 +90,28 @@ func TestLevelDBHelper(t *testing.T) {
db.WriteBatch(batch, true)
val1, err1 = db.Get([]byte("key1"))
testutil.AssertNoError(t, err1, "")
testutil.AssertEquals(t, string(val1), "value1")
assert.NoError(t, err1, "")
assert.Equal(t, "value1", string(val1))
val2, err2 = db.Get([]byte("key2"))
testutil.AssertNoError(t, err2, "")
testutil.AssertEquals(t, string(val2), "value2")
assert.NoError(t, err2, "")
assert.Equal(t, "value2", string(val2))
val3, err3 = db.Get([]byte("key3"))
testutil.AssertNoError(t, err3, "")
testutil.AssertEquals(t, string(val3), "")
assert.NoError(t, err3, "")
assert.Equal(t, "", string(val3))
keys := []string{}
itr := db.GetIterator(nil, nil)
for itr.Next() {
keys = append(keys, string(itr.Key()))
}
testutil.AssertEquals(t, keys, []string{"key1", "key2"})
assert.Equal(t, []string{"key1", "key2"}, keys)
}
func TestCreateDBInEmptyDir(t *testing.T) {
testutil.AssertNoError(t, os.RemoveAll(testDBPath), "")
testutil.AssertNoError(t, os.MkdirAll(testDBPath, 0775), "")
assert.NoError(t, os.RemoveAll(testDBPath), "")
assert.NoError(t, os.MkdirAll(testDBPath, 0775), "")
db := CreateDB(&Conf{testDBPath})
defer db.Close()
defer func() {
......@@ -123,10 +123,10 @@ func TestCreateDBInEmptyDir(t *testing.T) {
}
func TestCreateDBInNonEmptyDir(t *testing.T) {
testutil.AssertNoError(t, os.RemoveAll(testDBPath), "")
testutil.AssertNoError(t, os.MkdirAll(testDBPath, 0775), "")
assert.NoError(t, os.RemoveAll(testDBPath), "")
assert.NoError(t, os.MkdirAll(testDBPath, 0775), "")
file, err := os.Create(filepath.Join(testDBPath, "dummyfile.txt"))
testutil.AssertNoError(t, err, "")
assert.NoError(t, err, "")
file.Close()
db := CreateDB(&Conf{testDBPath})
defer db.Close()
......
......@@ -20,7 +20,7 @@ import (
"fmt"
"testing"
"github.com/hyperledger/fabric/common/ledger/testutil"
"github.com/stretchr/testify/assert"
)
func TestDBBasicWriteAndReads(t *testing.T) {
......@@ -79,14 +79,14 @@ func TestBatchedUpdates(t *testing.T) {
for _, db := range dbs {
val1, _ := db.Get([]byte("key1"))
testutil.AssertEquals(t, string(val1), "value1")
assert.Equal(t, "value1", string(val1))
val2, err2 := db.Get([]byte("key2"))
testutil.AssertNoError(t, err2, "")
testutil.AssertNil(t, val2)
assert.NoError(t, err2, "")
assert.Nil(t, val2)
val3, _ := db.Get([]byte("key3"))
testutil.AssertEquals(t, string(val3), "value3")
assert.Equal(t, "value3", string(val3))
}
}
......@@ -105,34 +105,34 @@ func testDBBasicWriteAndReads(t *testing.T, dbNames ...string) {
for _, dbName := range dbNames {
db := p.GetDBHandle(dbName)
val, err := db.Get([]byte("key1"))
testutil.AssertNoError(t, err, "")
testutil.AssertEquals(t, val, []byte("value1_"+dbName))
assert.NoError(t, err, "")
assert.Equal(t, []byte("value1_"+dbName), val)
val, err = db.Get([]byte("key2"))
testutil.AssertNoError(t, err, "")
testutil.AssertEquals(t, val, []byte("value2_"+dbName))
assert.NoError(t, err, "")
assert.Equal(t, []byte("value2_"+dbName), val)
val, err = db.Get([]byte("key3"))
testutil.AssertNoError(t, err, "")
testutil.AssertEquals(t, val, []byte("value3_"+dbName))
assert.NoError(t, err, "")
assert.Equal(t, []byte("value3_"+dbName), val)
}
for _, dbName := range dbNames {
db := p.GetDBHandle(dbName)
testutil.AssertNoError(t, db.Delete([]byte("key1"), false), "")
assert.NoError(t, db.Delete([]byte("key1"), false), "")
val, err := db.Get([]byte("key1"))
testutil.AssertNoError(t, err, "")
testutil.AssertNil(t, val)
assert.NoError(t, err, "")
assert.Nil(t, val)
testutil.AssertNoError(t, db.Delete([]byte("key2"), false), "")
assert.NoError(t, db.Delete([]byte("key2"), false), "")
val, err = db.Get([]byte("key2"))
testutil.AssertNoError(t, err, "")
testutil.AssertNil(t, val)
assert.NoError(t, err, "")
assert.Nil(t, val)
testutil.AssertNoError(t, db.Delete([]byte("key3"), false), "")
assert.NoError(t, db.Delete([]byte("key3"), false), "")
val, err = db.Get([]byte("key3"))
testutil.AssertNoError(t, err, "")
testutil.AssertNil(t, val)
assert.NoError(t, err, "")
assert.Nil(t, val)
}
}
......@@ -144,9 +144,9 @@ func checkItrResults(t *testing.T, itr *Iterator, expectedKeys []string, expecte
actualKeys = append(actualKeys, string(itr.Key()))
actualValues = append(actualValues, string(itr.Value()))
}
testutil.AssertEquals(t, actualKeys, expectedKeys)
testutil.AssertEquals(t, actualValues, expectedValues)
testutil.AssertEquals(t, itr.Next(), false)
assert.Equal(t, expectedKeys, actualKeys)
assert.Equal(t, expectedValues, actualValues)
assert.Equal(t, false, itr.Next())
}
func createTestKey(i int) string {
......
......@@ -20,7 +20,7 @@ import (
"os"
"testing"
"github.com/hyperledger/fabric/common/ledger/testutil"
"github.com/stretchr/testify/assert"
)
const testDBPath = "/tmp/fabric/ledgertests/util/leveldbhelper"
......@@ -55,12 +55,12 @@ func (dbEnv *testDBEnv) cleanup() {
if dbEnv.db != nil {
dbEnv.db.Close()
}
testutil.AssertNoError(dbEnv.t, os.RemoveAll(dbEnv.path), "")
assert.NoError(dbEnv.t, os.RemoveAll(dbEnv.path))
}
func (providerEnv *testDBProviderEnv) cleanup() {
if providerEnv.provider != nil {
providerEnv.provider.Close()
}
testutil.AssertNoError(providerEnv.t, os.RemoveAll(providerEnv.path), "")
assert.NoError(providerEnv.t, os.RemoveAll(providerEnv.path))
}
......@@ -20,7 +20,7 @@ import (
"testing"
"github.com/golang/protobuf/proto"
"github.com/hyperledger/fabric/common/ledger/testutil"
"github.com/stretchr/testify/assert"
)
func TestBuffer(t *testing.T) {
......@@ -36,11 +36,11 @@ func TestBuffer(t *testing.T) {
b := NewBuffer(pb.Bytes())
b.DecodeVarint()
testutil.AssertEquals(t, b.GetBytesConsumed(), pos1)
assert.Equal(t, pos1, b.GetBytesConsumed())
b.DecodeRawBytes(false)
testutil.AssertEquals(t, b.GetBytesConsumed(), pos2)
assert.Equal(t, pos2, b.GetBytesConsumed())
b.DecodeRawBytes(false)
testutil.AssertEquals(t, b.GetBytesConsumed(), pos3)
assert.Equal(t, pos3, b.GetBytesConsumed())
b.DecodeVarint()
testutil.AssertEquals(t, b.GetBytesConsumed(), pos4)
assert.Equal(t, pos4, b.GetBytesConsumed())
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment