forked from flow-hydraulics/flow-pds
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest_lib.go
188 lines (154 loc) · 4.84 KB
/
test_lib.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
package main
import (
"context"
big "math/big"
"reflect"
"strings"
"testing"
"github.com/flow-hydraulics/flow-pds/service/app"
"github.com/flow-hydraulics/flow-pds/service/common"
"github.com/flow-hydraulics/flow-pds/service/config"
"github.com/flow-hydraulics/flow-pds/service/flow_helpers"
"github.com/flow-hydraulics/flow-pds/service/http"
"github.com/flow-hydraulics/flow-pds/service/transactions"
"github.com/onflow/cadence"
flow "github.com/onflow/flow-go-sdk"
flowGrpc "github.com/onflow/flow-go-sdk/access/grpc"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"gorm.io/gorm"
)
func cleanTestDatabase(cfg *config.Config, db *gorm.DB) {
// Only run this if database DSN contains "test"
if strings.Contains(strings.ToLower(cfg.DatabaseDSN), "test") {
// "If you perform a batch delete without any conditions, GORM WON’T run it, and will return ErrMissingWhereClause error
// You have to use some conditions or use raw SQL or enable AllowGlobalUpdate"
// Unscoped to prevent Soft Delete
db.Unscoped().Where("1 = 1").Delete(&app.Distribution{})
db.Unscoped().Where("1 = 1").Delete(&app.Bucket{})
db.Unscoped().Where("1 = 1").Delete(&app.Pack{})
db.Unscoped().Where("1 = 1").Delete(&app.Settlement{})
db.Unscoped().Where("1 = 1").Delete(&app.SettlementCollectible{})
db.Unscoped().Where("1 = 1").Delete(&app.Minting{})
db.Unscoped().Where("1 = 1").Delete(&app.CirculatingPackContract{})
db.Unscoped().Where("1 = 1").Delete(&transactions.StorableTransaction{})
}
}
func getTestCfg(t *testing.T, b *testing.B) *config.Config {
cfg, err := config.ParseConfig(&config.ConfigOptions{EnvFilePath: ".env.test"})
if err != nil {
panic(err)
}
cfg.DatabaseDSN = "postgresql://pds:pds@localhost:5432/pds"
cfg.DatabaseType = "psql"
return cfg
}
func getTestApp(cfg *config.Config, poll bool) (*app.App, func()) {
flowClient, err := flowGrpc.NewBaseClient(cfg.AccessAPIHost, grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
panic(err)
}
db, err := common.NewGormDB(cfg)
if err != nil {
panic(err)
}
cleanTestDatabase(cfg, db)
// Migrate app database
if err := app.Migrate(db); err != nil {
panic(err)
}
if err := transactions.Migrate(db); err != nil {
panic(err)
}
app, err := app.New(cfg, db, flowClient, poll)
if err != nil {
panic(err)
}
clean := func() {
app.Close()
flowClient.Close()
cleanTestDatabase(cfg, db)
}
return app, clean
}
func getTestServer(cfg *config.Config, poll bool) (*http.Server, func()) {
app, cleanupApp := getTestApp(cfg, poll)
clean := func() {
cleanupApp()
}
return http.NewServer(cfg, app), clean
}
func makeTestCollection(size int) []common.FlowID {
collection := make([]common.FlowID, size)
for i := range collection {
collection[i] = common.FlowID{Int64: int64(i + 1), Valid: true}
}
return collection
}
func getExampleNFTBalance(flowClient *flowGrpc.BaseClient, address flow.Address) (uint64, error) {
balanceScript, err := flow_helpers.ParseCadenceTemplate(
"./cadence-scripts/collectibleNFT/balance.cdc",
&flow_helpers.CadenceTemplateVars{
NonFungibleToken: "f8d6e0586b0a20c7",
CollectibleNFTName: "ExampleNFT",
CollectibleNFTAddress: "01cf0e2f2f715450",
},
)
if err != nil {
return 0, err
}
balanceArgs := []cadence.Value{
cadence.NewAddress(address),
}
v, err := flowClient.ExecuteScriptAtLatestBlock(context.Background(), balanceScript, balanceArgs)
if err != nil {
return 0, err
}
return v.ToGoValue().(*big.Int).Uint64(), err
}
func getExampleNFTIDs(flowClient *flowGrpc.BaseClient, address flow.Address, balance uint64) (common.FlowIDList, error) {
idsScript, err := flow_helpers.ParseCadenceTemplate(
"./cadence-scripts/collectibleNFT/balance_ids.cdc",
&flow_helpers.CadenceTemplateVars{
NonFungibleToken: "f8d6e0586b0a20c7",
CollectibleNFTName: "ExampleNFT",
CollectibleNFTAddress: "01cf0e2f2f715450",
},
)
if err != nil {
return nil, err
}
var res common.FlowIDList
limit := uint64(10000)
for offset := uint64(0); offset < balance; offset = offset + limit {
idsArgs := []cadence.Value{
cadence.NewAddress(address),
cadence.NewUInt64(offset),
cadence.NewUInt64(limit),
}
ids, err := flowClient.ExecuteScriptAtLatestBlock(context.Background(), idsScript, idsArgs)
if err != nil {
return nil, err
}
for _, v := range ids.ToGoValue().([]interface{}) {
uintID, ok := v.(uint64)
if !ok {
panic("unable to parse uint")
}
res = append(res, common.FlowID{Int64: int64(uintID), Valid: true})
}
}
return res, err
}
func AssertEqual(t *testing.T, a interface{}, b interface{}) {
if a == b {
return
}
t.Errorf("Received %v (type %v), expected %v (type %v)", a, reflect.TypeOf(a), b, reflect.TypeOf(b))
}
func AssertNotEqual(t *testing.T, a interface{}, b interface{}) {
if a != b {
return
}
t.Error("Did not expect to equal")
}