github.com/hasnat/dolt/go@v0.0.0-20210628190320-9eb5d843fbb7/store/cmd/noms/noms_show_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 "fmt" 27 "testing" 28 29 "github.com/stretchr/testify/suite" 30 31 "github.com/dolthub/dolt/go/store/chunks" 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 "github.com/dolthub/dolt/go/store/util/test" 36 ) 37 38 func TestNomsShow(t *testing.T) { 39 suite.Run(t, &nomsShowTestSuite{}) 40 } 41 42 type nomsShowTestSuite struct { 43 clienttest.ClientTestSuite 44 } 45 46 const ( 47 res1 = "struct Commit {\n meta: struct {},\n parents: set {},\n parents_list: [],\n value: #nl181uu1ioc2j6t7mt9paidjlhlcjtgj,\n}" 48 res2 = "\"test string\"" 49 res3 = "struct Commit {\n meta: struct {},\n parents: set {\n #4g7ggl6999v5mlucl4a507n7k3kvckiq,\n },\n parents_list: [\n #4g7ggl6999v5mlucl4a507n7k3kvckiq,\n ],\n value: #82adk7hfcudg8fktittm672to66t6qeu,\n}" 50 res4 = "[\n \"elem1\",\n 2,\n \"elem3\",\n]" 51 res5 = "struct Commit {\n meta: struct {},\n parents: set {\n #3tmg89vabs2k6hotdock1kuo13j4lmqv,\n },\n parents_list: [\n #3tmg89vabs2k6hotdock1kuo13j4lmqv,\n ],\n value: #5cgfu2vk4nc21m1vjkjjpd2kvcm2df7q,\n}" 52 ) 53 54 func (s *nomsShowTestSuite) spec(str string) spec.Spec { 55 sp, err := spec.ForDataset(str) 56 s.NoError(err) 57 return sp 58 } 59 func (s *nomsShowTestSuite) writeTestData(str string, value types.Value) types.Ref { 60 sp := s.spec(str) 61 defer sp.Close() 62 63 db := sp.GetDatabase(context.Background()) 64 r1, err := db.WriteValue(context.Background(), value) 65 s.NoError(err) 66 _, err = db.CommitValue(context.Background(), sp.GetDataset(context.Background()), r1) 67 s.NoError(err) 68 69 return r1 70 } 71 72 func (s *nomsShowTestSuite) TestNomsShow() { 73 datasetName := "dsTest" 74 str := spec.CreateValueSpecString("nbs", s.DBDir, datasetName) 75 76 s1 := types.String("test string") 77 r := s.writeTestData(str, s1) 78 res, _ := s.MustRun(main, []string{"show", str}) 79 s.Equal(res1, res) 80 81 str1 := spec.CreateValueSpecString("nbs", s.DBDir, "#"+r.TargetHash().String()) 82 res, _ = s.MustRun(main, []string{"show", str1}) 83 s.Equal(res2, res) 84 85 sp := s.spec(str) 86 defer sp.Close() 87 list, err := types.NewList(context.Background(), sp.GetDatabase(context.Background()), types.String("elem1"), types.Float(2), types.String("elem3")) 88 s.NoError(err) 89 r = s.writeTestData(str, list) 90 res, _ = s.MustRun(main, []string{"show", str}) 91 test.EqualsIgnoreHashes(s.T(), res3, res) 92 93 str1 = spec.CreateValueSpecString("nbs", s.DBDir, "#"+r.TargetHash().String()) 94 res, _ = s.MustRun(main, []string{"show", str1}) 95 s.Equal(res4, res) 96 97 _ = s.writeTestData(str, s1) 98 res, _ = s.MustRun(main, []string{"show", str}) 99 test.EqualsIgnoreHashes(s.T(), res5, res) 100 } 101 102 func (s *nomsShowTestSuite) TestNomsShowNotFound() { 103 str := spec.CreateValueSpecString("nbs", s.DBDir, "not-there") 104 stdout, stderr, err := s.Run(main, []string{"show", str}) 105 s.Equal("", stdout) 106 s.Equal(fmt.Sprintf("Object not found: %s\n", str), stderr) 107 s.Nil(err) 108 } 109 110 func (s *nomsShowTestSuite) TestNomsShowRaw() { 111 datasetName := "showRaw" 112 str := spec.CreateValueSpecString("nbs", s.DBDir, datasetName) 113 sp, err := spec.ForDataset(str) 114 s.NoError(err) 115 defer sp.Close() 116 117 db := sp.GetDatabase(context.Background()) 118 119 // Put a value into the db, get its raw serialization, then deserialize it and ensure it comes 120 // out to same thing. 121 test := func(in types.Value) { 122 r1, err := db.WriteValue(context.Background(), in) 123 s.NoError(err) 124 db.CommitValue(context.Background(), sp.GetDataset(context.Background()), r1) 125 res, _ := s.MustRun(main, []string{"show", "--raw", 126 spec.CreateValueSpecString("nbs", s.DBDir, "#"+r1.TargetHash().String())}) 127 ch := chunks.NewChunk([]byte(res)) 128 out, err := types.DecodeValue(ch, db) 129 s.NoError(err) 130 s.True(out.Equals(in)) 131 } 132 133 // Primitive value with no child chunks 134 test(types.String("hello")) 135 136 // Ref (one child chunk) 137 test(mustValue(db.WriteValue(context.Background(), types.Float(42)))) 138 139 // Prolly tree with multiple child chunks 140 items := make([]types.Value, 10000) 141 for i := 0; i < len(items); i++ { 142 items[i] = types.Float(i) 143 } 144 l, err := types.NewList(context.Background(), db, items...) 145 s.NoError(err) 146 147 numChildChunks := 0 148 _ = l.WalkRefs(db.Format(), func(r types.Ref) error { 149 numChildChunks++ 150 return nil 151 }) 152 s.True(numChildChunks > 0) 153 test(l) 154 }