github.com/tenywen/fabric@v1.0.0-beta.0.20170620030522-a5b1ed380643/core/ledger/kvledger/txmgmt/statedb/statedb_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 statedb
    18  
    19  import (
    20  	"sort"
    21  	"testing"
    22  
    23  	"github.com/hyperledger/fabric/common/ledger/testutil"
    24  	"github.com/hyperledger/fabric/core/ledger/kvledger/txmgmt/version"
    25  )
    26  
    27  func TestPanic(t *testing.T) {
    28  	defer func() {
    29  		if r := recover(); r == nil {
    30  			t.Fatalf("Nil value to Put() did not panic\n")
    31  		}
    32  	}()
    33  
    34  	batch := NewUpdateBatch()
    35  	// The following call to Put() should result in panic
    36  	batch.Put("ns1", "key1", nil, nil)
    37  }
    38  
    39  //Test Put(), Get(), and Delete()
    40  func TestPutGetDeleteExistsGetUpdates(t *testing.T) {
    41  	batch := NewUpdateBatch()
    42  	batch.Put("ns1", "key1", []byte("value1"), version.NewHeight(1, 1))
    43  
    44  	//Get() should return above inserted <k,v> pair
    45  	actualVersionedValue := batch.Get("ns1", "key1")
    46  	testutil.AssertEquals(t, actualVersionedValue, &VersionedValue{Value: []byte("value1"), Version: version.NewHeight(1, 1)})
    47  	//Exists() should return false as key2 does not exist
    48  	actualResult := batch.Exists("ns1", "key2")
    49  	expectedResult := false
    50  	testutil.AssertEquals(t, actualResult, expectedResult)
    51  
    52  	//Exists() should return false as ns3 does not exist
    53  	actualResult = batch.Exists("ns3", "key2")
    54  	expectedResult = false
    55  	testutil.AssertEquals(t, actualResult, expectedResult)
    56  
    57  	//Get() should return nill as key2 does not exist
    58  	actualVersionedValue = batch.Get("ns1", "key2")
    59  	testutil.AssertNil(t, actualVersionedValue)
    60  	//Get() should return nill as ns3 does not exist
    61  	actualVersionedValue = batch.Get("ns3", "key2")
    62  	testutil.AssertNil(t, actualVersionedValue)
    63  
    64  	batch.Put("ns1", "key2", []byte("value2"), version.NewHeight(1, 2))
    65  	//Exists() should return true as key2 exists
    66  	actualResult = batch.Exists("ns1", "key2")
    67  	expectedResult = true
    68  	testutil.AssertEquals(t, actualResult, expectedResult)
    69  
    70  	//GetUpdatedNamespaces should return 3 namespaces
    71  	batch.Put("ns2", "key2", []byte("value2"), version.NewHeight(1, 2))
    72  	batch.Put("ns3", "key2", []byte("value2"), version.NewHeight(1, 2))
    73  	actualNamespaces := batch.GetUpdatedNamespaces()
    74  	sort.Strings(actualNamespaces)
    75  	expectedNamespaces := []string{"ns1", "ns2", "ns3"}
    76  	testutil.AssertEquals(t, actualNamespaces, expectedNamespaces)
    77  
    78  	//GetUpdates should return two VersionedValues for the namespace ns1
    79  	expectedUpdates := make(map[string]*VersionedValue)
    80  	expectedUpdates["key1"] = &VersionedValue{Value: []byte("value1"), Version: version.NewHeight(1, 1)}
    81  	expectedUpdates["key2"] = &VersionedValue{Value: []byte("value2"), Version: version.NewHeight(1, 2)}
    82  	actualUpdates := batch.GetUpdates("ns1")
    83  	testutil.AssertEquals(t, actualUpdates, expectedUpdates)
    84  
    85  	actualUpdates = batch.GetUpdates("ns4")
    86  	testutil.AssertNil(t, actualUpdates)
    87  
    88  	//Delete the above inserted <k,v> pair
    89  	batch.Delete("ns1", "key2", version.NewHeight(1, 2))
    90  	//Exists() should return false as key2 is deleted
    91  	actualResult = batch.Exists("ns1", "key2")
    92  	expectedResult = true
    93  	testutil.AssertEquals(t, actualResult, expectedResult)
    94  
    95  }
    96  
    97  func TestUpdateBatchIterator(t *testing.T) {
    98  	batch := NewUpdateBatch()
    99  	batch.Put("ns1", "key1", []byte("value1"), version.NewHeight(1, 1))
   100  	batch.Put("ns1", "key2", []byte("value2"), version.NewHeight(1, 2))
   101  	batch.Put("ns1", "key3", []byte("value3"), version.NewHeight(1, 3))
   102  
   103  	batch.Put("ns2", "key6", []byte("value6"), version.NewHeight(2, 3))
   104  	batch.Put("ns2", "key5", []byte("value5"), version.NewHeight(2, 2))
   105  	batch.Put("ns2", "key4", []byte("value4"), version.NewHeight(2, 1))
   106  
   107  	checkItrResults(t, batch.GetRangeScanIterator("ns1", "key2", "key3"), []*VersionedKV{
   108  		&VersionedKV{CompositeKey{"ns1", "key2"}, VersionedValue{[]byte("value2"), version.NewHeight(1, 2)}},
   109  	})
   110  
   111  	checkItrResults(t, batch.GetRangeScanIterator("ns2", "key0", "key8"), []*VersionedKV{
   112  		&VersionedKV{CompositeKey{"ns2", "key4"}, VersionedValue{[]byte("value4"), version.NewHeight(2, 1)}},
   113  		&VersionedKV{CompositeKey{"ns2", "key5"}, VersionedValue{[]byte("value5"), version.NewHeight(2, 2)}},
   114  		&VersionedKV{CompositeKey{"ns2", "key6"}, VersionedValue{[]byte("value6"), version.NewHeight(2, 3)}},
   115  	})
   116  
   117  	checkItrResults(t, batch.GetRangeScanIterator("ns2", "", ""), []*VersionedKV{
   118  		&VersionedKV{CompositeKey{"ns2", "key4"}, VersionedValue{[]byte("value4"), version.NewHeight(2, 1)}},
   119  		&VersionedKV{CompositeKey{"ns2", "key5"}, VersionedValue{[]byte("value5"), version.NewHeight(2, 2)}},
   120  		&VersionedKV{CompositeKey{"ns2", "key6"}, VersionedValue{[]byte("value6"), version.NewHeight(2, 3)}},
   121  	})
   122  
   123  	checkItrResults(t, batch.GetRangeScanIterator("non-existing-ns", "", ""), nil)
   124  }
   125  
   126  func checkItrResults(t *testing.T, itr ResultsIterator, expectedResults []*VersionedKV) {
   127  	for i := 0; i < len(expectedResults); i++ {
   128  		res, _ := itr.Next()
   129  		testutil.AssertEquals(t, res, expectedResults[i])
   130  	}
   131  	lastRes, err := itr.Next()
   132  	testutil.AssertNoError(t, err, "")
   133  	testutil.AssertNil(t, lastRes)
   134  	itr.Close()
   135  }