github.com/dolthub/dolt/go@v0.40.5-0.20240520175717-68db7794bea6/store/prolly/tree/content_address_test.go (about)

     1  // Copyright 2022 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 tree
    16  
    17  import (
    18  	"context"
    19  	"testing"
    20  
    21  	"github.com/stretchr/testify/assert"
    22  	"github.com/stretchr/testify/require"
    23  
    24  	"github.com/dolthub/dolt/go/store/hash"
    25  	"github.com/dolthub/dolt/go/store/prolly/message"
    26  	"github.com/dolthub/dolt/go/store/val"
    27  )
    28  
    29  var goldenHash = hash.Hash{
    30  	0x33, 0xba, 0x6a, 0x18, 0xcb,
    31  	0xcb, 0xa7, 0x41, 0x4a, 0xdb,
    32  	0x1e, 0x3d, 0xbf, 0x3f, 0x1e,
    33  	0xea, 0x7d, 0x47, 0x69, 0x6c,
    34  }
    35  
    36  // todo(andy): need an analogous test in pkg prolly
    37  func TestContentAddress(t *testing.T) {
    38  	tups, _ := AscendingUintTuples(12345)
    39  	m := makeTree(t, tups)
    40  	require.NotNil(t, m)
    41  	require.Equal(t, goldenHash, m.HashOf())
    42  	tc, err := m.TreeCount()
    43  	require.NoError(t, err)
    44  	assert.Equal(t, 12345, tc)
    45  }
    46  
    47  func makeTree(t *testing.T, tuples [][2]val.Tuple) Node {
    48  	ctx := context.Background()
    49  	ns := NewTestNodeStore()
    50  
    51  	// todo(andy): move this test
    52  	s := message.NewProllyMapSerializer(val.TupleDesc{}, ns.Pool())
    53  	chunker, err := newEmptyChunker(ctx, ns, s)
    54  	require.NoError(t, err)
    55  	for _, pair := range tuples {
    56  		err := chunker.AddPair(ctx, Item(pair[0]), Item(pair[1]))
    57  		require.NoError(t, err)
    58  	}
    59  	root, err := chunker.Done(ctx)
    60  	require.NoError(t, err)
    61  	return root
    62  }