github.com/dolthub/dolt/go@v0.40.5-0.20240520175717-68db7794bea6/store/cmd/noms/noms_blob_get_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 main
    23  
    24  import (
    25  	"bytes"
    26  	"context"
    27  	"fmt"
    28  	"os"
    29  	"path/filepath"
    30  	"testing"
    31  
    32  	"github.com/stretchr/testify/suite"
    33  
    34  	"github.com/dolthub/dolt/go/store/datas"
    35  	"github.com/dolthub/dolt/go/store/spec"
    36  	"github.com/dolthub/dolt/go/store/types"
    37  	"github.com/dolthub/dolt/go/store/util/clienttest"
    38  )
    39  
    40  func TestNomsBlobGet(t *testing.T) {
    41  	suite.Run(t, &nbeSuite{})
    42  }
    43  
    44  type nbeSuite struct {
    45  	clienttest.ClientTestSuite
    46  }
    47  
    48  func (s *nbeSuite) TestNomsBlobGet() {
    49  	sp, err := spec.ForDatabase(s.TempDir)
    50  	s.NoError(err)
    51  	defer sp.Close()
    52  	ctx := context.Background()
    53  	db := sp.GetDatabase(ctx)
    54  	vrw := sp.GetVRW(ctx)
    55  
    56  	blobBytes := []byte("hello")
    57  	blob, err := types.NewBlob(ctx, vrw, bytes.NewBuffer(blobBytes))
    58  	s.NoError(err)
    59  
    60  	ref, err := vrw.WriteValue(ctx, blob)
    61  	s.NoError(err)
    62  
    63  	ref, err = vrw.WriteValue(context.Background(), blob)
    64  	s.NoError(err)
    65  	ds, err := db.GetDataset(context.Background(), "datasetID")
    66  	s.NoError(err)
    67  	ds, err = db.GetDataset(context.Background(), "datasetID")
    68  	s.NoError(err)
    69  	_, err = datas.CommitValue(context.Background(), db, ds, ref)
    70  	s.NoError(err)
    71  
    72  	hashSpec := fmt.Sprintf("%s::#%s", s.TempDir, ref.TargetHash().String())
    73  	filePath := filepath.Join(s.TempDir, "out")
    74  	s.MustRun(main, []string{"blob", "export", hashSpec, filePath})
    75  
    76  	fileBytes, err := os.ReadFile(filePath)
    77  	s.NoError(err)
    78  	s.Equal(blobBytes, fileBytes)
    79  
    80  	stdout, _ := s.MustRun(main, []string{"blob", "export", hashSpec})
    81  	fmt.Println("stdout:", stdout)
    82  	s.Equal(blobBytes, []byte(stdout))
    83  }