Skip to content

Commit 695715e

Browse files
committed
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 <[email protected]>
1 parent 7559dd9 commit 695715e

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

core/chaincode/shim/mockstub_test.go

+30
Original file line numberDiff line numberDiff line change
@@ -143,3 +143,33 @@ func TestGetStateByPartialCompositeKey(t *testing.T) {
143143
}
144144
}
145145
}
146+
147+
func TestGetStateByPartialCompositeKeyCollision(t *testing.T) {
148+
stub := NewMockStub("GetStateByPartialCompositeKeyCollisionTest", nil)
149+
stub.MockTransactionStart("init")
150+
151+
vehicle1Bytes := []byte("vehicle1")
152+
compositeKeyVehicle1, _ := stub.CreateCompositeKey("Vehicle", []string{"VIN_1234"})
153+
stub.PutState(compositeKeyVehicle1, vehicle1Bytes)
154+
155+
vehicleListing1Bytes := []byte("vehicleListing1")
156+
compositeKeyVehicleListing1, _ := stub.CreateCompositeKey("VehicleListing", []string{"LIST_1234"})
157+
stub.PutState(compositeKeyVehicleListing1, vehicleListing1Bytes)
158+
159+
stub.MockTransactionEnd("init")
160+
161+
// Only the single "Vehicle" object should be returned, not the "VehicleListing" object
162+
rqi, _ := stub.GetStateByPartialCompositeKey("Vehicle", []string{})
163+
i := 0
164+
fmt.Println("Running loop")
165+
for rqi.HasNext() {
166+
i++
167+
key, value, err := rqi.Next()
168+
fmt.Println("Loop", i, "got", key, value, err)
169+
}
170+
// Only the single "Vehicle" object should be returned, not the "VehicleListing" object
171+
if i != 1 {
172+
fmt.Println("Expected 1, got", i)
173+
t.FailNow()
174+
}
175+
}

0 commit comments

Comments
 (0)