github.com/hasnat/dolt/go@v0.0.0-20210628190320-9eb5d843fbb7/libraries/doltcore/rowconv/field_mapping_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 "reflect" 19 "testing" 20 21 "github.com/dolthub/dolt/go/libraries/doltcore/schema" 22 "github.com/dolthub/dolt/go/libraries/utils/filesys" 23 "github.com/dolthub/dolt/go/store/types" 24 ) 25 26 var fieldsA = schema.NewColCollection( 27 schema.NewColumn("a", 0, types.StringKind, true), 28 schema.NewColumn("b", 1, types.StringKind, false), 29 schema.NewColumn("c", 2, types.StringKind, false)) 30 31 var fieldsB = schema.NewColCollection( 32 schema.NewColumn("a", 0, types.StringKind, true), 33 schema.NewColumn("b", 1, types.StringKind, false)) 34 35 var fieldsC = schema.NewColCollection( 36 schema.NewColumn("key", 3, types.UUIDKind, true), 37 schema.NewColumn("value", 4, types.StringKind, false)) 38 39 var fieldsCNoPK = schema.NewColCollection( 40 schema.NewColumn("key", 3, types.UUIDKind, true), 41 schema.NewColumn("value", 4, types.StringKind, false)) 42 43 var fieldsD = schema.NewColCollection( 44 schema.NewColumn("key", 3, types.StringKind, true), 45 schema.NewColumn("value", 4, types.StringKind, false)) 46 47 var schemaA = schema.MustSchemaFromCols(fieldsA) 48 var schemaB = schema.MustSchemaFromCols(fieldsB) 49 var schemaC = schema.MustSchemaFromCols(fieldsC) 50 var schemaCNoPK = schema.MustSchemaFromCols(fieldsCNoPK) 51 var schemaD = schema.MustSchemaFromCols(fieldsD) 52 53 func TestFieldMapping(t *testing.T) { 54 tests := []struct { 55 mappingJSON string 56 inSch schema.Schema 57 outSch schema.Schema 58 expectErr bool 59 expected map[uint64]uint64 60 identity bool 61 }{ 62 {"", schemaA, schemaA, false, map[uint64]uint64{0: 0, 1: 1, 2: 2}, true}, 63 {"", schemaA, schemaB, false, map[uint64]uint64{0: 0, 1: 1}, false}, 64 {"", schemaB, schemaA, false, map[uint64]uint64{0: 0, 1: 1}, false}, 65 {"", schemaA, schemaC, true, nil, false}, 66 {`{"invalid_json": }`, schemaA, schemaC, true, nil, false}, 67 {`{"b": "value"}`, schemaA, schemaC, false, map[uint64]uint64{1: 4}, false}, 68 {`{"c": "key", "b": "value"}`, schemaA, schemaC, false, map[uint64]uint64{2: 3, 1: 4}, false}, 69 {`{"a": "key", "b": "value"}`, schemaA, schemaC, false, map[uint64]uint64{0: 3, 1: 4}, false}, 70 {`{"a": "key", "b": "value"}`, schemaB, schemaC, false, map[uint64]uint64{0: 3, 1: 4}, false}, 71 {`{"a": "key", "b": "value"}`, schemaB, schemaCNoPK, false, map[uint64]uint64{0: 3, 1: 4}, false}, 72 {`{"a": "key", "b": "value"}`, schemaB, schemaD, false, map[uint64]uint64{0: 3, 1: 4}, true}, 73 } 74 75 for _, test := range tests { 76 fs := filesys.NewInMemFS([]string{"/"}, nil, "/") 77 78 mappingFile := "" 79 if test.mappingJSON != "" { 80 mappingFile = "mapping.json" 81 fs.WriteFile(mappingFile, []byte(test.mappingJSON)) 82 } 83 84 var mapping *FieldMapping 85 var err error 86 if mappingFile != "" { 87 var nm NameMapper 88 nm, err = NameMapperFromFile(mappingFile, fs) 89 if err == nil { 90 mapping, err = NameMapping(test.inSch, test.outSch, nm) 91 } 92 } else { 93 mapping, err = TagMapping(test.inSch, test.outSch) 94 } 95 96 if (err != nil) != test.expectErr { 97 if test.expectErr { 98 t.Fatal("Expected an error that didn't come.") 99 } else { 100 t.Fatal("Unexpected error creating mapping.", err) 101 } 102 } 103 104 if !test.expectErr { 105 if !reflect.DeepEqual(mapping.SrcToDest, test.expected) { 106 t.Error("Mapping does not match expected. Expected:", test.expected, "Actual:", mapping.SrcToDest) 107 } 108 109 //if test.identity != mapping.IsIdentityMapping() { 110 // t.Error("identity expected", test.identity, "actual:", !test.identity) 111 //} 112 } 113 } 114 }