Files
dolt/chunks/test_utils.go
Chris Masone 119a56c3a9 Move ReadValue and WriteValue to DataStore
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
2016-03-17 12:57:44 -07:00

70 lines
1.4 KiB
Go

package chunks
import (
"sync"
"github.com/attic-labs/noms/ref"
"github.com/stretchr/testify/assert"
)
func assertInputInStore(input string, ref ref.Ref, s ChunkStore, assert *assert.Assertions) {
chunk := s.Get(ref)
assert.False(chunk.IsEmpty(), "Shouldn't get empty chunk for %s", ref.String())
assert.Equal(input, string(chunk.Data()))
}
func assertInputNotInStore(input string, ref ref.Ref, s ChunkStore, assert *assert.Assertions) {
data := s.Get(ref)
assert.Nil(data, "Shouldn't have gotten data for %s", ref.String())
}
type TestStore struct {
MemoryStore
Reads int
Hases int
Writes int
}
func NewTestStore() *TestStore {
return &TestStore{
MemoryStore: MemoryStore{
mu: &sync.Mutex{},
},
}
}
func (s *TestStore) Get(ref ref.Ref) Chunk {
s.Reads++
return s.MemoryStore.Get(ref)
}
func (s *TestStore) Has(ref ref.Ref) bool {
s.Hases++
return s.MemoryStore.Has(ref)
}
func (s *TestStore) Put(c Chunk) {
s.Writes++
s.MemoryStore.Put(c)
}
type testStoreFactory struct {
stores map[string]*TestStore
}
func NewTestStoreFactory() *testStoreFactory {
return &testStoreFactory{map[string]*TestStore{}}
}
func (f *testStoreFactory) CreateStore(ns string) ChunkStore {
if cs, present := f.stores[ns]; present {
return cs
}
f.stores[ns] = NewTestStore()
return f.stores[ns]
}
func (f *testStoreFactory) Shutter() {
f.stores = map[string]*TestStore{}
}