Skip to content

Commit c03bc5a

Browse files
committed
add fields to QueryTest and mv script tests to funtion_queries.go
1 parent 1245968 commit c03bc5a

File tree

5 files changed

+318
-214
lines changed

5 files changed

+318
-214
lines changed

enginetest/enginetests.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ func TestQueries(t *testing.T, harness Harness) {
8383
if IsServerEngine(e) && tt.SkipServerEngine {
8484
t.Skip("skipping for server engine")
8585
}
86-
TestQueryWithContext(t, ctx, e, harness, tt.Query, tt.Expected, tt.ExpectedColumns, nil, nil)
86+
TestQueryWithEngine(t, harness, e, tt)
8787
})
8888
}
8989

enginetest/evaluation.go

Lines changed: 94 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,27 @@ func TestQueryWithEngine(t *testing.T, harness Harness, e QueryEngine, tt querie
407407
}
408408

409409
ctx := NewContext(harness)
410-
TestQueryWithContext(t, ctx, e, harness, tt.Query, tt.Expected, tt.ExpectedColumns, tt.Bindings, nil)
410+
411+
if tt.ExpectedErr != nil {
412+
AssertErr(t, e, harness, tt.Query, tt.Bindings, tt.ExpectedErr)
413+
} else if tt.ExpectedErrStr != "" {
414+
AssertErrWithCtx(t, e, harness, ctx, tt.Query, tt.Bindings, nil, tt.ExpectedErrStr)
415+
} else if tt.ExpectedWarning != 0 {
416+
if IsServerEngine(e) && tt.SkipServerEngine {
417+
t.Skip()
418+
}
419+
AssertWarningAndTestQuery(t, e, ctx, harness,
420+
tt.Query,
421+
tt.Expected,
422+
tt.ExpectedColumns,
423+
tt.ExpectedWarning,
424+
tt.ExpectedWarningsCount,
425+
tt.ExpectedWarningMessageSubstring,
426+
false,
427+
)
428+
} else {
429+
TestQueryWithContext(t, ctx, e, harness, tt.Query, tt.Expected, tt.ExpectedColumns, tt.Bindings, nil)
430+
}
411431
})
412432
}
413433

@@ -539,7 +559,17 @@ func TestPreparedQueryWithEngine(t *testing.T, harness Harness, e QueryEngine, t
539559
}
540560
}
541561
ctx := NewContext(harness)
542-
TestPreparedQueryWithContext(t, ctx, e, harness, tt.Query, tt.Expected, tt.ExpectedColumns, nil, false)
562+
563+
if tt.ExpectedErr != nil {
564+
AssertErr(t, e, harness, tt.Query, tt.Bindings, tt.ExpectedErr)
565+
} else if tt.ExpectedErrStr != "" {
566+
AssertErrWithCtx(t, e, harness, ctx, tt.Query, tt.Bindings, nil, tt.ExpectedErrStr)
567+
} else if tt.ExpectedWarning != 0 {
568+
AssertWarningAndTestPreparedQuery(t, e, ctx, harness, tt.Query, tt.Expected, tt.ExpectedColumns,
569+
tt.ExpectedWarning, tt.ExpectedWarningsCount, tt.ExpectedWarningMessageSubstring, false)
570+
} else {
571+
TestPreparedQueryWithContext(t, ctx, e, harness, tt.Query, tt.Expected, tt.ExpectedColumns, nil, false)
572+
}
543573
})
544574
}
545575

@@ -1136,6 +1166,68 @@ func AssertWarningAndTestQuery(
11361166
validateEngine(t, ctx, harness, e)
11371167
}
11381168

1169+
// AssertWarningAndTestPreparedQuery is similar to AssertWarningAndTestQuery but works with prepared statements
1170+
func AssertWarningAndTestPreparedQuery(
1171+
t *testing.T,
1172+
e QueryEngine,
1173+
ctx *sql.Context,
1174+
harness Harness,
1175+
query string,
1176+
expected []sql.Row,
1177+
expectedCols []*sql.Column,
1178+
expectedCode int,
1179+
expectedWarningsCount int,
1180+
expectedWarningMessageSubstring string,
1181+
skipResultsCheck bool,
1182+
) {
1183+
req := require.New(t)
1184+
if ctx == nil {
1185+
ctx = NewContext(harness)
1186+
}
1187+
ctx.ClearWarnings()
1188+
ctx = ctx.WithQuery(query)
1189+
1190+
sch, iter, _, err := e.QueryWithBindings(ctx, query, nil, nil, nil)
1191+
req.NoError(err, "Unexpected error for query %s", query)
1192+
1193+
rows, err := sql.RowIterToRows(ctx, iter)
1194+
req.NoError(err, "Unexpected error for query %s", query)
1195+
1196+
if !IsServerEngine(e) {
1197+
// check warnings depend on context, which ServerEngine does not depend on
1198+
if expectedWarningsCount > 0 {
1199+
assert.Equal(t, expectedWarningsCount, len(ctx.Warnings()))
1200+
// Verify that if warnings are expected, we also configured a specific value check.
1201+
if expectedCode == 0 && len(expectedWarningMessageSubstring) == 0 {
1202+
req.Fail("Invalid test setup. Warning expected, but no value validation was configured.")
1203+
}
1204+
} else {
1205+
if expectedCode != 0 || len(expectedWarningMessageSubstring) != 0 {
1206+
req.Fail("Invalid test setup. No warnings expected, but value validation was configured")
1207+
}
1208+
assert.Zero(t, len(ctx.Warnings()), "Unexpected warnings")
1209+
}
1210+
1211+
if expectedCode > 0 {
1212+
// Not ideal. We are only supporting all warning codes being identical in a given test.
1213+
for _, warning := range ctx.Warnings() {
1214+
assert.Equal(t, expectedCode, warning.Code, "Unexpected warning code")
1215+
}
1216+
}
1217+
if len(expectedWarningMessageSubstring) > 0 {
1218+
// Not ideal. All messages must have the same substring for a given test.
1219+
for _, warning := range ctx.Warnings() {
1220+
assert.Contains(t, warning.Message, expectedWarningMessageSubstring, "Unexpected warning message")
1221+
}
1222+
}
1223+
}
1224+
1225+
if !skipResultsCheck {
1226+
CheckResults(ctx, t, harness, expected, expectedCols, sch, rows, query, e)
1227+
}
1228+
validateEngine(t, ctx, harness, e)
1229+
}
1230+
11391231
func assertSchemasEqualWithDefaults(t *testing.T, expected, actual sql.Schema) bool {
11401232
if len(expected) != len(actual) {
11411233
return assert.Equal(t, expected, actual)

enginetest/queries/function_queries.go

Lines changed: 209 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,223 @@
1515
package queries
1616

1717
import (
18+
"fmt"
1819
"time"
1920

2021
"github.com/dolthub/go-mysql-server/sql"
22+
"github.com/dolthub/go-mysql-server/sql/expression/function"
2123
"github.com/dolthub/go-mysql-server/sql/types"
24+
"github.com/dolthub/vitess/go/mysql"
2225
)
2326

2427
// FunctionQueryTests contains queries that primarily test SQL function calls
2528
var FunctionQueryTests = []QueryTest{
29+
// https://github.com/dolthub/dolt/issues/9916
30+
{
31+
Query: "SELECT TRUNCATE(1.223,1)",
32+
Expected: []sql.Row{
33+
{"1.2"},
34+
},
35+
},
36+
{
37+
Query: "SELECT TRUNCATE(1.999,1)",
38+
Expected: []sql.Row{
39+
{"1.9"},
40+
},
41+
},
42+
{
43+
Query: "SELECT TRUNCATE(1.999,0)",
44+
Expected: []sql.Row{
45+
{"1"},
46+
},
47+
},
48+
{
49+
Query: "SELECT TRUNCATE(-1.999,1)",
50+
Expected: []sql.Row{
51+
{"-1.9"},
52+
},
53+
},
54+
{
55+
Query: "SELECT TRUNCATE(122,-2)",
56+
Expected: []sql.Row{
57+
{100},
58+
},
59+
},
60+
{
61+
Query: "SELECT TRUNCATE(10.28*100,0)",
62+
Expected: []sql.Row{
63+
{"1028"},
64+
},
65+
},
66+
{
67+
Query: "SELECT TRUNCATE(NULL,1)",
68+
Expected: []sql.Row{
69+
{nil},
70+
},
71+
},
72+
{
73+
Query: "SELECT TRUNCATE(1.223,NULL)",
74+
Expected: []sql.Row{
75+
{nil},
76+
},
77+
},
78+
{
79+
Query: "SELECT TRUNCATE(0.5,0)",
80+
Expected: []sql.Row{
81+
{"0"},
82+
},
83+
},
84+
{
85+
Query: "SELECT TRUNCATE(-0.5,0)",
86+
Expected: []sql.Row{
87+
{"0"},
88+
},
89+
},
90+
{
91+
Query: "SELECT TRUNCATE(1.223,100)",
92+
Expected: []sql.Row{
93+
{"1.223"},
94+
},
95+
},
96+
{
97+
Query: "SELECT TRUNCATE(1.223,-100)",
98+
Expected: []sql.Row{
99+
{"0"},
100+
},
101+
},
102+
{
103+
Query: "SELECT TRUNCATE('abc',1)",
104+
Expected: []sql.Row{
105+
{0.0},
106+
},
107+
},
108+
{
109+
Query: "SELECT TRUNCATE(1.223,'xyz')",
110+
Expected: []sql.Row{
111+
{"1"},
112+
},
113+
},
114+
{
115+
Query: "SELECT TRUNCATE(1.223,1.5)",
116+
Expected: []sql.Row{
117+
{"1.22"},
118+
},
119+
},
120+
{
121+
Query: "SELECT TRUNCATE(1.223,1.7)",
122+
Expected: []sql.Row{
123+
{"1.22"},
124+
},
125+
},
126+
{
127+
Query: "SELECT TRUNCATE(1.223,0.1)",
128+
Expected: []sql.Row{
129+
{"1"},
130+
},
131+
},
132+
{
133+
Query: "SELECT TRUNCATE(1.223,0.9)",
134+
Expected: []sql.Row{
135+
{"1.2"},
136+
},
137+
},
138+
{
139+
Query: "SELECT TRUNCATE(1.223,-0.5)",
140+
Expected: []sql.Row{
141+
{"0"},
142+
},
143+
},
144+
{
145+
Query: "SELECT TRUNCATE(1.223,-0.9)",
146+
Expected: []sql.Row{
147+
{"0"},
148+
},
149+
},
150+
{
151+
Dialect: "mysql",
152+
Query: "SELECT TRUNCATE('123abc',1)",
153+
Expected: []sql.Row{
154+
{123.0},
155+
},
156+
ExpectedWarning: mysql.ERTruncatedWrongValue,
157+
ExpectedWarningsCount: 1,
158+
ExpectedWarningMessageSubstring: fmt.Sprintf(sql.ErrTruncatedIncorrect.Message, types.Float64.String(), "123abc"),
159+
},
160+
{
161+
Dialect: "mysql",
162+
Query: "SELECT TRUNCATE('1.5abc',1)",
163+
Expected: []sql.Row{
164+
{1.5},
165+
},
166+
ExpectedWarning: mysql.ERTruncatedWrongValue,
167+
ExpectedWarningsCount: 1,
168+
ExpectedWarningMessageSubstring: fmt.Sprintf(sql.ErrTruncatedIncorrect.Message, types.Float64.String(), "1.5abc"),
169+
},
170+
{
171+
Dialect: "mysql",
172+
Query: "SELECT TRUNCATE('999xyz',2)",
173+
Expected: []sql.Row{
174+
{999.0},
175+
},
176+
ExpectedWarning: mysql.ERTruncatedWrongValue,
177+
ExpectedWarningsCount: 1,
178+
ExpectedWarningMessageSubstring: fmt.Sprintf(sql.ErrTruncatedIncorrect.Message, types.Float64.String(), "999xyz"),
179+
},
180+
{
181+
Dialect: "mysql",
182+
Query: "SELECT TRUNCATE(1.223,'1.5abc')",
183+
Expected: []sql.Row{
184+
{"1.2"},
185+
},
186+
ExpectedWarning: mysql.ERTruncatedWrongValue,
187+
ExpectedWarningsCount: 2, // Both input and precision conversions generate warnings
188+
ExpectedWarningMessageSubstring: fmt.Sprintf(sql.ErrTruncatedIncorrect.Message, types.Int32.String(), "1.5abc"),
189+
},
190+
{
191+
Dialect: "mysql",
192+
Query: "SELECT TRUNCATE(1.223,'0.5')",
193+
Expected: []sql.Row{
194+
{"1"},
195+
},
196+
ExpectedWarning: mysql.ERTruncatedWrongValue,
197+
ExpectedWarningsCount: 2, // Both input and precision conversions generate warnings
198+
ExpectedWarningMessageSubstring: fmt.Sprintf(sql.ErrTruncatedIncorrect.Message, types.Int32.String(), "0.5"),
199+
},
200+
{
201+
Dialect: "mysql",
202+
Query: "SELECT TRUNCATE(1.223,'2.7')",
203+
Expected: []sql.Row{
204+
{"1.22"},
205+
},
206+
ExpectedWarning: mysql.ERTruncatedWrongValue,
207+
ExpectedWarningsCount: 2, // Both input and precision conversions generate warnings
208+
ExpectedWarningMessageSubstring: fmt.Sprintf(sql.ErrTruncatedIncorrect.Message, types.Int32.String(), "2.7"),
209+
},
210+
{
211+
Dialect: "mysql",
212+
Query: "SELECT TRUNCATE(1.223,'invalid_precision')",
213+
Expected: []sql.Row{
214+
{"1"},
215+
},
216+
ExpectedWarning: mysql.ERTruncatedWrongValue,
217+
ExpectedWarningsCount: 2, // Both input and precision conversions generate warnings
218+
ExpectedWarningMessageSubstring: fmt.Sprintf(sql.ErrTruncatedIncorrect.Message, types.Int32.String(), "invalid_precision"),
219+
},
220+
{
221+
Query: "SELECT TRUNCATE()",
222+
ExpectedErr: sql.ErrInvalidArgumentNumber,
223+
ExpectedErrStr: fmt.Sprintf(sql.ErrInvalidArgumentNumber.Message, function.TruncateFunctionName, 2, 0),
224+
},
225+
{
226+
Query: "SELECT TRUNCATE(1)",
227+
ExpectedErr: sql.ErrInvalidArgumentNumber,
228+
ExpectedErrStr: fmt.Sprintf(sql.ErrInvalidArgumentNumber.Message, function.TruncateFunctionName, 2, 1),
229+
},
230+
{
231+
Query: "SELECT TRUNCATE(1,2,3)",
232+
ExpectedErr: sql.ErrInvalidArgumentNumber,
233+
ExpectedErrStr: fmt.Sprintf(sql.ErrInvalidArgumentNumber.Message, function.TruncateFunctionName, 2, 3),
234+
},
26235
// String Functions
27236
{
28237
Query: `SELECT CONCAT("a", "b", "c")`,

enginetest/queries/queries.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,20 @@ type QueryTest struct {
4343
// Query is the query string to execute
4444
Query string
4545
// Expected is the expected result of the query
46-
Expected []sql.Row
46+
Expected []sql.Row
47+
ExpectedErr *errors.Kind
48+
// ExpectedErrStr should be set for tests that expect a specific error string this is not linked to a custom error.
49+
// In most cases, errors should be linked to a custom error, however there are exceptions where this is not possible,
50+
// such as the use of the SIGNAL statement.
51+
ExpectedErrStr string
52+
// ExpectedWarning contains the expected warning code when a query generates warnings but not errors.
53+
ExpectedWarning int
54+
// ExpectedWarningsCount is used to test the expected number of warnings generated by a query.
55+
// The ExpectedWarning field must be set for warning counts to be checked.
56+
ExpectedWarningsCount int
57+
// ExpectedWarningMessageSubstring is used to test the contents of warning messages generated by a
58+
// query. The ExpectedWarning field must be set for warning messages to be checked.
59+
ExpectedWarningMessageSubstring string
4760
// ExpectedColumns is the set of expected column names for the query results, if specified.
4861
// Only the Name and Type matter of the columns are checked.
4962
ExpectedColumns sql.Schema

0 commit comments

Comments
 (0)