github.com/dolthub/go-mysql-server@v0.18.0/sql/rowexec/insert_test.go (about) 1 // Copyright 2022 Dolthub, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package rowexec 16 17 import ( 18 "testing" 19 "time" 20 21 "github.com/dolthub/vitess/go/sqltypes" 22 "github.com/stretchr/testify/require" 23 24 "github.com/dolthub/go-mysql-server/memory" 25 "github.com/dolthub/go-mysql-server/sql" 26 "github.com/dolthub/go-mysql-server/sql/expression" 27 "github.com/dolthub/go-mysql-server/sql/plan" 28 "github.com/dolthub/go-mysql-server/sql/types" 29 ) 30 31 func TestInsertIgnoreConversions(t *testing.T) { 32 testCases := []struct { 33 name string 34 colType sql.Type 35 value interface{} 36 valueType sql.Type 37 expected interface{} 38 err bool 39 }{ 40 { 41 name: "inserting a string into a integer defaults to a 0", 42 colType: types.Int64, 43 value: "dadasd", 44 valueType: types.Text, 45 expected: int64(0), 46 err: true, 47 }, 48 { 49 name: "string too long gets truncated", 50 colType: types.MustCreateStringWithDefaults(sqltypes.VarChar, 2), 51 value: "dadsa", 52 valueType: types.Text, 53 expected: "da", 54 err: true, 55 }, 56 { 57 name: "inserting a string into a datetime results in 0 time", 58 colType: types.Datetime, 59 value: "dadasd", 60 valueType: types.Text, 61 expected: time.Unix(-62167219200, 0).UTC(), 62 err: true, 63 }, 64 { 65 name: "inserting a negative into an unsigned int results in 0", 66 colType: types.Uint64, 67 value: int64(-1), 68 expected: uint64(1<<64 - 1), 69 valueType: types.Uint64, 70 err: true, 71 }, 72 } 73 74 for _, tc := range testCases { 75 t.Run(tc.name, func(t *testing.T) { 76 db := memory.NewDatabase("foo") 77 pro := memory.NewDBProvider(db) 78 ctx := newContext(pro) 79 80 table := memory.NewTable(db.BaseDatabase, "foo", sql.NewPrimaryKeySchema(sql.Schema{ 81 {Name: "c1", Source: "foo", Type: tc.colType}, 82 }), nil) 83 84 insertPlan := plan.NewInsertInto(sql.UnresolvedDatabase(""), plan.NewResolvedTable(table, nil, nil), plan.NewValues([][]sql.Expression{{ 85 expression.NewLiteral(tc.value, tc.valueType), 86 }}), false, []string{"c1"}, []sql.Expression{}, true) 87 88 ri, err := DefaultBuilder.Build(ctx, insertPlan, nil) 89 require.NoError(t, err) 90 91 row, err := ri.Next(ctx) 92 require.NoError(t, err) 93 94 require.Equal(t, sql.Row{tc.expected}, row) 95 96 var warningCnt int 97 if tc.err { 98 warningCnt = 1 99 } 100 require.Equal(t, ctx.WarningCount(), uint16(warningCnt)) 101 }) 102 } 103 }