statecouchdb_test.go 3.54 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/*
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 statecouchdb

import (
	"os"
	"testing"

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/hyperledger/fabric/core/ledger/ledgerconfig"
28
	ledgertestutil "github.com/hyperledger/fabric/core/ledger/testutil"
29
30
31
32
33
34
	"github.com/spf13/viper"
)

func TestMain(m *testing.M) {

	//call a helper method to load the core.yaml, will be used to detect if CouchDB is enabled
35
	ledgertestutil.SetupCoreYAMLConfig("./../../../../../../peer")
36
37

	viper.Set("ledger.state.couchDBConfig.couchDBAddress", "127.0.0.1:5984")
38
	viper.Set("peer.fileSystemPath", "/tmp/fabric/ledgertests/kvledger/txmgmt/statedb/statecouchdb")
39
40
41
42
43
44
45
46

	os.Exit(m.Run())
}

func TestBasicRW(t *testing.T) {
	if ledgerconfig.IsCouchDBEnabled() == true {

		env := NewTestVDBEnv(t)
47
48
		env.Cleanup("TestDB")
		defer env.Cleanup("TestDB")
49
50
51
52
53
54
55
56
57
		commontests.TestBasicRW(t, env.DBProvider)

	}
}

func TestMultiDBBasicRW(t *testing.T) {
	if ledgerconfig.IsCouchDBEnabled() == true {

		env := NewTestVDBEnv(t)
58
59
60
61
		env.Cleanup("TestDB1")
		env.Cleanup("TestDB2")
		defer env.Cleanup("TestDB1")
		defer env.Cleanup("TestDB2")
62
63
64
65
66
67
68
69
		commontests.TestMultiDBBasicRW(t, env.DBProvider)

	}
}

/* TODO add delete support in couchdb and then convert key value of nil to a couch delete. This will resolve TestDeletes
func TestDeletes(t *testing.T) {
	env := NewTestVDBEnv(t)
70
71
	env.Cleanup("TestDB")
	defer env.Cleanup("TestDB")
72
73
74
75
76
77
78
79
	commontests.TestDeletes(t, env.DBProvider)
}
*/

func TestIterator(t *testing.T) {
	if ledgerconfig.IsCouchDBEnabled() == true {

		env := NewTestVDBEnv(t)
80
81
		env.Cleanup("TestDB")
		defer env.Cleanup("TestDB")
82
83
84
85
86
87
		commontests.TestIterator(t, env.DBProvider)

	}
}

func TestEncodeDecodeValueAndVersion(t *testing.T) {
88
89
	testValueAndVersionEncoding(t, []byte("value1"), version.NewHeight(1, 2))
	testValueAndVersionEncoding(t, []byte{}, version.NewHeight(50, 50))
90
91
}

92
93
94
func testValueAndVersionEncoding(t *testing.T, value []byte, version *version.Height) {
	encodedValue := statedb.EncodeValue(value, version)
	val, ver := statedb.DecodeValue(encodedValue)
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
	testutil.AssertEquals(t, val, value)
	testutil.AssertEquals(t, ver, version)
}

func TestCompositeKey(t *testing.T) {
	if ledgerconfig.IsCouchDBEnabled() == true {

		testCompositeKey(t, "ns", "key")
		testCompositeKey(t, "ns", "")

	}
}

func testCompositeKey(t *testing.T, ns string, key string) {
	compositeKey := constructCompositeKey(ns, key)
	t.Logf("compositeKey=%#v", compositeKey)
	ns1, key1 := splitCompositeKey(compositeKey)
	testutil.AssertEquals(t, ns1, ns)
	testutil.AssertEquals(t, key1, key)
}

// The following tests are unique to couchdb, they are not used in leveldb
//  query test
func TestQuery(t *testing.T) {
	if ledgerconfig.IsCouchDBEnabled() == true {

		env := NewTestVDBEnv(t)
122
123
		env.Cleanup("TestDB")
		defer env.Cleanup("TestDB")
124
125
126
127
		commontests.TestQuery(t, env.DBProvider)

	}
}