-
-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathframework.go
287 lines (264 loc) · 8.83 KB
/
framework.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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
// Copyright 2023 Dolthub, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package _go
import (
"context"
"encoding/json"
"errors"
"fmt"
"math"
"net"
"os"
"testing"
"time"
"github.com/dolthub/dolt/go/libraries/utils/svcs"
"github.com/dolthub/go-mysql-server/server"
"github.com/dolthub/go-mysql-server/sql"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgtype"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
dserver "github.com/dolthub/doltgresql/server"
)
// ScriptTest defines a consistent structure for testing queries.
type ScriptTest struct {
// Name of the script.
Name string
// The database to create and use. If not provided, then it defaults to "postgres".
Database string
// The SQL statements to execute as setup, in order. Results are not checked, but statements must not error.
SetUpScript []string
// The set of assertions to make after setup, in order
Assertions []ScriptTestAssertion
// When using RunScripts, setting this on one (or more) tests causes RunScripts to ignore all tests that have this
// set to false (which is the default value). This allows a developer to easily "focus" on a specific test without
// having to comment out other tests, pull it into a different function, etc. In addition, CI ensures that this is
// false before passing, meaning this prevents the commented-out situation where the developer forgets to uncomment
// their code.
Focus bool
// Skip is used to completely skip a test including setup
Skip bool
}
// ScriptTestAssertion are the assertions upon which the script executes its main "testing" logic.
type ScriptTestAssertion struct {
Query string
Expected []sql.Row
ExpectedErr bool
// SkipResultsCheck is used to skip assertions on the expected rows returned from a query. For now, this is
// included as some messages do not have a full logical implementation. Skipping the results check allows us to
// force the test client to not send of those messages.
SkipResultsCheck bool
// Skip is used to completely skip a test, not execute its query at all, and record it as a skipped test
// in the test suite results.
Skip bool
}
// RunScript runs the given script.
func RunScript(t *testing.T, script ScriptTest) {
scriptDatabase := script.Database
if len(scriptDatabase) == 0 {
scriptDatabase = "postgres"
}
ctx, conn, controller := CreateServer(t, scriptDatabase)
defer func() {
conn.Close(ctx)
controller.Stop()
err := controller.WaitForStop()
require.NoError(t, err)
}()
t.Run(script.Name, func(t *testing.T) {
if script.Skip {
t.Skip("Skip has been set in the script")
}
// Run the setup
for _, query := range script.SetUpScript {
_, err := conn.Exec(ctx, query)
require.NoError(t, err)
}
// Run the assertions
for _, assertion := range script.Assertions {
t.Run(assertion.Query, func(t *testing.T) {
if assertion.Skip {
t.Skip("Skip has been set in the assertion")
}
// If we're skipping the results check, then we call Execute, as it uses a simplified message model.
// The more complicated model is only partially implemented, and therefore won't work for all queries.
if assertion.SkipResultsCheck || assertion.ExpectedErr {
_, err := conn.Exec(ctx, assertion.Query)
if assertion.ExpectedErr {
require.Error(t, err)
} else {
require.NoError(t, err)
}
} else {
rows, err := conn.Query(ctx, assertion.Query)
require.NoError(t, err)
readRows, err := ReadRows(rows)
require.NoError(t, err)
assert.Equal(t, NormalizeRows(assertion.Expected), readRows)
}
})
}
})
}
// RunScripts runs the given collection of scripts.
func RunScripts(t *testing.T, scripts []ScriptTest) {
// First, we'll run through the scripts to check for the Focus variable. If it's true, then append it to the new slice.
focusScripts := make([]ScriptTest, 0, len(scripts))
for _, script := range scripts {
if script.Focus {
// If this is running in GitHub Actions, then we'll panic, because someone forgot to disable it before committing
if _, ok := os.LookupEnv("GITHUB_ACTION"); ok {
panic(fmt.Sprintf("The script `%s` has Focus set to `true`. GitHub Actions requires that "+
"all tests are run, which Focus circumvents, leading to this error. Please disable Focus on "+
"all tests.", script.Name))
}
focusScripts = append(focusScripts, script)
}
}
// If we have scripts with Focus set, then we replace the normal script slice with the new slice.
if len(focusScripts) > 0 {
scripts = focusScripts
}
for _, script := range scripts {
RunScript(t, script)
}
}
// CreateServer creates a server with the given database, returning a connection to the server. The server will close
// when the connection is closed (or loses its connection to the server). The accompanying WaitGroup may be used to wait
// until the server has closed.
func CreateServer(t *testing.T, database string) (context.Context, *pgx.Conn, *svcs.Controller) {
require.NotEmpty(t, database)
port := GetUnusedPort(t)
server.DefaultProtocolListenerFunc = dserver.NewLimitedListener
controller, err := dserver.RunInMemory([]string{fmt.Sprintf("--port=%d", port), "--host=127.0.0.1"})
require.NoError(t, err)
fmt.Printf("port is %d\n", port)
ctx := context.Background()
err = func() error {
// The connection attempt may be made before the server has grabbed the port, so we'll retry the first
// connection a few times.
var conn *pgx.Conn
var err error
for i := 0; i < 3; i++ {
conn, err = pgx.Connect(ctx, fmt.Sprintf("postgres://postgres:[email protected]:%d/", port))
if err == nil {
break
} else {
time.Sleep(time.Second)
}
}
if err != nil {
return err
}
defer conn.Close(ctx)
_, err = conn.Exec(ctx, fmt.Sprintf("CREATE DATABASE %s;", database))
return err
}()
require.NoError(t, err)
conn, err := pgx.Connect(ctx, fmt.Sprintf("postgres://postgres:[email protected]:%d/%s", port, database))
require.NoError(t, err)
return ctx, conn, controller
}
// ReadRows reads all of the given rows into a slice, then closes the rows. This also normalizes all of the rows.
func ReadRows(rows pgx.Rows) (readRows []sql.Row, err error) {
defer func() {
err = errors.Join(err, rows.Err())
}()
var slice []sql.Row
for rows.Next() {
row, err := rows.Values()
if err != nil {
return nil, err
}
slice = append(slice, row)
}
return NormalizeRows(slice), nil
}
// NormalizeRow normalizes each value's type, as the tests only want to compare values. Returns a new row.
func NormalizeRow(row sql.Row) sql.Row {
if len(row) == 0 {
return nil
}
newRow := make(sql.Row, len(row))
for i := range row {
switch val := row[i].(type) {
case int:
newRow[i] = int64(val)
case int8:
newRow[i] = int64(val)
case int16:
newRow[i] = int64(val)
case int32:
newRow[i] = int64(val)
case uint:
newRow[i] = int64(val)
case uint8:
newRow[i] = int64(val)
case uint16:
newRow[i] = int64(val)
case uint32:
newRow[i] = int64(val)
case uint64:
// PostgreSQL does not support an uint64 type, so we can always convert this to an int64 safely.
newRow[i] = int64(val)
case float32:
newRow[i] = float64(val)
case pgtype.Numeric:
if val.NaN {
newRow[i] = math.NaN()
} else if val.InfinityModifier != pgtype.Finite {
newRow[i] = math.Inf(int(val.InfinityModifier))
} else if !val.Valid {
newRow[i] = nil
} else {
fVal, err := val.Float64Value()
if err != nil {
panic(err)
}
if !fVal.Valid {
panic("no idea why the numeric float value is invalid")
}
newRow[i] = fVal.Float64
}
case time.Time:
newRow[i] = val.Format("2006-01-02 15:04:05")
case map[string]interface{}:
str, err := json.Marshal(val)
if err != nil {
panic(err)
}
newRow[i] = string(str)
default:
newRow[i] = val
}
}
return newRow
}
// NormalizeRows normalizes each value's type within each row, as the tests only want to compare values. Returns a new
// set of rows in the same order.
func NormalizeRows(rows []sql.Row) []sql.Row {
newRows := make([]sql.Row, len(rows))
for i := range rows {
newRows[i] = NormalizeRow(rows[i])
}
return newRows
}
// GetUnusedPort returns an unused port.
func GetUnusedPort(t *testing.T) int {
listener, err := net.Listen("tcp", ":0")
require.NoError(t, err)
port := listener.Addr().(*net.TCPAddr).Port
require.NoError(t, listener.Close())
return port
}