Skip to content

Commit bc70daf

Browse files
committed
Adding more tests
1 parent 041cca5 commit bc70daf

File tree

2 files changed

+103
-30
lines changed

2 files changed

+103
-30
lines changed

server/handler_test.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import (
3535
"github.com/dolthub/go-mysql-server/sql/analyzer"
3636
"github.com/dolthub/go-mysql-server/sql/expression"
3737
"github.com/dolthub/go-mysql-server/sql/types"
38+
"github.com/dolthub/go-mysql-server/sql/variables"
3839
)
3940

4041
func TestHandlerOutput(t *testing.T) {
@@ -681,6 +682,52 @@ func TestSchemaToFields(t *testing.T) {
681682
}
682683
}
683684

685+
// TestHandlerMaxTextResponseBytes tests that the handler calculates the correct max text response byte
686+
// metadata for TEXT types, including honoring the character_set_results session variable. This is tested
687+
// here, instead of in string type unit tests, because of the dependency on system variables being loaded.
688+
func TestHandlerMaxTextResponseBytes(t *testing.T) {
689+
variables.InitSystemVariables()
690+
session := sql.NewBaseSession()
691+
ctx := sql.NewContext(
692+
context.Background(),
693+
sql.WithSession(session),
694+
)
695+
696+
tinyTextUtf8mb4 := types.MustCreateString(sqltypes.Text, types.TinyTextBlobMax, sql.Collation_Default)
697+
textUtf8mb4 := types.MustCreateString(sqltypes.Text, types.TextBlobMax, sql.Collation_Default)
698+
mediumTextUtf8mb4 := types.MustCreateString(sqltypes.Text, types.MediumTextBlobMax, sql.Collation_Default)
699+
longTextUtf8mb4 := types.MustCreateString(sqltypes.Text, types.LongTextBlobMax, sql.Collation_Default)
700+
701+
// When character_set_results is set to utf8mb4, the multibyte character multiplier is 4
702+
require.NoError(t, session.SetSessionVariable(ctx, "character_set_results", "utf8mb4"))
703+
require.EqualValues(t, types.TinyTextBlobMax*4, tinyTextUtf8mb4.MaxTextResponseByteLength(ctx))
704+
require.EqualValues(t, types.TextBlobMax*4, textUtf8mb4.MaxTextResponseByteLength(ctx))
705+
require.EqualValues(t, types.MediumTextBlobMax*4, mediumTextUtf8mb4.MaxTextResponseByteLength(ctx))
706+
require.EqualValues(t, types.LongTextBlobMax, longTextUtf8mb4.MaxTextResponseByteLength(ctx))
707+
708+
// When character_set_results is set to utf8mb3, the multibyte character multiplier is 3
709+
require.NoError(t, session.SetSessionVariable(ctx, "character_set_results", "utf8mb3"))
710+
require.EqualValues(t, types.TinyTextBlobMax*3, tinyTextUtf8mb4.MaxTextResponseByteLength(ctx))
711+
require.EqualValues(t, types.TextBlobMax*3, textUtf8mb4.MaxTextResponseByteLength(ctx))
712+
require.EqualValues(t, types.MediumTextBlobMax*3, mediumTextUtf8mb4.MaxTextResponseByteLength(ctx))
713+
require.EqualValues(t, types.LongTextBlobMax, longTextUtf8mb4.MaxTextResponseByteLength(ctx))
714+
715+
// When character_set_results is set to utf8, the multibyte character multiplier is 3
716+
require.NoError(t, session.SetSessionVariable(ctx, "character_set_results", "utf8"))
717+
require.EqualValues(t, types.TinyTextBlobMax*3, tinyTextUtf8mb4.MaxTextResponseByteLength(ctx))
718+
require.EqualValues(t, types.TextBlobMax*3, textUtf8mb4.MaxTextResponseByteLength(ctx))
719+
require.EqualValues(t, types.MediumTextBlobMax*3, mediumTextUtf8mb4.MaxTextResponseByteLength(ctx))
720+
require.EqualValues(t, types.LongTextBlobMax, longTextUtf8mb4.MaxTextResponseByteLength(ctx))
721+
722+
// When character_set_results is set to NULL, the multibyte character multiplier is taken from
723+
// the type's charset (4 in this case)
724+
require.NoError(t, session.SetSessionVariable(ctx, "character_set_results", nil))
725+
require.EqualValues(t, types.TinyTextBlobMax*4, tinyTextUtf8mb4.MaxTextResponseByteLength(ctx))
726+
require.EqualValues(t, types.TextBlobMax*4, textUtf8mb4.MaxTextResponseByteLength(ctx))
727+
require.EqualValues(t, types.MediumTextBlobMax*4, mediumTextUtf8mb4.MaxTextResponseByteLength(ctx))
728+
require.EqualValues(t, types.LongTextBlobMax, longTextUtf8mb4.MaxTextResponseByteLength(ctx))
729+
}
730+
684731
func TestHandlerTimeout(t *testing.T) {
685732
require := require.New(t)
686733

sql/types/strings_test.go

Lines changed: 56 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
package types
1616

1717
import (
18+
"context"
1819
"fmt"
1920
"reflect"
2021
"strings"
@@ -170,56 +171,80 @@ func TestStringCreateBlobInvalidBaseTypes(t *testing.T) {
170171

171172
func TestStringCreateString(t *testing.T) {
172173
tests := []struct {
173-
baseType query.Type
174-
length int64
175-
collation sql.CollationID
176-
expectedType StringType
177-
expectedErr bool
174+
baseType query.Type
175+
length int64
176+
collation sql.CollationID
177+
expectedType StringType
178+
expectedMaxTextBytes uint32
179+
expectedErr bool
178180
}{
179181
{sqltypes.Binary, 10, sql.Collation_binary,
180-
StringType{sqltypes.Binary, 10, 10, sql.Collation_binary}, false},
182+
StringType{sqltypes.Binary, 10, 10, sql.Collation_binary},
183+
10, false},
181184
{sqltypes.Blob, 10, sql.Collation_binary,
182-
StringType{sqltypes.Blob, TinyTextBlobMax, TinyTextBlobMax, sql.Collation_binary}, false},
185+
StringType{sqltypes.Blob, TinyTextBlobMax, TinyTextBlobMax, sql.Collation_binary},
186+
TinyTextBlobMax, false},
183187
{sqltypes.Char, 10, sql.Collation_Default,
184-
StringType{sqltypes.Char, 10, 40, sql.Collation_Default}, false},
188+
StringType{sqltypes.Char, 10, 40, sql.Collation_Default},
189+
40, false},
185190
{sqltypes.Text, 10, sql.Collation_Default,
186-
// TODO: Test MaxTextResponseBytes: uint32(TinyTextBlobMax * sql.Collation_Default.CharacterSet().MaxLength())
187-
StringType{sqltypes.Text, TinyTextBlobMax / sql.Collation_Default.CharacterSet().MaxLength(), TinyTextBlobMax, sql.Collation_Default}, false},
191+
StringType{sqltypes.Text, TinyTextBlobMax / sql.Collation_Default.CharacterSet().MaxLength(), TinyTextBlobMax, sql.Collation_Default},
192+
uint32(TinyTextBlobMax * sql.Collation_Default.CharacterSet().MaxLength()), false},
188193
{sqltypes.Text, 1000, sql.Collation_Default,
189-
// TODO: Test MaxTextResponseBytes: uint32(TextBlobMax * sql.Collation_Default.CharacterSet().MaxLength())
190-
StringType{sqltypes.Text, TextBlobMax / sql.Collation_Default.CharacterSet().MaxLength(), TextBlobMax, sql.Collation_Default}, false},
194+
StringType{sqltypes.Text, TextBlobMax / sql.Collation_Default.CharacterSet().MaxLength(), TextBlobMax, sql.Collation_Default},
195+
uint32(TextBlobMax * sql.Collation_Default.CharacterSet().MaxLength()), false},
191196
{sqltypes.Text, 1000000, sql.Collation_Default,
192-
// TODO: Test MaxTextResponseBytes: uint32(MediumTextBlobMax * sql.Collation_Default.CharacterSet().MaxLength())
193-
StringType{sqltypes.Text, MediumTextBlobMax / sql.Collation_Default.CharacterSet().MaxLength(), MediumTextBlobMax, sql.Collation_Default}, false},
197+
StringType{sqltypes.Text, MediumTextBlobMax / sql.Collation_Default.CharacterSet().MaxLength(), MediumTextBlobMax, sql.Collation_Default},
198+
uint32(MediumTextBlobMax * sql.Collation_Default.CharacterSet().MaxLength()), false},
194199
{sqltypes.Text, LongTextBlobMax, sql.Collation_Default,
195-
// TODO: Test MaxTextResponseBytes: uint32(LongTextBlobMax)
196-
StringType{sqltypes.Text, LongTextBlobMax / sql.Collation_Default.CharacterSet().MaxLength(), LongTextBlobMax, sql.Collation_Default}, false},
200+
StringType{sqltypes.Text, LongTextBlobMax / sql.Collation_Default.CharacterSet().MaxLength(), LongTextBlobMax, sql.Collation_Default},
201+
uint32(LongTextBlobMax), false},
197202
{sqltypes.VarBinary, 10, sql.Collation_binary,
198-
StringType{sqltypes.VarBinary, 10, 10, sql.Collation_binary}, false},
203+
StringType{sqltypes.VarBinary, 10, 10, sql.Collation_binary},
204+
10, false},
199205
{sqltypes.VarChar, 10, sql.Collation_Default,
200-
StringType{sqltypes.VarChar, 10, 40, sql.Collation_Default}, false},
206+
StringType{sqltypes.VarChar, 10, 40, sql.Collation_Default},
207+
40, false},
201208
{sqltypes.Char, 10, sql.Collation_binary,
202-
StringType{sqltypes.Binary, 10, 10, sql.Collation_binary}, false},
209+
StringType{sqltypes.Binary, 10, 10, sql.Collation_binary},
210+
10, false},
203211
{sqltypes.Text, 10, sql.Collation_binary,
204-
StringType{sqltypes.Blob, TinyTextBlobMax, TinyTextBlobMax, sql.Collation_binary}, false},
212+
StringType{sqltypes.Blob, TinyTextBlobMax, TinyTextBlobMax, sql.Collation_binary},
213+
TinyTextBlobMax, false},
205214
{sqltypes.VarChar, 10, sql.Collation_binary,
206-
StringType{sqltypes.VarBinary, 10, 10, sql.Collation_binary}, false},
215+
StringType{sqltypes.VarBinary, 10, 10, sql.Collation_binary},
216+
10, false},
207217

208-
{sqltypes.Binary, charBinaryMax + 1, sql.Collation_binary, StringType{}, true},
209-
{sqltypes.Blob, LongTextBlobMax + 1, sql.Collation_binary, StringType{}, true},
210-
{sqltypes.Char, charBinaryMax + 1, sql.Collation_Default, StringType{}, true},
211-
{sqltypes.Text, LongTextBlobMax + 1, sql.Collation_Default, StringType{}, true},
218+
// Out of bounds error cases
219+
{sqltypes.Binary, charBinaryMax + 1, sql.Collation_binary, StringType{},
220+
0, true},
221+
{sqltypes.Blob, LongTextBlobMax + 1, sql.Collation_binary, StringType{},
222+
0, true},
223+
{sqltypes.Char, charBinaryMax + 1, sql.Collation_Default, StringType{},
224+
0, true},
225+
{sqltypes.Text, LongTextBlobMax + 1, sql.Collation_Default, StringType{},
226+
0, true},
212227

213228
// JSON strings can also come in over the wire as VARBINARY types, and JSON allows a much larger length limit (1GB).
214-
{sqltypes.VarBinary, MaxJsonFieldByteLength + 1, sql.Collation_binary, StringType{}, true},
215-
{sqltypes.VarChar, varcharVarbinaryMax + 1, sql.Collation_Default, StringType{}, true},
229+
{sqltypes.VarBinary, MaxJsonFieldByteLength + 1, sql.Collation_binary, StringType{},
230+
0, true},
231+
{sqltypes.VarChar, varcharVarbinaryMax + 1, sql.Collation_Default, StringType{},
232+
0, true},
216233

217234
// Default collation is not valid for these types
218-
{sqltypes.Binary, 10, sql.Collation_Default, StringType{}, true},
219-
{sqltypes.Blob, 10, sql.Collation_Default, StringType{}, true},
220-
{sqltypes.VarBinary, 10, sql.Collation_Default, StringType{}, true},
235+
{sqltypes.Binary, 10, sql.Collation_Default, StringType{},
236+
0, true},
237+
{sqltypes.Blob, 10, sql.Collation_Default, StringType{},
238+
0, true},
239+
{sqltypes.VarBinary, 10, sql.Collation_Default, StringType{},
240+
0, true},
221241
}
222242

243+
ctx := sql.NewContext(
244+
context.Background(),
245+
sql.WithSession(sql.NewBaseSession()),
246+
)
247+
223248
for _, test := range tests {
224249
t.Run(fmt.Sprintf("%v %v %v", test.baseType, test.length, test.collation), func(t *testing.T) {
225250
typ, err := CreateString(test.baseType, test.length, test.collation)
@@ -228,6 +253,7 @@ func TestStringCreateString(t *testing.T) {
228253
} else {
229254
require.NoError(t, err)
230255
assert.Equal(t, test.expectedType, typ)
256+
assert.Equal(t, test.expectedMaxTextBytes, typ.MaxTextResponseByteLength(ctx))
231257
}
232258
})
233259
}

0 commit comments

Comments
 (0)