-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtransaction_integration_test.go
131 lines (111 loc) · 5.28 KB
/
transaction_integration_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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package main
import (
"bytes"
"log"
"os"
"testing"
"github.com/stretchr/testify/assert"
"github.com/bjartek/go-with-the-flow/v2/gwtf"
)
/*
Tests must be in the same folder as flow.json with contracts and transactions/scripts in subdirectories in order for the path resolver to work correctly
*/
func TestTransaction(t *testing.T) {
g := gwtf.NewTestingEmulator()
t.Parallel()
t.Run("fail on missing signer", func(t *testing.T) {
g.TransactionFromFile("create_nft_collection").
Test(t). //This method will return a TransactionResult that we can assert upon
AssertFailure("You need to set the main signer") //we assert that there is a failure
})
t.Run("fail on wrong transaction name", func(t *testing.T) {
g.TransactionFromFile("create_nf_collection").
SignProposeAndPayAs("first").
Test(t). //This method will return a TransactionResult that we can assert upon
AssertFailure("Could not read transaction file from path=./transactions/create_nf_collection.cdc") //we assert that there is a failure
})
t.Run("Create NFT collection", func(t *testing.T) {
g.TransactionFromFile("create_nft_collection").
SignProposeAndPayAs("first").
Test(t). //This method will return a TransactionResult that we can assert upon
AssertSuccess(). //Assert that there are no errors and that the transactions succeeds
AssertNoEvents() //Assert that we did not emit any events.
})
t.Run("Mint tokens assert events", func(t *testing.T) {
g.TransactionFromFile("mint_tokens").
SignProposeAndPayAsService().
AccountArgument("first").
UFix64Argument("100.0").
Test(t).
AssertSuccess().
AssertEventCount(3). //assert the number of events returned
AssertPartialEvent(gwtf.NewTestEvent("A.0ae53cb6e3f42a79.FlowToken.TokensDeposited", map[string]interface{}{"amount": "100.00000000"})). //assert a given event, can also take multiple events if you like
AssertEmitEventName("A.0ae53cb6e3f42a79.FlowToken.TokensMinted"). //assert the name of a single event
AssertEmitEventName("A.0ae53cb6e3f42a79.FlowToken.TokensMinted", "A.0ae53cb6e3f42a79.FlowToken.TokensDeposited", "A.0ae53cb6e3f42a79.FlowToken.MinterCreated"). //or assert more then one eventname in a go
AssertEmitEvent(gwtf.NewTestEvent("A.0ae53cb6e3f42a79.FlowToken.TokensMinted", map[string]interface{}{"amount": "100.00000000"})). //assert a given event, can also take multiple events if you like
AssertEmitEventJson("{\n \"name\": \"A.0ae53cb6e3f42a79.FlowToken.MinterCreated\",\n \"time\": \"1970-01-01T00:00:00Z\",\n \"fields\": {\n \"allowedAmount\": \"100.00000000\"\n }\n}") //assert a given event using json, can also take multiple events if you like
})
t.Run("Inline transaction with debug log", func(t *testing.T) {
g.Transaction(`
import Debug from "../contracts/Debug.cdc"
transaction(message:String) {
prepare(acct: AuthAccount, account2: AuthAccount) {
Debug.log(message)
}
}`).
SignProposeAndPayAs("first").
PayloadSigner("second").
StringArgument("foobar").
Test(t).
AssertSuccess().
AssertDebugLog("foobar") //assert that we have debug logged something. The assertion is contains so you do not need to write the entire debug log output if you do not like
})
t.Run("Raw account argument", func(t *testing.T) {
g.Transaction(`
import Debug from "../contracts/Debug.cdc"
transaction(user:Address) {
prepare(acct: AuthAccount) {
Debug.log(user.toString())
}
}`).
SignProposeAndPayAsService().
RawAccountArgument("0x1cf0e2f2f715450").
Test(t).
AssertSuccess().
AssertDebugLog("0x1cf0e2f2f715450")
})
t.Run("transaction that should fail", func(t *testing.T) {
g.Transaction(`
import Debug from "../contracts/Debug.cdc"
transaction(user:Address) {
prepare(acct: AuthAccount) {
Debug.log(user.toStrig())
}
}`).
SignProposeAndPayAsService().
RawAccountArgument("0x1cf0e2f2f715450").
Test(t).
AssertFailure("has no member `toStrig`") //assert failure with an error message. uses contains so you do not need to write entire message
})
t.Run("Assert print events", func(t *testing.T) {
var str bytes.Buffer
log.SetOutput(&str)
defer log.SetOutput(os.Stdout)
g.TransactionFromFile("mint_tokens").
SignProposeAndPayAsService().
AccountArgument("first").
UFix64Argument("100.0").RunPrintEventsFull()
assert.Contains(t, str.String(), "A.0ae53cb6e3f42a79.FlowToken.MinterCreated")
})
t.Run("Assert print events", func(t *testing.T) {
var str bytes.Buffer
log.SetOutput(&str)
defer log.SetOutput(os.Stdout)
g.TransactionFromFile("mint_tokens").
SignProposeAndPayAsService().
AccountArgument("first").
UFix64Argument("100.0").
RunPrintEvents(map[string][]string{"A.0ae53cb6e3f42a79.FlowToken.TokensDeposited": {"to"}})
assert.NotContains(t, str.String(), "0x1cf0e2f2f715450")
})
}