github.com/jbendotnet/noms@v0.0.0-20190904222105-c43e4293ea92/cmd/noms/noms_show_test.go (about)

     1  // Copyright 2016 Attic Labs, Inc. All rights reserved.
     2  // Licensed under the Apache License, version 2.0:
     3  // http://www.apache.org/licenses/LICENSE-2.0
     4  
     5  package main
     6  
     7  import (
     8  	"fmt"
     9  	"testing"
    10  
    11  	"github.com/attic-labs/noms/go/chunks"
    12  	"github.com/attic-labs/noms/go/spec"
    13  	"github.com/attic-labs/noms/go/types"
    14  	"github.com/attic-labs/noms/go/util/clienttest"
    15  	"github.com/attic-labs/noms/go/util/test"
    16  	"github.com/stretchr/testify/suite"
    17  )
    18  
    19  func TestNomsShow(t *testing.T) {
    20  	suite.Run(t, &nomsShowTestSuite{})
    21  }
    22  
    23  type nomsShowTestSuite struct {
    24  	clienttest.ClientTestSuite
    25  }
    26  
    27  const (
    28  	res1 = "struct Commit {\n  meta: struct {},\n  parents: set {},\n  value: #nl181uu1ioc2j6t7mt9paidjlhlcjtgj,\n}\n"
    29  	res2 = "\"test string\"\n"
    30  	res3 = "struct Commit {\n  meta: struct {},\n  parents: set {\n    #4g7ggl6999v5mlucl4a507n7k3kvckiq,\n  },\n  value: #82adk7hfcudg8fktittm672to66t6qeu,\n}\n"
    31  	res4 = "[\n  \"elem1\",\n  2,\n  \"elem3\",\n]\n"
    32  	res5 = "struct Commit {\n  meta: struct {},\n  parents: set {\n    #3tmg89vabs2k6hotdock1kuo13j4lmqv,\n  },\n  value: #5cgfu2vk4nc21m1vjkjjpd2kvcm2df7q,\n}\n"
    33  )
    34  
    35  func (s *nomsShowTestSuite) spec(str string) spec.Spec {
    36  	sp, err := spec.ForDataset(str)
    37  	s.NoError(err)
    38  	return sp
    39  }
    40  func (s *nomsShowTestSuite) writeTestData(str string, value types.Value) types.Ref {
    41  	sp := s.spec(str)
    42  	defer sp.Close()
    43  
    44  	db := sp.GetDatabase()
    45  	r1 := db.WriteValue(value)
    46  	_, err := db.CommitValue(sp.GetDataset(), r1)
    47  	s.NoError(err)
    48  
    49  	return r1
    50  }
    51  
    52  func (s *nomsShowTestSuite) TestNomsShow() {
    53  	datasetName := "dsTest"
    54  	str := spec.CreateValueSpecString("nbs", s.DBDir, datasetName)
    55  
    56  	s1 := types.String("test string")
    57  	r := s.writeTestData(str, s1)
    58  	res, _ := s.MustRun(main, []string{"show", str})
    59  	s.Equal(res1, res)
    60  
    61  	str1 := spec.CreateValueSpecString("nbs", s.DBDir, "#"+r.TargetHash().String())
    62  	res, _ = s.MustRun(main, []string{"show", str1})
    63  	s.Equal(res2, res)
    64  
    65  	sp := s.spec(str)
    66  	defer sp.Close()
    67  	list := types.NewList(sp.GetDatabase(), types.String("elem1"), types.Number(2), types.String("elem3"))
    68  	r = s.writeTestData(str, list)
    69  	res, _ = s.MustRun(main, []string{"show", str})
    70  	test.EqualsIgnoreHashes(s.T(), res3, res)
    71  
    72  	str1 = spec.CreateValueSpecString("nbs", s.DBDir, "#"+r.TargetHash().String())
    73  	res, _ = s.MustRun(main, []string{"show", str1})
    74  	s.Equal(res4, res)
    75  
    76  	_ = s.writeTestData(str, s1)
    77  	res, _ = s.MustRun(main, []string{"show", str})
    78  	test.EqualsIgnoreHashes(s.T(), res5, res)
    79  }
    80  
    81  func (s *nomsShowTestSuite) TestNomsShowNotFound() {
    82  	str := spec.CreateValueSpecString("nbs", s.DBDir, "not-there")
    83  	stdout, stderr, err := s.Run(main, []string{"show", str})
    84  	s.Equal("", stdout)
    85  	s.Equal(fmt.Sprintf("Value not found: %s\n", str), stderr)
    86  	s.Nil(err)
    87  }
    88  
    89  func (s *nomsShowTestSuite) TestNomsShowRaw() {
    90  	datasetName := "showRaw"
    91  	str := spec.CreateValueSpecString("nbs", s.DBDir, datasetName)
    92  	sp, err := spec.ForDataset(str)
    93  	s.NoError(err)
    94  	defer sp.Close()
    95  
    96  	db := sp.GetDatabase()
    97  
    98  	// Put a value into the db, get its raw serialization, then deserialize it and ensure it comes
    99  	// out to same thing.
   100  	test := func(in types.Value) {
   101  		r1 := db.WriteValue(in)
   102  		db.CommitValue(sp.GetDataset(), r1)
   103  		res, _ := s.MustRun(main, []string{"show", "--raw",
   104  			spec.CreateValueSpecString("nbs", s.DBDir, "#"+r1.TargetHash().String())})
   105  		ch := chunks.NewChunk([]byte(res))
   106  		out := types.DecodeValue(ch, db)
   107  		s.True(out.Equals(in))
   108  	}
   109  
   110  	// Primitive value with no child chunks
   111  	test(types.String("hello"))
   112  
   113  	// Ref (one child chunk)
   114  	test(db.WriteValue(types.Number(42)))
   115  
   116  	// Prolly tree with multiple child chunks
   117  	items := make([]types.Value, 10000)
   118  	for i := 0; i < len(items); i++ {
   119  		items[i] = types.Number(i)
   120  	}
   121  	l := types.NewList(db, items...)
   122  	numChildChunks := 0
   123  	l.WalkRefs(func(r types.Ref) {
   124  		numChildChunks++
   125  	})
   126  	s.True(numChildChunks > 0)
   127  	test(l)
   128  }