Skip to content

Commit

Permalink
✅ Create createAssetType test
Browse files Browse the repository at this point in the history
  • Loading branch information
andremacedopv committed Jan 27, 2023
1 parent 3416542 commit b379368
Show file tree
Hide file tree
Showing 2 changed files with 306 additions and 0 deletions.
33 changes: 33 additions & 0 deletions test/chaincode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ var testTxList = []tx.Transaction{
tx.CreateAsset,
tx.UpdateAsset,
tx.DeleteAsset,
tx.CreateAssetType,
tx.UpdateAssetType,
tx.DeleteAssetType,
}

var testAssetList = []assets.AssetType{
Expand Down Expand Up @@ -170,6 +173,36 @@ var testAssetList = []assets.AssetType{
},
},
},
{
Tag: "assetTypeListData",
Label: "AssetTypeListData",
Description: "AssetTypeListData",

Props: []assets.AssetProp{
{
Required: true,
IsKey: true,
Tag: "id",
Label: "ID",
DataType: "string",
Writers: []string{`org1MSP`},
},
{
Required: true,
Tag: "list",
Label: "List",
DataType: "[]@object",
Writers: []string{`org1MSP`},
},
{
Required: true,
Tag: "lastUpdated",
Label: "Last Updated",
DataType: "datetime",
Writers: []string{`org1MSP`},
},
},
},
}

var testCustomDataTypes = map[string]assets.DataType{
Expand Down
273 changes: 273 additions & 0 deletions test/tx_createAssetType_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,273 @@
package test

import (
"encoding/json"
"log"
"reflect"
"testing"
"time"

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

// TODO: Lista de assets com referencia

func TestCreateAssetType(t *testing.T) {
stub := mock.NewMockStub("org1MSP", new(testCC))
newType := map[string]interface{}{
"tag": "magazine",
"label": "Magazine",
"description": "Magazine definition",
"props": []map[string]interface{}{
{
"tag": "name",
"label": "Name",
"dataType": "string",
"required": true,
"writers": []string{"org1MSP"},
"isKey": true,
},
{
"tag": "images",
"label": "Images",
"dataType": "[]string",
},
},
}
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,
})
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()
}

// Create Asset
asset := map[string]interface{}{
"@assetType": "magazine",
"name": "MAG",
"images": []string{"url.com/1", "url.com/2"},
}
req = map[string]interface{}{
"asset": []map[string]interface{}{asset},
}
reqBytes, err = json.Marshal(req)
if err != nil {
t.FailNow()
}

res = stub.MockInvoke("createAsset", [][]byte{
[]byte("createAsset"),
reqBytes,
})
lastUpdated, _ := stub.GetTxTimestamp()
expectedResponse = map[string]interface{}{
"@key": "magazine:236a29db-f53c-59e1-ac6d-a4f264dbc477",
"@lastTouchBy": "org1MSP",
"@lastTx": "createAsset",
"@lastUpdated": lastUpdated.AsTime().Format(time.RFC3339),
"@assetType": "magazine",
"name": "MAG",
"images": []interface{}{
"url.com/1",
"url.com/2",
},
}

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

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

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

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

var state map[string]interface{}
stateBytes := stub.State["magazine:236a29db-f53c-59e1-ac6d-a4f264dbc477"]
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 TestCreateAssetTypeEmptyList(t *testing.T) {
// stub := mock.NewMockStub("org1MSP", new(testCC))

// req := map[string]interface{}{
// "asset": []map[string]interface{}{},
// }
// reqBytes, err := json.Marshal(req)
// if err != nil {
// t.FailNow()
// }

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

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

// if res.GetMessage() != "unable to get args: required argument 'asset' must be non-empty" {
// log.Printf("error message different from expected: %s", res.GetMessage())
// t.FailNow()
// }
// }

// func TestCreatePrivateType(t *testing.T) {
// stub := mock.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",
// }
// 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,
// })
// lastUpdated, _ := stub.GetTxTimestamp()
// expectedState := map[string]interface{}{
// "@key": "secret:73a3f9a7-eb91-5f4d-b1bb-c0487e90f40b",
// "@assetType": "secret",
// "@lastTouchBy": "org2MSP",
// "@lastTx": "createAsset",
// "@lastUpdated": lastUpdated.AsTime().Format(time.RFC3339),
// "secretName": "testSecret",
// "secret": "this is very secret",
// }

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

0 comments on commit b379368

Please sign in to comment.