Unverified Commit bd0caea1 authored by Steven Allen's avatar Steven Allen Committed by GitHub

Merge pull request #93 from ipfs/feat/testing

improve testing
parents af96e515 24298430
......@@ -30,9 +30,14 @@ func NewAutoBatching(d ds.Batching, size int) *Datastore {
// Delete deletes a key/value
func (d *Datastore) Delete(k ds.Key) error {
_, found := d.buffer[k]
delete(d.buffer, k)
return d.child.Delete(k)
err := d.child.Delete(k)
if found && err == ds.ErrNotFound {
return nil
}
return err
}
// Get retrieves a value given a key.
......
......@@ -6,27 +6,11 @@ import (
"testing"
ds "github.com/ipfs/go-datastore"
dstest "github.com/ipfs/go-datastore/test"
)
func TestBasicPuts(t *testing.T) {
d := NewAutoBatching(ds.NewMapDatastore(), 16)
k := ds.NewKey("test")
v := []byte("hello world")
err := d.Put(k, v)
if err != nil {
t.Fatal(err)
}
out, err := d.Get(k)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(out, v) {
t.Fatal("wasnt the same! ITS NOT THE SAME")
}
func TestAutobatch(t *testing.T) {
dstest.SubtestAll(t, NewAutoBatching(ds.NewMapDatastore(), 16))
}
func TestFlushing(t *testing.T) {
......
package datastore_test
import (
"testing"
dstore "github.com/ipfs/go-datastore"
dstest "github.com/ipfs/go-datastore/test"
)
func TestMapDatastore(t *testing.T) {
ds := dstore.NewMapDatastore()
dstest.SubtestAll(t, ds)
}
func TestNullDatastore(t *testing.T) {
ds := dstore.NewNullDatastore()
// The only test that passes. Nothing should be found.
dstest.SubtestNotFounds(t, ds)
}
......@@ -5,6 +5,7 @@ import (
"time"
datastore "github.com/ipfs/go-datastore"
dstest "github.com/ipfs/go-datastore/test"
delay "github.com/ipfs/go-ipfs-delay"
)
......@@ -24,3 +25,7 @@ func TestDelayed(t *testing.T) {
t.Fatal("There should have been a delay of 1 second in put and in get")
}
}
func TestDelayedAll(t *testing.T) {
dstest.SubtestAll(t, New(datastore.NewMapDatastore(), delay.Fixed(time.Millisecond)))
}
package sync
import (
"testing"
ds "github.com/ipfs/go-datastore"
dstest "github.com/ipfs/go-datastore/test"
)
func TestSync(t *testing.T) {
dstest.SubtestAll(t, MutexWrap(ds.NewMapDatastore()))
}
package dstest
import (
"reflect"
"runtime"
"testing"
dstore "github.com/ipfs/go-datastore"
)
// BasicSubtests is a list of all basic tests.
var BasicSubtests = []func(t *testing.T, ds dstore.Datastore){
SubtestBasicPutGet,
SubtestNotFounds,
SubtestManyKeysAndQuery,
}
// BatchSubtests is a list of all basic batching datastore tests.
var BatchSubtests = []func(t *testing.T, ds dstore.Batching){
RunBatchTest,
RunBatchDeleteTest,
}
func getFunctionName(i interface{}) string {
return runtime.FuncForPC(reflect.ValueOf(i).Pointer()).Name()
}
// SubtestAll tests the given datastore against all the subtests.
func SubtestAll(t *testing.T, ds dstore.Datastore) {
for _, f := range BasicSubtests {
t.Run(getFunctionName(f), func(t *testing.T) {
f(t, ds)
})
}
if _, ok := ds.(dstore.Batching); ok {
for _, f := range BasicSubtests {
t.Run(getFunctionName(f), func(t *testing.T) {
f(t, ds)
})
}
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment