Skip to content

Commit

Permalink
✅ Create more tests and correct leftovers from existing ones
Browse files Browse the repository at this point in the history
  • Loading branch information
andremacedopv committed Jan 30, 2023
1 parent 6cdaa3b commit b09b3a6
Show file tree
Hide file tree
Showing 9 changed files with 257 additions and 83 deletions.
4 changes: 2 additions & 2 deletions assets/dynamicAssetTypeFuncs.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,12 +167,12 @@ func CheckDataType(dataType string) errors.ICCError {

assetType := FetchAssetType(trimDataType)
if assetType == nil {
return errors.NewCCError(fmt.Sprintf("invalid dataType value %s", dataType), http.StatusBadRequest)
return errors.NewCCError(fmt.Sprintf("invalid dataType value '%s'", dataType), http.StatusBadRequest)
}
} else {
dataTypeObj := FetchDataType(trimDataType)
if dataTypeObj == nil {
return errors.NewCCError(fmt.Sprintf("invalid dataType value %s", dataType), http.StatusBadRequest)
return errors.NewCCError(fmt.Sprintf("invalid dataType value '%s'", dataType), http.StatusBadRequest)
}
}

Expand Down
111 changes: 111 additions & 0 deletions test/assets_dynamicAssetType_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package test

import (
"log"
"reflect"
"testing"

"github.com/goledgerdev/cc-tools/assets"
)

func TestBuildAssetPropValid(t *testing.T) {
propMap := map[string]interface{}{
"tag": "id",
"label": "CPF (Brazilian ID)",
"description": "",
"isKey": true,
"required": true,
"readOnly": false,
"defaultValue": nil,
"dataType": "cpf",
"writers": []interface{}{"org1MSP"},
}
prop, err := assets.BuildAssetProp(propMap)

expectedProp := assets.AssetProp{
Tag: "id",
Label: "CPF (Brazilian ID)",
Description: "",
IsKey: true,
Required: true,
ReadOnly: false,
DefaultValue: nil,
DataType: "cpf",
Writers: []string{"org1MSP"},
}

if err != nil {
log.Println("an error should not have occurred")
log.Println(err)
t.FailNow()
}

if !reflect.DeepEqual(prop, expectedProp) {
log.Println("these should be deeply equal")
log.Println(prop)
log.Println(expectedProp)
t.FailNow()
}
}

func TestBuildAssetPropInvalid(t *testing.T) {
propMap := map[string]interface{}{
"tag": "id",
"label": "CPF (Brazilian ID)",
"description": "",
"isKey": true,
"required": true,
"readOnly": false,
"defaultValue": nil,
"dataType": "inexistant",
"writers": []interface{}{"org1MSP"},
}
_, err := assets.BuildAssetProp(propMap)

err.Status()
if err.Status() != 400 {
log.Println(err)
t.FailNow()
}

if err.Message() != "failed checking data type: invalid dataType value 'inexistant'" {
log.Printf("error message different from expected: %s", err.Message())
t.FailNow()
}
}

func TestHandlePropUpdate(t *testing.T) {
prop := assets.AssetProp{
Tag: "id",
Label: "CPF (Brazilian ID)",
Description: "",
IsKey: true,
Required: true,
ReadOnly: false,
DefaultValue: nil,
DataType: "cpf",
}

propUpdateMap := map[string]interface{}{
"writers": []interface{}{"org1MSP"},
"defaultValue": "12345678901",
}

updatedProp, err := assets.HandlePropUpdate(prop, propUpdateMap)

if err != nil {
log.Println("an error should not have occurred")
log.Println(err)
t.FailNow()
}

prop.DefaultValue = "12345678901"
prop.Writers = []string{"org1MSP"}

if !reflect.DeepEqual(updatedProp, prop) {
log.Println("these should be deeply equal")
log.Println(updatedProp)
log.Println(prop)
t.FailNow()
}
}
1 change: 1 addition & 0 deletions test/chaincode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ var testTxList = []tx.Transaction{
tx.CreateAssetType,
tx.UpdateAssetType,
tx.DeleteAssetType,
tx.LoadAssetTypeList,
}

var testAssetList = []assets.AssetType{
Expand Down
40 changes: 39 additions & 1 deletion test/tx_createAssetType_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
)

// TODO: Lista de assets com referencia
// TODO: Verify is there is at least one isKey

func TestCreateAssetType(t *testing.T) {
stub := mock.NewMockStub("org1MSP", new(testCC))
Expand Down Expand Up @@ -245,3 +244,42 @@ func TestCreateExistingAssetType(t *testing.T) {
t.FailNow()
}
}

func TestCreateAssetTypeWithoutKey(t *testing.T) {
stub := mock.NewMockStub("org1MSP", new(testCC))
newType := map[string]interface{}{
"tag": "library",
"label": "Library",
"description": "Library definition",
"props": []map[string]interface{}{
{
"tag": "name",
"label": "Name",
"dataType": "string",
"required": true,
},
},
}
req := map[string]interface{}{
"assetTypes": []map[string]interface{}{newType},
}
reqBytes, err := json.Marshal(req)
if err != nil {
t.FailNow()
}

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

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

if res.GetMessage() != "failed to build asset type: asset type must have a key" {
log.Printf("error message different from expected: %s", res.GetMessage())
t.FailNow()
}
}
90 changes: 13 additions & 77 deletions test/tx_deleteAssetType_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,59 +45,12 @@ func TestDeleteAssetType(t *testing.T) {
[]byte("createAssetType"),
reqBytes,
})
expectedResponse := map[string]interface{}{
"description": "Magazine definition",
"dynamic": true,
"label": "Magazine",
"props": []interface{}{
map[string]interface{}{
"dataType": "string",
"description": "",
"isKey": true,
"label": "Name",
"readOnly": false,
"required": true,
"tag": "name",
"writers": []interface{}{"org1MSP"},
},
map[string]interface{}{
"dataType": "[]string",
"description": "",
"isKey": false,
"label": "Images",
"readOnly": false,
"required": false,
"tag": "images",
"writers": nil,
},
},
"tag": "magazine",
}

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()
}

// Delete Type
deleteReq := map[string]interface{}{
"tag": "magazine",
Expand All @@ -115,7 +68,7 @@ func TestDeleteAssetType(t *testing.T) {
[]byte("deleteAssetType"),
reqBytes,
})
expectedResponse = map[string]interface{}{
expectedResponse := map[string]interface{}{
"assetType": map[string]interface{}{
"description": "Magazine definition",
"dynamic": true,
Expand Down Expand Up @@ -182,8 +135,8 @@ func TestDeleteAssetTypeEmptyList(t *testing.T) {
t.FailNow()
}

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

Expand All @@ -198,49 +151,32 @@ func TestDeleteAssetTypeEmptyList(t *testing.T) {
}
}

func TestDeleteExistingAssetType(t *testing.T) {
func TestDeleteNonExistingAssetType(t *testing.T) {
stub := mock.NewMockStub("org1MSP", new(testCC))
newType := map[string]interface{}{
"tag": "library",
"label": "Library",
"description": "Library definition",
"props": []map[string]interface{}{
{
"tag": "name",
"label": "Name",
"dataType": "string",
"required": true,
"isKey": true,
},
},
deleteTag := map[string]interface{}{
"tag": "inexistent",
"force": true,
}
req := map[string]interface{}{
"assetTypes": []map[string]interface{}{newType},
"assetTypes": []map[string]interface{}{deleteTag},
}
reqBytes, err := json.Marshal(req)
if err != nil {
t.FailNow()
}

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

if res.GetStatus() != 200 {
if res.GetStatus() != 400 {
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) != 0 {
log.Println("response length should be 0")
if res.GetMessage() != "asset type 'inexistent' not found" {
log.Printf("error message different from expected: %s", res.GetMessage())
t.FailNow()
}
}
5 changes: 5 additions & 0 deletions test/tx_getTx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ func TestGetTx(t *testing.T) {
"label": "Delete Asset Type",
"tag": "deleteAssetType",
},
map[string]interface{}{
"description": "",
"label": "Load Asset Type List from blockchain",
"tag": "loadAssetTypeList",
},
map[string]interface{}{
"description": "",
"label": "Get Tx",
Expand Down
Loading

0 comments on commit b09b3a6

Please sign in to comment.