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