github.com/hasnat/dolt/go@v0.0.0-20210628190320-9eb5d843fbb7/libraries/doltcore/rowconv/row_converter_test.go (about) 1 // Copyright 2019 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 rowconv 16 17 import ( 18 "context" 19 "testing" 20 "time" 21 22 "github.com/google/uuid" 23 "github.com/stretchr/testify/assert" 24 "github.com/stretchr/testify/require" 25 26 "github.com/dolthub/dolt/go/libraries/doltcore/row" 27 "github.com/dolthub/dolt/go/libraries/doltcore/schema" 28 "github.com/dolthub/dolt/go/libraries/doltcore/schema/typeinfo" 29 "github.com/dolthub/dolt/go/libraries/doltcore/table/untyped" 30 "github.com/dolthub/dolt/go/store/types" 31 ) 32 33 var srcCols = schema.NewColCollection( 34 schema.NewColumn("uuidtostr", 0, types.UUIDKind, true), 35 schema.NewColumn("floattostr", 1, types.FloatKind, false), 36 schema.NewColumn("uinttostr", 2, types.UintKind, false), 37 schema.NewColumn("booltostr", 3, types.BoolKind, false), 38 schema.NewColumn("inttostr", 4, types.IntKind, false), 39 schema.NewColumn("stringtostr", 5, types.StringKind, false), 40 schema.NewColumn("timestamptostr", 6, types.TimestampKind, false), 41 ) 42 43 var srcSch = schema.MustSchemaFromCols(srcCols) 44 45 func TestRowConverter(t *testing.T) { 46 mapping, err := TypedToUntypedMapping(srcSch) 47 48 require.NoError(t, err) 49 50 vrw := types.NewMemoryValueStore() 51 rConv, err := NewRowConverter(context.Background(), vrw, mapping) 52 53 if err != nil { 54 t.Fatal("Error creating row converter") 55 } 56 57 id, _ := uuid.NewRandom() 58 tt := types.Timestamp(time.Now()) 59 inRow, err := row.New(vrw.Format(), srcSch, row.TaggedValues{ 60 0: types.UUID(id), 61 1: types.Float(1.25), 62 2: types.Uint(12345678), 63 3: types.Bool(true), 64 4: types.Int(-1234), 65 5: types.String("string string string"), 66 6: tt, 67 }) 68 69 require.NoError(t, err) 70 outData, err := rConv.Convert(inRow) 71 require.NoError(t, err) 72 73 destSch := mapping.DestSch 74 expected, err := row.New(vrw.Format(), destSch, row.TaggedValues{ 75 0: types.String(id.String()), 76 1: types.String("1.25"), 77 2: types.String("12345678"), 78 3: types.String("1"), 79 4: types.String("-1234"), 80 5: types.String("string string string"), 81 6: types.String(tt.String()), 82 }) 83 84 require.NoError(t, err) 85 86 if !row.AreEqual(outData, expected, destSch) { 87 t.Error("\n", row.Fmt(context.Background(), expected, destSch), "!=\n", row.Fmt(context.Background(), outData, destSch)) 88 } 89 } 90 91 func TestUnneccessaryConversion(t *testing.T) { 92 mapping, err := TagMapping(srcSch, srcSch) 93 if err != nil { 94 t.Fatal(err) 95 } 96 97 vrw := types.NewMemoryValueStore() 98 rconv, err := NewRowConverter(context.Background(), vrw, mapping) 99 if err != nil { 100 t.Fatal(err) 101 } 102 if !rconv.IdentityConverter { 103 t.Fatal("expected identity converter") 104 } 105 } 106 107 func TestSpecialBoolHandling(t *testing.T) { 108 col1, err := schema.NewColumnWithTypeInfo("pk", 0, typeinfo.Int64Type, true, "", false, "") 109 require.NoError(t, err) 110 col2, err := schema.NewColumnWithTypeInfo("v", 1, typeinfo.PseudoBoolType, false, "", false, "") 111 require.NoError(t, err) 112 colColl := schema.NewColCollection(col1, col2) 113 sch, err := schema.SchemaFromCols(colColl) 114 require.NoError(t, err) 115 untypedSch, err := untyped.UntypeSchema(sch) 116 require.NoError(t, err) 117 118 mapping, err := TagMapping(untypedSch, sch) 119 require.NoError(t, err) 120 vrw := types.NewMemoryValueStore() 121 rconv, err := NewImportRowConverter(context.Background(), vrw, mapping) 122 require.NoError(t, err) 123 inRow, err := row.New(vrw.Format(), untypedSch, row.TaggedValues{ 124 0: types.String("76"), 125 1: types.String("true"), 126 }) 127 require.NoError(t, err) 128 outData, err := rconv.Convert(inRow) 129 require.NoError(t, err) 130 require.NotNil(t, outData) 131 132 expected, err := row.New(vrw.Format(), mapping.DestSch, row.TaggedValues{ 133 0: types.Int(76), 134 1: types.Uint(1), 135 }) 136 require.NoError(t, err) 137 assert.True(t, row.AreEqual(outData, expected, mapping.DestSch)) 138 139 rconvNoHandle, err := NewRowConverter(context.Background(), vrw, mapping) 140 require.NoError(t, err) 141 results, errStr := rconvNoHandle.Convert(inRow) 142 assert.Nil(t, results) 143 assert.NotEmpty(t, errStr) 144 }