stateleveldb_test.go 2.43 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/*
Copyright IBM Corp. 2016 All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

		 http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package stateleveldb

import (
20
	"os"
21
22
	"testing"

23
	"github.com/hyperledger/fabric/common/ledger/testutil"
24
	"github.com/hyperledger/fabric/core/ledger/kvledger/txmgmt/statedb"
25
26
	"github.com/hyperledger/fabric/core/ledger/kvledger/txmgmt/statedb/commontests"
	"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
33
	os.Exit(m.Run())
}
34
35

func TestBasicRW(t *testing.T) {
36
37
38
39
40
41
42
	env := NewTestVDBEnv(t)
	defer env.Cleanup()
	commontests.TestBasicRW(t, env.DBProvider)
}

func TestMultiDBBasicRW(t *testing.T) {
	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
60
61
62
63
64
}

func TestEncodeDecodeValueAndVersion(t *testing.T) {
	testValueAndVersionEncodeing(t, []byte("value1"), version.NewHeight(1, 2))
	testValueAndVersionEncodeing(t, []byte{}, version.NewHeight(50, 50))
}

func testValueAndVersionEncodeing(t *testing.T, value []byte, version *version.Height) {
65
66
	encodedValue := statedb.EncodeValue(value, version)
	val, ver := statedb.DecodeValue(encodedValue)
67
68
69
70
71
	testutil.AssertEquals(t, val, value)
	testutil.AssertEquals(t, ver, version)
}

func TestCompositeKey(t *testing.T) {
72
73
	testCompositeKey(t, "ledger1", "ns", "key")
	testCompositeKey(t, "ledger2", "ns", "")
74
75
}

76
func testCompositeKey(t *testing.T, dbName string, ns string, key string) {
manish's avatar
manish committed
77
	compositeKey := constructCompositeKey(ns, key)
78
	t.Logf("compositeKey=%#v", compositeKey)
manish's avatar
manish committed
79
	ns1, key1 := splitCompositeKey(compositeKey)
80
81
82
	testutil.AssertEquals(t, ns1, ns)
	testutil.AssertEquals(t, key1, key)
}