github.com/turingchain2020/turingchain@v1.1.21/common/db/table/count_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 table
     6  
     7  import (
     8  	"testing"
     9  
    10  	"github.com/turingchain2020/turingchain/util"
    11  	"github.com/stretchr/testify/assert"
    12  )
    13  
    14  func TestCount(t *testing.T) {
    15  	dir, ldb, kvdb := util.CreateTestDB()
    16  	defer util.CloseTestDB(dir, ldb)
    17  	count := NewCount("prefix", "name#hello", kvdb)
    18  	count.Inc()
    19  	count.Dec()
    20  	count.Inc()
    21  	i, err := count.Get()
    22  	assert.Nil(t, err)
    23  	assert.Equal(t, i, int64(1))
    24  	kvs, err := count.Save()
    25  	assert.Nil(t, err)
    26  	util.SaveKVList(ldb, kvs)
    27  
    28  	count = NewCount("prefix", "name#hello", kvdb)
    29  	i, err = count.Get()
    30  	assert.Nil(t, err)
    31  	assert.Equal(t, i, int64(1))
    32  
    33  	count.Set(2)
    34  	i, err = count.Get()
    35  	assert.Nil(t, err)
    36  	assert.Equal(t, i, int64(2))
    37  }