github.com/eris-ltd/erisdb@v0.25.0/storage/cache_db_test.go (about)

     1  package storage
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  	dbm "github.com/tendermint/tendermint/libs/db"
     9  )
    10  
    11  func TestBatchCommit(t *testing.T) {
    12  	db := dbm.NewMemDB()
    13  	cdb := NewCacheDB(db)
    14  	foo := bz("foo")
    15  	bam := bz("bam")
    16  	bosh := bz("bosh")
    17  	boom := bz("boom")
    18  	db.Set(foo, bam)
    19  	assert.Equal(t, bam, cdb.Get(foo), "underlying writes should be seen")
    20  	cdb.Set(foo, bosh)
    21  	assert.Equal(t, bosh, cdb.Get(foo), "writes to CacheDB should be available")
    22  	batch := cdb.NewBatch()
    23  	batch.Set(foo, bam)
    24  	assert.Equal(t, bosh, cdb.Get(foo), "write to batch should not be seen")
    25  	batch.WriteSync()
    26  	cdb.Commit(db)
    27  	assert.Equal(t, bam, db.Get(foo), "changes should commit")
    28  	cdb.Set(foo, bosh)
    29  	assert.Equal(t, bam, db.Get(foo), "uncommitted changes should not be seen in db")
    30  	cdb.Delete(foo)
    31  	assert.Nil(t, cdb.Get(foo))
    32  	assert.Equal(t, bam, db.Get(foo))
    33  	cdb.Commit(db)
    34  	assert.Nil(t, db.Get(foo))
    35  	cdb.Set(foo, boom)
    36  	assert.Nil(t, db.Get(foo))
    37  }
    38  
    39  func TestCacheDB_Iterator(t *testing.T) {
    40  	db := dbm.NewMemDB()
    41  	cdb := NewCacheDB(db)
    42  	foo := bz("foo")
    43  	bam := bz("bam")
    44  	bosh := bz("bosh")
    45  	boom := bz("boom")
    46  
    47  	db.Set(append(foo, foo...), foo)
    48  	db.Set(append(foo, bam...), bam)
    49  	cdb.Set(append(foo, bosh...), bosh)
    50  	cdb.Set(boom, boom)
    51  
    52  	it := cdb.Iterator(nil, nil)
    53  	kvp := collectIterator(it)
    54  	fmt.Println(kvp)
    55  	cdb.Commit(db)
    56  
    57  	it = db.Iterator(nil, nil)
    58  	assert.Equal(t, kvp, collectIterator(it))
    59  }