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