chaincode_example03_test.go 2.61 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/*
Copyright IBM Corp. 2016 All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

		 http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main

import (
	"fmt"
	"testing"

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

25
26
func checkInit(t *testing.T, scc *SimpleChaincode, stub *shim.MockStub, args [][]byte) {
	_, err := stub.MockInit("1", args)
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
	if err != nil {
		fmt.Println("Init failed", err)
		t.FailNow()
	}
}

func checkState(t *testing.T, stub *shim.MockStub, name string, value string) {
	bytes := stub.State[name]
	if bytes == nil {
		fmt.Println("State", name, "failed to get value")
		t.FailNow()
	}
	if string(bytes) != value {
		fmt.Println("State value", name, "was not", value, "as expected")
		t.FailNow()
	}
}

45
46
func checkQuery(t *testing.T, scc *SimpleChaincode, stub *shim.MockStub, args [][]byte) {
	_, err := stub.MockInit("1", args)
47
	bytes, err := scc.Query(stub)
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
	if err != nil {
		// expected failure
		fmt.Println("Query below is expected to fail")
		fmt.Println("Query failed", err)
		fmt.Println("Query above is expected to fail")

		if err.Error() != "{\"Error\":\"Cannot put state within chaincode query\"}" {
			fmt.Println("Failure was not the expected \"Cannot put state within chaincode query\" : ", err)
			t.FailNow()
		}

	} else {
		fmt.Println("Query did not fail as expected (PutState within Query)!", bytes, err)
		t.FailNow()
	}
}

65
66
func checkInvoke(t *testing.T, scc *SimpleChaincode, stub *shim.MockStub, args [][]byte) {
	_, err := stub.MockInvoke("1", args)
67
68
69
70
71
72
73
	if err != nil {
		fmt.Println("Invoke", args, "failed", err)
		t.FailNow()
	}
}

func TestExample03_Init(t *testing.T) {
74
	scc := new(SimpleChaincode)
75
76
77
	stub := shim.NewMockStub("ex03", scc)

	// Init A=123 B=234
78
	checkInit(t, scc, stub, [][]byte{[]byte("init"), []byte("A"), []byte("123")})
79
80
81
82
83

	checkState(t, stub, "A", "123")
}

func TestExample03_Query(t *testing.T) {
84
	scc := new(SimpleChaincode)
85
86
87
	stub := shim.NewMockStub("ex03", scc)

	// Init A=345 B=456
88
	checkInit(t, scc, stub, [][]byte{[]byte("init"), []byte("A"), []byte("345")})
89
90

	// Query A
91
	checkQuery(t, scc, stub, [][]byte{[]byte("query"), []byte("A"), []byte("345")})
92
93
94
95
96
}

func TestExample03_Invoke(t *testing.T) {
	// Example03 does not implement Invoke()
}