github.com/hasnat/dolt/go@v0.0.0-20210628190320-9eb5d843fbb7/libraries/doltcore/diff/schema_diff_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 diff
    16  
    17  import (
    18  	"reflect"
    19  	"testing"
    20  
    21  	"github.com/stretchr/testify/require"
    22  
    23  	"github.com/dolthub/dolt/go/libraries/doltcore/schema"
    24  	"github.com/dolthub/dolt/go/store/types"
    25  )
    26  
    27  func TestDiffSchemas(t *testing.T) {
    28  	oldCols := []schema.Column{
    29  		schema.NewColumn("unchanged", 0, types.StringKind, true, schema.NotNullConstraint{}),
    30  		schema.NewColumn("dropped", 1, types.StringKind, true),
    31  		schema.NewColumn("renamed", 2, types.StringKind, false),
    32  		schema.NewColumn("type_changed", 3, types.StringKind, false),
    33  		schema.NewColumn("moved_to_pk", 4, types.StringKind, false),
    34  		schema.NewColumn("contraint_added", 5, types.StringKind, false),
    35  	}
    36  
    37  	newCols := []schema.Column{
    38  		schema.NewColumn("unchanged", 0, types.StringKind, true, schema.NotNullConstraint{}),
    39  		schema.NewColumn("renamed_new", 2, types.StringKind, false),
    40  		schema.NewColumn("type_changed", 3, types.IntKind, false),
    41  		schema.NewColumn("moved_to_pk", 4, types.StringKind, true),
    42  		schema.NewColumn("contraint_added", 5, types.StringKind, false, schema.NotNullConstraint{}),
    43  		schema.NewColumn("added", 6, types.StringKind, false),
    44  	}
    45  
    46  	oldColColl := schema.NewColCollection(oldCols...)
    47  	newColColl := schema.NewColCollection(newCols...)
    48  
    49  	oldSch, err := schema.SchemaFromCols(oldColColl)
    50  	require.NoError(t, err)
    51  	newSch, err := schema.SchemaFromCols(newColColl)
    52  	require.NoError(t, err)
    53  	diffs, _ := DiffSchColumns(oldSch, newSch)
    54  
    55  	expected := map[uint64]ColumnDifference{
    56  		0: {SchDiffNone, 0, &oldCols[0], &newCols[0]},
    57  		1: {SchDiffRemoved, 1, &oldCols[1], nil},
    58  		2: {SchDiffModified, 2, &oldCols[2], &newCols[1]},
    59  		3: {SchDiffModified, 3, &oldCols[3], &newCols[2]},
    60  		4: {SchDiffModified, 4, &oldCols[4], &newCols[3]},
    61  		5: {SchDiffModified, 5, &oldCols[5], &newCols[4]},
    62  		6: {SchDiffAdded, 6, nil, &newCols[5]},
    63  	}
    64  
    65  	if !reflect.DeepEqual(diffs, expected) {
    66  		t.Error(diffs, "!=", expected)
    67  	}
    68  }