forked from pgpkg/pgpkg
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtests.go
More file actions
254 lines (208 loc) · 6.64 KB
/
tests.go
File metadata and controls
254 lines (208 loc) · 6.64 KB
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
package pgpkg
// This file runs the user-defined tests. Tests are a bit more complicated than you might expect because
// we don't want any results to be written to the database, but we need to maintain
// various states so we can report errors and get stack traces.
//
// Nothing is overly complex; but it's not as simple as just executing the units directly.
import (
"fmt"
"os"
"strings"
)
type Tests struct {
*Bundle
state *stmtApplyState
NamedTests map[string]*Statement
BeforeTests map[string]*Statement
}
type TestFunctionType int
const (
TestFunctionOther TestFunctionType = iota // utility function, declared but not executed
TestFunctionTest // test function, called during testing
TestFunctionBefore // before function, called once, before tests start.
)
// Given a function name, is it a test function, a before function, or a utility function?
func getTestFunctionType(name string) TestFunctionType {
if strings.HasSuffix(name, "_test") {
return TestFunctionTest
}
if strings.HasSuffix(name, "_before") {
return TestFunctionBefore
}
if strings.HasSuffix(name, "_after") {
_, _ = fmt.Fprintf(os.Stderr, "warning: test name %s is reserved for future use\n", name)
return TestFunctionOther
}
if strings.HasPrefix(name, "test_") {
_, _ = fmt.Fprintf(os.Stderr, "warning: test name %s is deprecated; use %s_test instead\n", name, name[5:])
return TestFunctionTest
}
return TestFunctionOther
}
func (t *Tests) parse() error {
var pending []*Statement
namedTests := make(map[string]*Statement)
beforeTests := make(map[string]*Statement)
definitions := make(map[string]*Statement)
for _, u := range t.Units {
if Options.Verbose {
Verbose.Println("parsing tests", u.Location())
}
if err := u.Parse(); err != nil {
return fmt.Errorf("unable to parse tests: %w", err)
}
for _, stmt := range u.Statements {
obj, err := stmt.GetManagedObject()
if err != nil {
return err
}
if obj.ObjectType != "function" {
return PKGErrorf(stmt, nil, "only functions can be defined in tests; %s %s", obj.ObjectType, obj.ObjectName)
}
// Rewrite the statement to set the schema and security options.
err = rewrite(stmt)
if err != nil {
return err
}
// Get the unqualified name of the function.
fname := strings.ToLower(strings.TrimPrefix(obj.ObjectName, "\""+obj.ObjectSchema+"\"."))
argIndex := strings.IndexRune(fname, '(')
// strip the quotes and the args
fname = fname[1 : argIndex-1]
// Check for duplicate test definitions. This can be a subtle bug because
// all the statements are probably "create or replace", so we make it explicit.
objName := obj.ObjectType + ":" + obj.ObjectName
dupeStmt, dupe := definitions[objName]
if dupe {
return PKGErrorf(stmt, nil,
"duplicate declaration for %s %s; also defined in %s",
obj.ObjectType, obj.ObjectName, dupeStmt.Location())
}
// We save and execute definitions for all functions in the test scripts,
// even if they are non-test objects.
definitions[objName] = stmt
testFunctionType := getTestFunctionType(fname)
if (testFunctionType != TestFunctionOther) && len(obj.ObjectArgs) != 0 {
return PKGErrorf(stmt, nil, "test functions cannot receive arguments: %s %s", obj.ObjectType, obj.ObjectName)
}
switch testFunctionType {
case TestFunctionTest:
t.Package.StatTestCount++
namedTests[obj.ObjectName] = stmt
case TestFunctionBefore:
beforeTests[obj.ObjectName] = stmt
default:
// do nothing.
}
pending = append(pending, stmt)
}
}
t.NamedTests = namedTests
t.BeforeTests = beforeTests
t.state = &stmtApplyState{pending: pending}
return nil
}
// testStmt is the statement containing the test function.
// testStmt was executed when the tests were parsed, so it is only used to work out where
// problems might have happened.
func (t *Tests) runTest(tx *PkgTx, testName string, testStmt *Statement) error {
if _, spErr := tx.Exec("savepoint unittest"); spErr != nil {
return fmt.Errorf("unable to begin savepoint for test %s: %w", testName, spErr)
}
cmd := fmt.Sprintf("select %s", testName)
_, testErr := tx.Exec(cmd)
_, rberr := tx.Exec("rollback to savepoint unittest")
if rberr != nil {
panic(rberr)
}
if testErr == nil {
if Options.ShowTests {
Stdout.Println(" [pass]", testName)
}
return nil
}
if Options.ShowTests {
Stdout.Println("* [FAIL]", testName)
}
pe := PKGErrorf(testStmt, testErr, "test failed: %s", testName)
pe.Context = getErrorContext(tx, cmd, testErr)
tx = nil
return pe
}
func (t *Tests) runBefore(tx *PkgTx, beforeName string, beforeStmt *Statement) error {
cmd := fmt.Sprintf("select %s", beforeName)
_, testErr := tx.Exec(cmd)
if testErr == nil {
return nil
}
pe := PKGErrorf(beforeStmt, testErr, "before-test script failed: %s", beforeName)
pe.Context = getErrorContext(tx, cmd, testErr)
tx = nil
return pe
}
func (t *Tests) Run(tx *PkgTx) error {
// Rollback, and return either the error or an error from the rollback.
if !Options.KeepTestScripts {
defer func() {
if tx != nil {
_, rberr := tx.Exec("rollback to savepoint test")
if rberr != nil {
panic(rberr)
}
}
}()
}
// Create a savepoint for the entire set of tests. This savepoint ensures that the
// test scripts are removed after testing is complete.
// Note that there is a separate savepoint for the individual tests.
if !Options.KeepTestScripts {
_, err := tx.Exec("savepoint test")
if err != nil {
return fmt.Errorf("unable to begin test savepoint: %w", err)
}
}
// Parse all the functions.
err := t.parse()
if err != nil {
return err
}
err = applyState(tx, t.state)
if err != nil {
return err
}
// Run the before-tests. These are run in this outer savepoint so the results are
// available to all the tests within. Note that before-functions are global, not just limited
// to the current file.
for beforeName, beforeStmt := range t.BeforeTests {
if err = t.runBefore(tx, beforeName, beforeStmt); err != nil {
return err
}
}
// Run the actual tests.
for testName, testStmt := range t.NamedTests {
if Options.IncludePattern != nil {
if !Options.IncludePattern.MatchString(testName) {
if Options.ShowSkipped {
Stdout.Println("- [skip]", testName)
}
continue
}
}
if Options.ExcludePattern != nil {
if Options.ExcludePattern.MatchString(testName) {
if Options.ShowSkipped {
Stdout.Println("- [skip]", testName)
}
continue
}
}
if err = t.runTest(tx, testName, testStmt); err != nil {
return err
}
}
return nil
}
func (t *Tests) PrintInfo(w InfoWriter) {
w.Println("Test Bundle")
t.Bundle.PrintInfo(w)
}