-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathtransaction_test.go
259 lines (215 loc) · 8.02 KB
/
transaction_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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
package overflow
import (
"fmt"
"testing"
"github.com/sanity-io/litter"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
/*
* OverflowTesting
* - state
* - testing
* - func to setup overflow
* - store the height
* - run method that will revert to height before run method and revert again
* - at the end of TestMain expose coverage report to file, if we start with coverage flag
*/
func TestTransaction(t *testing.T) {
o, err := OverflowTesting()
require.NotNil(t, o)
require.NoError(t, err)
t.Run("Run simple tx", func(t *testing.T) {
res := o.Tx("arguments", WithArg("test", "foo"), WithSignerServiceAccount())
assert.NoError(t, res.Err)
})
t.Run("error on missing argument", func(t *testing.T) {
res := o.Tx("arguments", WithSignerServiceAccount())
assert.ErrorContains(t, res.Err, "the interaction 'arguments' is missing [test]")
})
t.Run("error on redundant argument", func(t *testing.T) {
res := o.Tx("arguments", WithArg("test2", "foo"), WithArg("test", "foo"), WithSignerServiceAccount())
assert.ErrorContains(t, res.Err, "the interaction 'arguments' has the following extra arguments [test2]")
})
t.Run("Run simple tx with sa proposer", func(t *testing.T) {
res := o.Tx("arguments", WithArg("test", "foo"), WithPayloadSigner("first"), WithProposerServiceAccount())
assert.Contains(t, res.EmulatorLog[0], "0x179b6b1cb6755e31")
})
t.Run("Run simple tx with custom proposer", func(t *testing.T) {
res := o.Tx("arguments", WithArg("test", "foo"), WithPayloadSigner("first"), WithProposer("account"))
litter.Dump(res.EmulatorLog)
assert.Contains(t, res.EmulatorLog[0], "0x179b6b1cb6755e31")
})
t.Run("Fail when invalid proposer", func(t *testing.T) {
res := o.Tx("arguments", WithArg("test", "foo"), WithPayloadSigner("first"), WithProposer("account2"))
assert.ErrorContains(t, res.Err, "could not find account with name emulator-account2 in the configuration")
})
t.Run("Run linine tx", func(t *testing.T) {
res := o.Tx(`
transaction(test:String) {
prepare(acct: auth(BorrowValue) &Account) {
log(test)
}
}
`, WithArg("test", "foo"), WithSignerServiceAccount())
assert.NoError(t, res.Err)
})
t.Run("Run linine tx", func(t *testing.T) {
res := o.Tx(`
transaction(test:UInt64) {
prepare(acct: auth(BorrowValue) &Account) {
log(test)
}
}
`, WithArg("test", uint64(1)), WithSignerServiceAccount())
assert.NoError(t, res.Err)
})
t.Run("Run simple tx with custom signer", func(t *testing.T) {
res := o.Tx("arguments", WithArg("test", "foo"), WithSigner("account"))
assert.NoError(t, res.Err)
})
t.Run("Error on wrong signer name", func(t *testing.T) {
res := o.Tx("arguments", WithArg("test", "foo"), WithSigner("account2"))
assert.ErrorContains(t, res.Err, "could not find account with name emulator-account2")
})
t.Run("compose a function", func(t *testing.T) {
serviceAccountTx := o.TxFN(WithSignerServiceAccount())
res := serviceAccountTx("arguments", WithArg("test", "foo"))
assert.NoError(t, res.Err)
})
t.Run("create function with name", func(t *testing.T) {
argumentTx := o.TxFileNameFN("arguments", WithSignerServiceAccount())
res := argumentTx(WithArg("test", "foo"))
assert.NoError(t, res.Err)
})
t.Run("Should not allow varags builder arg with single element", func(t *testing.T) {
res := o.Tx("arguments", WithArgs("test"))
assert.ErrorContains(t, res.Err, "please send in an even number of string : interface{} pairs")
})
t.Run("Should not allow varag with non string keys", func(t *testing.T) {
res := o.Tx("arguments", WithArgs(1, "test"))
assert.ErrorContains(t, res.Err, "even parameters in Args needs to be string")
})
t.Run("Arg, with cadence raw value", func(t *testing.T) {
res := o.Tx("arguments", WithSignerServiceAccount(), WithArg("test", cadenceString("test")))
assert.NoError(t, res.Err)
})
t.Run("date time arg", func(t *testing.T) {
res := o.BuildInteraction(`
transaction(test:UFix64) {
prepare(acct: auth(BorrowValue) &Account) {
}
}
`, "transaction", WithArgDateTime("test", "July 29, 2021 08:00:00 AM", "America/New_York"), WithSignerServiceAccount())
assert.NoError(t, res.Error)
})
t.Run("date time arg error", func(t *testing.T) {
res := o.BuildInteraction(`
transaction(test:UFix64) {
prepare(acct: auth(BorrowValue) &Account) {
}
}
`, "transaction", WithArgDateTime("test", "July 29021 08:00:00 AM", "America/New_York"), WithSignerServiceAccount())
assert.ErrorContains(t, res.Error, "cannot parse")
})
t.Run("Map args", func(t *testing.T) {
res := o.Tx("arguments", WithSignerServiceAccount(), WithArgsMap(map[string]interface{}{"test": "test"}))
assert.NoError(t, res.Err)
})
t.Run("Parse addresses should fail if not valid account name and hex", func(t *testing.T) {
res := o.BuildInteraction(`
transaction(test:[Address]) {
prepare(acct: auth(BorrowValue) &Account) {
}
}
`, "transaction", WithAddresses("test", "bjartek"), WithSignerServiceAccount())
assert.ErrorContains(t, res.Error, "bjartek is not an valid account name or an address")
})
t.Run("Parse array of addresses", func(t *testing.T) {
res := o.BuildInteraction(`
transaction(test:[Address]) {
prepare(acct: auth(BorrowValue) &Account) {
}
}
`, "transaction", WithAddresses("test", "account", "45a1763c93006ca"), WithSignerServiceAccount())
assert.Equal(t, "[0xf8d6e0586b0a20c7, 0x045a1763c93006ca]", fmt.Sprintf("%v", res.NamedArgs["test"]))
})
t.Run("Parse String to String map", func(t *testing.T) {
res := o.BuildInteraction(`
transaction(test:{String:String}) {
prepare(acct: auth(BorrowValue) &Account) {
}
}
`, "transaction", WithArg("test", `{ "foo" : "bar"}`), WithSignerServiceAccount())
assert.Equal(t, `{ "foo" : "bar"}`, fmt.Sprintf("%v", res.NamedArgs["test"]))
})
t.Run("Parse String to UFix64 map", func(t *testing.T) {
res := o.BuildInteraction(`
transaction(test:{String:UFix64}) {
prepare(acct: auth(BorrowValue) &Account) {
}
}
`, "transaction", WithArg("test", `{ "foo" : 1.0}`), WithSignerServiceAccount())
assert.Equal(t, `{ "foo" : 1.0}`, fmt.Sprintf("%v", res.NamedArgs["test"]))
})
t.Run("Error when parsing invalid address", func(t *testing.T) {
res := o.BuildInteraction(`
transaction(test:Address) {
prepare(acct: auth(BorrowValue) &Account) {
}
}
`, "transaction", WithArg("test", "bjartek"), WithSignerServiceAccount())
assert.ErrorContains(t, res.Error, "argument `test` with value `bjartek` is not expected type `Address`")
})
t.Run("Should set gas", func(t *testing.T) {
res := o.BuildInteraction(`
transaction(test:Address) {
prepare(acct: auth(BorrowValue) &Account) {
}
}
`, "transaction", WithArg("test", "bjartek"), WithSignerServiceAccount(), WithMaxGas(100))
assert.Equal(t, uint64(100), res.GasLimit)
})
t.Run("Should report error if invalid payload signer", func(t *testing.T) {
res := o.Tx(`
transaction{
prepare(acct: auth(BorrowValue) &Account, user:auth(BorrowValue) &Account) {
}
}
`, WithSignerServiceAccount(), WithPayloadSigner("bjartek"))
assert.Error(t, res.Err, "asd")
})
t.Run("ufix64", func(t *testing.T) {
res := o.BuildInteraction(`
transaction(test:UFix64) {
prepare(acct: auth(BorrowValue) &Account) {
}
}
`, "transaction", WithArg("test", 1.0), WithSignerServiceAccount())
assert.NoError(t, res.Error)
})
t.Run("add printer args", func(t *testing.T) {
res := o.BuildInteraction(`
transaction(test:UFix64) {
prepare(acct: auth(BorrowValue) &Account) {
}
}
`, "transaction", WithArg("test", 1.0), WithSignerServiceAccount(), WithPrintOptions(WithEmulatorLog()), WithPrintOptions(WithFullMeter()))
assert.NoError(t, res.Error)
assert.Equal(t, 2, len(*res.PrintOptions))
})
t.Run("add event filter", func(t *testing.T) {
filter := OverflowEventFilter{
"Deposit": []string{"id"},
}
res := o.BuildInteraction(`
transaction(test:UFix64) {
prepare(acct: auth(BorrowValue) &Account) {
}
}
`, "transaction", WithArg("test", 1.0), WithSignerServiceAccount(), WithoutGlobalEventFilter(), WithEventsFilter(filter))
assert.NoError(t, res.Error)
assert.True(t, res.IgnoreGlobalEventFilters)
assert.Equal(t, 1, len(res.EventFilter))
})
}