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

     1  package leveldb
     2  
     3  import (
     4  	"fmt"
     5  	"io/ioutil"
     6  	"os"
     7  	"testing"
     8  
     9  	"github.com/stretchr/testify/require"
    10  )
    11  
    12  func newTempDB(t *testing.T, backend string) (db DB, dbDir string) {
    13  	dirname, err := ioutil.TempDir("", "db_common_test")
    14  	require.Nil(t, err)
    15  	return NewDB("testdb", backend, dirname), dirname
    16  }
    17  
    18  func TestDBIteratorSingleKey(t *testing.T) {
    19  	for backend := range backends {
    20  		t.Run(fmt.Sprintf("Backend %s", backend), func(t *testing.T) {
    21  			db, dir := newTempDB(t, backend)
    22  			defer os.RemoveAll(dir)
    23  
    24  			db.Set([]byte("1"), []byte("value_1"))
    25  			itr := db.IteratorPrefixWithStart(nil, nil, false)
    26  			require.Equal(t, []byte(""), itr.Key())
    27  			require.Equal(t, true, itr.Next())
    28  			require.Equal(t, []byte("1"), itr.Key())
    29  		})
    30  	}
    31  }
    32  
    33  func TestDBIteratorTwoKeys(t *testing.T) {
    34  	for backend := range backends {
    35  		t.Run(fmt.Sprintf("Backend %s", backend), func(t *testing.T) {
    36  			db, dir := newTempDB(t, backend)
    37  			defer os.RemoveAll(dir)
    38  
    39  			db.SetSync([]byte("1"), []byte("value_1"))
    40  			db.SetSync([]byte("2"), []byte("value_1"))
    41  
    42  			itr := db.IteratorPrefixWithStart(nil, []byte("1"), false)
    43  
    44  			require.Equal(t, []byte("1"), itr.Key())
    45  
    46  			require.Equal(t, true, itr.Next())
    47  			itr = db.IteratorPrefixWithStart(nil, []byte("2"), false)
    48  
    49  			require.Equal(t, false, itr.Next())
    50  		})
    51  	}
    52  }
    53  
    54  func TestDBIterator(t *testing.T) {
    55  	dirname, err := ioutil.TempDir("", "db_common_test")
    56  	require.Nil(t, err)
    57  
    58  	db, err := NewGoLevelDB("testdb", dirname)
    59  	if err != nil {
    60  		t.Fatal(err)
    61  	}
    62  
    63  	defer func() {
    64  		db.Close()
    65  		os.RemoveAll(dirname)
    66  	}()
    67  
    68  	db.SetSync([]byte("aaa1"), []byte("value_1"))
    69  	db.SetSync([]byte("aaa22"), []byte("value_2"))
    70  	db.SetSync([]byte("bbb22"), []byte("value_3"))
    71  
    72  	itr := db.IteratorPrefixWithStart([]byte("aaa"), []byte("aaa1"), false)
    73  	defer itr.Release()
    74  
    75  	require.Equal(t, true, itr.Next())
    76  	require.Equal(t, []byte("aaa22"), itr.Key())
    77  
    78  	require.Equal(t, false, itr.Next())
    79  
    80  	itr = db.IteratorPrefixWithStart([]byte("aaa"), nil, false)
    81  
    82  	require.Equal(t, true, itr.Next())
    83  	require.Equal(t, []byte("aaa1"), itr.Key())
    84  
    85  	require.Equal(t, true, itr.Next())
    86  	require.Equal(t, []byte("aaa22"), itr.Key())
    87  
    88  	require.Equal(t, false, itr.Next())
    89  
    90  	itr = db.IteratorPrefixWithStart([]byte("bbb"), []byte("aaa1"), false)
    91  	require.Equal(t, false, itr.Next())
    92  }
    93  
    94  func TestDBIteratorReverse(t *testing.T) {
    95  	dirname, err := ioutil.TempDir("", "db_common_test")
    96  	require.Nil(t, err)
    97  
    98  	db, err := NewGoLevelDB("testdb", dirname)
    99  	if err != nil {
   100  		t.Fatal(err)
   101  	}
   102  
   103  	defer func() {
   104  		db.Close()
   105  		os.RemoveAll(dirname)
   106  	}()
   107  
   108  	db.SetSync([]byte("aaa1"), []byte("value_1"))
   109  	db.SetSync([]byte("aaa22"), []byte("value_2"))
   110  	db.SetSync([]byte("bbb22"), []byte("value_3"))
   111  
   112  	itr := db.IteratorPrefixWithStart([]byte("aaa"), []byte("aaa22"), true)
   113  	defer itr.Release()
   114  
   115  	require.Equal(t, true, itr.Next())
   116  	require.Equal(t, []byte("aaa1"), itr.Key())
   117  
   118  	require.Equal(t, false, itr.Next())
   119  
   120  	itr = db.IteratorPrefixWithStart([]byte("aaa"), nil, true)
   121  
   122  	require.Equal(t, true, itr.Next())
   123  	require.Equal(t, []byte("aaa22"), itr.Key())
   124  
   125  	require.Equal(t, true, itr.Next())
   126  	require.Equal(t, []byte("aaa1"), itr.Key())
   127  
   128  	require.Equal(t, false, itr.Next())
   129  
   130  	require.Equal(t, false, itr.Next())
   131  
   132  	itr = db.IteratorPrefixWithStart([]byte("bbb"), []byte("aaa1"), true)
   133  	require.Equal(t, false, itr.Next())
   134  }