github.com/hasnat/dolt/go@v0.0.0-20210628190320-9eb5d843fbb7/store/diff/patch_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  // This file incorporates work covered by the following copyright and
    16  // permission notice:
    17  //
    18  // Copyright 2016 Attic Labs, Inc. All rights reserved.
    19  // Licensed under the Apache License, version 2.0:
    20  // http://www.apache.org/licenses/LICENSE-2.0
    21  
    22  package diff
    23  
    24  import (
    25  	"math/rand"
    26  	"testing"
    27  
    28  	"github.com/stretchr/testify/assert"
    29  	"github.com/stretchr/testify/require"
    30  
    31  	"github.com/dolthub/dolt/go/store/types"
    32  )
    33  
    34  func TestPatchPathPartCompare(t *testing.T) {
    35  	assert := assert.New(t)
    36  
    37  	fieldPath1 := mustParsePath(assert, `.field1`)[0]
    38  	fieldPath2 := mustParsePath(assert, `.field2`)[0]
    39  	indexPath1 := mustParsePath(assert, `["field1"]`)[0]
    40  	indexPath2 := mustParsePath(assert, `["field2"]`)[0]
    41  	indexPathKey1 := mustParsePath(assert, `["field1"]@key`)[0]
    42  	indexPathKey2 := mustParsePath(assert, `["field2"]@key`)[0]
    43  	hashIndexPath1 := mustParsePath(assert, `[#01234567890123456789012345678901]`)[0]
    44  	hashIndexPath2 := mustParsePath(assert, `[#0123456789abcdef0123456789abcdef]`)[0]
    45  	hashIndexPathKey1 := mustParsePath(assert, `[#01234567890123456789012345678901]`)[0]
    46  	hashIndexPathKey2 := mustParsePath(assert, `[#0123456789abcdef0123456789abcdef]`)[0]
    47  
    48  	testCases := [][]types.PathPart{
    49  		{fieldPath1, fieldPath2},
    50  		{indexPath1, indexPath2},
    51  		{indexPathKey1, indexPathKey2},
    52  		{hashIndexPath1, hashIndexPath2},
    53  		{hashIndexPathKey1, hashIndexPathKey2},
    54  		{fieldPath2, indexPath1},
    55  		{fieldPath2, indexPathKey1},
    56  		{fieldPath2, hashIndexPath1},
    57  		{fieldPath2, hashIndexPathKey1},
    58  		{indexPath2, hashIndexPath1},
    59  		{indexPath2, hashIndexPathKey1},
    60  	}
    61  
    62  	for i, tc := range testCases {
    63  		res01, err := pathPartCompare(types.Format_7_18, tc[0], tc[1])
    64  		require.NoError(t, err)
    65  		res00, err := pathPartCompare(types.Format_7_18, tc[0], tc[0])
    66  		require.NoError(t, err)
    67  		res10, err := pathPartCompare(types.Format_7_18, tc[1], tc[0])
    68  		require.NoError(t, err)
    69  
    70  		assert.Equal(-1, res01, "test case %d failed, pp0: %s, pp1: %s", i, tc[0], tc[1])
    71  		assert.Equal(0, res00, "test case %d failed, pp0: %s, pp1: %s", i, tc[0], tc[1])
    72  		assert.Equal(1, res10, "test case %d failed, pp0: %s, pp1: %s", i, tc[0], tc[1])
    73  	}
    74  }
    75  
    76  func TestPatchPathIsLess(t *testing.T) {
    77  	assert := assert.New(t)
    78  
    79  	testCases := [][]string{
    80  		{``, `["field1"]`},
    81  		{`["field1"]`, `["field1"].f1`},
    82  		{`["field1"].f1`, `["field1"]["f1"]`},
    83  		{`["field1"]["f1"]@key`, `["field1"]["f1"]`},
    84  		{`["field1"]["f1"]`, `["field1"][#01234567890123456789012345678901]`},
    85  		{`["field1"][#01234567890123456789012345678901]`, `["field1"][#0123456789abcdef0123456789abcdef]`},
    86  	}
    87  
    88  	for i, tc := range testCases {
    89  		p0 := mustParsePath(assert, tc[0])
    90  		p1 := mustParsePath(assert, tc[1])
    91  		zeroLTOne, err := pathIsLess(types.Format_7_18, p0, p1)
    92  		require.NoError(t, err)
    93  		zeroLTZero, err := pathIsLess(types.Format_7_18, p0, p0)
    94  		require.NoError(t, err)
    95  		oneLTZero, err := pathIsLess(types.Format_7_18, p1, p0)
    96  		require.NoError(t, err)
    97  		assert.True(zeroLTOne, "test case %d failed", i)
    98  		assert.False(zeroLTZero, "test case %d failed", i)
    99  		assert.False(oneLTZero, "test case %d failed", i)
   100  	}
   101  	//p := mustParsePath(assert, `#0123456789abcdef0123456789abcdef.value`)
   102  	//fmt.Printf("p[0]: %s, type: %T\n", p[0], p[0])
   103  }
   104  
   105  func TestPatchSort(t *testing.T) {
   106  	assert := assert.New(t)
   107  
   108  	sortedPaths := Patch{
   109  		{Path: mustParsePath(assert, `["field1"]`)},
   110  		{Path: mustParsePath(assert, `["field1"].f1`)},
   111  		{Path: mustParsePath(assert, `["field1"]["f1"]`), ChangeType: types.DiffChangeRemoved},
   112  		{Path: mustParsePath(assert, `["field1"]["f1"]`), ChangeType: types.DiffChangeModified},
   113  		{Path: mustParsePath(assert, `["field1"]["f1"]`), ChangeType: types.DiffChangeAdded},
   114  		{Path: mustParsePath(assert, `["field1"][#01234567890123456789012345678901]`)},
   115  		{Path: mustParsePath(assert, `["field1"][#0123456789abcdef0123456789abcdef]`)},
   116  	}
   117  
   118  	rand.Perm(len(sortedPaths))
   119  	shuffledPaths := Patch{}
   120  	for _, idx := range rand.Perm(len(sortedPaths)) {
   121  		shuffledPaths = append(shuffledPaths, sortedPaths[idx])
   122  	}
   123  
   124  	types.SortWithErroringLess(PatchSort{shuffledPaths, types.Format_7_18})
   125  	assert.Equal(sortedPaths, shuffledPaths)
   126  }