github.com/turingchain2020/turingchain@v1.1.21/common/db/go_badger_db_test.go (about)

     1  // Copyright Turing Corp. 2018 All Rights Reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package db
     6  
     7  import (
     8  	"io/ioutil"
     9  	"testing"
    10  
    11  	"github.com/dgraph-io/badger"
    12  	"github.com/stretchr/testify/require"
    13  )
    14  
    15  // badgerdb迭代器测试
    16  func TestGoBadgerDBIterator(t *testing.T) {
    17  	dir, err := ioutil.TempDir("", "badger")
    18  	require.NoError(t, err)
    19  	t.Log(dir)
    20  
    21  	badgerdb, err := NewGoBadgerDB("gobadgerdb", dir, 128)
    22  	require.NoError(t, err)
    23  	defer badgerdb.Close()
    24  
    25  	testDBIterator(t, badgerdb)
    26  }
    27  
    28  func TestGoBadgerDBIteratorDel(t *testing.T) {
    29  	dir, err := ioutil.TempDir("", "badger")
    30  	require.NoError(t, err)
    31  	t.Log(dir)
    32  
    33  	badgerdb, err := NewGoBadgerDB("gobadgerdb", dir, 128)
    34  	require.NoError(t, err)
    35  	defer badgerdb.Close()
    36  
    37  	testDBIteratorDel(t, badgerdb)
    38  }
    39  
    40  // badgerdb边界测试
    41  func TestGoBadgerDBBoundary(t *testing.T) {
    42  	dir, err := ioutil.TempDir("", "badger")
    43  	require.NoError(t, err)
    44  	t.Log(dir)
    45  
    46  	badgerdb, err := NewGoBadgerDB("gobadgerdb", dir, 128)
    47  	require.NoError(t, err)
    48  	defer badgerdb.Close()
    49  
    50  	testDBBoundary(t, badgerdb)
    51  }
    52  
    53  func TestBadgerDB(t *testing.T) {
    54  	dir, err := ioutil.TempDir("", "badger")
    55  	require.NoError(t, err)
    56  	t.Log(dir)
    57  
    58  	opts := badger.DefaultOptions(dir)
    59  	badgerdb, err := badger.Open(opts)
    60  	require.NoError(t, err)
    61  	defer badgerdb.Close()
    62  
    63  	err = badgerdb.Update(func(txn *badger.Txn) error {
    64  		err := txn.Set([]byte("key1"), []byte("hello"))
    65  		return err
    66  	})
    67  	require.NoError(t, err)
    68  
    69  	err = badgerdb.View(func(txn *badger.Txn) error {
    70  		item, err := txn.Get([]byte("key1"))
    71  		if err != nil {
    72  			return err
    73  		}
    74  		val, err := item.ValueCopy(nil)
    75  		if err != nil {
    76  			return err
    77  		}
    78  		t.Log("The answer is: ", string(val))
    79  		return nil
    80  	})
    81  
    82  	require.NoError(t, err)
    83  }
    84  
    85  func TestBadgerBatchDB(t *testing.T) {
    86  	dir, err := ioutil.TempDir("", "badger121")
    87  	require.NoError(t, err)
    88  	//t.Log(dir)
    89  
    90  	badgerdb, err := NewGoBadgerDB("gobadgerdb", dir, 128)
    91  	require.NoError(t, err)
    92  	batch := badgerdb.NewBatch(true)
    93  
    94  	batch.Set([]byte("123"), []byte("121"))
    95  	batch.Set([]byte("124"), []byte("111"))
    96  	batch.Set([]byte("125"), []byte("111"))
    97  	batch.Write()
    98  
    99  	batch.Reset()
   100  
   101  	batch.Set([]byte("126"), []byte("111"))
   102  	batch.Write()
   103  
   104  	value, err := badgerdb.Get([]byte("123"))
   105  	require.NoError(t, err)
   106  	require.Equal(t, string(value), "121")
   107  	value, err = badgerdb.Get([]byte("126"))
   108  	require.NoError(t, err)
   109  	require.Equal(t, string(value), "111")
   110  }
   111  
   112  func TestBadgerBatch(t *testing.T) {
   113  	dir, err := ioutil.TempDir("", "badger1212")
   114  	require.NoError(t, err)
   115  	t.Log(dir)
   116  
   117  	db, err := NewGoBadgerDB("gobadgerdb", dir, 128)
   118  	require.NoError(t, err)
   119  	defer db.Close()
   120  	testBatch(t, db)
   121  }