github.com/dolthub/dolt/go@v0.40.5-0.20240520175717-68db7794bea6/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 "context" 26 "math/rand" 27 "testing" 28 29 "github.com/stretchr/testify/assert" 30 "github.com/stretchr/testify/require" 31 32 "github.com/dolthub/dolt/go/store/types" 33 ) 34 35 func TestPatchPathPartCompare(t *testing.T) { 36 assert := assert.New(t) 37 ctx := context.Background() 38 vs := newTestValueStore() 39 defer vs.Close() 40 41 fieldPath1 := mustParsePath(assert, `.field1`)[0] 42 fieldPath2 := mustParsePath(assert, `.field2`)[0] 43 indexPath1 := mustParsePath(assert, `["field1"]`)[0] 44 indexPath2 := mustParsePath(assert, `["field2"]`)[0] 45 indexPathKey1 := mustParsePath(assert, `["field1"]@key`)[0] 46 indexPathKey2 := mustParsePath(assert, `["field2"]@key`)[0] 47 hashIndexPath1 := mustParsePath(assert, `[#01234567890123456789012345678901]`)[0] 48 hashIndexPath2 := mustParsePath(assert, `[#0123456789abcdef0123456789abcdef]`)[0] 49 hashIndexPathKey1 := mustParsePath(assert, `[#01234567890123456789012345678901]`)[0] 50 hashIndexPathKey2 := mustParsePath(assert, `[#0123456789abcdef0123456789abcdef]`)[0] 51 52 testCases := [][]types.PathPart{ 53 {fieldPath1, fieldPath2}, 54 {indexPath1, indexPath2}, 55 {indexPathKey1, indexPathKey2}, 56 {hashIndexPath1, hashIndexPath2}, 57 {hashIndexPathKey1, hashIndexPathKey2}, 58 {fieldPath2, indexPath1}, 59 {fieldPath2, indexPathKey1}, 60 {fieldPath2, hashIndexPath1}, 61 {fieldPath2, hashIndexPathKey1}, 62 {indexPath2, hashIndexPath1}, 63 {indexPath2, hashIndexPathKey1}, 64 } 65 66 for i, tc := range testCases { 67 res01, err := pathPartCompare(ctx, vs.Format(), tc[0], tc[1]) 68 require.NoError(t, err) 69 res00, err := pathPartCompare(ctx, vs.Format(), tc[0], tc[0]) 70 require.NoError(t, err) 71 res10, err := pathPartCompare(ctx, vs.Format(), tc[1], tc[0]) 72 require.NoError(t, err) 73 74 assert.Equal(-1, res01, "test case %d failed, pp0: %s, pp1: %s", i, tc[0], tc[1]) 75 assert.Equal(0, res00, "test case %d failed, pp0: %s, pp1: %s", i, tc[0], tc[1]) 76 assert.Equal(1, res10, "test case %d failed, pp0: %s, pp1: %s", i, tc[0], tc[1]) 77 } 78 } 79 80 func TestPatchPathIsLess(t *testing.T) { 81 assert := assert.New(t) 82 ctx := context.Background() 83 vs := newTestValueStore() 84 defer vs.Close() 85 86 testCases := [][]string{ 87 {``, `["field1"]`}, 88 {`["field1"]`, `["field1"].f1`}, 89 {`["field1"].f1`, `["field1"]["f1"]`}, 90 {`["field1"]["f1"]@key`, `["field1"]["f1"]`}, 91 {`["field1"]["f1"]`, `["field1"][#01234567890123456789012345678901]`}, 92 {`["field1"][#01234567890123456789012345678901]`, `["field1"][#0123456789abcdef0123456789abcdef]`}, 93 } 94 95 for i, tc := range testCases { 96 p0 := mustParsePath(assert, tc[0]) 97 p1 := mustParsePath(assert, tc[1]) 98 zeroLTOne, err := pathIsLess(ctx, vs.Format(), p0, p1) 99 require.NoError(t, err) 100 zeroLTZero, err := pathIsLess(ctx, vs.Format(), p0, p0) 101 require.NoError(t, err) 102 oneLTZero, err := pathIsLess(ctx, vs.Format(), p1, p0) 103 require.NoError(t, err) 104 assert.True(zeroLTOne, "test case %d failed", i) 105 assert.False(zeroLTZero, "test case %d failed", i) 106 assert.False(oneLTZero, "test case %d failed", i) 107 } 108 //p := mustParsePath(assert, `#0123456789abcdef0123456789abcdef.value`) 109 //fmt.Printf("p[0]: %s, type: %T\n", p[0], p[0]) 110 } 111 112 func TestPatchSort(t *testing.T) { 113 assert := assert.New(t) 114 ctx := context.Background() 115 vs := newTestValueStore() 116 defer vs.Close() 117 118 sortedPaths := Patch{ 119 {Path: mustParsePath(assert, `["field1"]`)}, 120 {Path: mustParsePath(assert, `["field1"].f1`)}, 121 {Path: mustParsePath(assert, `["field1"]["f1"]`), ChangeType: types.DiffChangeRemoved}, 122 {Path: mustParsePath(assert, `["field1"]["f1"]`), ChangeType: types.DiffChangeModified}, 123 {Path: mustParsePath(assert, `["field1"]["f1"]`), ChangeType: types.DiffChangeAdded}, 124 {Path: mustParsePath(assert, `["field1"][#01234567890123456789012345678901]`)}, 125 {Path: mustParsePath(assert, `["field1"][#0123456789abcdef0123456789abcdef]`)}, 126 } 127 128 rand.Perm(len(sortedPaths)) 129 shuffledPaths := Patch{} 130 for _, idx := range rand.Perm(len(sortedPaths)) { 131 shuffledPaths = append(shuffledPaths, sortedPaths[idx]) 132 } 133 134 types.SortWithErroringLess(ctx, vs.Format(), PatchSort{shuffledPaths}) 135 assert.Equal(sortedPaths, shuffledPaths) 136 }