github.com/dolthub/dolt/go@v0.40.5-0.20240520175717-68db7794bea6/store/cmd/noms/noms_ds_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  	"context"
    26  	"testing"
    27  
    28  	"github.com/stretchr/testify/suite"
    29  
    30  	"github.com/dolthub/dolt/go/store/datas"
    31  	"github.com/dolthub/dolt/go/store/nbs"
    32  	"github.com/dolthub/dolt/go/store/spec"
    33  	"github.com/dolthub/dolt/go/store/types"
    34  	"github.com/dolthub/dolt/go/store/util/clienttest"
    35  )
    36  
    37  func TestDs(t *testing.T) {
    38  	suite.Run(t, &nomsDsTestSuite{})
    39  }
    40  
    41  type nomsDsTestSuite struct {
    42  	clienttest.ClientTestSuite
    43  }
    44  
    45  func (s *nomsDsTestSuite) TestEmptyNomsDs() {
    46  	dir := s.DBDir
    47  
    48  	cs, err := nbs.NewLocalStore(context.Background(), types.Format_Default.VersionString(), dir, clienttest.DefaultMemTableSize, nbs.NewUnlimitedMemQuotaProvider())
    49  	s.NoError(err)
    50  	ds := datas.NewDatabase(cs)
    51  
    52  	ds.Close()
    53  
    54  	dbSpec := spec.CreateDatabaseSpecString("nbs", dir)
    55  	rtnVal, _ := s.MustRun(main, []string{"ds", dbSpec})
    56  	s.Equal("", rtnVal)
    57  }
    58  
    59  func (s *nomsDsTestSuite) TestNomsDs() {
    60  	dir := s.DBDir
    61  
    62  	cs, err := nbs.NewLocalStore(context.Background(), types.Format_Default.VersionString(), dir, clienttest.DefaultMemTableSize, nbs.NewUnlimitedMemQuotaProvider())
    63  
    64  	var golden1, golden2 string
    65  	switch types.Format_Default {
    66  	case types.Format_DOLT:
    67  		golden1 = "c7g244286kom2a1326kkgs85pi97cjs7"
    68  		golden2 = "rn7dsl1146qr2n4chtg41n24n0jqgnte"
    69  	case types.Format_LD_1:
    70  		golden1 = "oetp3jigkp5pid2f5c4mknpo17mso31b"
    71  		golden2 = "tsbj1qq88llk3k8qqqb5n3188sbpiu7r"
    72  	default:
    73  		s.Fail("no golden values exist for NBF %s", types.Format_Default.VersionString())
    74  	}
    75  
    76  	s.NoError(err)
    77  	db := datas.NewDatabase(cs)
    78  
    79  	id := "testdataset"
    80  	set, err := db.GetDataset(context.Background(), id)
    81  	s.NoError(err)
    82  	set, err = datas.CommitValue(context.Background(), db, set, types.String("Commit Value"))
    83  	s.NoError(err)
    84  
    85  	id2 := "testdataset2"
    86  	set2, err := db.GetDataset(context.Background(), id2)
    87  	s.NoError(err)
    88  	set2, err = datas.CommitValue(context.Background(), db, set2, types.String("Commit Value2"))
    89  	s.NoError(err)
    90  
    91  	err = db.Close()
    92  	s.NoError(err)
    93  
    94  	dbSpec := spec.CreateDatabaseSpecString("nbs", dir)
    95  	datasetName := spec.CreateValueSpecString("nbs", dir, id)
    96  	dataset2Name := spec.CreateValueSpecString("nbs", dir, id2)
    97  
    98  	// both datasets show up
    99  	rtnVal, _ := s.MustRun(main, []string{"ds", dbSpec})
   100  	s.Equal(id+"\n"+id2+"\n", rtnVal)
   101  
   102  	// both datasets again, to make sure printing doesn't change them
   103  	rtnVal, _ = s.MustRun(main, []string{"ds", dbSpec})
   104  	s.Equal(id+"\n"+id2+"\n", rtnVal)
   105  
   106  	// delete one dataset, print message at delete
   107  	rtnVal, _ = s.MustRun(main, []string{"ds", "-d", datasetName})
   108  	s.Equal("Deleted "+datasetName+" (was #"+golden1+")\n", rtnVal)
   109  
   110  	// print datasets, just one left
   111  	rtnVal, _ = s.MustRun(main, []string{"ds", dbSpec})
   112  	s.Equal(id2+"\n", rtnVal)
   113  
   114  	// delete the second dataset
   115  	rtnVal, _ = s.MustRun(main, []string{"ds", "-d", dataset2Name})
   116  	s.Equal("Deleted "+dataset2Name+" (was #"+golden2+")\n", rtnVal)
   117  
   118  	// print datasets, none left
   119  	rtnVal, _ = s.MustRun(main, []string{"ds", dbSpec})
   120  	s.Equal("", rtnVal)
   121  }