rwset_test.go 3.36 KB
Newer Older
manish's avatar
manish committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/*
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.
*/

17
package rwset
manish's avatar
manish committed
18
19

import (
20
	"fmt"
manish's avatar
manish committed
21
22
	"testing"

23
	"github.com/golang/protobuf/proto"
24
	"github.com/hyperledger/fabric/common/ledger/testutil"
25
	"github.com/hyperledger/fabric/core/ledger/kvledger/txmgmt/version"
manish's avatar
manish committed
26
27
)

28
29
30
31
func TestNilTxRWSet(t *testing.T) {
	txRW := &TxReadWriteSet{}
	nsRW1 := &NsReadWriteSet{"ns1",
		[]*KVRead{&KVRead{"key1", nil}},
32
33
		[]*KVWrite{&KVWrite{"key1", false, []byte("value1")}},
		nil}
34
35
36
37
38
39
40
41
42
43
44
	txRW.NsRWs = append(txRW.NsRWs, nsRW1)
	b, err := txRW.Marshal()
	testutil.AssertNoError(t, err, "Error while marshalling changeset")

	deserializedRWSet := &TxReadWriteSet{}
	err = deserializedRWSet.Unmarshal(b)
	testutil.AssertNoError(t, err, "Error while unmarshalling changeset")
	t.Logf("Unmarshalled changeset = %#+v", deserializedRWSet.NsRWs[0].Writes[0].IsDelete)
	testutil.AssertEquals(t, deserializedRWSet, txRW)
}

manish's avatar
manish committed
45
46
47
func TestTxRWSetMarshalUnmarshal(t *testing.T) {
	txRW := &TxReadWriteSet{}
	nsRW1 := &NsReadWriteSet{"ns1",
48
		[]*KVRead{&KVRead{"key1", version.NewHeight(1, 1)}},
49
50
		[]*KVWrite{&KVWrite{"key2", false, []byte("value2")}},
		nil}
manish's avatar
manish committed
51
52

	nsRW2 := &NsReadWriteSet{"ns2",
53
		[]*KVRead{&KVRead{"key3", version.NewHeight(1, 2)}},
54
55
		[]*KVWrite{&KVWrite{"key4", true, nil}},
		nil}
manish's avatar
manish committed
56
57

	nsRW3 := &NsReadWriteSet{"ns3",
58
		[]*KVRead{&KVRead{"key5", version.NewHeight(1, 3)}},
59
60
		[]*KVWrite{&KVWrite{"key6", false, []byte("value6")}, &KVWrite{"key7", false, []byte("value7")}},
		nil}
manish's avatar
manish committed
61

62
63
64
65
	nsRW4 := &NsReadWriteSet{"ns4",
		[]*KVRead{&KVRead{"key8", version.NewHeight(1, 3)}},
		[]*KVWrite{&KVWrite{"key9", false, []byte("value9")}, &KVWrite{"key10", false, []byte("value10")}},
		[]*RangeQueryInfo{&RangeQueryInfo{"startKey1", "endKey1", true, nil, testutil.ConstructRandomBytes(t, 10)}}}
manish's avatar
manish committed
66

67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
	buf := proto.NewBuffer(nil)
	rqInfo := &RangeQueryInfo{"startKey2", "endKey2", false, []*KVRead{&KVRead{"key11", version.NewHeight(1, 3)}}, nil}
	rqInfo.Marshal(buf)

	rqInfo1 := &RangeQueryInfo{}
	rqInfo1.Unmarshal(buf)
	fmt.Printf("rqInfo=%#v\n", rqInfo)
	fmt.Printf("rqInfo1=%#v\n", rqInfo1)

	nsRW5 := &NsReadWriteSet{"ns5",
		nil,
		nil,
		[]*RangeQueryInfo{&RangeQueryInfo{"startKey2", "endKey2", false, []*KVRead{&KVRead{"key11", version.NewHeight(1, 3)}}, nil}}}

	nsRW6 := &NsReadWriteSet{"ns6",
		nil,
		nil,
		[]*RangeQueryInfo{
			&RangeQueryInfo{"startKey2", "endKey2", false, []*KVRead{&KVRead{"key11", version.NewHeight(1, 3)}}, nil},
			&RangeQueryInfo{"startKey3", "endKey3", true, []*KVRead{&KVRead{"key12", version.NewHeight(2, 4)}}, nil}}}

	txRW.NsRWs = append(txRW.NsRWs, nsRW1, nsRW2, nsRW3, nsRW4, nsRW5, nsRW6)
	t.Logf("Testing txRWSet = %s", txRW)
manish's avatar
manish committed
90
91
92
93
94
95
96
97
	b, err := txRW.Marshal()
	testutil.AssertNoError(t, err, "Error while marshalling changeset")

	deserializedRWSet := &TxReadWriteSet{}
	err = deserializedRWSet.Unmarshal(b)
	testutil.AssertNoError(t, err, "Error while unmarshalling changeset")
	testutil.AssertEquals(t, deserializedRWSet, txRW)
}