github.com/hasnat/dolt/go@v0.0.0-20210628190320-9eb5d843fbb7/store/valuefile/value_file_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 valuefile
    16  
    17  import (
    18  	"context"
    19  	"os"
    20  	"path/filepath"
    21  	"testing"
    22  
    23  	"github.com/google/uuid"
    24  	"github.com/stretchr/testify/require"
    25  
    26  	"github.com/dolthub/dolt/go/store/types"
    27  )
    28  
    29  func TestReadWriteValueFile(t *testing.T) {
    30  	const numMaps = 1
    31  	const numMapValues = 1
    32  
    33  	ctx := context.Background()
    34  	store, err := NewFileValueStore(types.Format_Default)
    35  	require.NoError(t, err)
    36  
    37  	var values []types.Value
    38  	for i := 0; i < numMaps; i++ {
    39  		var kvs []types.Value
    40  		for j := 0; j < numMapValues; j++ {
    41  			kvs = append(kvs, types.Int(j), types.String(uuid.New().String()))
    42  		}
    43  		m, err := types.NewMap(ctx, store, kvs...)
    44  		require.NoError(t, err)
    45  
    46  		values = append(values, m)
    47  	}
    48  
    49  	path := filepath.Join(os.TempDir(), "file.nvf")
    50  	err = WriteValueFile(ctx, path, store, values...)
    51  	require.NoError(t, err)
    52  
    53  	results, err := ReadValueFile(ctx, path)
    54  	require.NoError(t, err)
    55  	require.Equal(t, len(values), len(results))
    56  
    57  	for i := 0; i < len(values); i++ {
    58  		require.True(t, values[i].Equals(results[i]))
    59  	}
    60  }