-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathstore_test.go
153 lines (117 loc) · 3.54 KB
/
store_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
package postgres
import (
"database/sql"
"os"
"testing"
"github.com/ory/dockertest/v3"
"github.com/sirupsen/logrus"
"github.com/code-payments/code-server/pkg/code/data/treasury"
"github.com/code-payments/code-server/pkg/code/data/treasury/tests"
postgrestest "github.com/code-payments/code-server/pkg/database/postgres/test"
_ "github.com/jackc/pgx/v4/stdlib"
)
const (
// Used for testing ONLY, the table and migrations are external to this repository
tableCreate = `
CREATE TABLE codewallet__core_treasurypool(
id SERIAL NOT NULL PRIMARY KEY,
data_version INTEGER NOT NULL,
name TEXT NOT NULL,
address TEXT NOT NULL,
bump INTEGER NOT NULL,
vault TEXT NOT NULL,
vault_bump INTEGER NOT NULL,
authority TEXT NOT NULL,
merkle_tree_levels INTEGER NOT NULL,
current_index INTEGER NOT NULL,
history_list_size INTEGER NOT NULL,
solana_block INTEGER NOT NULL,
state INTEGER NOT NULL,
last_updated_at TIMESTAMP WITH TIME ZONE,
CONSTRAINT codewallet__core_treasurypool__uniq__name UNIQUE (name),
CONSTRAINT codewallet__core_treasurypool__uniq__address UNIQUE (address),
CONSTRAINT codewallet__core_treasurypool__uniq__vault UNIQUE (vault)
);
CREATE TABLE codewallet__core_treasurypoolrecentroot(
id SERIAL NOT NULL PRIMARY KEY,
pool TEXT NOT NULL,
index INTEGER NOT NULL,
recent_root TEXT NOT NULL,
at_solana_block INTEGER NOT NULL,
CONSTRAINT codewallet__core_treasurypoolrecentroot__uniq__pool__and__index__and__at_solana_block UNIQUE (pool, index, at_solana_block)
);
CREATE TABLE codewallet__core_treasurypoolfunding(
id SERIAL NOT NULL PRIMARY KEY,
vault TEXT NOT NULL,
delta_quarks BIGINT NOT NULL,
transaction_id TEXT NOT NULL,
state INTEGER NOT NULL,
created_at TIMESTAMP WITH TIME ZONE,
CONSTRAINT codewallet__core_treasurypoolfunding__uniq__transaction_id UNIQUE (transaction_id)
);
`
// Used for testing ONLY, the table and migrations are external to this repository
tableDestroy = `
DROP TABLE codewallet__core_treasurypool;
DROP TABLE codewallet__core_treasurypoolrecentroot;
DROP TABLE codewallet__core_treasurypoolfunding;
`
)
var (
testStore treasury.Store
teardown func()
)
func TestMain(m *testing.M) {
log := logrus.StandardLogger()
testPool, err := dockertest.NewPool("")
if err != nil {
log.WithError(err).Error("Error creating docker pool")
os.Exit(1)
}
var cleanUpFunc func()
db, cleanUpFunc, err := postgrestest.StartPostgresDB(testPool)
if err != nil {
log.WithError(err).Error("Error starting postgres image")
os.Exit(1)
}
defer db.Close()
if err := createTestTables(db); err != nil {
logrus.StandardLogger().WithError(err).Error("Error creating test tables")
cleanUpFunc()
os.Exit(1)
}
testStore = New(db)
teardown = func() {
if pc := recover(); pc != nil {
cleanUpFunc()
panic(pc)
}
if err := resetTestTables(db); err != nil {
logrus.StandardLogger().WithError(err).Error("Error resetting test tables")
cleanUpFunc()
os.Exit(1)
}
}
code := m.Run()
cleanUpFunc()
os.Exit(code)
}
func TestTreasuryPoolPostgresStore(t *testing.T) {
tests.RunTests(t, testStore, teardown)
}
func createTestTables(db *sql.DB) error {
_, err := db.Exec(tableCreate)
if err != nil {
logrus.StandardLogger().WithError(err).Error("could not create test tables")
return err
}
return nil
}
func resetTestTables(db *sql.DB) error {
_, err := db.Exec(tableDestroy)
if err != nil {
logrus.StandardLogger().WithError(err).Error("could not drop test tables")
return err
}
return createTestTables(db)
}