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

     1  package storage
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  	"github.com/stretchr/testify/require"
     9  
    10  	dbm "github.com/tendermint/tendermint/libs/db"
    11  )
    12  
    13  func mockDBWithStuff() dbm.DB {
    14  	db := dbm.NewMemDB()
    15  	// Under "key" prefix
    16  	db.Set(bz("key"), bz("value"))
    17  	db.Set(bz("key1"), bz("value1"))
    18  	db.Set(bz("key2"), bz("value2"))
    19  	db.Set(bz("key3"), bz("value3"))
    20  	db.Set(bz("something"), bz("else"))
    21  	db.Set(bz(""), bz(""))
    22  	db.Set(bz("k"), bz("val"))
    23  	db.Set(bz("ke"), bz("valu"))
    24  	db.Set(bz("kee"), bz("valuu"))
    25  	return db
    26  }
    27  
    28  func TestPrefixDBSimple(t *testing.T) {
    29  	db := mockDBWithStuff()
    30  	pdb := NewPrefixDB(db, "key")
    31  
    32  	checkValue(t, pdb, bz("key"), nil)
    33  	checkValue(t, pdb, bz(""), bz("value"))
    34  	checkValue(t, pdb, bz("key1"), nil)
    35  	checkValue(t, pdb, bz("1"), bz("value1"))
    36  	checkValue(t, pdb, bz("key2"), nil)
    37  	checkValue(t, pdb, bz("2"), bz("value2"))
    38  	checkValue(t, pdb, bz("key3"), nil)
    39  	checkValue(t, pdb, bz("3"), bz("value3"))
    40  	checkValue(t, pdb, bz("something"), nil)
    41  	checkValue(t, pdb, bz("k"), nil)
    42  	checkValue(t, pdb, bz("ke"), nil)
    43  	checkValue(t, pdb, bz("kee"), nil)
    44  }
    45  
    46  func TestPrefixDBIterator1(t *testing.T) {
    47  	db := mockDBWithStuff()
    48  	pdb := NewPrefixDB(db, "key")
    49  
    50  	itr := pdb.Iterator(nil, nil)
    51  	checkDomain(t, itr, nil, nil)
    52  	checkItem(t, itr, bz(""), bz("value"))
    53  	checkNext(t, itr, true)
    54  	checkItem(t, itr, bz("1"), bz("value1"))
    55  	checkNext(t, itr, true)
    56  	checkItem(t, itr, bz("2"), bz("value2"))
    57  	checkNext(t, itr, true)
    58  	checkItem(t, itr, bz("3"), bz("value3"))
    59  	checkNext(t, itr, false)
    60  	checkInvalid(t, itr)
    61  	itr.Close()
    62  }
    63  
    64  func TestPrefixDBIterator2(t *testing.T) {
    65  	db := mockDBWithStuff()
    66  	pdb := NewPrefixDB(db, "key")
    67  
    68  	itr := pdb.Iterator(nil, bz(""))
    69  	checkDomain(t, itr, nil, bz(""))
    70  	checkInvalid(t, itr)
    71  	itr.Close()
    72  }
    73  
    74  func TestPrefixDBIterator3(t *testing.T) {
    75  	db := mockDBWithStuff()
    76  	pdb := NewPrefixDB(db, "key")
    77  
    78  	itr := pdb.Iterator(bz(""), nil)
    79  	checkDomain(t, itr, bz(""), nil)
    80  	checkItem(t, itr, bz(""), bz("value"))
    81  	checkNext(t, itr, true)
    82  	checkItem(t, itr, bz("1"), bz("value1"))
    83  	checkNext(t, itr, true)
    84  	checkItem(t, itr, bz("2"), bz("value2"))
    85  	checkNext(t, itr, true)
    86  	checkItem(t, itr, bz("3"), bz("value3"))
    87  	checkNext(t, itr, false)
    88  	checkInvalid(t, itr)
    89  	itr.Close()
    90  }
    91  
    92  func TestPrefixDBIterator4(t *testing.T) {
    93  	db := mockDBWithStuff()
    94  	pdb := NewPrefixDB(db, "key")
    95  
    96  	itr := pdb.Iterator(bz(""), bz(""))
    97  	checkDomain(t, itr, bz(""), bz(""))
    98  	checkInvalid(t, itr)
    99  	itr.Close()
   100  }
   101  
   102  func TestPrefixDBReverseIterator1(t *testing.T) {
   103  	db := mockDBWithStuff()
   104  	pdb := NewPrefixDB(db, "key")
   105  
   106  	itr := pdb.ReverseIterator(nil, nil)
   107  	checkDomain(t, itr, nil, nil)
   108  	checkItem(t, itr, bz("3"), bz("value3"))
   109  	checkNext(t, itr, true)
   110  	checkItem(t, itr, bz("2"), bz("value2"))
   111  	checkNext(t, itr, true)
   112  	checkItem(t, itr, bz("1"), bz("value1"))
   113  	checkNext(t, itr, true)
   114  	checkItem(t, itr, bz(""), bz("value"))
   115  	checkNext(t, itr, false)
   116  	checkInvalid(t, itr)
   117  	itr.Close()
   118  }
   119  
   120  func TestPrefixDBReverseIterator2(t *testing.T) {
   121  	db := mockDBWithStuff()
   122  	pdb := NewPrefixDB(db, "key")
   123  
   124  	itr := pdb.ReverseIterator(bz(""), nil)
   125  	checkDomain(t, itr, bz(""), nil)
   126  	checkItem(t, itr, bz("3"), bz("value3"))
   127  	checkNext(t, itr, true)
   128  	checkItem(t, itr, bz("2"), bz("value2"))
   129  	checkNext(t, itr, true)
   130  	checkItem(t, itr, bz("1"), bz("value1"))
   131  	checkNext(t, itr, true)
   132  	checkItem(t, itr, bz(""), bz("value"))
   133  	checkNext(t, itr, false)
   134  	checkInvalid(t, itr)
   135  	itr.Close()
   136  }
   137  
   138  func TestPrefixDBReverseIterator3(t *testing.T) {
   139  	db := mockDBWithStuff()
   140  	pdb := NewPrefixDB(db, "key")
   141  
   142  	itr := pdb.ReverseIterator(nil, bz(""))
   143  	checkDomain(t, itr, nil, bz(""))
   144  	checkInvalid(t, itr)
   145  	itr.Close()
   146  }
   147  
   148  func TestPrefixDBReverseIterator4(t *testing.T) {
   149  	db := mockDBWithStuff()
   150  	pdb := NewPrefixDB(db, "key")
   151  
   152  	itr := pdb.ReverseIterator(bz(""), bz(""))
   153  	checkDomain(t, itr, bz(""), bz(""))
   154  	checkInvalid(t, itr)
   155  	itr.Close()
   156  }
   157  
   158  func TestPrefixDBReverseIterator5(t *testing.T) {
   159  	db := mockDBWithStuff()
   160  	pdb := NewPrefixDB(db, "key")
   161  
   162  	itr := pdb.ReverseIterator(bz("1"), nil)
   163  	checkDomain(t, itr, bz("1"), nil)
   164  	checkItem(t, itr, bz("3"), bz("value3"))
   165  	checkNext(t, itr, true)
   166  	checkItem(t, itr, bz("2"), bz("value2"))
   167  	checkNext(t, itr, true)
   168  	checkItem(t, itr, bz("1"), bz("value1"))
   169  	checkNext(t, itr, false)
   170  	checkInvalid(t, itr)
   171  	itr.Close()
   172  }
   173  
   174  func TestPrefixDBReverseIterator6(t *testing.T) {
   175  	db := mockDBWithStuff()
   176  	pdb := NewPrefixDB(db, "key")
   177  
   178  	itr := pdb.ReverseIterator(bz("2"), nil)
   179  	checkDomain(t, itr, bz("2"), nil)
   180  	checkItem(t, itr, bz("3"), bz("value3"))
   181  	checkNext(t, itr, true)
   182  	checkItem(t, itr, bz("2"), bz("value2"))
   183  	checkNext(t, itr, false)
   184  	checkInvalid(t, itr)
   185  	itr.Close()
   186  }
   187  
   188  func TestPrefixDBReverseIterator7(t *testing.T) {
   189  	db := mockDBWithStuff()
   190  	pdb := NewPrefixDB(db, "key")
   191  
   192  	itr := pdb.ReverseIterator(nil, bz("2"))
   193  	checkDomain(t, itr, nil, bz("2"))
   194  	checkItem(t, itr, bz("1"), bz("value1"))
   195  	checkNext(t, itr, true)
   196  	checkItem(t, itr, bz(""), bz("value"))
   197  	checkNext(t, itr, false)
   198  	checkInvalid(t, itr)
   199  	itr.Close()
   200  }
   201  
   202  func (p Prefix) BadSuffix(key []byte) []byte {
   203  	return key[len(p):]
   204  }
   205  
   206  func TestBadSuffix(t *testing.T) {
   207  	p := Prefix([]byte("foo"))
   208  	fmt.Println(cap(p))
   209  	key1 := p.BadSuffix([]byte("fooaaa"))
   210  	fmt.Println(cap(p), p, string(key1))
   211  	key2 := p.BadSuffix([]byte("foobbb"))
   212  	fmt.Println(cap(p), p, string(key1))
   213  	fmt.Println(cap(p), p, string(key2))
   214  
   215  }
   216  
   217  func checkValue(t *testing.T, db dbm.DB, key []byte, valueWanted []byte) {
   218  	valueGot := db.Get(key)
   219  	assert.Equal(t, valueWanted, valueGot)
   220  }
   221  
   222  func checkValid(t *testing.T, itr dbm.Iterator, expected bool) {
   223  	valid := itr.Valid()
   224  	require.Equal(t, expected, valid)
   225  }
   226  
   227  func checkNext(t *testing.T, itr dbm.Iterator, expected bool) {
   228  	itr.Next()
   229  	valid := itr.Valid()
   230  	require.Equal(t, expected, valid)
   231  }
   232  
   233  func checkNextPanics(t *testing.T, itr dbm.Iterator) {
   234  	assert.Panics(t, func() { itr.Next() }, "checkNextPanics expected panic but didn't")
   235  }
   236  
   237  func checkDomain(t *testing.T, itr dbm.Iterator, start, end []byte) {
   238  	ds, de := itr.Domain()
   239  	assert.Equal(t, start, ds, "checkDomain domain start incorrect")
   240  	assert.Equal(t, end, de, "checkDomain domain end incorrect")
   241  }
   242  
   243  func checkItem(t *testing.T, itr dbm.Iterator, key []byte, value []byte) {
   244  	k, v := itr.Key(), itr.Value()
   245  	assert.Exactly(t, key, k)
   246  	assert.Exactly(t, value, v)
   247  }
   248  
   249  func checkInvalid(t *testing.T, itr dbm.Iterator) {
   250  	checkValid(t, itr, false)
   251  	checkKeyPanics(t, itr)
   252  	checkValuePanics(t, itr)
   253  	checkNextPanics(t, itr)
   254  }
   255  
   256  func checkKeyPanics(t *testing.T, itr dbm.Iterator) {
   257  	assert.Panics(t, func() { itr.Key() }, "checkKeyPanics expected panic but didn't")
   258  }
   259  
   260  func checkValuePanics(t *testing.T, itr dbm.Iterator) {
   261  	assert.Panics(t, func() { itr.Key() }, "checkValuePanics expected panic but didn't")
   262  }