github.com/bytom/bytom@v1.1.2-0.20221014091027-bbcba3df6075/database/leveldb/mem_db_test.go (about)

     1  package leveldb
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/assert"
     7  	"github.com/stretchr/testify/require"
     8  )
     9  
    10  func TestMemDbIterator(t *testing.T) {
    11  	db := NewMemDB()
    12  	keys := make([][]byte, 100)
    13  	for i := 0; i < 100; i++ {
    14  		keys[i] = []byte{byte(i)}
    15  	}
    16  
    17  	value := []byte{5}
    18  	for _, k := range keys {
    19  		db.Set(k, value)
    20  	}
    21  
    22  	iter := db.Iterator()
    23  	i := 0
    24  	for iter.Next() {
    25  		assert.Equal(t, db.Get(iter.Key()), iter.Value(), "values dont match for key")
    26  		i++
    27  	}
    28  	assert.Equal(t, i, len(db.db), "iterator didnt cover whole db")
    29  }
    30  
    31  func TestMemDBClose(t *testing.T) {
    32  	db := NewMemDB()
    33  	copyDB := func(orig map[string][]byte) map[string][]byte {
    34  		copy := make(map[string][]byte)
    35  		for k, v := range orig {
    36  			copy[k] = v
    37  		}
    38  		return copy
    39  	}
    40  	k, v := []byte("foo"), []byte("bar")
    41  	db.Set(k, v)
    42  	require.Equal(t, db.Get(k), v, "expecting a successful get")
    43  	copyBefore := copyDB(db.db)
    44  	db.Close()
    45  	require.Equal(t, db.Get(k), v, "Close is a noop, expecting a successful get")
    46  	copyAfter := copyDB(db.db)
    47  	require.Equal(t, copyBefore, copyAfter, "Close is a noop and shouldn't modify any internal data")
    48  }