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