github.com/leonlxy/hyperledger@v1.0.0-alpha.0.20170427033203-34922035d248/core/ledger/kvledger/txmgmt/statedb/stateleveldb/stateleveldb_test.go (about)

     1  /*
     2  Copyright IBM Corp. 2016 All Rights Reserved.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8  		 http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package stateleveldb
    18  
    19  import (
    20  	"os"
    21  	"testing"
    22  
    23  	"github.com/hyperledger/fabric/common/ledger/testutil"
    24  	"github.com/hyperledger/fabric/core/ledger/kvledger/txmgmt/statedb"
    25  	"github.com/hyperledger/fabric/core/ledger/kvledger/txmgmt/statedb/commontests"
    26  	"github.com/hyperledger/fabric/core/ledger/kvledger/txmgmt/version"
    27  	"github.com/spf13/viper"
    28  )
    29  
    30  func TestMain(m *testing.M) {
    31  	viper.Set("peer.fileSystemPath", "/tmp/fabric/ledgertests/kvledger/txmgmt/statedb/stateleveldb")
    32  	os.Exit(m.Run())
    33  }
    34  
    35  func TestBasicRW(t *testing.T) {
    36  	env := NewTestVDBEnv(t)
    37  	defer env.Cleanup()
    38  	commontests.TestBasicRW(t, env.DBProvider)
    39  }
    40  
    41  func TestMultiDBBasicRW(t *testing.T) {
    42  	env := NewTestVDBEnv(t)
    43  	defer env.Cleanup()
    44  	commontests.TestMultiDBBasicRW(t, env.DBProvider)
    45  }
    46  
    47  func TestDeletes(t *testing.T) {
    48  	env := NewTestVDBEnv(t)
    49  	defer env.Cleanup()
    50  	commontests.TestDeletes(t, env.DBProvider)
    51  }
    52  
    53  func TestIterator(t *testing.T) {
    54  	env := NewTestVDBEnv(t)
    55  	defer env.Cleanup()
    56  	commontests.TestIterator(t, env.DBProvider)
    57  }
    58  
    59  func TestEncodeDecodeValueAndVersion(t *testing.T) {
    60  	testValueAndVersionEncodeing(t, []byte("value1"), version.NewHeight(1, 2))
    61  	testValueAndVersionEncodeing(t, []byte{}, version.NewHeight(50, 50))
    62  }
    63  
    64  func testValueAndVersionEncodeing(t *testing.T, value []byte, version *version.Height) {
    65  	encodedValue := statedb.EncodeValue(value, version)
    66  	val, ver := statedb.DecodeValue(encodedValue)
    67  	testutil.AssertEquals(t, val, value)
    68  	testutil.AssertEquals(t, ver, version)
    69  }
    70  
    71  func TestCompositeKey(t *testing.T) {
    72  	testCompositeKey(t, "ledger1", "ns", "key")
    73  	testCompositeKey(t, "ledger2", "ns", "")
    74  }
    75  
    76  func testCompositeKey(t *testing.T, dbName string, ns string, key string) {
    77  	compositeKey := constructCompositeKey(ns, key)
    78  	t.Logf("compositeKey=%#v", compositeKey)
    79  	ns1, key1 := splitCompositeKey(compositeKey)
    80  	testutil.AssertEquals(t, ns1, ns)
    81  	testutil.AssertEquals(t, key1, key)
    82  }