From 695715e15d2a4c3d30bd999eca8f68e805ca710b Mon Sep 17 00:00:00 2001 From: denyeart Date: Wed, 15 Feb 2017 21:09:03 -0500 Subject: [PATCH] Add unit test for GetStateByPartialCompositeKey Add a unit test to ensure no collisions across similarly named object types. Change-Id: I4911ce8bba1f9851db379f084c9b69ab2889934b Signed-off-by: denyeart --- core/chaincode/shim/mockstub_test.go | 30 ++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/core/chaincode/shim/mockstub_test.go b/core/chaincode/shim/mockstub_test.go index d449a90517b..b71aaee7e74 100644 --- a/core/chaincode/shim/mockstub_test.go +++ b/core/chaincode/shim/mockstub_test.go @@ -143,3 +143,33 @@ func TestGetStateByPartialCompositeKey(t *testing.T) { } } } + +func TestGetStateByPartialCompositeKeyCollision(t *testing.T) { + stub := NewMockStub("GetStateByPartialCompositeKeyCollisionTest", nil) + stub.MockTransactionStart("init") + + vehicle1Bytes := []byte("vehicle1") + compositeKeyVehicle1, _ := stub.CreateCompositeKey("Vehicle", []string{"VIN_1234"}) + stub.PutState(compositeKeyVehicle1, vehicle1Bytes) + + vehicleListing1Bytes := []byte("vehicleListing1") + compositeKeyVehicleListing1, _ := stub.CreateCompositeKey("VehicleListing", []string{"LIST_1234"}) + stub.PutState(compositeKeyVehicleListing1, vehicleListing1Bytes) + + stub.MockTransactionEnd("init") + + // Only the single "Vehicle" object should be returned, not the "VehicleListing" object + rqi, _ := stub.GetStateByPartialCompositeKey("Vehicle", []string{}) + i := 0 + fmt.Println("Running loop") + for rqi.HasNext() { + i++ + key, value, err := rqi.Next() + fmt.Println("Loop", i, "got", key, value, err) + } + // Only the single "Vehicle" object should be returned, not the "VehicleListing" object + if i != 1 { + fmt.Println("Expected 1, got", i) + t.FailNow() + } +}