github.com/dolthub/dolt/go@v0.40.5-0.20240520175717-68db7794bea6/store/spec/absolute_path_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 spec
    23  
    24  import (
    25  	"context"
    26  	"fmt"
    27  	"strings"
    28  	"testing"
    29  
    30  	"github.com/stretchr/testify/assert"
    31  	"github.com/stretchr/testify/require"
    32  
    33  	"github.com/dolthub/dolt/go/store/chunks"
    34  	"github.com/dolthub/dolt/go/store/datas"
    35  	"github.com/dolthub/dolt/go/store/hash"
    36  	"github.com/dolthub/dolt/go/store/prolly/tree"
    37  	"github.com/dolthub/dolt/go/store/types"
    38  )
    39  
    40  func TestAbsolutePathToAndFromString(t *testing.T) {
    41  	assert := assert.New(t)
    42  
    43  	test := func(str string) {
    44  		p, err := NewAbsolutePath(str)
    45  		assert.NoError(err)
    46  		assert.Equal(str, p.String())
    47  	}
    48  
    49  	h, err := types.Float(42).Hash(types.Format_Default) // arbitrary hash
    50  	assert.NoError(err)
    51  	test("/refs/heads/main")
    52  	test(fmt.Sprintf("#%s", h.String()))
    53  }
    54  
    55  func TestAbsolutePaths(t *testing.T) {
    56  	assert := assert.New(t)
    57  	storage := &chunks.MemoryStorage{}
    58  	cs := storage.NewView()
    59  	vs := types.NewValueStore(cs)
    60  	db := datas.NewTypesDatabase(vs, tree.NewNodeStore(cs))
    61  
    62  	s0, s1 := types.String("foo"), types.String("bar")
    63  	list, err := types.NewList(context.Background(), vs, s0, s1)
    64  	assert.NoError(err)
    65  	emptySet, err := types.NewSet(context.Background(), vs)
    66  	assert.NoError(err)
    67  
    68  	_, err = vs.WriteValue(context.Background(), s0)
    69  	assert.NoError(err)
    70  	_, err = vs.WriteValue(context.Background(), s1)
    71  	assert.NoError(err)
    72  	_, err = vs.WriteValue(context.Background(), list)
    73  	assert.NoError(err)
    74  	_, err = vs.WriteValue(context.Background(), emptySet)
    75  	assert.NoError(err)
    76  
    77  	ds, err := db.GetDataset(context.Background(), "ds")
    78  	assert.NoError(err)
    79  	ds, err = datas.CommitValue(context.Background(), db, ds, list)
    80  	assert.NoError(err)
    81  	head, hasHead := ds.MaybeHead()
    82  	assert.True(hasHead)
    83  
    84  	resolvesTo := func(exp types.Value, str string) {
    85  		p, err := NewAbsolutePath(str)
    86  		assert.NoError(err)
    87  		act, err := p.Resolve(context.Background(), db, vs)
    88  		assert.NoError(err)
    89  		if exp == nil {
    90  			assert.Nil(act)
    91  		} else {
    92  			assert.True(exp.Equals(act), "%s Expected %s Actual %s", str, mustString(types.EncodedValue(context.Background(), exp)), mustString(types.EncodedValue(context.Background(), act)))
    93  		}
    94  	}
    95  
    96  	resolvesTo(head, "ds")
    97  	resolvesTo(head, "#"+mustHash(head.Hash(vs.Format())).String())
    98  	resolvesTo(list, "#"+mustHash(list.Hash(vs.Format())).String())
    99  	resolvesTo(s0, "#"+mustHash(s0.Hash(vs.Format())).String())
   100  	resolvesTo(s1, "#"+mustHash(s1.Hash(vs.Format())).String())
   101  
   102  	resolvesTo(nil, "foo")
   103  	resolvesTo(nil, "#"+mustHash(types.String("baz").Hash(vs.Format())).String())
   104  	resolvesTo(nil, "#"+mustHash(types.String("baz").Hash(vs.Format())).String()+"[0]")
   105  }
   106  
   107  func TestAbsolutePathParseErrors(t *testing.T) {
   108  	test := func(path, errMsg string) {
   109  		p, err := NewAbsolutePath(path)
   110  		assert.Equal(t, AbsolutePath{}, p)
   111  		require.Error(t, err)
   112  		assert.Equal(t, errMsg, err.Error())
   113  	}
   114  
   115  	test("", "empty path")
   116  	test("#", "invalid hash: ")
   117  	test("#abc", "invalid hash: abc")
   118  	invHash := strings.Repeat("z", hash.StringLen)
   119  	test("#"+invHash, "invalid hash: "+invHash)
   120  }