github.com/dolthub/dolt/go@v0.40.5-0.20240520175717-68db7794bea6/libraries/doltcore/sqle/replication_test.go (about) 1 // Copyright 2021 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 sqle 16 17 import ( 18 "context" 19 "testing" 20 21 "github.com/dolthub/go-mysql-server/sql" 22 "github.com/stretchr/testify/assert" 23 "github.com/stretchr/testify/require" 24 "go.uber.org/zap/buffer" 25 26 "github.com/dolthub/dolt/go/libraries/doltcore/doltdb" 27 "github.com/dolthub/dolt/go/libraries/doltcore/ref" 28 "github.com/dolthub/dolt/go/libraries/doltcore/sqle/dsess" 29 ) 30 31 func TestCommitHooksNoErrors(t *testing.T) { 32 dEnv, err := CreateEnvWithSeedData() 33 require.NoError(t, err) 34 defer dEnv.DoltDB.Close() 35 36 sql.SystemVariables.SetGlobal(dsess.SkipReplicationErrors, true) 37 sql.SystemVariables.SetGlobal(dsess.ReplicateToRemote, "unknown") 38 bThreads := sql.NewBackgroundThreads() 39 hooks, err := GetCommitHooks(context.Background(), bThreads, dEnv, &buffer.Buffer{}) 40 assert.NoError(t, err) 41 if len(hooks) < 1 { 42 t.Error("failed to produce noop hook") 43 } else { 44 switch h := hooks[0].(type) { 45 case *doltdb.LogHook: 46 default: 47 t.Errorf("expected LogHook, found: %s", h) 48 } 49 } 50 } 51 52 func TestReplicationBranches(t *testing.T) { 53 tests := []struct { 54 remote []string 55 local []string 56 expToDelete []string 57 }{ 58 { 59 remote: []string{"main", "feature1", "feature2"}, 60 local: []string{"main", "feature1", "feature2"}, 61 expToDelete: []string{}, 62 }, 63 { 64 remote: []string{"main", "feature1"}, 65 local: []string{"main", "feature1", "feature2"}, 66 expToDelete: []string{"feature2"}, 67 }, 68 { 69 remote: []string{"main", "feature1", "feature2"}, 70 local: []string{"main", "feature1"}, 71 expToDelete: []string{}, 72 }, 73 { 74 remote: []string{"main", "feature1"}, 75 local: []string{"main", "feature2"}, 76 expToDelete: []string{"feature2"}, 77 }, 78 { 79 remote: []string{"main", "feature1", "feature2", "feature3"}, 80 local: []string{"feature4", "feature5", "feature6", "feature7", "feature8", "feature9"}, 81 expToDelete: []string{"feature4", "feature5", "feature6", "feature7", "feature8", "feature9"}, 82 }, 83 } 84 85 for _, tt := range tests { 86 remoteRefs := make([]doltdb.RefWithHash, len(tt.remote)) 87 for i := range tt.remote { 88 remoteRefs[i] = doltdb.RefWithHash{Ref: ref.NewRemoteRef("", tt.remote[i])} 89 } 90 localRefs := make([]doltdb.RefWithHash, len(tt.local)) 91 for i := range tt.local { 92 localRefs[i] = doltdb.RefWithHash{Ref: ref.NewBranchRef(tt.local[i])} 93 } 94 diff := refsToDelete(remoteRefs, localRefs) 95 diffNames := make([]string, len(diff)) 96 for i := range diff { 97 diffNames[i] = diff[i].Ref.GetPath() 98 } 99 assert.Equal(t, diffNames, tt.expToDelete) 100 } 101 }