1515package types
1616
1717import (
18+ "context"
1819 "fmt"
1920 "reflect"
2021 "strings"
@@ -170,56 +171,80 @@ func TestStringCreateBlobInvalidBaseTypes(t *testing.T) {
170171
171172func 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