vitess.io/vitess@v0.16.2/go/vt/vttablet/onlineddl/vrepl/columns_test.go (about)

     1  /*
     2     Copyright 2016 GitHub Inc.
     3  	 See https://github.com/github/gh-ost/blob/master/LICENSE
     4  */
     5  /*
     6  Copyright 2021 The Vitess Authors.
     7  
     8  Licensed under the Apache License, Version 2.0 (the "License");
     9  you may not use this file except in compliance with the License.
    10  You may obtain a copy of the License at
    11  
    12      http://www.apache.org/licenses/LICENSE-2.0
    13  
    14  Unless required by applicable law or agreed to in writing, software
    15  distributed under the License is distributed on an "AS IS" BASIS,
    16  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    17  See the License for the specific language governing permissions and
    18  limitations under the License.
    19  */
    20  
    21  package vrepl
    22  
    23  import (
    24  	"testing"
    25  
    26  	"github.com/stretchr/testify/assert"
    27  )
    28  
    29  var (
    30  	columnsA = &ColumnList{
    31  		columns: []Column{
    32  			{
    33  				Name: "id",
    34  			},
    35  			{
    36  				Name: "cint",
    37  			},
    38  			{
    39  				Name: "cgen1",
    40  			},
    41  			{
    42  				Name: "cgen2",
    43  			},
    44  			{
    45  				Name: "cchar",
    46  			},
    47  			{
    48  				Name: "cremoved",
    49  			},
    50  			{
    51  				Name:       "cnullable",
    52  				IsNullable: true,
    53  			},
    54  			{
    55  				Name:          "cnodefault",
    56  				IsNullable:    false,
    57  				IsDefaultNull: true,
    58  			},
    59  		},
    60  		Ordinals: ColumnsMap{},
    61  	}
    62  	columnsB = &ColumnList{
    63  		columns: []Column{
    64  			{
    65  				Name: "id",
    66  			},
    67  			{
    68  				Name: "cint",
    69  			},
    70  			{
    71  				Name: "cgen1",
    72  			},
    73  			{
    74  				Name: "cchar_alternate",
    75  			},
    76  			{
    77  				Name:       "cnullable",
    78  				IsNullable: true,
    79  			},
    80  			{
    81  				Name:          "cnodefault",
    82  				IsNullable:    false,
    83  				IsDefaultNull: true,
    84  			},
    85  		},
    86  		Ordinals: ColumnsMap{},
    87  	}
    88  	columnsVirtual = ParseColumnList("cgen1,cgen2")
    89  )
    90  
    91  func TestGetSharedColumns(t *testing.T) {
    92  	tt := []struct {
    93  		name                                    string
    94  		sourceCols                              *ColumnList
    95  		targetCols                              *ColumnList
    96  		renameMap                               map[string]string
    97  		expectSourceSharedColNames              []string
    98  		expectTargetSharedColNames              []string
    99  		expectDroppedSourceNonGeneratedColNames []string
   100  	}{
   101  		{
   102  			name:                                    "rename map empty",
   103  			sourceCols:                              columnsA,
   104  			targetCols:                              columnsB,
   105  			renameMap:                               map[string]string{},
   106  			expectSourceSharedColNames:              []string{"id", "cint", "cnullable", "cnodefault"},
   107  			expectTargetSharedColNames:              []string{"id", "cint", "cnullable", "cnodefault"},
   108  			expectDroppedSourceNonGeneratedColNames: []string{"cchar", "cremoved"},
   109  		},
   110  		{
   111  			name:                                    "renamed column",
   112  			sourceCols:                              columnsA,
   113  			targetCols:                              columnsB,
   114  			renameMap:                               map[string]string{"cchar": "cchar_alternate"},
   115  			expectSourceSharedColNames:              []string{"id", "cint", "cchar", "cnullable", "cnodefault"},
   116  			expectTargetSharedColNames:              []string{"id", "cint", "cchar_alternate", "cnullable", "cnodefault"},
   117  			expectDroppedSourceNonGeneratedColNames: []string{"cremoved"},
   118  		},
   119  	}
   120  
   121  	parser := NewAlterTableParser()
   122  	for _, tc := range tt {
   123  		t.Run(tc.name, func(t *testing.T) {
   124  			parser.columnRenameMap = tc.renameMap
   125  			sourceSharedCols, targetSharedCols, droppedNonGeneratedCols, _ := GetSharedColumns(
   126  				tc.sourceCols, tc.targetCols,
   127  				columnsVirtual, columnsVirtual,
   128  				parser,
   129  			)
   130  			assert.Equal(t, tc.expectSourceSharedColNames, sourceSharedCols.Names())
   131  			assert.Equal(t, tc.expectTargetSharedColNames, targetSharedCols.Names())
   132  			assert.Equal(t, tc.expectDroppedSourceNonGeneratedColNames, droppedNonGeneratedCols.Names())
   133  		})
   134  	}
   135  }