-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbank_test.go
110 lines (89 loc) · 3.27 KB
/
bank_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package main
import (
"encoding/json"
"fmt"
"testing"
"time"
"github.com/abbeydabiri/hdlchaincode/models"
"github.com/abbeydabiri/hdlchaincode/utils"
"github.com/google/uuid"
"github.com/hyperledger/fabric-chaincode-go/shim"
"github.com/hyperledger/fabric-chaincode-go/shimtest"
"github.com/stretchr/testify/assert"
)
func TestBankWR(t *testing.T) {
fmt.Println("Entering TestBankLogic")
assert := assert.New(t)
// Instantiate mockStub using HDLChaincode as the target chaincode to unit test
stub := shimtest.NewMockStub("TestStub", new(HDLChaincode))
//Verify stub is available
assert.NotNil(stub, "Stub is nil, Test stub creation failed")
uid := uuid.New().String()
writeResp := stub.MockInvoke(uid,
[][]byte{[]byte(utils.BankW),
[]byte("2"),
[]byte("Test Bank 2"),
[]byte("Test Hq 2"),
[]byte("Test BankCategory 2"),
[]byte("2"),
[]byte("Test Location 2"),
[]byte("Test LocationLat 2"),
[]byte("Test LocationLong 2"),
})
assert.EqualValues(shim.OK, writeResp.GetStatus(), "logic.BankW write test Bank to state failed.")
testID := "2"
readResp := stub.MockInvoke(uid,
[][]byte{[]byte(utils.BankR),
[]byte(testID),
})
assert.EqualValues(shim.OK, readResp.GetStatus(), "logic.BankR read test Bank from state failed.")
var ccResp struct {
Code string `json:"code"`
Message string `json:"message"`
Payload models.Bank `json:"payload"`
}
if err := json.Unmarshal(readResp.GetPayload(), &ccResp); err != nil {
panic(err)
}
assert.Equal(testID, ccResp.Payload.BankID, "Retrieved Bank ID mismatch")
assert.Equal(utils.BANKOO, ccResp.Payload.ObjectType, "Retrieved Object Type mismatch")
}
func TestBankModel(t *testing.T) {
fmt.Println("Entering TestBankModel")
assert := assert.New(t)
// Instantiate mockStub using HDLChaincode as the target chaincode to unit test
stub := shimtest.NewMockStub("TestStub", new(HDLChaincode))
//Verify stub is available
assert.NotNil(stub, "Stub is nil, Test stub creation failed")
epochTime, _ := stub.GetTxTimestamp()
created := time.Unix(epochTime.GetSeconds(), 0)
putBankBase := models.BankBase{
BankID: 1, //args[0],
BankName: "Test Bank",
HqAddress: "Test HqAddress",
BankCategory: "Test BankCategory",
BankAdminUserID: 1, //args[4],
Location: "Test Location",
LocationLat: "Test LocationLat",
LocationLong: "Test LocationLong",
Created: created,
Createdby: "Test Models Caller",
}
putBank := &models.Bank{ObjectType: utils.BANKOO, BankID: "1", Data: putBankBase}
stub.MockTransactionStart("2")
putResp := putBank.PutState(stub)
assert.EqualValues(shim.OK, putResp.GetStatus(), "models.Bank.PutState test Bank to chain state failed.")
var ccResp struct {
Code string `json:"code"`
Message string `json:"message"`
Payload models.Bank `json:"payload"`
}
getBank := &models.Bank{BankID: "1"}
getResp := getBank.GetState(stub)
assert.EqualValues(shim.OK, getResp.GetStatus(), "models.Bank.GetState test Bank from chain state failed ")
if err := json.Unmarshal(getResp.GetPayload(), &ccResp); err != nil {
panic(err)
}
assert.Equal(putBank.BankID, ccResp.Payload.BankID, "Retrieved Bank ID mismatch")
assert.Equal(putBank.ObjectType, ccResp.Payload.ObjectType, "Retrieved Object Type mismatch")
}