mirror of
https://github.com/dolthub/dolt.git
synced 2026-02-11 10:33:08 -06:00
This patch is the first step in moving all reading and writing to the DataStore API, so that we can validate data commited to Noms. The big change here is that types.ReadValue() no longer exists and is replaced with a ReadValue() method on DataStore. A similar WriteValue() method deprecates types.WriteValue(), but fully removing that is left for a later patch. Since a lot of code in the types package needs to read and write values, but cannot import the datas package without creating an import cycle, the types package exports ValueReader and ValueWriter interfaces, which DataStore implements. Thus, a DataStore can be passed to anything in the types package which needs to read or write values (e.g. a collection constructor or typed-ref) Relatedly, this patch also introduces the DataSink interface, so that some public-facing apis no longer need to provide a ChunkSink. Towards #654
141 lines
3.5 KiB
Go
141 lines
3.5 KiB
Go
package dataset
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/attic-labs/noms/chunks"
|
|
"github.com/attic-labs/noms/datas"
|
|
"github.com/attic-labs/noms/types"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func createTestDataset(name string) Dataset {
|
|
return NewDataset(datas.NewDataStore(chunks.NewTestStore()), name)
|
|
}
|
|
|
|
func TestValidateRef(t *testing.T) {
|
|
ds := createTestDataset("test")
|
|
r := ds.Store().WriteValue(types.Bool(true))
|
|
|
|
assert.Panics(t, func() { ds.validateRefAsCommit(r) })
|
|
}
|
|
|
|
func NewList(ds Dataset, vs ...types.Value) types.Ref {
|
|
v := types.NewList(vs...)
|
|
r := ds.Store().WriteValue(v)
|
|
return types.NewRef(r)
|
|
}
|
|
|
|
func NewMap(ds Dataset, vs ...types.Value) types.Ref {
|
|
v := types.NewMap(vs...)
|
|
r := ds.Store().WriteValue(v)
|
|
return types.NewRef(r)
|
|
}
|
|
|
|
func NewSet(ds Dataset, vs ...types.Value) types.Ref {
|
|
v := types.NewSet(vs...)
|
|
r := ds.Store().WriteValue(v)
|
|
return types.NewRef(r)
|
|
}
|
|
|
|
func pullTest(t *testing.T, topdown bool) {
|
|
assert := assert.New(t)
|
|
|
|
sink := createTestDataset("sink")
|
|
source := createTestDataset("source")
|
|
|
|
// Give sink and source some initial shared context.
|
|
sourceInitialValue := types.NewMap(
|
|
types.NewString("first"), NewList(source),
|
|
types.NewString("second"), NewList(source, types.Int32(2)))
|
|
sinkInitialValue := types.NewMap(
|
|
types.NewString("first"), NewList(sink),
|
|
types.NewString("second"), NewList(sink, types.Int32(2)))
|
|
|
|
var err error
|
|
source, err = source.Commit(sourceInitialValue)
|
|
assert.NoError(err)
|
|
sink, err = sink.Commit(sinkInitialValue)
|
|
assert.NoError(err)
|
|
|
|
// Add some new stuff to source.
|
|
updatedValue := sourceInitialValue.Set(
|
|
types.NewString("third"), NewList(source, types.Int32(3)))
|
|
source, err = source.Commit(updatedValue)
|
|
assert.NoError(err)
|
|
|
|
// Add some more stuff, so that source isn't directly ahead of sink.
|
|
updatedValue = updatedValue.Set(
|
|
types.NewString("fourth"), NewList(source, types.Int32(4)))
|
|
source, err = source.Commit(updatedValue)
|
|
assert.NoError(err)
|
|
|
|
sink, err = sink.pull(source.Store(), source.Head().Ref(), 1, topdown)
|
|
assert.NoError(err)
|
|
assert.True(source.Head().Equals(sink.Head()))
|
|
}
|
|
|
|
func TestPullTopDown(t *testing.T) {
|
|
pullTest(t, true)
|
|
}
|
|
|
|
func TestPullExclude(t *testing.T) {
|
|
pullTest(t, false)
|
|
}
|
|
|
|
func pullFirstCommit(t *testing.T, topdown bool) {
|
|
assert := assert.New(t)
|
|
|
|
sink := createTestDataset("sink")
|
|
source := createTestDataset("source")
|
|
|
|
sourceInitialValue := types.NewMap(
|
|
types.NewString("first"), NewList(source),
|
|
types.NewString("second"), NewList(source, types.Int32(2)))
|
|
|
|
NewList(sink)
|
|
NewList(sink, types.Int32(2))
|
|
|
|
source, err := source.Commit(sourceInitialValue)
|
|
assert.NoError(err)
|
|
|
|
sink, err = sink.pull(source.Store(), source.Head().Ref(), 1, topdown)
|
|
assert.NoError(err)
|
|
assert.True(source.Head().Equals(sink.Head()))
|
|
}
|
|
|
|
func TestPullFirstCommitTopDown(t *testing.T) {
|
|
pullFirstCommit(t, true)
|
|
}
|
|
|
|
func TestPullFirstCommitExclude(t *testing.T) {
|
|
pullFirstCommit(t, false)
|
|
}
|
|
|
|
func pullDeepRef(t *testing.T, topdown bool) {
|
|
assert := assert.New(t)
|
|
|
|
sink := createTestDataset("sink")
|
|
source := createTestDataset("source")
|
|
|
|
sourceInitialValue := types.NewList(
|
|
types.NewList(NewList(source)),
|
|
types.NewSet(NewSet(source)),
|
|
types.NewMap(NewMap(source), NewMap(source)))
|
|
|
|
source, err := source.Commit(sourceInitialValue)
|
|
assert.NoError(err)
|
|
|
|
sink, err = sink.pull(source.Store(), source.Head().Ref(), 1, topdown)
|
|
assert.NoError(err)
|
|
assert.True(source.Head().Equals(sink.Head()))
|
|
}
|
|
|
|
func TestPullDeepRefTopDown(t *testing.T) {
|
|
pullDeepRef(t, true)
|
|
}
|
|
|
|
func TestPullDeepRefExclude(t *testing.T) {
|
|
pullDeepRef(t, false)
|
|
}
|