Skip to content

Commit

Permalink
Improve transactions package tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bandreghetti committed Jul 13, 2021
1 parent 1e40dd6 commit 6235356
Show file tree
Hide file tree
Showing 9 changed files with 436 additions and 48 deletions.
2 changes: 1 addition & 1 deletion assets/dataType.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type DataType struct {
Description string `json:"description,omitempty"`

// DropDownValues is a set of predetermined values to be used in a dropdown menu on frontend rendering
DropDownValues map[string]interface{}
DropDownValues map[string]interface{} `json:"DropDownValues"`

// Parse is called to check if the input value is valid, make necessary
// conversions and returns a string representation of the value
Expand Down
36 changes: 16 additions & 20 deletions assets/existsInLedger.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,21 @@ package assets
import (
"github.com/goledgerdev/cc-tools/errors"
sw "github.com/goledgerdev/cc-tools/stubwrapper"
"github.com/hyperledger/fabric/core/chaincode/shim"
)

// ExistsInLedger checks if asset currently has a state on the ledger.
func (a *Asset) ExistsInLedger(stub *sw.StubWrapper) (bool, errors.ICCError) {
func existsInLedger(stub *sw.StubWrapper, isPrivate bool, typeTag, key string) (bool, errors.ICCError) {
var assetBytes []byte
var err error
if a.IsPrivate() {
assetBytes, err = stub.GetPrivateDataHash(a.TypeTag(), a.Key())
if isPrivate {
_, isMock := stub.Stub.(*shim.MockStub)
if isMock {
assetBytes, err = stub.GetPrivateData(typeTag, key)
} else {
assetBytes, err = stub.GetPrivateDataHash(typeTag, key)
}
} else {
assetBytes, err = stub.GetState(a.Key())
assetBytes, err = stub.GetState(key)
}
if err != nil {
return false, errors.WrapErrorWithStatus(err, "unable to check asset existence", 400)
Expand All @@ -24,21 +29,12 @@ func (a *Asset) ExistsInLedger(stub *sw.StubWrapper) (bool, errors.ICCError) {
return false, nil
}

// ExistsInLedger checks if asset currently has a state on the ledger.
func (a *Asset) ExistsInLedger(stub *sw.StubWrapper) (bool, errors.ICCError) {
return existsInLedger(stub, a.IsPrivate(), a.TypeTag(), a.Key())
}

// ExistsInLedger checks if asset referenced by a key object currently has a state on the ledger.
func (k *Key) ExistsInLedger(stub *sw.StubWrapper) (bool, errors.ICCError) {
var assetBytes []byte
var err error
if k.IsPrivate() {
assetBytes, err = stub.GetPrivateDataHash(k.TypeTag(), k.Key())
} else {
assetBytes, err = stub.GetState(k.Key())
}
if err != nil {
return false, errors.WrapErrorWithStatus(err, "unable to check asset existence", 400)
}
if assetBytes != nil {
return true, nil
}

return false, nil
return existsInLedger(stub, k.IsPrivate(), k.TypeTag(), k.Key())
}
89 changes: 88 additions & 1 deletion transactions/createAsset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func TestCreateAsset(t *testing.T) {
var resPayload []map[string]interface{}
err = json.Unmarshal(res.GetPayload(), &resPayload)
if err != nil {
log.Println(resPayload)
log.Println(err)
t.FailNow()
}

Expand All @@ -61,6 +61,21 @@ func TestCreateAsset(t *testing.T) {
log.Printf("%#v\n", expectedResponse)
t.FailNow()
}

var state map[string]interface{}
stateBytes := stub.State["person:47061146-c642-51a1-844a-bf0b17cb5e19"]
err = json.Unmarshal(stateBytes, &state)
if err != nil {
log.Println(err)
t.FailNow()
}

if !reflect.DeepEqual(state, expectedResponse) {
log.Println("these should be equal")
log.Printf("%#v\n", state)
log.Printf("%#v\n", expectedResponse)
t.FailNow()
}
}

func TestCreateAssetEmptyList(t *testing.T) {
Expand Down Expand Up @@ -89,3 +104,75 @@ func TestCreateAssetEmptyList(t *testing.T) {
t.FailNow()
}
}

func TestCreatePrivate(t *testing.T) {
stub := shim.NewMockStub("org2MSP", new(testCC))
secret := map[string]interface{}{
"@assetType": "secret",
"secretName": "testSecret",
"secret": "this is very secret",
}
expectedResponse := map[string]interface{}{
"@key": "secret:73a3f9a7-eb91-5f4d-b1bb-c0487e90f40b",
"@assetType": "secret",
}
expectedState := map[string]interface{}{
"@key": "secret:73a3f9a7-eb91-5f4d-b1bb-c0487e90f40b",
"@assetType": "secret",
"@lastTouchBy": "org2MSP",
"@lastTx": "createAsset",
"secretName": "testSecret",
"secret": "this is very secret",
}
req := map[string]interface{}{
"asset": []map[string]interface{}{secret},
}
reqBytes, err := json.Marshal(req)
if err != nil {
t.FailNow()
}

res := stub.MockInvoke("createAsset", [][]byte{
[]byte("createAsset"),
reqBytes,
})

if res.GetStatus() != 200 {
log.Println(res)
t.FailNow()
}

var resPayload []map[string]interface{}
err = json.Unmarshal(res.GetPayload(), &resPayload)
if err != nil {
log.Println(err)
t.FailNow()
}

if len(resPayload) != 1 {
log.Println("response length should be 1")
t.FailNow()
}

if !reflect.DeepEqual(resPayload[0], expectedResponse) {
log.Println("these should be equal")
log.Printf("%#v\n", resPayload[0])
log.Printf("%#v\n", expectedResponse)
t.FailNow()
}

var state map[string]interface{}
stateBytes := stub.PvtState["secret"]["secret:73a3f9a7-eb91-5f4d-b1bb-c0487e90f40b"]
err = json.Unmarshal(stateBytes, &state)
if err != nil {
log.Println(err)
t.FailNow()
}

if !reflect.DeepEqual(state, expectedState) {
log.Println("these should be equal")
log.Printf("%#v\n", state)
log.Printf("%#v\n", expectedState)
t.FailNow()
}
}
22 changes: 0 additions & 22 deletions transactions/getArgs_test.go

This file was deleted.

54 changes: 54 additions & 0 deletions transactions/getDataTypes_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package transactions

import (
"log"
"testing"

"github.com/hyperledger/fabric/core/chaincode/shim"
)

func TestGetDataTypes(t *testing.T) {
stub := shim.NewMockStub("testcc", new(testCC))

expectedResponse := map[string]interface{}{
"boolean": map[string]interface{}{
"acceptedFormats": []interface{}{
"boolean",
},
"DropDownValues": nil,
},
"cpf": map[string]interface{}{
"acceptedFormats": nil,
"DropDownValues": nil,
},
"datetime": map[string]interface{}{
"acceptedFormats": []interface{}{
"string",
},
"DropDownValues": nil,
},
"integer": map[string]interface{}{
"acceptedFormats": []interface{}{
"number",
},
"DropDownValues": nil,
},
"number": map[string]interface{}{
"acceptedFormats": []interface{}{
"number",
},
"DropDownValues": nil,
},
"string": map[string]interface{}{
"acceptedFormats": []interface{}{
"string",
},
"DropDownValues": nil,
},
}
err := invokeAndVerify(stub, "getDataTypes", nil, expectedResponse, 200)
if err != nil {
log.Println("getDataTypes fail")
t.FailNow()
}
}
30 changes: 30 additions & 0 deletions transactions/getHeader_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package transactions

import (
"log"
"testing"

"github.com/hyperledger/fabric/core/chaincode/shim"
)

func TestGetHeader(t *testing.T) {
stub := shim.NewMockStub("org1MSP", new(testCC))

expectedResponse := map[string]interface{}{
"ccToolsVersion": "v0.7.0-rc.3",
"colors": []interface{}{
"#4267B2",
"#34495E",
"#ECF0F1",
},
"name": "CC Tools Test",
"orgMSP": "org1MSP",
"orgTitle": "CC Tools Demo",
"version": "v0.7.0",
}
err := invokeAndVerify(stub, "getHeader", nil, expectedResponse, 200)
if err != nil {
log.Println("getHeader fail")
t.FailNow()
}
}
107 changes: 107 additions & 0 deletions transactions/getSchema_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package transactions

import (
"log"
"testing"

"github.com/hyperledger/fabric/core/chaincode/shim"
)

func TestGetSchema(t *testing.T) {
stub := shim.NewMockStub("org1MSP", new(testCC))

expectedResponse := []interface{}{
map[string]interface{}{
"description": "Personal data of someone",
"label": "Person",
"tag": "person",
"writers": nil,
},
map[string]interface{}{
"description": "Library as a collection of books",
"label": "Library",
"tag": "library",
"writers": nil,
},
map[string]interface{}{
"description": "Book",
"label": "Book",
"tag": "book",
"writers": nil,
},
map[string]interface{}{
"description": "Secret between Org2 and Org3",
"label": "Secret",
"readers": []interface{}{
"org2MSP",
"org3MSP",
},
"tag": "secret",
"writers": nil,
},
}
err := invokeAndVerify(stub, "getSchema", nil, expectedResponse, 200)
if err != nil {
log.Println("getSchema fail")
t.FailNow()
}

req := map[string]interface{}{
"assetType": "person",
}
expectedPersonSchema := map[string]interface{}{
"tag": "person",
"label": "Person",
"description": "Personal data of someone",
"props": []interface{}{
map[string]interface{}{
"dataType": "cpf",
"description": "",
"isKey": true,
"label": "CPF (Brazilian ID)",
"readOnly": false,
"required": true,
"tag": "id",
"writers": []interface{}{
"org1MSP",
},
},
map[string]interface{}{
"dataType": "string",
"description": "",
"isKey": false,
"label": "Name of the person",
"readOnly": false,
"required": true,
"tag": "name",
"writers": nil,
},
map[string]interface{}{
"dataType": "datetime",
"description": "",
"isKey": false,
"label": "Date of Birth",
"readOnly": false,
"required": false,
"tag": "dateOfBirth",
"writers": nil,
},
map[string]interface{}{
"dataType": "number",
"defaultValue": 0.0,
"description": "",
"isKey": false,
"label": "Person's height",
"readOnly": false,
"required": false,
"tag": "height",
"writers": nil,
},
},
}
err = invokeAndVerify(stub, "getSchema", req, expectedPersonSchema, 200)
if err != nil {
log.Println("getSchema of person fail")
t.FailNow()
}
}
Loading

0 comments on commit 6235356

Please sign in to comment.