github.com/hasnat/dolt/go@v0.0.0-20210628190320-9eb5d843fbb7/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  
    32  	"github.com/dolthub/dolt/go/store/chunks"
    33  	"github.com/dolthub/dolt/go/store/datas"
    34  	"github.com/dolthub/dolt/go/store/hash"
    35  	"github.com/dolthub/dolt/go/store/types"
    36  )
    37  
    38  func TestAbsolutePathToAndFromString(t *testing.T) {
    39  	assert := assert.New(t)
    40  
    41  	test := func(str string) {
    42  		p, err := NewAbsolutePath(str)
    43  		assert.NoError(err)
    44  		assert.Equal(str, p.String())
    45  	}
    46  
    47  	h, err := types.Float(42).Hash(types.Format_7_18) // arbitrary hash
    48  	assert.NoError(err)
    49  	test(fmt.Sprintf("foo.bar[#%s]", h.String()))
    50  	test(fmt.Sprintf("#%s.bar[42]", h.String()))
    51  }
    52  
    53  func TestAbsolutePaths(t *testing.T) {
    54  	assert := assert.New(t)
    55  	storage := &chunks.MemoryStorage{}
    56  	db := datas.NewDatabase(storage.NewView())
    57  
    58  	s0, s1 := types.String("foo"), types.String("bar")
    59  	list, err := types.NewList(context.Background(), db, s0, s1)
    60  	assert.NoError(err)
    61  	emptySet, err := types.NewSet(context.Background(), db)
    62  	assert.NoError(err)
    63  
    64  	_, err = db.WriteValue(context.Background(), s0)
    65  	assert.NoError(err)
    66  	_, err = db.WriteValue(context.Background(), s1)
    67  	assert.NoError(err)
    68  	_, err = db.WriteValue(context.Background(), list)
    69  	assert.NoError(err)
    70  	_, err = db.WriteValue(context.Background(), emptySet)
    71  	assert.NoError(err)
    72  
    73  	ds, err := db.GetDataset(context.Background(), "ds")
    74  	assert.NoError(err)
    75  	ds, err = db.CommitValue(context.Background(), ds, list)
    76  	assert.NoError(err)
    77  	head, hasHead := ds.MaybeHead()
    78  	assert.True(hasHead)
    79  
    80  	resolvesTo := func(exp types.Value, str string) {
    81  		p, err := NewAbsolutePath(str)
    82  		assert.NoError(err)
    83  		act := p.Resolve(context.Background(), db)
    84  		if exp == nil {
    85  			assert.Nil(act)
    86  		} else {
    87  			assert.True(exp.Equals(act), "%s Expected %s Actual %s", str, mustString(types.EncodedValue(context.Background(), exp)), mustString(types.EncodedValue(context.Background(), act)))
    88  		}
    89  	}
    90  
    91  	resolvesTo(head, "ds")
    92  	resolvesTo(emptySet, "ds.parents")
    93  	resolvesTo(list, "ds.value")
    94  	resolvesTo(s0, "ds.value[0]")
    95  	resolvesTo(s1, "ds.value[1]")
    96  	resolvesTo(head, "#"+mustHash(head.Hash(types.Format_7_18)).String())
    97  	resolvesTo(list, "#"+mustHash(list.Hash(types.Format_7_18)).String())
    98  	resolvesTo(s0, "#"+mustHash(s0.Hash(types.Format_7_18)).String())
    99  	resolvesTo(s1, "#"+mustHash(s1.Hash(types.Format_7_18)).String())
   100  	resolvesTo(s0, "#"+mustHash(list.Hash(types.Format_7_18)).String()+"[0]")
   101  	resolvesTo(s1, "#"+mustHash(list.Hash(types.Format_7_18)).String()+"[1]")
   102  
   103  	resolvesTo(nil, "foo")
   104  	resolvesTo(nil, "foo.parents")
   105  	resolvesTo(nil, "foo.value")
   106  	resolvesTo(nil, "foo.value[0]")
   107  	resolvesTo(nil, "#"+mustHash(types.String("baz").Hash(types.Format_7_18)).String())
   108  	resolvesTo(nil, "#"+mustHash(types.String("baz").Hash(types.Format_7_18)).String()+"[0]")
   109  }
   110  
   111  func TestReadAbsolutePaths(t *testing.T) {
   112  	assert := assert.New(t)
   113  	storage := &chunks.MemoryStorage{}
   114  	db := datas.NewDatabase(storage.NewView())
   115  
   116  	s0, s1 := types.String("foo"), types.String("bar")
   117  	list, err := types.NewList(context.Background(), db, s0, s1)
   118  	assert.NoError(err)
   119  
   120  	ds, err := db.GetDataset(context.Background(), "ds")
   121  	assert.NoError(err)
   122  	_, err = db.CommitValue(context.Background(), ds, list)
   123  	assert.NoError(err)
   124  
   125  	vals, err := ReadAbsolutePaths(context.Background(), db, "ds.value[0]", "ds.value[1]")
   126  	assert.NoError(err)
   127  
   128  	assert.Equal(2, len(vals))
   129  	assert.Equal("foo", string(vals[0].(types.String)))
   130  	assert.Equal("bar", string(vals[1].(types.String)))
   131  
   132  	vals, err = ReadAbsolutePaths(context.Background(), db, "!!#")
   133  	assert.Nil(vals)
   134  	assert.Equal("invalid input path '!!#'", err.Error())
   135  
   136  	vals, err = ReadAbsolutePaths(context.Background(), db, "invalid.monkey")
   137  	assert.Nil(vals)
   138  	assert.Equal("input path 'invalid.monkey' does not exist in database", err.Error())
   139  }
   140  
   141  func TestAbsolutePathParseErrors(t *testing.T) {
   142  	assert := assert.New(t)
   143  
   144  	test := func(path, errMsg string) {
   145  		p, err := NewAbsolutePath(path)
   146  		assert.Equal(AbsolutePath{}, p)
   147  		assert.Error(err)
   148  		assert.Equal(errMsg, err.Error())
   149  	}
   150  
   151  	test("", "empty path")
   152  	test(".foo", "invalid dataset name: .foo")
   153  	test(".foo.bar.baz", "invalid dataset name: .foo.bar.baz")
   154  	test("#", "invalid hash: ")
   155  	test("#abc", "invalid hash: abc")
   156  	invHash := strings.Repeat("z", hash.StringLen)
   157  	test("#"+invHash, "invalid hash: "+invHash)
   158  }