github.com/hasnat/dolt/go@v0.0.0-20210628190320-9eb5d843fbb7/libraries/doltcore/schema/alterschema/renametable_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 alterschema
    16  
    17  import (
    18  	"context"
    19  	"testing"
    20  
    21  	"github.com/stretchr/testify/assert"
    22  	"github.com/stretchr/testify/require"
    23  
    24  	"github.com/dolthub/dolt/go/libraries/doltcore/doltdb"
    25  	"github.com/dolthub/dolt/go/libraries/doltcore/dtestutils"
    26  	"github.com/dolthub/dolt/go/libraries/doltcore/row"
    27  	"github.com/dolthub/dolt/go/libraries/doltcore/schema"
    28  	"github.com/dolthub/dolt/go/store/types"
    29  )
    30  
    31  func TestRenameTable(t *testing.T) {
    32  	otherTable := "other"
    33  	cc := schema.NewColCollection(
    34  		schema.NewColumn("id", uint64(100), types.UUIDKind, true, schema.NotNullConstraint{}),
    35  	)
    36  	otherSch, err := schema.SchemaFromCols(cc)
    37  	require.NoError(t, err)
    38  
    39  	tests := []struct {
    40  		name           string
    41  		tableName      string
    42  		newTableName   string
    43  		expectedSchema schema.Schema
    44  		expectedRows   []row.Row
    45  		expectedErr    string
    46  	}{
    47  		{
    48  			name:           "rename table",
    49  			tableName:      "people",
    50  			newTableName:   "newPeople",
    51  			expectedSchema: dtestutils.TypedSchema,
    52  			expectedRows:   dtestutils.TypedRows,
    53  		},
    54  		{
    55  			name:         "table not found",
    56  			tableName:    "notFound",
    57  			newTableName: "newNotfound",
    58  			expectedErr:  doltdb.ErrTableNotFound.Error(),
    59  		},
    60  		{
    61  			name:         "name already in use",
    62  			tableName:    "people",
    63  			newTableName: otherTable,
    64  			expectedErr:  doltdb.ErrTableExists.Error(),
    65  		},
    66  	}
    67  
    68  	for _, tt := range tests {
    69  		t.Run(tt.name, func(t *testing.T) {
    70  			dEnv := dtestutils.CreateEnvWithSeedData(t)
    71  			ctx := context.Background()
    72  
    73  			dtestutils.CreateTestTable(t, dEnv, otherTable, otherSch)
    74  
    75  			root, err := dEnv.WorkingRoot(ctx)
    76  			require.NoError(t, err)
    77  
    78  			updatedRoot, err := RenameTable(ctx, root, tt.tableName, tt.newTableName)
    79  			if len(tt.expectedErr) > 0 {
    80  				require.Error(t, err)
    81  				assert.Contains(t, err.Error(), tt.expectedErr)
    82  				return
    83  			} else {
    84  				require.NoError(t, err)
    85  			}
    86  
    87  			has, err := updatedRoot.HasTable(ctx, tt.tableName)
    88  			require.NoError(t, err)
    89  			assert.False(t, has)
    90  			newTable, ok, err := updatedRoot.GetTable(ctx, tt.newTableName)
    91  			require.NoError(t, err)
    92  			require.True(t, ok)
    93  
    94  			sch, err := newTable.GetSchema(ctx)
    95  			require.NoError(t, err)
    96  			require.Equal(t, tt.expectedSchema, sch)
    97  
    98  			rowData, err := newTable.GetRowData(ctx)
    99  			require.NoError(t, err)
   100  			var foundRows []row.Row
   101  			err = rowData.Iter(ctx, func(key, value types.Value) (stop bool, err error) {
   102  				tpl, err := row.FromNoms(tt.expectedSchema, key.(types.Tuple), value.(types.Tuple))
   103  				foundRows = append(foundRows, tpl)
   104  				return false, err
   105  			})
   106  
   107  			assert.NoError(t, err)
   108  			assert.Equal(t, tt.expectedRows, foundRows)
   109  		})
   110  	}
   111  }