github.com/hasnat/dolt/go@v0.0.0-20210628190320-9eb5d843fbb7/store/merge/three_way_set_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 merge
    23  
    24  import (
    25  	"context"
    26  	"testing"
    27  
    28  	"github.com/stretchr/testify/suite"
    29  
    30  	"github.com/dolthub/dolt/go/store/types"
    31  )
    32  
    33  func TestThreeWaySetMerge(t *testing.T) {
    34  	suite.Run(t, &ThreeWaySetMergeSuite{})
    35  }
    36  
    37  type items []interface{}
    38  
    39  func (kv items) items() []interface{} {
    40  	return kv
    41  }
    42  
    43  type ThreeWaySetMergeSuite struct {
    44  	ThreeWayMergeSuite
    45  }
    46  
    47  func (s *ThreeWaySetMergeSuite) SetupSuite() {
    48  	s.create = func(i seq) (val types.Value, err error) {
    49  		if i != nil {
    50  			keyValues, err := valsToTypesValues(s.create, i.items()...)
    51  			s.NoError(err)
    52  			val, err = types.NewSet(context.Background(), s.vs, keyValues...)
    53  			s.NoError(err)
    54  		}
    55  
    56  		return val, nil
    57  	}
    58  	s.typeStr = "Set"
    59  }
    60  
    61  var (
    62  	flat  = items{"a1", "a2", "a3", "a4"}
    63  	flatA = items{"a1", "a2", "a5", "a6"}
    64  	flatB = items{"a1", "a4", "a7", "a5"}
    65  	flatM = items{"a1", "a5", "a6", "a7"}
    66  
    67  	ss1       = items{}
    68  	ss1a      = items{"k1", flatA, items{"a", 0}}
    69  	ss1b      = items{"k1", items{"a", 0}, flatB}
    70  	ss1Merged = items{"k1", items{"a", 0}, flatA, flatB}
    71  )
    72  
    73  func (s *ThreeWaySetMergeSuite) TestThreeWayMerge_DoNothing() {
    74  	s.tryThreeWayMerge(nil, nil, flat, flat)
    75  }
    76  
    77  func (s *ThreeWaySetMergeSuite) TestThreeWayMerge_Primitives() {
    78  	s.tryThreeWayMerge(flatA, flatB, flat, flatM)
    79  	s.tryThreeWayMerge(flatB, flatA, flat, flatM)
    80  }
    81  
    82  func (s *ThreeWaySetMergeSuite) TestThreeWayMerge_HandleEmpty() {
    83  	s.tryThreeWayMerge(ss1a, ss1b, ss1, ss1Merged)
    84  	s.tryThreeWayMerge(ss1b, ss1a, ss1, ss1Merged)
    85  }
    86  
    87  func (s *ThreeWaySetMergeSuite) TestThreeWayMerge_HandleNil() {
    88  	s.tryThreeWayMerge(ss1a, ss1b, nil, ss1Merged)
    89  	s.tryThreeWayMerge(ss1b, ss1a, nil, ss1Merged)
    90  }
    91  
    92  func (s *ThreeWaySetMergeSuite) TestThreeWayMerge_Refs() {
    93  	v, err := types.NewStruct(types.Format_7_18, "Foo", types.StructData{"life": types.Float(42)})
    94  	s.NoError(err)
    95  	strRef, err := s.vs.WriteValue(context.Background(), v)
    96  	s.NoError(err)
    97  
    98  	m := items{mustValue(s.vs.WriteValue(context.Background(), mustValue(s.create(flatA)))), mustValue(s.vs.WriteValue(context.Background(), mustValue(s.create(flatB))))}
    99  	ma := items{"r1", mustValue(s.vs.WriteValue(context.Background(), mustValue(s.create(flatA))))}
   100  	mb := items{"r1", strRef, mustValue(s.vs.WriteValue(context.Background(), mustValue(s.create(flatA))))}
   101  	mMerged := items{"r1", strRef, mustValue(s.vs.WriteValue(context.Background(), mustValue(s.create(flatA))))}
   102  
   103  	s.tryThreeWayMerge(ma, mb, m, mMerged)
   104  	s.tryThreeWayMerge(mb, ma, m, mMerged)
   105  }
   106  
   107  func (s *ThreeWaySetMergeSuite) TestThreeWayMerge_ImmediateConflict() {
   108  	s.tryThreeWayConflict(mustValue(types.NewMap(context.Background(), s.vs)), mustValue(s.create(ss1b)), mustValue(s.create(ss1)), "Cannot merge Map<> with "+s.typeStr)
   109  	s.tryThreeWayConflict(mustValue(s.create(ss1b)), mustValue(types.NewMap(context.Background(), s.vs)), mustValue(s.create(ss1)), "Cannot merge "+s.typeStr)
   110  }